Version française
Home     About     Download     Resources     Contact us    
Browse thread
Question on language design (keywords vs Pervasives)
[ 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: Question on language design (keywords vs Pervasives)
> I'm curious about some interesting language design choices in ocaml
> 3.0.
> 
> * "raise", "exn" are not keywords, but definitions in the Pervasives module.
> 
> This means that I can rebind them, for ex., like this:
[...]
> Similarly for other types in the Pervasives module, like int,
> etc. (However, "exception", for declaring a new exception is a
> reserved word).

You encounter here the border between syntax, language design choices
and semantics: ``exception'' must be a keyword, since it introduces a
syntactic construct (precisely the addition of a new constructor to
the predefined type exn: ident of type).

On the other hand, ``raise'' does not introduce a syntactic construct
since exception raising is parsed as a regular application
(application of the identifier raise to an exception value). Hence,
strictly speaking, raise has not to be a keyword. However there are
two good reasons to turn raise into a keyword:

1) To prevent the redefinition of the predefined raise primitive.
2) To parse the exception raising slightly differently than the
regular application: for instance, we could spare some parentheses in
case of a functional exception constructor, and write 
   raise Invalid_argument "divide"
instead of
   raise (Invalid_Argument "divide").

This is as reasonable as the current syntactic status of raise: indeed,
this scheme has been used in Caml for a long time.

> I can speculate that the intention is precisely that praogrammers be
> able to rebind them at will, or maybe that it simplifies implementing
> interpreters/compilers, but I don't know for sure. Could anyone
> (preferably someone involved in the design) comment on the rationale
> for these choices?
> 
> Thanks,
> 
> Fermin Reig
> 
> ------------------------------------------------------
> Department of Computing Science, University of Glasgow
> http://www.dcs.gla.ac.uk/~reig

The ``users can rebind'' argument is definitively what we intended
when we decided to allow the rebinding of usual operators (infix or
prefix symbols, such as +, -, *, ...). For ``raise'' this is not so
clear, since redefining ``raise'' is a very bad idea to obtain a
readable Caml program. Anyway, this treatment of ``raise'' saved one
or 2 lines in the parser of the language, and several characters in
the lexer definition. Long time ago, we were hesitating before addding
that little amount of code to the compiler. Now that the parser has
become considerably bigger, we may give it a second thought! In
particular, if we consider that knowing for sure that ``raise'' indeed
raises exceptions is important for the programmer... (and probably for
automatic tools that can try to analyse exception handling in Caml
programs).

Pierre Weis

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