Professional Documents
Culture Documents
Skim the lectures on λ-calculus (#10 and #11). Study the following:
1
2 Radu Razvan Slavescu - Functional Programming notes
• To see the step-by-step reductions, in de Brujin notation, switch the verbose mode on:
:verbose on
Example 1: if you want to see the definition of 1 loaded in mikrokosmos, type at its prompt:
mikro> 1
and you should get the following answer:
λa.λb.a b ⇒ 1
If you are in verbose mode, you get the result in both de Brujin and classical notation:
λλ(2 1)
λa.λb.a b ⇒ 1
Example 2: typing S at the prompt will yield the definition of combinator S: λλλ((31)(21))
in de Brujin notation, or λa.λb.λc.(ac)(bc)
Example 3: To define a new expression, you can use the equality sign (i.e., =). The right side
of it is first simplified, then the result gets bound to the name at its left side. E.g., to define
the successor SUCC, you do (mind the use of backslash for λ as well as the space after the dot):
mikro> SUCC = \p. \q. \r. q (p q r)
If you do now:
mikro> SUCC 0
you should get the step-by-step reduction:
(λλλ(2 ((3 2) 1)) λλ1)
λλ(2 ((λλ1 2) 1))
λλ(2 (λ1 1))
λλ(2 1)
λa.λb.a b ⇒ 1
So, the result is λa.λb.a b, which, as highlighted by the interpretor, is the definition of 1.
The cyan elements are the positions to be replaced in the next β-reduction, while the dark
green ones are the values to be "plugged in".
3
assuming the folder where mikrokosmos resides has been added to your PATH
Synopsis Lab 12 FP 3
• If you also need the combinator-based version of a λ-expression, switch the ski mode on:
:ski on
For SUCC 0 in the example above, the interpreter will now reply λa.λb.a b ⇒ I ⇒ 1
The result is 1, which has the same representation as combinator I (you can check this by
successively applying a β-reduction and an η-reduction on expression 1 x).
• You can store the definitions in a file and later load and use them; each expression must be
defined before being used. To enter comments, start them with #. The name of the file storing
the definitions always ends in .mkr, but this part must be omitted when loading it. For example,
if the definition of SUCC is stored in file foo.mkr, one can load it by typing:
:load foo
mikrokosmos seeks for foo.mkr in your home directory (~ in short), in ~/.mikrokosmos, in
the current folder (aka .) and in ./lib. Absolute and relative file names can also be used.
• When defining a new expression, if one needs to defer simplification of the right side, operator
!= must be used instead of = . For example, when introducing combinator Y, we must in-
hibit simplification before parameter binding, otherwise mikrokosmos will generate the infinite
sequence which is "embedded" into Y’s definition.
12.2 Tasks
1. Write with variables the following expressions in de Brujin notation:
(a) λλ 1
(b) λλ(2 2)
(c) λλλ((3 2) (3 1))
2. Simplify the following expressions which contain combinators, showing each step:
(a) S K K
(b) K I S
(c) S (K S) K
3. Given the definitions of 0, 1, 2, . . . , successor S (aka SUCC), PLUS, combinators, do the following:
(a) follow, step by step, the demo provided by mikrokosmos for the equivalence S 0 ≡ 1
(b) follow, step by step, the demo provided by mikrokosmos for the equivalence PLUS 1 2 ≡ 3
(c) find out the S-K representations for 0, 1, 2
(d) simplify the expression S K
(e) check to see what the expression 3 S 1 returns
(f) figure out what the following expression yields: SA = \m. \n. m SUCC n
(g) figure out what the following expression yields: SM = \m. \n. \f. m (n f)
(h) find what each of the following expressions yields: 2 3; 0 1; 1 0; 0 0; 0 1 2; C; C 0 1 2.
Can you write an expression SP having two parameters x and y, which returns xy ?
4 Radu Razvan Slavescu - Functional Programming notes
(i) (optional) can you come up with an expression SUB, having two parameters x and y,
which returns the difference (x − y) by applying x times the predecessor expression over
the second parameter y? Note: if x is lower than y, the result should be 0.
(j) (optional) can you suggest an expression isOdd which checks if its argument n is odd?
4. Write an expression called SQSUM which adds up the squares of the first n natural numbers,
X
n X
n−1
2 2
based on the recursive formula i =n + i2 .
i=1 i=1