English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Dummy polymorphic constructors
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-12-17 (01:10)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] Dummy polymorphic constructors
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