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
variables in 'let rec'
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2000-03-24 (15:31)
From: Xavier Leroy <Xavier.Leroy@i...>
Subject: Re: variables in 'let rec'
> I'm currently writing a program where I need to create both variables
> and functions in a 'let rec'. The problem is that it is not allowed to
> define a simple variable (i.e not a variant) in a 'let rec', because it
> would be able to write things such as:
> # let rec x = x+1;;
> This kind of expression is not allowed as right-hand side of `let rec'
> This error message seems okay to me.
> However, the error message also occurs in situations where there is no
> problem:
> # let rec x = fact 5    (* using usual fact *)
>               ^^^^^^
>   and even = function
>   | 0 -> true
>   | n -> odd (n-1)
>   and odd = function
>   | 1 -> true
>   | n -> even (n-1);;
> This kind of expression is not allowed as right-hand side of `let rec'
> What is "funny" is that it is possible to write 'Some (fact 5)'...

You're correct that the example above could be accepted because "x"
could be computed in advance (fact 5 doesn't depend on the variables of
the recursive equations).  However, this would complicate both the
well-formedness test and the compilation scheme for "let rec".  We
chose to err on the conservative side here.  Notice that your example
can (and should, in the interest of clarity) be written
        let x = fact 5
        let rec even = ... and odd = ...

> Anyway, if this problem is really to complex, or undecidable, or not
> worth the effot, why not allow any variable definition in a 'let rec'
> and print a warning message? Then, the programmer would decide if the
> definition may lead to an error...

We would have no guarantees that the generated code for the "let rec"
is correct, e.g. with the current compilation scheme it could crash
the program.  Better be restrictive here!

- Xavier Leroy