Loading ...
Sorry, an error occurred while loading the content.

Re: "ocaml_beginners"::[] using modules question

Expand Messages
  • William D. Neumann
    ... Well, the problem that you re having here is that the module type NUMBER has a signature that hides the concrete type of Float.inner_type and
    Message 1 of 2 , Jul 31, 2007
    • 0 Attachment
      On Tue, 31 Jul 2007, Sashan Govender wrote:

      > As far as I can tell float and Float.inner_type are the same type but
      > different names. I've had a look at the set.ml to see how it does
      > things and it binds the innertype of Ord to type elt (i.e type elt =
      > Ord.t) and that looks like it's doing a similar thing to what I'm
      > trying to do above.

      Well, the problem that you're having here is that the module type NUMBER
      has a signature that hides the concrete type of Float.inner_type and
      Float.outer_type. This is why float and Float.inner_type are incompatible
      here, even though they are the same underlying type.

      Now, part of the confusion you may have stems from you doing something
      different from what the exercise on in the O'Reilly book is asking. What
      you have done is created a module type containg abstract types and used it
      to restrict the signature for the Float. What the example wants you to do
      is to create a module Float (among others) that implements that signature
      (i.e. that can be given that signature without error), and apply a functor
      that you are supposed to create to that module. What you are trying to do
      is shown in this silly example that's not really what they're asking for
      either:

      # module type NUMBER =
      sig
      type a
      type t
      val create : a -> t
      val add : t -> t -> t
      val string_of : t -> string
      end
      ;;
      module type NUMBER =
      sig
      type a
      type t
      val create : a -> t
      val add : t -> t -> t
      val string_of : t -> string
      end

      # module Float2 =
      struct
      type a = float
      type t = float
      let create f = f
      let add x y = x +. y
      let string_of x = string_of_float x
      end
      ;;
      module Float2 :
      sig
      type a = float
      type t = float
      val create : 'a -> 'a
      val add : float -> float -> float
      val string_of : float -> string
      end

      # module FVector = functor (Num:NUMBER) ->
      struct
      type elt = Num.t
      type t = elt array

      let create x : t = Array.init 10 (fun _ -> Num.create x)
      end ;;
      module FVector :
      functor (Num : NUMBER) ->
      sig
      type elt = Num.t
      type t = elt array
      val create : Num.a -> t
      end
      # module FlVec = FVector(Float2);;
      module FlVec :
      sig
      type elt = Float2.t
      type t = elt array val
      create : Float2.a -> t
      end
      # FlVec.create 1.;;
      - : FlVec.t = [|1.; 1.; 1.; 1.; 1.; 1.; 1.; 1.; 1.; 1.|]

      Hope that helps...

      William D. Neumann

      ---

      "There's just so many extra children, we could just feed the
      children to these tigers. We don't need them, we're not doing
      anything with them.

      Tigers are noble and sleek; children are loud and messy."

      -- Neko Case

      Life is unfair. Kill yourself or get over it.
      -- Black Box Recorder
    Your message has been successfully submitted and would be delivered to recipients shortly.