Re: Constante NIL

From: Pierre Weis (Pierre.Weis@inria.fr)
Date: Mon Jun 14 1999 - 22:48:19 MET DST


From: Pierre Weis <Pierre.Weis@inria.fr>
Message-Id: <199906142048.WAA24106@pauillac.inria.fr>
Subject: Re: Constante NIL
To: Xavier.Leroy@inria.fr (Xavier Leroy)
Date: Mon, 14 Jun 1999 22:48:19 +0200 (MET DST)
In-Reply-To: <19990602210210.48838@pauillac.inria.fr> from "Xavier Leroy" at Jun 2, 99 09:02:10 pm

> > N'est t'il pas concevable de rajouter a Objectif Caml une constante
> > NIL, UNDEF ou je sais pas trop comment la nommer dont la valeur serait
> > '_a. Ceci permettrait d'initialiser "par defaut", par exemples,
> > les variables d'instances d'une classe lorsque l'on ne possede aucune
> > valeur par defaut du bon type. Je sais qu'il y'a toujours la possibilite
> > d'utiliser le type 'a option mais ca devient lourd a gerer...

As Xavier pointed out a nil constant is fairly difficult and runtime
consuming to implement in Caml. On the type level, you cannot assign
it the ``for all 'a. 'a'' type scheme, since otherwise you would get
as many ``bus errors'' as you want (nil can be considered a function
with such a type scheme). Uses of Obj.magic has also to be prohibited
for the same reasons (fairly too difficult to avoid bus error).

> Le problème de la constante "nil" est d'assurer qu'elle n'est pas
> utilisée comme une valeur "normale" du type en question.

Right. So why not considering a nil construct that IS always a value
of the corresponding type ?

You may consider to add a special construct ``any'' to the language,
known by the typechecker and the compiler. The typechecker would
ensure that ``any'' is never used in a (fully) polymorphic way, and
the compiler may generate the code to build a value of the type
assigned to ``any'' by the typechecker. This way, you would get for
instance:

#(any : int);;
0 : int

#(any : string);;
"" : string

#(any : 'a list);;
[] : 'a list

But still, any is not a ``nil'' polymorphic value:

#any;;

Compilation Failed: cannot create a value of type 'a

And this could work just fine for more complicated (and polymorphic)
data structures:

#type 'a dlist = {mutable hd : 'a dlist; mutable tl : 'a dlist};;
Type dlist is defined

#let l = (any : 'a dlist);;
Value l is dlist0
      where dlist0 = {hd=dlist0; tl=dlist0} : '_a dlist

Hope this helps,

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://cristal.inria.fr/~weis/



This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:23 MET