From: Pierre Weis <Pierre.Weis@inria.fr>
To: Xavier.Leroy@inria.fr (Xavier Leroy)
Cc: caml-list@inria.fr
Subject: Re: Constante NIL
Date: Mon, 14 Jun 1999 22:48:19 +0200 (MET DST) [thread overview]
Message-ID: <199906142048.WAA24106@pauillac.inria.fr> (raw)
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/
prev parent reply other threads:[~1999-06-14 20:49 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
1999-05-31 14:20 Vyskocil Vladimir
1999-06-01 16:16 ` Francois Rouaix
1999-06-02 7:27 ` Jean-Francois Monin
1999-06-02 19:02 ` Xavier Leroy
1999-06-14 20:48 ` Pierre Weis [this message]
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=199906142048.WAA24106@pauillac.inria.fr \
--to=pierre.weis@inria.fr \
--cc=Xavier.Leroy@inria.fr \
--cc=caml-list@inria.fr \
/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