* [Caml-list] record declaration, SML @ 2003-01-09 13:56 A. Ozmen 2003-01-09 18:27 ` brogoff 2003-01-09 22:42 ` Daniel de Rauglaudre 0 siblings, 2 replies; 10+ messages in thread From: A. Ozmen @ 2003-01-09 13:56 UTC (permalink / raw) To: caml-list Hi, What's best translation of the following SML type? datatype t = C of { f : int } I have to define extra record types, I guess. Then, not including these types in interface (.mli) files? Is this the right approach? Thanks. __________________________________________________________ Get your Private, Free Email from HTTP://www.DmailMan.Com ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] record declaration, SML 2003-01-09 13:56 [Caml-list] record declaration, SML A. Ozmen @ 2003-01-09 18:27 ` brogoff 2003-01-09 23:15 ` A. Ozmen 2003-01-10 9:21 ` Xavier Leroy 2003-01-09 22:42 ` Daniel de Rauglaudre 1 sibling, 2 replies; 10+ messages in thread From: brogoff @ 2003-01-09 18:27 UTC (permalink / raw) To: A. Ozmen; +Cc: caml-list Hi, You've noticed that OCaml records aren't like SML records. Here are a few places where they differ. You can't have anonymous records in OCaml, so in the case of a datatype declaration like yours you need something like type t = C of t_record and t_record = { f : int } If you hide the record definition, then you can't match on it, but I imagine you know that already. In the case of anonymous records as function arguments, you could try labels. In the case of records in a module sharing field names, well, you can't do that either, so you'll need to use classes or find some way to disambiguate the fields. This is one of those annoyances of Caml (like the lack of overloading :) that you just need to get used to. SML is a bit better here IMO, but still not ideal; I think that there was an SML variant (SML#) that was appealing but it looks like that idea isn't going anywhere. There are a few other conveniences in SML record handling too, like the ... notation, that don't exist in OCaml. Oh yeah, also be careful initializing records with stateful functions, because you may be surprised at the order of evaluation. -- Brian On Thu, 9 Jan 2003, A. Ozmen wrote: > Hi, > > What's best translation of the following SML type? > datatype t = C of { f : int } > > I have to define extra record types, I guess. Then, not including these > types in interface (.mli) files? Is this the right approach? > > Thanks. > __________________________________________________________ > Get your Private, Free Email from HTTP://www.DmailMan.Com > ------------------- > To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr > Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] record declaration, SML 2003-01-09 18:27 ` brogoff @ 2003-01-09 23:15 ` A. Ozmen 2003-01-10 9:21 ` Xavier Leroy 1 sibling, 0 replies; 10+ messages in thread From: A. Ozmen @ 2003-01-09 23:15 UTC (permalink / raw) To: caml-list Thanks for the answers. Another thing with the records is that, if I don't open the module the record defined in, I also have to prefix field names with the module name, even though I prefix the constructor (M.C{M.f=1}). ps: Anybody having problems with yahoo? I tried to subscribe to the beginners list. Gives some server error. I also experienced the same a few weeks ago with free email subscription. __________________________________________________________ Get your Private, Free Email from HTTP://www.DmailMan.Com ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] record declaration, SML 2003-01-09 18:27 ` brogoff 2003-01-09 23:15 ` A. Ozmen @ 2003-01-10 9:21 ` Xavier Leroy 2003-01-10 14:34 ` brogoff 1 sibling, 1 reply; 10+ messages in thread From: Xavier Leroy @ 2003-01-10 9:21 UTC (permalink / raw) To: brogoff; +Cc: A. Ozmen, caml-list > > What's best translation of the following SML type? > > datatype t = C of { f : int } > In the case of records in a module sharing field names, well, you can't do > that either, so you'll need to use classes or find some way to disambiguate > the fields. Sometimes, it's acceptable to just omit the record type: type t = C of int You lose the naming of the arguments of the constructor, but for small numbers of arguments (e.g. 1 or 2), this is often tolerable. > There are a few other conveniences in SML record handling too, like the ... > notation, that don't exist in OCaml. You mean, in pattern-matching over records? Caml offers the same functionality without the ... notation, e.g. type r = { x: int; y: int } match r with { x = 1 } -> ... Because records are declared in advance, there is no requirement that all record labels be mentioned in a pattern matching. - Xavier Leroy ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] record declaration, SML 2003-01-10 9:21 ` Xavier Leroy @ 2003-01-10 14:34 ` brogoff 0 siblings, 0 replies; 10+ messages in thread From: brogoff @ 2003-01-10 14:34 UTC (permalink / raw) To: Xavier Leroy; +Cc: A. Ozmen, caml-list On Fri, 10 Jan 2003, Xavier Leroy wrote: > > There are a few other conveniences in SML record handling too, like the ... > > notation, that don't exist in OCaml. > > You mean, in pattern-matching over records? Caml offers the same > functionality without the ... notation, e.g. > > type r = { x: int; y: int } > > match r with { x = 1 } -> ... > > Because records are declared in advance, there is no requirement that > all record labels be mentioned in a pattern matching. Right, but if I'm not mistaken, SML forces you to use the ... in record pattern matching when you want to ignore some labels in the match. As you suggest, this is a lot more important in SML, but even in OCaml it would be a bit better IMO to explicitly distinguish between ignoring some labels and forgetting some, so that slovenly programmers (like yours truly) could depend on the type checker to slap them when they miss fields. Unfortunately, OCaml behaves as though every record pattern match had an implicit ..., so I don't see a good way out even if the developers agreed that this was worth fixing. File that one under "petty complaints", or, if you're feeling generous in this new year, under the non-petty "Oh how I wish we had more polymorphism in records!" complaint :-) -- Brian ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] record declaration, SML 2003-01-09 13:56 [Caml-list] record declaration, SML A. Ozmen 2003-01-09 18:27 ` brogoff @ 2003-01-09 22:42 ` Daniel de Rauglaudre 2003-01-09 23:14 ` Alessandro Baretta 1 sibling, 1 reply; 10+ messages in thread From: Daniel de Rauglaudre @ 2003-01-09 22:42 UTC (permalink / raw) To: caml-list Hi, > What's best translation of the following SML type? > datatype t = C of { f : int } A solution is: type t = C of < f : int > It is what does the converter SML->OCaml by Camlp4: camlp4 pa_sml.cmo pr_o.cmo -impl file.sml -- Daniel de RAUGLAUDRE http://cristal.inria.fr/~ddr/ ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] record declaration, SML 2003-01-09 22:42 ` Daniel de Rauglaudre @ 2003-01-09 23:14 ` Alessandro Baretta 2003-01-10 2:59 ` Daniel de Rauglaudre 2003-01-10 3:45 ` brogoff 0 siblings, 2 replies; 10+ messages in thread From: Alessandro Baretta @ 2003-01-09 23:14 UTC (permalink / raw) To: Ocaml Daniel de Rauglaudre wrote: > Hi, > > >>What's best translation of the following SML type? >>datatype t = C of { f : int } > > > A solution is: > type t = C of < f : int > Interesting solution. However, Ocaml does not support defining objects on the fly. The following > let c = new object method f = 1 end in C c is not valid Ocaml. So, in order to define just one value one needs to define an entire class as in the following example. class f_one = object method f = 1 end let res = C (new f_one) BTW, why is something like the above not in the language? It seems only natural to have such a feature since an analogous construct exists for structs. let module C = struct let f = 1 end in C.f Alex ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] record declaration, SML 2003-01-09 23:14 ` Alessandro Baretta @ 2003-01-10 2:59 ` Daniel de Rauglaudre 2003-01-10 3:45 ` brogoff 1 sibling, 0 replies; 10+ messages in thread From: Daniel de Rauglaudre @ 2003-01-10 2:59 UTC (permalink / raw) To: caml-list Hi, On Fri, Jan 10, 2003 at 12:14:17AM +0100, Alessandro Baretta wrote: > > Interesting solution. However, Ocaml does not support > defining objects on the fly. The following > > let c = new object method f = 1 end in C c > is not valid Ocaml. You can do this: let module M = struct class a = object val f = 1 method f = f end end in C (new M.a) It is a little bit tricky, but useful in an automatic translation from SML to OCaml. -- Daniel de RAUGLAUDRE http://cristal.inria.fr/~ddr/ ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] record declaration, SML 2003-01-09 23:14 ` Alessandro Baretta 2003-01-10 2:59 ` Daniel de Rauglaudre @ 2003-01-10 3:45 ` brogoff 2003-01-10 7:45 ` Alessandro Baretta 1 sibling, 1 reply; 10+ messages in thread From: brogoff @ 2003-01-10 3:45 UTC (permalink / raw) To: caml-list On Fri, 10 Jan 2003, Alessandro Baretta wrote: > Daniel de Rauglaudre wrote: > > Hi, > > > > > >>What's best translation of the following SML type? > >>datatype t = C of { f : int } > > > > > > A solution is: > > type t = C of < f : int > > > Interesting solution. However, Ocaml does not support > defining objects on the fly. The following > > let c = new object method f = 1 end in C c > is not valid Ocaml. > > So, in order to define just one value one needs to define an > entire class as in the following example. > > class f_one = object method f = 1 end > let res = C (new f_one) Is this such a big problem? I imagine we're more likely to provide a smart constructor for t, something like let mkC o = C(o :> < f : int >) and just stuff any object with method f : int in there. If we only want there to be one kind of thing, we're better off using a record. Of course, Daniel doesn't have that option since he is discussing a syntactic transformation from SML records into OCaml constructs, and hence using the class system makes sense on account of the differences between SML record typing and that of OCaml. > BTW, why is something like the above not in the language? It > seems only natural to have such a feature since an analogous > construct exists for structs. There are no anonymous records (like SML has) either, so if you see classes as analogous to records rather than structs it doesn't seem so unnatural. No, I realize that's not a real explanation. -- Brian ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] record declaration, SML 2003-01-10 3:45 ` brogoff @ 2003-01-10 7:45 ` Alessandro Baretta 0 siblings, 0 replies; 10+ messages in thread From: Alessandro Baretta @ 2003-01-10 7:45 UTC (permalink / raw) To: Ocaml brogoff@speakeasy.net wrote: > On Fri, 10 Jan 2003, Alessandro Baretta wrote: >>So, in order to define just one value one needs to define an >>entire class as in the following example. >> >>class f_one = object method f = 1 end >>let res = C (new f_one) > > > Is this such a big problem? I imagine we're more likely to provide a > smart constructor for t, something like > > let mkC o = C(o :> < f : int >) No, it's not a bit problem. I was just wondering > and just stuff any object with method f : int in there. If we only want there > to be one kind of thing, we're better off using a record. Of course, Daniel > doesn't have that option since he is discussing a syntactic transformation > from SML records into OCaml constructs, and hence using the class system makes > sense on account of the differences between SML record typing and that of > OCaml. Besides, Daniel showed us that you can actually define a record on the fly without cluttering the class namespace. let module M = struct class a = object val f = 1 method f = f end end in C (new M.a) This raises one more question: let module P = struct type t = { f : int } let x = { f = 1 } end in P.x;; The above is not valid Ocaml because P.x has a type which escapes its scope. This is quite evident. Now I would expect Daniel's code to be rejected by the same standards, but it is not. Why? Does class a not escape its scope? > >>BTW, why is something like the above not in the language? It >>seems only natural to have such a feature since an analogous >>construct exists for structs. > > > There are no anonymous records (like SML has) either, so if you see classes as > analogous to records rather than structs it doesn't seem so unnatural. No, I > realize that's not a real explanation. Right. Even unnamed records are missing. But it's different because the Ocaml type system does not support record subtyping--even if record type b in module B defines all fields of record type a in module A with the same types, B is not a subtype of A. On the other hand, object subtyping exists already in the language, so on-the-fly definitions of objects à la façon de Daniel are more meaningful and useful. Alex ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2003-01-10 14:34 UTC | newest] Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2003-01-09 13:56 [Caml-list] record declaration, SML A. Ozmen 2003-01-09 18:27 ` brogoff 2003-01-09 23:15 ` A. Ozmen 2003-01-10 9:21 ` Xavier Leroy 2003-01-10 14:34 ` brogoff 2003-01-09 22:42 ` Daniel de Rauglaudre 2003-01-09 23:14 ` Alessandro Baretta 2003-01-10 2:59 ` Daniel de Rauglaudre 2003-01-10 3:45 ` brogoff 2003-01-10 7:45 ` Alessandro Baretta
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox