Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: alex@barettadeit.com
Cc: caml-list@inria.fr, luca@barettadeit.com,
	ernesto@barettadeit.com, dome@barettadeit.com,
	info@gerd-stolpmann.de
Subject: Re: [Caml-list] Dummy polymorphic constructors
Date: Fri, 17 Dec 2004 10:10:39 +0900 (JST)	[thread overview]
Message-ID: <20041217.101039.122623912.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <41C1E091.1040508@barettadeit.com>

From: Alex Baretta <alex@barettadeit.com>

> Currently ocaml does not support empty polymorphic variant sum types. 
> Say, I cannot write the following.
> 
> type empty = [ ]
> 
> This fails due to a syntax error rather than a typing error, which is a 
> sensible, given that the type expression I have written is actually 
> perfectly meaningful.
> 
> Is there a design decision behind this, or have the Caml breeders simply 
> overlooked the potential need for empty types?

This is completely intentional.
Maybe this should be a typing error rather than a syntactic one, to
make the intention clearer.
Empty sum types are not allowed, because, while they are not really
dangerous, they may delay type errors.
For instance, consider the following code:

let f = function `A -> 1 | `B -> 2
let g = function `C -> 3 | `D -> 4
let h x = f x + g x
                  ^
This expression has type [< `A | `B ] but is here used with type [< `C | `D ]
These two variant types have no intersection

If we allowed empty sums, h would be accepted with the type
  [] -> int
But it's clear that this function can never be called.

Note that you can still write unusable functions, as checking for
non-emptyness of types is not always worth the pain, but this is a bit
less trivial than the above code, and the type contains more
information on what happened.

let f = function `A x -> x | `B x -> x+1
let g = function `A f -> truncate f | `B x -> truncate (x +. 0.5)
let h x = f x + g x
val h : [< `A of float & int | `B of float & int ] -> int = <fun>

Another result of excluding the empty sum is that sums with only one
case can be made monomorphic.

let f (`A x) = x
val f : [ `A of 'a ] -> 'a = <fun>

Jacques Garrigue


  parent reply	other threads:[~2004-12-17  1:10 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-12-16 19:22 Alex Baretta
2004-12-16 22:38 ` [Caml-list] " John Prevost
2004-12-17  7:42   ` Alex Baretta
2004-12-17  7:59   ` skaller
2004-12-17  1:10 ` Jacques Garrigue [this message]
2004-12-17  9:03   ` 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=20041217.101039.122623912.garrigue@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=alex@barettadeit.com \
    --cc=caml-list@inria.fr \
    --cc=dome@barettadeit.com \
    --cc=ernesto@barettadeit.com \
    --cc=info@gerd-stolpmann.de \
    --cc=luca@barettadeit.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