From: skaller <skaller@users.sourceforge.net>
To: Jon Harrop <jon@jdh30.plus.com>
Cc: caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] Polymorphism and the "for" loop
Date: 23 Oct 2004 00:02:26 +1000 [thread overview]
Message-ID: <1098453745.7584.58.camel@pelican.wigram> (raw)
In-Reply-To: <200410220838.49340.jon@jdh30.plus.com>
On Fri, 2004-10-22 at 17:38, Jon Harrop wrote:
> Surely a "procedure"
> returns no information, which can be achieved by returning the only value
> from a type representing a singleton set, i.e. _the_ value of the type unit.
Clearly is can, since that what Ocaml does. But as I pointed
out it fails to prevent certain abuses.
> > type void = []
>
> Why not "`void"?
Because `void is a value so if you write:
type void = [`void]
all you've done is define a non-canonical unit,
which is categorically *not* a void :)
> I've been wondering about this recently: how do the compilers store types
> which contain "unit".
If the compiler is smart it just optimises it away.
Felix usually optimises it away. In Ocaml, it can't
always be optimised away:
> For example, if we have a tree:
>
> type 'a 'b tree = Leaf of 'a | Node of 'b * 'a 'b tree * 'a 'b tree
>
> Does a "unit unit tree" take up less space than a "int int tree"?
No, this isn't possible usually, because any polymorphic
algorithm using this data structure expects a fixed
layout in memory.
So in this case, a dummy value must exist.
This problem can't arise in Felix or C++, because
they both use extensional polymorphism
(compile time instantiation).
The only way this might work in Ocaml is if
the pointer to value was replaced by NULL,
but that would require a NULL check, and slow
down the algorithm for all types just to save
some memory in degenerate cases.
> The reason I'm asking is that it might be nice to generalise data structures
> as much as possible and then specialise them using "unit" arguments.
If you use a functor, the compiler might be able to
optimise it away.
I think you can assume most uses like this:
let f () = ...
f ()
actually don't pass the () around: that's such an obvious
thing to optimise. Still, there must be a version
of f that isn't optimised, otherwise you couldn't
pass it to a higher order function.
--
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850,
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net
-------------------
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
next prev parent reply other threads:[~2004-10-22 14:02 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-10-21 18:50 Jon Harrop
2004-10-21 19:11 ` David Brown
2004-10-22 1:19 ` skaller
2004-10-22 2:22 ` Robert M. Solovay
2004-10-22 6:19 ` skaller
2004-10-22 7:38 ` Jon Harrop
2004-10-22 8:06 ` David Brown
2004-10-22 9:39 ` Andreas Rossberg
2004-10-22 8:13 ` William Lovas
2004-10-22 8:17 ` Jon Harrop
2004-10-22 14:02 ` skaller [this message]
2004-10-22 14:31 ` David Brown
2004-10-22 17:06 ` Brian Hurt
2004-10-22 4:17 ` William Lovas
2004-10-22 6:42 ` skaller
2004-10-21 19:12 ` Ville-Pertti Keinonen
2004-10-21 19:20 ` Jon Harrop
2004-10-21 19:42 ` Ville-Pertti Keinonen
2004-10-23 11:41 ` Richard Jones
2004-10-23 12:06 ` Matti Jokinen
2004-10-22 14:07 Harrison, John R
2004-10-22 14:45 ` John Carr
2004-10-23 20:58 ` Christophe Raffalli
2004-10-22 14:47 ` Andreas Rossberg
2004-10-22 16:00 Harrison, John R
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=1098453745.7584.58.camel@pelican.wigram \
--to=skaller@users.sourceforge.net \
--cc=caml-list@inria.fr \
--cc=jon@jdh30.plus.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