caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Newbie: Aliases for polymorphic variants
@ 2004-02-28 12:44 andrew cooke
  0 siblings, 0 replies; only message in thread
From: andrew cooke @ 2004-02-28 12:44 UTC (permalink / raw)
  To: ocaml list


Hi,

I've just been looking at polymorphic variants.  They seem quite neat. 
However, I have some questions....

1 - It seems to me that they could be particularly useful for solving
those nasty practical problems that occur way down the line with old
systems where you want to combine previously separate features in some
way.

However, to do that, it seems like all code has to use polymorphic
variants from the start.  Is that right?

That seems OK, except that they seem to make for a lot more typing.  Is
there a syntax for defining something like type aliases?  I'm looking for
something like:

alias 'a Node = `Node int * 'a `Node * 'a `Node

so that I can type just 'a Node instead of `Node int * 'a `Node * 'a `Node
everywhere

2 - Alternatively, say I have

type 'a beginWithF = [Foo of 'a | Flob of int * 'a]
type 'a beginWithG = [Goo of 'a]

is there a syntax something like:

type 'a beginWithFOrG = ['a beginWithF | 'a beginWithG]

or do I have to explicitly list all the separate constructors?

3 - Am I right in thinking that fucntions cannot be defined with pattern
matching to the left of "=" in OCaml?  (ie not like Haskell)  If this were
possible then I think you'd have ad-hoc polymorphism for free because you
could have:

fun sum (`Integer i) (`Integer j) = `Integer (i + j)
fun sum (`Real x) (`Real y) = `Real (x +. y)

Does that make sense?

4 - Taking (3) further, couldn't you give all types an implicit
polymorphic variant constructor?  Then you could have types like [int |
float] directly.  Does that make sense?

Thanks,
Andrew

-- 
  __ _ __ ___  ___| |_____   work web site: http://www.ctio.noao.edu/~andrew
 / _` / _/ _ \/ _ \ / / -_)  personal web site: http://www.acooke.org/andrew
 \__,_\__\___/\___/_\_\___|  personal gallery: http://www.acooke.org/pancito

-------------------
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] only message in thread

only message in thread, other threads:[~2004-02-28 12:44 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-02-28 12:44 [Caml-list] Newbie: Aliases for polymorphic variants andrew cooke

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).