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
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
> 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
--
Serge Aleynikov
Routing R&D, IDT Telecom
Tel: +1 (973) 438-3436
Fax: +1 (973) 438-1464
_______________________________________________
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").
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
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
_______________________________________________
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
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
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
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
_______________________________________________
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
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?
> 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