Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Caml-list] parameterized pattern

6 views
Skip to first unread message

Serge Aleynikov

unread,
Nov 6, 2006, 4:19:10 PM11/6/06
to caml...@inria.fr
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

Jon Harrop

unread,
Nov 6, 2006, 7:03:11 PM11/6/06
to caml...@yquem.inria.fr
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

Martin Jambon

unread,
Nov 6, 2006, 7:07:20 PM11/6/06
to Serge Aleynikov
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

Serge Aleynikov

unread,
Nov 6, 2006, 7:17:24 PM11/6/06
to Jon Harrop
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

_______________________________________________

Lukasz Stafiniak

unread,
Nov 8, 2006, 6:58:54 PM11/8/06
to Serge Aleynikov
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").

brogoff

unread,
Nov 8, 2006, 8:48:22 PM11/8/06
to caml...@inria.fr
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

Jon Harrop

unread,
Nov 9, 2006, 12:24:00 AM11/9/06
to caml...@yquem.inria.fr
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

_______________________________________________

skaller

unread,
Nov 9, 2006, 3:54:33 AM11/9/06
to Jon Harrop
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

brogoff

unread,
Nov 9, 2006, 11:29:49 AM11/9/06
to skaller
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

micha

unread,
Nov 9, 2006, 11:45:30 AM11/9/06
to caml...@yquem.inria.fr
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:ano...@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

skaller

unread,
Nov 9, 2006, 12:59:50 PM11/9/06
to brogoff
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

_______________________________________________

Don Syme

unread,
Nov 14, 2006, 6:15:15 PM11/14/06
to caml...@yquem.inria.fr

> 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

brogoff

unread,
Nov 14, 2006, 8:02:57 PM11/14/06
to caml...@yquem.inria.fr
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?

Don Syme

unread,
Nov 14, 2006, 8:38:56 PM11/14/06
to brogoff, caml...@yquem.inria.fr
[ 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-lis...@yquem.inria.fr
[mailto:caml-lis...@yquem.inria.fr] On Behalf Of brogoff

0 new messages