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

Re: "ocaml_beginners"::[] help with functors and polymorphism

Expand Messages
  • Richard Jones
    ... If I understand the problem correctly, what you need are called existential types . OCaml doesn t have them directly, but you can emulate them using
    Message 1 of 3 , Jan 1, 2009
    • 0 Attachment
      On Tue, Dec 30, 2008 at 08:06:45AM -0800, Ben Stoker wrote:
      > but it doesn't compile, because "type 'a t constraint 'a = blah" isn't the
      > same as "type 'a t". I guess what I need is some way to parameterize MapMake
      > over the set of types that 'a may take on. Can this be done in OCaml? If
      > not, how might the language be extended to make it possible?

      If I understand the problem correctly, what you need are called
      'existential types'. OCaml doesn't have them directly, but you can
      emulate them using objects.

      This is the first hit in Google ...

      http://caml.inria.fr/pub/ml-archives/caml-list/2007/11/a2043613b23c2fd496cd6e115f853d1a.en.html

      Here's another one:

      http://alaska-kamtchatka.blogspot.com/2008/03/ocaml-and-typeful-programming-annotated.html

      If that doesn't help, then it's probably better to take this to the
      senior list.

      BTW, mixing objects and functors is a bit of a minefield. Certainly
      when I tried (circa OCaml 3.06 era) it was fairly easy to tickle
      compiler bugs.

      Rich.

      --
      Richard Jones
      Red Hat
    • code17
      Hello ... Note that the type you ve provided are indeed less general than required by the module s type signature, since when a is constraint by
      Message 2 of 3 , Jan 2, 2009
      • 0 Attachment
        Hello

        On 12/30/2008 5:06 PM, Ben Stoker wrote:
        > I wanted the keys to have a 'cmp' method that would be used to order them. I
        > tried this:
        >
        > module OMap = MapMake (struct
        > type 'a t = 'a constraint 'a = < cmp: 'a -> int; .. >
        > let compare a b = a#cmp b
        > end)
        >
        > but it doesn't compile, because "type 'a t constraint 'a = blah" isn't the
        > same as "type 'a t". I guess what I need is some way to parameterize MapMake
        > over the set of types that 'a may take on. Can this be done in OCaml? If
        > not, how might the language be extended to make it possible?

        Note that the type you've provided are indeed less general than required
        by the module's type signature, since when 'a is constraint by <cmp: 'a
        -> int; .. >, so is the 'a in 'a t.

        The problem boils down to how to isolate the polymorphic part of 'a t
        (i.e., the rest methods except "cmp"). I don't know how to do that
        implicitly, as methods are not first-class values. But if you can
        control how the type is defined, you can always ask for sth like

        type 'a t = <cmp: 'a t -> int; rest: 'a>

        --
        code17
      Your message has been successfully submitted and would be delivered to recipients shortly.