Re: variables in 'let rec'

From: Xavier Leroy (Xavier.Leroy@inria.fr)
Date: Thu Mar 23 2000 - 16:57:36 MET

  • Next message: David Gurr: "Re: Interpreter vs hardware threads"

    > 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



    This archive was generated by hypermail 2b29 : Fri Mar 24 2000 - 16:35:19 MET