caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Dummy polymorphic constructors
@ 2004-12-16 19:22 Alex Baretta
  2004-12-16 22:38 ` [Caml-list] " John Prevost
  2004-12-17  1:10 ` Jacques Garrigue
  0 siblings, 2 replies; 6+ messages in thread
From: Alex Baretta @ 2004-12-16 19:22 UTC (permalink / raw)
  To: Ocaml, Luca Pascali, Ernesto Ferrari, Domenico Cosentino; +Cc: Gerd Stolpmann

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?

What do you guys think of the following patch?

Alex

diff -Naur stable/parsing/parser.mly patched/parsing/parser.mly
--- stable/parsing/parser.mly   2004-12-16 20:15:53.000000000 +0100
+++ patched/parsing/parser.mly  2004-12-16 20:16:16.000000000 +0100
@@ -10,6 +10,9 @@
  /*                                                                     */
  /***********************************************************************/

+/* Patched by Baretta DE&IT to add support for empty variant types */
+
+
  /* $Id: parser.mly,v 1.120 2004/05/19 12:15:19 doligez Exp $ */

  /* The parser definition */
@@ -1285,8 +1288,8 @@
        { mktyp(Ptyp_variant([$2], true, None)) }
    | LBRACKET BAR row_field_list RBRACKET
        { mktyp(Ptyp_variant(List.rev $3, true, None)) }
-  | LBRACKET row_field BAR row_field_list RBRACKET
-      { mktyp(Ptyp_variant($2 :: List.rev $4, true, None)) }
+  | LBRACKET row_field_list RBRACKET
+      { mktyp(Ptyp_variant(List.rev $2, true, None)) }
    | LBRACKETGREATER opt_bar row_field_list RBRACKET
        { mktyp(Ptyp_variant(List.rev $3, false, None)) }
    | LBRACKETGREATER RBRACKET
@@ -1297,7 +1300,8 @@
        { mktyp(Ptyp_variant(List.rev $3, true, Some (List.rev $5))) }
  ;
  row_field_list:
-    row_field                                   { [$1] }
+    /* empty */                                 { [] }
+  | row_field                                   { [$1] }
    | row_field_list BAR row_field                { $3 :: $1 }
  ;
  row_field:

-- 
*********************************************************************
http://www.barettadeit.com/
Baretta DE&IT
A division of Baretta SRL

tel. +39 02 370 111 55
fax. +39 02 370 111 54

Our technology:

The Application System/Xcaml (AS/Xcaml)
<http://www.asxcaml.org/>

The FreerP Project
<http://www.freerp.org/>


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Dummy polymorphic constructors
  2004-12-16 19:22 Dummy polymorphic constructors Alex Baretta
@ 2004-12-16 22:38 ` John Prevost
  2004-12-17  7:42   ` Alex Baretta
  2004-12-17  7:59   ` skaller
  2004-12-17  1:10 ` Jacques Garrigue
  1 sibling, 2 replies; 6+ messages in thread
From: John Prevost @ 2004-12-16 22:38 UTC (permalink / raw)
  To: Alex Baretta; +Cc: Ocaml

On Thu, 16 Dec 2004 20:22:57 +0100, Alex Baretta <alex@barettadeit.com> wrote:
> 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.

I'm somewhat confused as to why this is different from simply
declaring a new opaque type:

type empty

Since there is no way to construct a value of the type, nor any way to
deconstruct such a value, how is it different?

John.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Dummy polymorphic constructors
  2004-12-16 19:22 Dummy polymorphic constructors Alex Baretta
  2004-12-16 22:38 ` [Caml-list] " John Prevost
@ 2004-12-17  1:10 ` Jacques Garrigue
  2004-12-17  9:03   ` skaller
  1 sibling, 1 reply; 6+ messages in thread
From: Jacques Garrigue @ 2004-12-17  1:10 UTC (permalink / raw)
  To: alex; +Cc: caml-list, luca, ernesto, dome, info

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Dummy polymorphic constructors
  2004-12-16 22:38 ` [Caml-list] " John Prevost
@ 2004-12-17  7:42   ` Alex Baretta
  2004-12-17  7:59   ` skaller
  1 sibling, 0 replies; 6+ messages in thread
From: Alex Baretta @ 2004-12-17  7:42 UTC (permalink / raw)
  To: Ocaml

John Prevost wrote:
>>type empty = [ ]

> 
> I'm somewhat confused as to why this is different from simply
> declaring a new opaque type:
> 
> type empty

Ah, this is interesting!

> Since there is no way to construct a value of the type, nor any way to
> deconstruct such a value, how is it different?
> 
> John.
> 

Not much, actually. I thought opaque types could only be *declared*.

module M : sig type t end = struct type t = *** end

I never realized that a type could be *defined* as opaque.

module M = struct type t end

Alex

-- 
*********************************************************************
http://www.barettadeit.com/
Baretta DE&IT
A division of Baretta SRL

tel. +39 02 370 111 55
fax. +39 02 370 111 54

Our technology:

The Application System/Xcaml (AS/Xcaml)
<http://www.asxcaml.org/>

The FreerP Project
<http://www.freerp.org/>


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Dummy polymorphic constructors
  2004-12-16 22:38 ` [Caml-list] " John Prevost
  2004-12-17  7:42   ` Alex Baretta
@ 2004-12-17  7:59   ` skaller
  1 sibling, 0 replies; 6+ messages in thread
From: skaller @ 2004-12-17  7:59 UTC (permalink / raw)
  To: John Prevost; +Cc: Alex Baretta, Ocaml

On Fri, 2004-12-17 at 09:38, John Prevost wrote:
> On Thu, 16 Dec 2004 20:22:57 +0100, Alex Baretta <alex@barettadeit.com> wrote:
> > Currently ocaml does not support empty polymorphic variant sum types.
> > Say, I cannot write the following.
> > 
> > type empty = [ ]

> I'm somewhat confused as to why this is different from simply
> declaring a new opaque type:
> 
> type empty
> 
> Since there is no way to construct a value of the type, nor any way to
> deconstruct such a value, how is it different?

Polymorphic variants are extensible and algebraic .. 
an abstract type is neither.

-- 
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




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Dummy polymorphic constructors
  2004-12-17  1:10 ` Jacques Garrigue
@ 2004-12-17  9:03   ` skaller
  0 siblings, 0 replies; 6+ messages in thread
From: skaller @ 2004-12-17  9:03 UTC (permalink / raw)
  To: Jacques Garrigue; +Cc: alex, caml-list, luca, ernesto, dome, info

On Fri, 2004-12-17 at 12:10, Jacques Garrigue wrote:

> > type empty = [ ]

> 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.

How about: make it an error, unless -rectypes is specified.
In that case allow it. (Seems sensible to allow three cases:
(a) error (b) warning (c) no warning .. but I can't think
of how to provide that ..)

-- 
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




^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2004-12-17  9:03 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-16 19:22 Dummy polymorphic constructors 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
2004-12-17  9:03   ` skaller

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).