From: Jacques GARRIGUE <garrigue@math.nagoya-u.ac.jp>
To: tom.primozic@gmail.com
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Polymorphic Variants
Date: Wed, 17 Jan 2007 11:19:27 +0900 (JST) [thread overview]
Message-ID: <20070117.111927.2004173151.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <c1490a380701161232u7b60e286l163a4a5837d3083c@mail.gmail.com>
From: Tom <tom.primozic@gmail.com>
> So... why actually are polymorphic variants useful? Why can't they simply be
> implemented as normal, concrete (or how would you call them? ...) variants?
The original motivation was for the LablTk library, where some types
(index for instance) have lots of small variations. At that point
there where several options
* overloading (but ocaml loathes overloading, you could say that the
total absence of overloading is essential to the language)
* refinement types: define a type with all constructors, and have
restricted versions of it where only some constructors are allowed
* full polymorphism, i.e. polymorphic variants
If you eliminate the first option, then the choice is between
refinement types and polymorphic variants. Refinement types are rather
attractive: they combine precise typing with explicit declarations.
The arguments in favour of polymorphic variants are
* they were somehow easier to add, as they are an orthogonal extension
of the type system
* one can add new cases afterwards.
* they are defined structurally: two identical definitions in two
different modules are equivalent. This can be pretty handy at times.
* you don't need to open a module to use them: mixes nicely with ocaml
programming style
>From modularity considerations, it ends up being nicer to write
type t = [`A of int |`B of bool]
type u = [t | `C of char]
than
type u = A of int | B of bool | C of char
type t = u[A B]
Afterwards I discovered that, using some idioms, they also allow
extensible programs in a very concise way. (Much more concise than
objects, when they are relevant.)
> Doesn't the use of polymorphic variants just mess up the function type?
What do you mean by "mess up the function type"?
If you mean that, without type annotations, types and errors become
very hard to read, this is true, but the manual explicitely encourages
to add type annotations :-)
> I'm not orthogonally against polymorphic variants, it's just that I am
> looking for an alternative concept that could be used instead... Maybe
> subtyped records?
In terms of expressiveness, you can simulate polymorphic variants
using objects (which are subtyped records,) but this is often much
more verbose, and requires higher order types. I have some code using
objects (visitor pattern), recursive modules, lazyness, and private
row types, in an utterly non trivial way, just to do what can be done
by standard recursive function definitions using polymorphic variants...
Jacques Garrigue
next prev parent reply other threads:[~2007-01-17 2:19 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-01-16 20:32 Tom
2007-01-16 20:49 ` [Caml-list] " Seth J. Fogarty
2007-01-16 21:05 ` Tom
2007-01-16 21:23 ` Seth J. Fogarty
2007-01-16 21:45 ` Edgar Friendly
2007-01-16 22:18 ` Lukasz Stafiniak
2007-01-17 5:55 ` skaller
2007-01-17 0:30 ` Jonathan Roewen
2007-01-17 2:19 ` Jacques GARRIGUE [this message]
2007-01-17 3:24 ` Christophe TROESTLER
2007-01-18 2:12 ` Jacques Garrigue
2007-01-17 6:09 ` skaller
2007-01-17 13:34 ` Andrej Bauer
2007-01-17 21:13 ` Tom
2007-01-17 22:53 ` Jon Harrop
2007-01-17 23:07 ` Tom
[not found] ` <200701172349.53331.jon@ffconsultancy.com>
[not found] ` <c1490a380701180407j670a7cccyb679c71fde20aa4b@mail.gmail.com>
2007-01-18 16:23 ` Fwd: " Tom
2007-01-18 21:14 ` Jon Harrop
2007-01-19 9:26 ` Dirk Thierbach
2007-01-19 10:35 ` Tom
2007-01-19 11:14 ` Dirk Thierbach
2007-01-19 12:03 ` Tom
2007-01-18 21:43 ` Christophe TROESTLER
2007-01-18 1:28 ` Jacques Garrigue
2007-01-18 1:46 ` Jon Harrop
2007-01-18 4:05 ` skaller
2007-01-18 6:20 ` Jacques Garrigue
2007-01-18 9:48 ` skaller
2007-01-18 12:23 ` Tom
-- strict thread matches above, loose matches on Subject: below --
2002-04-17 9:49 [Caml-list] Polymorphic variants John Max Skaller
2002-04-17 10:43 ` Remi VANICAT
2002-04-17 23:49 ` John Max Skaller
2002-04-18 1:23 ` Jacques Garrigue
2002-04-18 9:04 ` John Max Skaller
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20070117.111927.2004173151.garrigue@math.nagoya-u.ac.jp \
--to=garrigue@math.nagoya-u.ac.jp \
--cc=caml-list@yquem.inria.fr \
--cc=tom.primozic@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox