Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Re: Constante NIL
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Pierre Weis <Pierre.Weis@i...>
Subject: Re: Constante NIL
> > 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

#(any : int);;
0 : int

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

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

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


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