r/ocaml•
queus

No point free definitions in let rec ?

Here is my example. It is about converting the a number to its English representation: 1002 -> one thousand two. And here is the relevant fragment of the code

and up_to_thousand n = upto 1000L 100L "hundred" up_to_million n
and up_to_million n  = upto 1000_000L 1000L "thousand" up_to_billion n
and up_to_billion n  = upto 1000_000_000L 1000_000L "million" up_to_trillion n
and up_to_trillion n = upto 1000_000_000_000L 1000_000_000L "billion" (fun _ -> assert false) n

and upto max factor word next = function (* more stuff *)

this works. But this

and up_to_thousand = upto 1000L 100L "hundred" up_to_million
and up_to_million  = upto 1000_000L 1000L "thousand" up_to_billion
and up_to_billion  = upto 1000_000_000L 1000_000L "million" up_to_trillion
and up_to_trillion = upto 1000_000_000_000L 1000_000_000L "billion" (fun _ -> assert false)

and upto max factor word next = function (* more stuff *)

does not. The only difference being, that the parameter n is removed from both sides in the second case. Just curious if this is limitation in the compiler or something more fundamental, which I'm missing

Posted Nov 5, 2016, 9:36 PM UTC

48
jdh30•

Can you give a complete working example code or error message?

2
queusOP•

Working code here - http://exercism.io/submissions/8d2a355950564a45875e07d205817798

remove the n from both sides and it fails

ocamlbuild say.byte                                                                          
+ .opam/4.03.0+flambda/bin/ocamlc.opt -c -o say.cmo say.ml                                                          
File "say.ml", line 46, characters 21-60:                                                                                     
Error: This kind of expression is not allowed as right-hand side of `let rec' 
1
jeffsco•

The core language allows let rec only for the definition of functions; i.e., the value bound to the identifier must be a lambda. There is a language extension that allows certain restricted forms: Recursive Definitions of Values.

The reason to forbid simple definitions like let rec a = b is (just guessing) to avoid definitions like let rec a = a or let rec a = b and b = a.

Update

(@andrejbauer explains: the real problem is with a definition like let rec b = not b. And he isn't just guessing :-)

2
andrejbauer•

The reason for limiting let rec to functions has to do with the fact that OCaml is a call-by-value language. In a call-by-value language the values of a given type need not form a structure that supports recursive definitions (they form a predomain rather than a domain). For instance, the type bool of booleans has precisely two values, false and true. Caveat: it is not the case that bool contains bottom/undefined/divergence as a value. There is a computation of type bool which diverges, but that computation never results in a value. This is different from call-by-name languages such as Haskell. Now, which of the two values of type bool satisfies let rec (b : bool) = not b? Neither! Which natural number satisfies let rec n = n + 1? None! So we cannot have arbitrary recursion. Function types do support recursive definitions (because they form domains), and so we allow those.

6
queusOP•

Thanks, but now I just must find out what a predomain is, how it differs from a domain.

Not that, I mind..

1
andrejbauer•

If you know what a domain is, you can imagine a predomain is like a domain which may be missing the bottom element. For instance:

  • predomain: a partially ordered set (D, ≤) in which every increasing sequence of elements has a supremum
  • a domain: a predomain which also has the smallest element ⊥

The Tarski-Knaster theorem, which gurantees existence of fixed points and therefore gives sound semantics to recursive definitions, works for domains but not for predomains.

3
arbitrarycivilian•

Thanks, this is exactly the argument I need to use to explain to people why Haskell does not (and cannot) represent the natural numbers.

1
queusOP•

Thanks. It makes sense, and I was missing that

1
[deleted]•

[deleted]

1
[deleted]•

[deleted]

1