You are on page 1of 4

Synopsis Lab 12

Skim the lectures on λ-calculus (#10 and #11). Study the following:

• Church numerals (the way natural numbers can be encoded in λ-calculus)


• encoding of logical true and false T and F and of logical operations ∧, ∨, ¬, as well as COND
• defining expressions for successor S, addition PLUS, test-if-zero Z and predecessor P
• how to do a recursive call using the Y combinator
• de Brujin notation for variables
• combinators S, K and I and proving combinator equivalences (e.g., I ≡ S K K)

12.1 Mikrokosmos for λ-calculus

12.1.1 Installing mikrokosmos


All examples of this lab will be run in mikrokosmos1 , a didactic interpreter for λ-calculus. We will
use the compiled version rather than the jupyter notebook.
To install mikrokosmos, follow the steps in the "Installation" section2 of the documentation. Make
sure you have understood each step before proceeding. You must select either a stack-base or a
cabal-based installation and perform only the steps pertaining to your choice. The recom-
mended choice is stack. The following procedure should work (it was tested on Ubuntu Linux
18.04).

1. install stack, if not already present on your computer


wget -qO- https://get.haskellstack.org/ | sh
stack upgrade
2. install mikrokosmos via stack
git clone https://github.com/mroman42/mikrokosmos.git
cd mikrokosmos
stack install
3. add the folder where mikrokosmos resides (e.g., ~/.local/bin) to your PATH.
1
available at https://mroman42.github.io/mikrokosmos/tutorial.html
2
see https://mroman42.github.io/mikrokosmos/userguide.html

1
2 Radu Razvan Slavescu - Functional Programming notes

12.1.2 Getting start with mikrokosmos


• To start mikrokosmos, just type3 :
mikrokosmos
Definitions for some basic expressions are automatically loaded and made available.

• To quit, type at the mikrokosmos prompt:


:quit

• You may get help by typing:


:help

• 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

You might also like