* parameterized pattern @ 2006-11-06 21:15 Serge Aleynikov 2006-11-06 23:58 ` [Caml-list] " Jon Harrop ` (2 more replies) 0 siblings, 3 replies; 15+ messages in thread From: Serge Aleynikov @ 2006-11-06 21:15 UTC (permalink / raw) To: caml-list Hi, The section 6.6 of OCaml's manual introduces a notion of "Parenthesized patterns". I couldn't find any examples on how to use this feature, and brute-force approach doesn't work: # match 1.0 with (y : float) -> print_float y | (s : string) -> print_string s;; This pattern matches values of type string but is here used to match values of type float # Could anyone point at a suitable resource? Thanks. Serge -- Serge Aleynikov Routing R&D, IDT Telecom Tel: +1 (973) 438-3436 Fax: +1 (973) 438-1464 ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [Caml-list] parameterized pattern 2006-11-06 21:15 parameterized pattern Serge Aleynikov @ 2006-11-06 23:58 ` Jon Harrop 2006-11-07 0:12 ` Serge Aleynikov 2006-11-06 23:59 ` Martin Jambon 2006-11-08 23:55 ` Lukasz Stafiniak 2 siblings, 1 reply; 15+ messages in thread From: Jon Harrop @ 2006-11-06 23:58 UTC (permalink / raw) To: caml-list On Monday 06 November 2006 21:15, Serge Aleynikov wrote: > Could anyone point at a suitable resource? I wasn't even aware that you could add type annotations inside a pattern. Your example will not work simply because OCaml's type system will not allow float to unify with string. You need some context where type inference will allow different possibilities, like a tuple containing both a float and a string: # let f = function | (y : float), _ -> print_float y | _, (s : string) -> print_string s;; or a polymorphic variant containing either a float or a string: # let f = function | `F (y : float) -> print_float y | `S (s : string) -> print_string s;; val f : [< `F of float | `S of string ] -> unit = <fun> -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. Objective CAML for Scientists http://www.ffconsultancy.com/products/ocaml_for_scientists ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [Caml-list] parameterized pattern 2006-11-06 23:58 ` [Caml-list] " Jon Harrop @ 2006-11-07 0:12 ` Serge Aleynikov 0 siblings, 0 replies; 15+ messages in thread From: Serge Aleynikov @ 2006-11-07 0:12 UTC (permalink / raw) To: Jon Harrop; +Cc: caml-list Jon Harrop wrote: > On Monday 06 November 2006 21:15, Serge Aleynikov wrote: > >> Could anyone point at a suitable resource? >> > > I wasn't even aware that you could add type annotations inside a pattern. I had a similar reaction when I saw that construct in the formal language spec. > Your > example will not work simply because OCaml's type system will not allow float > to unify with string. You need some context where type inference will allow > different possibilities, like a tuple containing both a float and a string: > > # let f = function > | (y : float), _ -> print_float y > | _, (s : string) -> print_string s;; > > or a polymorphic variant containing either a float or a string: > > # let f = function > | `F (y : float) -> print_float y > | `S (s : string) -> print_string s;; > val f : [< `F of float | `S of string ] -> unit = <fun> > Thanks. Several people also replied clarifying that the type annotations were merely used for aiding the type inferencer. Serge -- Serge Aleynikov Routing R&D, IDT Telecom Tel: +1 (973) 438-3436 Fax: +1 (973) 438-1464 ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [Caml-list] parameterized pattern 2006-11-06 21:15 parameterized pattern Serge Aleynikov 2006-11-06 23:58 ` [Caml-list] " Jon Harrop @ 2006-11-06 23:59 ` Martin Jambon 2006-11-08 23:55 ` Lukasz Stafiniak 2 siblings, 0 replies; 15+ messages in thread From: Martin Jambon @ 2006-11-06 23:59 UTC (permalink / raw) To: Serge Aleynikov; +Cc: caml-list On Mon, 6 Nov 2006, Serge Aleynikov wrote: > Hi, > > The section 6.6 of OCaml's manual introduces a notion of "Parenthesized > patterns". > > I couldn't find any examples on how to use this feature, and brute-force > approach doesn't work: > > # match 1.0 with > (y : float) -> print_float y > | (s : string) -> print_string s;; > This pattern matches values of type string > but is here used to match values of type float > # > > Could anyone point at a suitable resource? What you wrote is equivalent to: match ((1.0 : float) : string) with y -> print_float y | s -> print_string s In OCaml, match-with is a test against the structure of a value, not its type. What you want to do is not currently possible in OCaml. Martin -- Martin Jambon, PhD http://martin.jambon.free.fr ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [Caml-list] parameterized pattern 2006-11-06 21:15 parameterized pattern Serge Aleynikov 2006-11-06 23:58 ` [Caml-list] " Jon Harrop 2006-11-06 23:59 ` Martin Jambon @ 2006-11-08 23:55 ` Lukasz Stafiniak 2006-11-09 1:45 ` brogoff 2006-11-09 5:18 ` Jon Harrop 2 siblings, 2 replies; 15+ messages in thread From: Lukasz Stafiniak @ 2006-11-08 23:55 UTC (permalink / raw) To: Serge Aleynikov; +Cc: caml-list You can do this kind of ad-hoc polymorphism with with G'Caml: http://web.yl.is.s.u-tokyo.ac.jp/~furuse/gcaml/ BTW, I think that G'Caml deserves more attention on this list (see http://lambda-the-ultimate.org/node/1278). (I've even thought that it is still a patch to OCaml 2.0, but it seems to be up-to-date now.) You can check also a related attempt in Meta OCaml, by Oleg Kiselyov: http://pobox.com/~oleg/ftp/ML/gprint/ (search the archives for "Generic print function"). On 11/6/06, Serge Aleynikov <serge@hq.idt.net> wrote: > Hi, > > The section 6.6 of OCaml's manual introduces a notion of "Parenthesized > patterns". > > I couldn't find any examples on how to use this feature, and brute-force > approach doesn't work: > > # match 1.0 with > (y : float) -> print_float y > | (s : string) -> print_string s;; > This pattern matches values of type string > but is here used to match values of type float > # > > Could anyone point at a suitable resource? > > Thanks. > > Serge > > -- > Serge Aleynikov > Routing R&D, IDT Telecom > Tel: +1 (973) 438-3436 > Fax: +1 (973) 438-1464 > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [Caml-list] parameterized pattern 2006-11-08 23:55 ` Lukasz Stafiniak @ 2006-11-09 1:45 ` brogoff 2006-11-09 5:19 ` Jon Harrop 2006-11-09 5:18 ` Jon Harrop 1 sibling, 1 reply; 15+ messages in thread From: brogoff @ 2006-11-09 1:45 UTC (permalink / raw) To: caml-list On Thu, 9 Nov 2006, Lukasz Stafiniak wrote: > You can do this kind of ad-hoc polymorphism with with G'Caml: > > http://web.yl.is.s.u-tokyo.ac.jp/~furuse/gcaml/ > > BTW, I think that G'Caml deserves more attention on this list (see > http://lambda-the-ultimate.org/node/1278). (I've even thought that it > is still a patch to OCaml 2.0, but it seems to be up-to-date now.) The GCaml docs say (and have said for at least a year) that it doesn't support all of OCaml, neither objects nor polymorphic variants. I'd guess that it doesn't support recursive modules either. It's a pity, as I've often wished that OCaml supported the extensional polymorphism that GCaml has, but I don't think that's going to happen. It would probaby make more sense to create a separate language at this point, since OCaml is complicated enough. -- Brian ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [Caml-list] parameterized pattern 2006-11-09 1:45 ` brogoff @ 2006-11-09 5:19 ` Jon Harrop 2006-11-09 8:51 ` skaller 0 siblings, 1 reply; 15+ messages in thread From: Jon Harrop @ 2006-11-09 5:19 UTC (permalink / raw) To: caml-list On Thursday 09 November 2006 01:45, brogoff wrote: > It's a pity, as I've often wished that OCaml supported the extensional > polymorphism that GCaml has, but I don't think that's going to happen. > It would probaby make more sense to create a separate language at this > point, since OCaml is complicated enough. I think F# provides some form of extensional polymorphism. I'm not convinced that it is a good idea yet... -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. Objective CAML for Scientists http://www.ffconsultancy.com/products/ocaml_for_scientists ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [Caml-list] parameterized pattern 2006-11-09 5:19 ` Jon Harrop @ 2006-11-09 8:51 ` skaller 2006-11-09 16:22 ` brogoff 0 siblings, 1 reply; 15+ messages in thread From: skaller @ 2006-11-09 8:51 UTC (permalink / raw) To: Jon Harrop; +Cc: caml-list On Thu, 2006-11-09 at 05:19 +0000, Jon Harrop wrote: > On Thursday 09 November 2006 01:45, brogoff wrote: > > It's a pity, as I've often wished that OCaml supported the extensional > > polymorphism that GCaml has, but I don't think that's going to happen. > > It would probaby make more sense to create a separate language at this > > point, since OCaml is complicated enough. > > I think F# provides some form of extensional polymorphism. I'm not convinced > that it is a good idea yet... Well FYI Felix has traditional (open) overloading, but since it doesn't allow traditional C++ style dependent name lookup because that would destroy parametricity of polymorphic functions, something else was needed. So it now has first order typeclasses to solve this problem. [By first order I mean typeclass arguments can be types but not type constructors, i.e. it only supports data polymorphism, not functorial polymorphism/polyadic programming] This allows you to write stuff like this: typeclass Integer[t] { virtual fun add: t * t -> t; } fun sum3[t where Integer[t]] (x:t,y:t,z:t)=> add(x,add(y,z)); print (sum3(1,2,3)); instance Integer[int] { fun add: int * int -> int = "$1+$2"; // C code } open Integer[int]; print (add(add(1,2),3)); I'm not sure i really like typeclasses much, ML functors seem more general, and the coupling is explicit. -- John Skaller <skaller at users dot sf dot net> Felix, successor to C++: http://felix.sf.net ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [Caml-list] parameterized pattern 2006-11-09 8:51 ` skaller @ 2006-11-09 16:22 ` brogoff 2006-11-09 17:55 ` skaller 2006-11-14 23:12 ` Don Syme 0 siblings, 2 replies; 15+ messages in thread From: brogoff @ 2006-11-09 16:22 UTC (permalink / raw) To: skaller; +Cc: Jon Harrop, caml-list On Thu, 9 Nov 2006, skaller wrote: > On Thu, 2006-11-09 at 05:19 +0000, Jon Harrop wrote: > > On Thursday 09 November 2006 01:45, brogoff wrote: > > > It's a pity, as I've often wished that OCaml supported the extensional > > > polymorphism that GCaml has, but I don't think that's going to happen. > > > It would probaby make more sense to create a separate language at this > > > point, since OCaml is complicated enough. > > > > I think F# provides some form of extensional polymorphism. I just did a quick scan of some F# docs and I saw nothing. What did you have in mind? > > I'm not convinced that it is a good idea yet... For almost any given language feature, there will be people who like it, and people who don't. Do you think having class based OO in OCaml is a good idea? I find it useful, especially since OCaml records are far too restrictive, but I hope that in some future ML that there are other approaches as the class/object system is complex, and the interactions with "core ML + modules" is tricky. That said, the class system is being used and it won't go away, and some people really like it. > Well FYI Felix has traditional (open) overloading, but since it > doesn't allow traditional C++ style dependent name lookup because > that would destroy parametricity of polymorphic functions, > something else was needed. > > So it now has first order typeclasses to solve this problem. Did you consider GCaml style generic functions? -- Brian ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [Caml-list] parameterized pattern 2006-11-09 16:22 ` brogoff @ 2006-11-09 17:55 ` skaller 2006-11-14 23:12 ` Don Syme 1 sibling, 0 replies; 15+ messages in thread From: skaller @ 2006-11-09 17:55 UTC (permalink / raw) To: brogoff; +Cc: Jon Harrop, caml-list On Thu, 2006-11-09 at 08:22 -0800, brogoff wrote: > On Thu, 9 Nov 2006, skaller wrote: [] > > So it now has first order typeclasses to solve this problem. > > Did you consider GCaml style generic functions? Yes, but they seem a bit harder to implement. The closure is nice though. The example has a type like: { 'a -> 'a -> 'a < [| int -> int -> int | float -> float -> float |] } but I don't know how to unify alternatives. Feels a bit like a GLR parser .. you fork the unification with each case, drop a thread when unification fails, and merge successes into alternatives? -- John Skaller <skaller at users dot sf dot net> Felix, successor to C++: http://felix.sf.net ^ permalink raw reply [flat|nested] 15+ messages in thread
* RE: [Caml-list] parameterized pattern 2006-11-09 16:22 ` brogoff 2006-11-09 17:55 ` skaller @ 2006-11-14 23:12 ` Don Syme 2006-11-15 1:00 ` brogoff 1 sibling, 1 reply; 15+ messages in thread From: Don Syme @ 2006-11-14 23:12 UTC (permalink / raw) To: caml-list > I just did a quick scan of some F# docs and > I saw nothing. What did you have in mind? .NET type parameters are extensional, i.e. "you can always find out what 'a is at runtime". In particular in C# you can just write "typeof(T)", and in F# "(type 'a)", in each case getting a System.Type value. Supporting exact runtime types was a design decision we made in the early design stages for .NET generics. As a result all .NET languages that support generics (polymorphism) have extensional polymorphism. It gets used heavily in the kind of meta-activities we're all familiar with: marshalling, pretty-printing, debugging (yes, Visual Studio 2005 shows you the values of "T", except when they've been optimized away). It also gets used internally in some libraries for adhoc optimization purposes, e.g. generating efficient comparison functions for default .NET comparers based on type arguments, and the F# matrix library uses it to detect when generic matrices are really floating point matrices, hence thunking out to more efficient matrix routines. There are downsides to extensional polymorphism (e.g. you can wind up take up extra registers passing type parameters), but they don't seem to bite in practice. At the last ML Workshop a group at Cambridge University recently reported on an experiment to modify core-OCaml to pass runtime types, and IIRC saw no significant performance loss. FWIW if you're interested I'd also like to mention the huge impact OCaml had on the design of .NET generics and C# 2.0, which I've never properly described on this list. It was seeing and experiencing polymorphic programming in OCaml and SML that made us persevere with the long and torturous process of convincing Microsoft that they should add such an "experimental and academic" feature as generics to their flagship runtime and languages. Of course we were in reality just making 1970s ideas work in practice, but at least now even Visual Basic has generics. Cheers, Don -----Original Message----- From: caml-list-bounces@yquem.inria.fr [mailto:caml-list-bounces@yquem.inria.fr] On Behalf Of brogoff Sent: 09 November 2006 16:22 To: skaller Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] parameterized pattern On Thu, 9 Nov 2006, skaller wrote: > On Thu, 2006-11-09 at 05:19 +0000, Jon Harrop wrote: > > On Thursday 09 November 2006 01:45, brogoff wrote: > > > It's a pity, as I've often wished that OCaml supported the extensional > > > polymorphism that GCaml has, but I don't think that's going to happen. > > > It would probaby make more sense to create a separate language at this > > > point, since OCaml is complicated enough. > > > > I think F# provides some form of extensional polymorphism. I just did a quick scan of some F# docs and I saw nothing. What did you have in mind? > > I'm not convinced that it is a good idea yet... For almost any given language feature, there will be people who like it, and people who don't. Do you think having class based OO in OCaml is a good idea? I find it useful, especially since OCaml records are far too restrictive, but I hope that in some future ML that there are other approaches as the class/object system is complex, and the interactions with "core ML + modules" is tricky. That said, the class system is being used and it won't go away, and some people really like it. > Well FYI Felix has traditional (open) overloading, but since it > doesn't allow traditional C++ style dependent name lookup because > that would destroy parametricity of polymorphic functions, > something else was needed. > > So it now has first order typeclasses to solve this problem. Did you consider GCaml style generic functions? -- Brian _______________________________________________ Caml-list mailing list. Subscription management: http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list Archives: http://caml.inria.fr Beginner's list: http://groups.yahoo.com/group/ocaml_beginners Bug reports: http://caml.inria.fr/bin/caml-bugs ^ permalink raw reply [flat|nested] 15+ messages in thread
* RE: [Caml-list] parameterized pattern 2006-11-14 23:12 ` Don Syme @ 2006-11-15 1:00 ` brogoff 2006-11-15 1:36 ` Don Syme 0 siblings, 1 reply; 15+ messages in thread From: brogoff @ 2006-11-15 1:00 UTC (permalink / raw) To: caml-list On Tue, 14 Nov 2006, Don Syme wrote: > > I just did a quick scan of some F# docs and > > I saw nothing. What did you have in mind? > > .NET type parameters are extensional, i.e. "you can always find out what > 'a is at runtime". In particular in C# you can just write "typeof(T)", > and in F# "(type 'a)", in each case getting a System.Type value. > Supporting exact runtime types was a design decision we made in the > early design stages for .NET generics. As a mostly Java programmer now, I have to say I'm a bit envious. C# generics look a lot better to me than the Java 5 ones. What I didn't notice while looking at the F# docs was a way to declare a generic function/value, where by "generic" here I mean in the GCaml/CLOS sense, not the Java/Ada sense. Is something like that in F#, or planned? -- Brian ^ permalink raw reply [flat|nested] 15+ messages in thread
* RE: [Caml-list] parameterized pattern 2006-11-15 1:00 ` brogoff @ 2006-11-15 1:36 ` Don Syme 0 siblings, 0 replies; 15+ messages in thread From: Don Syme @ 2006-11-15 1:36 UTC (permalink / raw) To: brogoff, caml-list [ FWIW let's take discussions about F# off-list, e.g. to hubFS? ] > As a mostly Java programmer now, I have to say I'm a bit > envious. C# generics look a lot better to me than the Java 5 ones. Well, this comparison point certainly helped to persuade Microsoft management to do the feature. :-) > What I didn't notice while looking at the F# docs was a > way to declare a generic function/value, where by "generic" > here I mean in the GCaml/CLOS sense, not the Java/Ada sense. > Is something like that in F#, or planned? Yes and no, though the topic often comes up. Currently, operators are overloaded through a statically-resolved version of Ada-style trait constraints, which works well enough in practice. Haskell-style type classes or the proposed default parameters for Scala are other possible design points. These are a little less compelling when you can't redesign the whole .NET library design to take advantage of the feature, but still potentially worthwhile. Best wishes, Don -----Original Message----- From: caml-list-bounces@yquem.inria.fr [mailto:caml-list-bounces@yquem.inria.fr] On Behalf Of brogoff Sent: 15 November 2006 01:01 To: caml-list@yquem.inria.fr Subject: RE: [Caml-list] parameterized pattern On Tue, 14 Nov 2006, Don Syme wrote: > > I just did a quick scan of some F# docs and > > I saw nothing. What did you have in mind? > > .NET type parameters are extensional, i.e. "you can always find out what > 'a is at runtime". In particular in C# you can just write "typeof(T)", > and in F# "(type 'a)", in each case getting a System.Type value. > Supporting exact runtime types was a design decision we made in the > early design stages for .NET generics. As a mostly Java programmer now, I have to say I'm a bit envious. C# generics look a lot better to me than the Java 5 ones. What I didn't notice while looking at the F# docs was a way to declare a generic function/value, where by "generic" here I mean in the GCaml/CLOS sense, not the Java/Ada sense. Is something like that in F#, or planned? -- Brian _______________________________________________ Caml-list mailing list. Subscription management: http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list Archives: http://caml.inria.fr Beginner's list: http://groups.yahoo.com/group/ocaml_beginners Bug reports: http://caml.inria.fr/bin/caml-bugs ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [Caml-list] parameterized pattern 2006-11-08 23:55 ` Lukasz Stafiniak 2006-11-09 1:45 ` brogoff @ 2006-11-09 5:18 ` Jon Harrop 2006-11-09 16:42 ` micha 1 sibling, 1 reply; 15+ messages in thread From: Jon Harrop @ 2006-11-09 5:18 UTC (permalink / raw) To: caml-list On Wednesday 08 November 2006 23:55, Lukasz Stafiniak wrote: > You can do this kind of ad-hoc polymorphism with with G'Caml: > > http://web.yl.is.s.u-tokyo.ac.jp/~furuse/gcaml/ > > BTW, I think that G'Caml deserves more attention on this list (see > http://lambda-the-ultimate.org/node/1278). (I've even thought that it > is still a patch to OCaml 2.0, but it seems to be up-to-date now.) I am certainly interested in GCaml but: . Where do you get it from (the CVS doesn't seem to have it)? . How fast is it? . Does it support native code? . What version of OCaml is it based upon? -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. Objective CAML for Scientists http://www.ffconsultancy.com/products/ocaml_for_scientists ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [Caml-list] parameterized pattern 2006-11-09 5:18 ` Jon Harrop @ 2006-11-09 16:42 ` micha 0 siblings, 0 replies; 15+ messages in thread From: micha @ 2006-11-09 16:42 UTC (permalink / raw) To: caml-list Jon Harrop schrieb: > > I am certainly interested in GCaml but: > > . Where do you get it from (the CVS doesn't seem to have it)? > I did: export CVSROOT=:pserver:anoncvs@camlcvs.inria.fr:/caml cvs login >empty password cvs co -r gcaml3090 ocaml this gives me an ocaml source with gcamllib and README.gcaml... Maybe this is the right source... cheers Michael ^ permalink raw reply [flat|nested] 15+ messages in thread
end of thread, other threads:[~2006-11-15 1:36 UTC | newest] Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2006-11-06 21:15 parameterized pattern Serge Aleynikov 2006-11-06 23:58 ` [Caml-list] " Jon Harrop 2006-11-07 0:12 ` Serge Aleynikov 2006-11-06 23:59 ` Martin Jambon 2006-11-08 23:55 ` Lukasz Stafiniak 2006-11-09 1:45 ` brogoff 2006-11-09 5:19 ` Jon Harrop 2006-11-09 8:51 ` skaller 2006-11-09 16:22 ` brogoff 2006-11-09 17:55 ` skaller 2006-11-14 23:12 ` Don Syme 2006-11-15 1:00 ` brogoff 2006-11-15 1:36 ` Don Syme 2006-11-09 5:18 ` Jon Harrop 2006-11-09 16:42 ` micha
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox