I'm practicing with OCaml compiler and I'm doing a small assignment where we have to implement Church numerals defined as:
zz = pair c0 c0; ss = λp. pair ( snd p) ( plus c1 (snd p)); prd = λm. fst (m ss zz );
and to calculate the ss i wanna implement plus:
plus = λm. λn. λs. λz. m s (n s z)
so my question is, how to implement funtion plus, like n times succ 0?
I've tried like
plus = lambda m. lambda n. lambda s. lambda z. m s (n s z);
but it is not correct in the compiler.
I've notice that I work in the OCaml compiler and write all my functions in the func.f file not in the .ml file
https://www.cis.upenn.edu/~bcpierce/tapl/ code is from there, fulluntyped folder
Instead of
lambdaas in lambda calculus, you usefunto encode Church numerals in OCaml. You define numerals as functions that takes a unary function (namedsfor "successor") and base value (zfor "zero"). For example, three is:If you want to translate this representation to an OCaml integer, you define:
Then you can have the OCaml integer representation of
threeby calling it like so:It effectively calls
int_succsuccessively on 0, on the innermost result (ie. 1), etc. until you get 3.But you can manipulate those numbers in their Church representation as follows. For example, to compute the successor Church number of an arbitrary Churn number n:
We have to return a Church number, the result is a function of two parameters,
sandz. It also takes a single input, a church numbern. The result is callingson(n s z), namely the successor of numbern. For example:Likewise, we can define
add:Here, the function takes two numbers. The expression
(y s z)represents the computation made on numbery, and it is used as the base value when applyingxin(x s (y s z)). If you useint_succand 0 forsandz, you can see that(y s z)will increment from 0 as many times as encoded byy, then increment from that value as many times as encoded byx.For example:
Then:
Notice that the types are weak, you don't need to worry too much about it but that means that once you call five with a given
sandz, you won't be able to reusefivewith different types.And now, the type is fully known: