* module type... @ 2004-11-11 8:35 Pietro Abate 2004-11-11 8:56 ` [Caml-list] " skaller 0 siblings, 1 reply; 10+ messages in thread From: Pietro Abate @ 2004-11-11 8:35 UTC (permalink / raw) To: ocaml ml Hi all, I've a problem that boils down to this code below. If I define (* -------------- *) module TermType = struct type t = int let copy t = t let to_string t = "this is a string" end ;; let l : TermType.t list = [1;2];; (* -------------- *) everything is ok. and I get val l : TermType.t list = [1; 2] but if I define: (* -------------- *) module type ElType = sig type t val copy : t -> t val to_string : t -> string end ;; module TermType : ElType = struct type t = int let copy t = t let to_string t = "this is a string" end ;; let l : TermType.t list = [1;2];; (* -------------- *) now the compiler tells me that: This expression has type int but is here used with type TermType.t but TermType.t should be unified with int... why ? I can I solve this problem ? and an other small question... why functor sigs are not defined in the std library (ie for the Set.Make module ?) thanks. p -- ++ "All great truths begin as blasphemies." -George Bernard Shaw ++ Please avoid sending me Word or PowerPoint attachments. See http://www.fsf.org/philosophy/no-word-attachments.html ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] module type... 2004-11-11 8:35 module type Pietro Abate @ 2004-11-11 8:56 ` skaller 2004-11-11 9:09 ` Pietro Abate 0 siblings, 1 reply; 10+ messages in thread From: skaller @ 2004-11-11 8:56 UTC (permalink / raw) To: Pietro Abate; +Cc: ocaml ml On Thu, 2004-11-11 at 19:35, Pietro Abate wrote: > Hi all, > I've a problem that boils down to this code below. > module type ElType = > sig > type t > end > ;; > module TermType : ElType = > struct > type t = int > end > ;; > let l : TermType.t list = [1;2];; > > (* -------------- *) > > now the compiler tells me that: > > This expression has type int but is here used with type TermType.t > > but TermType.t should be unified with int... > By coercing TermType to ElType, you have coerced 'int' to 't', where 't' is an abstract type, in other words the coercion is hiding the representation. There are two ways around this (a) don't hide the representation, put type t = int into the module type ElType as well. (b) add a type constructor function to the module TermType let term_of_int i = i with declaration in ElType: val term_of_int: int -> t -- John Skaller, mailto:skaller@users.sf.net voice: 061-2-9660-0850, snail: PO BOX 401 Glebe NSW 2037 Australia Checkout the Felix programming language http://felix.sf.net ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] module type... 2004-11-11 8:56 ` [Caml-list] " skaller @ 2004-11-11 9:09 ` Pietro Abate 2004-11-11 9:48 ` Virgile Prevosto 0 siblings, 1 reply; 10+ messages in thread From: Pietro Abate @ 2004-11-11 9:09 UTC (permalink / raw) To: ocaml ml Thanks John. On Thu, Nov 11, 2004 at 07:56:10PM +1100, skaller wrote: > By coercing TermType to ElType, you have coerced 'int' to 't', > where 't' is an abstract type, in other words the coercion is hiding > the representation. There are two ways around this ok, I got the problem... but neither of your solutions solve my more general problem as I need ElType.t to be an abstract data type as I'm using it in a functor... now it says: This expression has type int but is here used with type SetOfTerm.Set.elt = TermType.t for the same reason as before, but I can't change the ElType.t without breaking the functor [....] module MakeSet = functor (Q : ElType) -> functor (SetFunc : SetFuncType) -> struct module Set = SetFunc ( struct type t = Q.t let compare = compare end ) [....] below the extended version of the problem with the same error... any advice on how to modify my modules/classes design to overcome this problem ? what I'm trying to do is to have set, setofset, setofsetofset etc using the same code base and functors... :) p -------------------------------- module type ElType = sig type t val copy : t -> t val to_string : t -> string end module type OrderedType = sig type t val compare : t -> t -> int end module type SetFuncType = functor (Ord : OrderedType) -> sig type elt = Ord.t type t val empty : t val compare : t -> t -> int val add : elt -> t -> t end module MakeSet = functor (Q : ElType) -> functor (SetFunc : SetFuncType) -> struct module Set = SetFunc ( struct type t = Q.t let compare = compare end ) class set ?(el=[]) () = object(self) initializer self#add el val mutable s = Set.empty method add el = s <- List.fold_left ( fun s' e -> Set.add e s' ) s el end end module TermType : ElType = struct type t = int let copy t = t let to_string t = "this is a string" end ;; module SetOfTerm = MakeSet (TermType) (Set.Make);; (* ------------------ *) module SetOfTermType : ElType = struct type t = SetOfTerm.set let copy t = t let to_string t = "this is an other string" end ;; module SetOfSet = MakeSet (SetOfTermType) (Set.Make);; let newset_naked ?(el=[]) () = new SetOfTerm.set ~el:el let newset ?(el=[]) () = (`Set (new SetOfTerm.set ~el:el)) let newsetofset ?(el=[]) () = (`Set (newset ~el:el ())) let a = newset_naked ~el:[1;2] ();; let b = newsetofset ~el:[a] ();; -- ++ "All great truths begin as blasphemies." -George Bernard Shaw ++ Please avoid sending me Word or PowerPoint attachments. See http://www.fsf.org/philosophy/no-word-attachments.html ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] module type... 2004-11-11 9:09 ` Pietro Abate @ 2004-11-11 9:48 ` Virgile Prevosto 2004-11-12 16:11 ` Specifying abstract type in a record josh 0 siblings, 1 reply; 10+ messages in thread From: Virgile Prevosto @ 2004-11-11 9:48 UTC (permalink / raw) To: caml-list Hello, Le 11.11.2004, à 20:09:31, Pietro Abate a écrit: > > below the extended version of the problem with the same error... > > any advice on how to modify my modules/classes design to overcome this > problem ? what I'm trying to do is to have set, setofset, > setofsetofset etc using the same code base and functors... > You can use a variant of the solution (a), with t still being abstract in ElType and the use of explicit type equalities (see the reference manual,<http://pauillac.inria.fr/ocaml/htmlman/manual004.html>, section 2.4) for each of the implementations: > module type ElType = > sig > type t (* type t is abstract here. *) > val copy : t -> t > val to_string : t -> string > end > module TermType : ElType with type t = int (* TermType.t is unifiable with int *) ... > module SetOfTerm = MakeSet (TermType) (Set.Make);; > > module SetOfTermType : ElType with type t = SetOfTerm.set (* SetOfTermType.t is unifiable with Set.OfTerm.set *) ... -- E tutto per oggi, a la prossima volta Virgile ^ permalink raw reply [flat|nested] 10+ messages in thread
* Specifying abstract type in a record 2004-11-11 9:48 ` Virgile Prevosto @ 2004-11-12 16:11 ` josh 2004-11-12 16:33 ` [Caml-list] " Luc Maranget ` (4 more replies) 0 siblings, 5 replies; 10+ messages in thread From: josh @ 2004-11-12 16:11 UTC (permalink / raw) To: caml-list OK, Here's what I'm trying to do: # type doer = { file_name:string ; actor: ('a -> unit) };; But when I do this, it tells me that I've got "Unbound type parameter 'a ". However, if I change the 'a to int like this: # type doer = { file_name:string; actor: (int -> unit) };; It works as expected, but is not polymorphic (which is what I was trying to do). If I create an abstract type like: # type t # type doer = { file_name:string; actor (t -> unit) };; It works until I try to use a created record: # let b = {file_name = "one"; actor = (fun x -> () ) };; # b.actor 10;; The expression has type int but is used with type t even if I try to do this # type t = int;; it doesn't work. So, how _can_ I specify a record with an abstract field? Or is this the Wrong Way to Do Things? Thank you for your help. -jbs ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] Specifying abstract type in a record 2004-11-12 16:11 ` Specifying abstract type in a record josh @ 2004-11-12 16:33 ` Luc Maranget 2004-11-12 16:37 ` Aleksey Nogin ` (3 subsequent siblings) 4 siblings, 0 replies; 10+ messages in thread From: Luc Maranget @ 2004-11-12 16:33 UTC (permalink / raw) To: josh; +Cc: caml-list > OK, Here's what I'm trying to do: > > # type doer = { file_name:string ; actor: ('a -> unit) };; > Try this type 'a doer = { file_name:string ; actor: ('a -> unit) };; -- Luc ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] Specifying abstract type in a record 2004-11-12 16:11 ` Specifying abstract type in a record josh 2004-11-12 16:33 ` [Caml-list] " Luc Maranget @ 2004-11-12 16:37 ` Aleksey Nogin 2004-11-12 16:37 ` Richard Jones ` (2 subsequent siblings) 4 siblings, 0 replies; 10+ messages in thread From: Aleksey Nogin @ 2004-11-12 16:37 UTC (permalink / raw) To: josh, Caml List josh wrote: > OK, Here's what I'm trying to do: > > # type doer = { file_name:string ; actor: ('a -> unit) };; > What exactly are you trying to accomplish? Would something like type 'a doer = { file_name:string ; actor: ('a -> unit) } work for you? -- Aleksey Nogin Home Page: http://nogin.org/ E-Mail: nogin@cs.caltech.edu (office), aleksey@nogin.org (personal) Office: Jorgensen 70, tel: (626) 395-2907 ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] Specifying abstract type in a record 2004-11-12 16:11 ` Specifying abstract type in a record josh 2004-11-12 16:33 ` [Caml-list] " Luc Maranget 2004-11-12 16:37 ` Aleksey Nogin @ 2004-11-12 16:37 ` Richard Jones 2004-11-12 16:40 ` Andrew Bagdanov 2004-11-12 16:44 ` Matt Gushee 4 siblings, 0 replies; 10+ messages in thread From: Richard Jones @ 2004-11-12 16:37 UTC (permalink / raw) To: josh; +Cc: caml-list [-- Attachment #1: Type: text/plain, Size: 1959 bytes --] On Fri, Nov 12, 2004 at 10:11:51AM -0600, josh wrote: > OK, Here's what I'm trying to do: > > # type doer = { file_name:string ; actor: ('a -> unit) };; > > But when I do this, it tells me that I've got "Unbound type parameter 'a ". You need to do this: # type 'a doer = { file_name:string ; actor: ('a -> unit) };; type 'a doer = { file_name : string; actor : 'a -> unit; } because you're creating a polymorphic type ('a doer). Note that this won't let you store an actor function of type, say, int -> unit and then replace it with another function of type, say, string -> unit. For the same reason it won't let you pass an ``int doer'' to a function expecting a ``string doer''. > # type t > # type doer = { file_name:string; actor (t -> unit) };; > > It works until I try to use a created record: > > # let b = {file_name = "one"; actor = (fun x -> () ) };; > # b.actor 10;; > The expression has type int but is used with type t > > even if I try to do this > > # type t = int;; > > it doesn't work. OK, this is another problem. Here what you're doing is defining a _new_ type ``t'', which is unrelated to your old type ``t''. Here's another example: # let f () = "hello";; val f : unit -> string = <fun> # f ();; - : string = "hello" # let g = f;; val g : unit -> string = <fun> # let f () = "goodbye";; val f : unit -> string = <fun> # f ();; - : string = "goodbye" # g ();; - : string = "hello" Notice that ``g'' still calls the old ``f'', even after ``f'' has been redefined. There's a beginner's list: > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners Rich. -- Richard Jones. http://www.annexia.org/ http://www.j-london.com/ >>> http://www.team-notepad.com/ - collaboration tools for teams <<< Merjis Ltd. http://www.merjis.com/ - improving website return on investment Use Perl libs in OCaml - http://www.merjis.com/developers/perl4caml [-- Attachment #2: Digital signature --] [-- Type: application/pgp-signature, Size: 189 bytes --] ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] Specifying abstract type in a record 2004-11-12 16:11 ` Specifying abstract type in a record josh ` (2 preceding siblings ...) 2004-11-12 16:37 ` Richard Jones @ 2004-11-12 16:40 ` Andrew Bagdanov 2004-11-12 16:44 ` Matt Gushee 4 siblings, 0 replies; 10+ messages in thread From: Andrew Bagdanov @ 2004-11-12 16:40 UTC (permalink / raw) To: caml-list josh writes: > OK, Here's what I'm trying to do: > > # type doer = { file_name:string ; actor: ('a -> unit) };; > > But when I do this, it tells me that I've got "Unbound type parameter 'a ". > You need to add a type parameter to the doer type: # type 'a doer = {file_name:string; actor: ('a -> unit) };; type 'a doer = { file_name : string; actor : 'a -> unit; } This binds the type parameter 'a in your original definition. Now things should work the way you want: # let b = {file_name = "one"; actor = (fun x -> () ) };; val b : 'a doer = {file_name = "one"; actor = <fun>} # b.actor 10;; - : unit = () Note that in this definition, b.actor remains polymorphic. If you were to do this: # let b = {file_name = "one"; actor = (fun x -> Printf.printf "%d\n" x) };; val b : int doer = {file_name = "one"; actor = <fun>} then 'a becones concrete (int). Hope this helps... -Andy ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] Specifying abstract type in a record 2004-11-12 16:11 ` Specifying abstract type in a record josh ` (3 preceding siblings ...) 2004-11-12 16:40 ` Andrew Bagdanov @ 2004-11-12 16:44 ` Matt Gushee 4 siblings, 0 replies; 10+ messages in thread From: Matt Gushee @ 2004-11-12 16:44 UTC (permalink / raw) To: caml-list On Fri, Nov 12, 2004 at 10:11:51AM -0600, josh wrote: > # type doer = { file_name:string ; actor: ('a -> unit) };; > > But when I do this, it tells me that I've got "Unbound type parameter 'a > ". Your first impulse was on the right track, but see below. > # type t > # type doer = { file_name:string; actor (t -> unit) };; > > It works until I try to use a created record: > > # let b = {file_name = "one"; actor = (fun x -> () ) };; > # b.actor 10;; > The expression has type int but is used with type t Right. You can't directly *use* an abstract type. Generally, you would declare an abstract type in an interface--an .mli file, a module sig, or a class type--then specify it ('type t = ...') in an implementation. Abstract types are good for keeping implementation details hidden from the user, but they don't, by themselves, give you polymorphism. > it doesn't work. So, how _can_ I specify a record with an abstract > field? type 'a doer = { file_name : string; actor : ('a -> unit) } That's known as a parameterized record type. You would also do the same thing for other data structures, and for objects (though the syntax is a bit different in the latter case) -- Matt Gushee When a nation follows the Way, Haven Rock Press Horses bear manure through Englewood, Colorado, USA its fields; books@havenrock.com When a nation ignores the Way, Horses bear soldiers through its streets. --Lao Tzu (Peter Merel, trans.) ^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2004-11-12 16:44 UTC | newest] Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2004-11-11 8:35 module type Pietro Abate 2004-11-11 8:56 ` [Caml-list] " skaller 2004-11-11 9:09 ` Pietro Abate 2004-11-11 9:48 ` Virgile Prevosto 2004-11-12 16:11 ` Specifying abstract type in a record josh 2004-11-12 16:33 ` [Caml-list] " Luc Maranget 2004-11-12 16:37 ` Aleksey Nogin 2004-11-12 16:37 ` Richard Jones 2004-11-12 16:40 ` Andrew Bagdanov 2004-11-12 16:44 ` Matt Gushee
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox