caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] parameterized classes, modules & polymorphic variants
@ 2012-11-09 14:25 Didier Cassirame
  2012-11-11  8:21 ` Jacques Garrigue
  0 siblings, 1 reply; 3+ messages in thread
From: Didier Cassirame @ 2012-11-09 14:25 UTC (permalink / raw)
  To: caml-list

Dear caml-list,

I have been trying recently to combine classes, modules and variants
in the following fashion:

module A1 = struct

  class ['a] t = object
       constraint 'a = [>`a]
       method m : 'a -> string = function `a -> "a" | `a1 -> "a1" | _ -> "_"
   end

end;;

module A2 = struct

  class ['a] t = object
       constraint 'a = [>`a]
       method m : 'a -> string = function `a -> "a" | `a2 -> "a2" | _ -> "_"
   end

end;;

module A3 = struct

  class ['a] t = object
       constraint 'a = [>`a]
       method m : 'a -> string = function `a -> "a" | `a3 -> "a3" | _ -> "_"
   end

end;;

module type A = sig

  class ['a] t : object
       constraint 'a = [>`a]
       method m : 'a -> string
   end

end;;

type m = (module A);;

let l: m list = [ (module A1); (module A2); (module A3)];;

--------------------------------

Unfortunately the list typecheck fails. However, making a list of
class instances from A1.t, A2.t, A3.t succeed, with the type:

 [> `a | `a1 | `a2 | `a3 ] ct list

ct being defined as equal to A.t.

I thought that perhaps I should parameterize the type m from the type
parameter 'a of A.t to solve my problem, but I am not sure of the
syntax, or if it's the problem. Does anyone have an idea?

didier

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] parameterized classes, modules & polymorphic variants
  2012-11-09 14:25 [Caml-list] parameterized classes, modules & polymorphic variants Didier Cassirame
@ 2012-11-11  8:21 ` Jacques Garrigue
  2012-11-11  9:25   ` Didier Cassirame
  0 siblings, 1 reply; 3+ messages in thread
From: Jacques Garrigue @ 2012-11-11  8:21 UTC (permalink / raw)
  To: Didier Cassirame; +Cc: caml-list

On 2012/11/09, at 23:25, Didier Cassirame <didier.cassirame@gmail.com> wrote:

> Dear caml-list,
> 
> I have been trying recently to combine classes, modules and variants
> in the following fashion:
> 
> module A1 = struct
> 
>  class ['a] t = object
>       constraint 'a = [>`a]
>       method m : 'a -> string = function `a -> "a" | `a1 -> "a1" | _ -> "_"
>   end
> 
> end;;
> 
> […]
> 
> module type A = sig
> 
>  class ['a] t : object
>       constraint 'a = [>`a]
>       method m : 'a -> string
>   end
> 
> end;;
> 
> type m = (module A);;
> 
> let l: m list = [ (module A1); (module A2); (module A3)];;
> 
> --------------------------------
> 
> Unfortunately the list typecheck fails. However, making a list of
> class instances from A1.t, A2.t, A3.t succeed, with the type:
> 
> [> `a | `a1 | `a2 | `a3 ] ct list
> 
> ct being defined as equal to A.t.
> 
> I thought that perhaps I should parameterize the type m from the type
> parameter 'a of A.t to solve my problem, but I am not sure of the
> syntax, or if it's the problem. Does anyone have an idea?

Actually the parameterization would not help here, since you want to put them all in the same list.
The idea of using first-class modules is to be explicit about types, so using an explicit type definition for a solves the problem.

Jacques Garrigue

module A1 = struct
  type a = private [> `a | `a1]

  class t = object
    method m : a -> string = function `a -> "a" | `a1 -> "a1" | _ -> "_"
  end
end;;

module A2 = struct
  type a = private [> `a | `a2]

  class t = object
    method m : a -> string = function `a -> "a" | `a2 -> "a2" | _ -> "_"
  end
end;;

module A3 = struct
  type a = private [> `a | `a3]

  class t = object
    method m : a -> string = function `a -> "a" | `a3 -> "a3" | _ -> "_"
  end
end;;

module type A = sig
  type a = private [> `a]
  class t : object
    method m : a -> string
  end
end;;

type m = (module A);;

let l: m list = [ (module A1); (module A2); (module A3)];;


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] parameterized classes, modules & polymorphic variants
  2012-11-11  8:21 ` Jacques Garrigue
@ 2012-11-11  9:25   ` Didier Cassirame
  0 siblings, 0 replies; 3+ messages in thread
From: Didier Cassirame @ 2012-11-11  9:25 UTC (permalink / raw)
  To: Jacques Garrigue; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 2313 bytes --]

It's Great! Thanks!

didier

2012/11/11 Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>

> On 2012/11/09, at 23:25, Didier Cassirame <didier.cassirame@gmail.com>
> wrote:
>
> > Dear caml-list,
> >
> > I have been trying recently to combine classes, modules and variants
> > in the following fashion:
> >
> > module A1 = struct
> >
> >  class ['a] t = object
> >       constraint 'a = [>`a]
> >       method m : 'a -> string = function `a -> "a" | `a1 -> "a1" | _ ->
> "_"
> >   end
> >
> > end;;
> >
> > […]
> >
> > module type A = sig
> >
> >  class ['a] t : object
> >       constraint 'a = [>`a]
> >       method m : 'a -> string
> >   end
> >
> > end;;
> >
> > type m = (module A);;
> >
> > let l: m list = [ (module A1); (module A2); (module A3)];;
> >
> > --------------------------------
> >
> > Unfortunately the list typecheck fails. However, making a list of
> > class instances from A1.t, A2.t, A3.t succeed, with the type:
> >
> > [> `a | `a1 | `a2 | `a3 ] ct list
> >
> > ct being defined as equal to A.t.
> >
> > I thought that perhaps I should parameterize the type m from the type
> > parameter 'a of A.t to solve my problem, but I am not sure of the
> > syntax, or if it's the problem. Does anyone have an idea?
>
> Actually the parameterization would not help here, since you want to put
> them all in the same list.
> The idea of using first-class modules is to be explicit about types, so
> using an explicit type definition for a solves the problem.
>
> Jacques Garrigue
>
> module A1 = struct
>   type a = private [> `a | `a1]
>
>   class t = object
>     method m : a -> string = function `a -> "a" | `a1 -> "a1" | _ -> "_"
>   end
> end;;
>
> module A2 = struct
>   type a = private [> `a | `a2]
>
>   class t = object
>     method m : a -> string = function `a -> "a" | `a2 -> "a2" | _ -> "_"
>   end
> end;;
>
> module A3 = struct
>   type a = private [> `a | `a3]
>
>   class t = object
>     method m : a -> string = function `a -> "a" | `a3 -> "a3" | _ -> "_"
>   end
> end;;
>
> module type A = sig
>   type a = private [> `a]
>   class t : object
>     method m : a -> string
>   end
> end;;
>
> type m = (module A);;
>
> let l: m list = [ (module A1); (module A2); (module A3)];;
>
>

[-- Attachment #2: Type: text/html, Size: 3362 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-11-11  9:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-11-09 14:25 [Caml-list] parameterized classes, modules & polymorphic variants Didier Cassirame
2012-11-11  8:21 ` Jacques Garrigue
2012-11-11  9:25   ` Didier Cassirame

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).