You are on page 1of 5

CM0081 Automata and Formal Languages

Programming Lab 2: Church Encoding


Andrés Sicard-Ramírez

3rd November 2021, version 0.1

1 Deadline
See the course homepage.

2 Lambda-Definable Functions
For representing the numeric-theoretic functions in the λ-calculus (see, for
example, [Barendregt and Barendsen 2000; Paulson 2000]) there are several
known encodings.
In the Church encoding, the natural numbers are represented by the
Church numerals:

c0 := λf x.x,
c1 := λf x.f x,
c2 := λf x.f (f x),
c3 := λf x.f (f (f x)),
..
.
cn := λf x.f n x, for n ∈ N.

Natural numbers can be defined from zero and successor:

c0 := λf x.x,
succ := λnf x.f (nf x),
cn+1 := succ cn .

1
Encoding for the arithmetic operations of addition, multiplication, ex-
ponentiation and predecessor is as follows.

add := λmnf x.mf (nf x),


mult := λmnf x.m(nf )x,
(1)
exp := λmnf x.(nm)f x,
pred := λnf x.n(λgh.h(gf ))(λu.x)(λu.u),

where

add cm cn =β cm+n , (2)


mult cm cn =β cm×n , (3)
exp cm cn =β c mn , (4)
pred c0 =β c0 , (5)
pred cn+1 =β cn . (6)

3 Assignment (70%)
(i) Implementation (20%).
To write a Haskell program that implements the Church numerals and
the combinators in (1), using the Simpler, Easier! implementation for
the (untyped) λ-calculus by Augustsson.1

import Numeric.Natural (Natural)

c0 :: Expr
cN :: Natural → Expr

add, mult, exp, pred :: Expr

(ii) Testing (20%).


Equations (2) to (6) are the specifications of the combinators (1). Test
these specifications using the QuickCheck library. The Arbitrary in-
stance for the data type Natural data type is available in the
quickcheck-instances library.
1
http://augustss.blogspot.com/2007_10_01_archive.html.

2
(iii) Documentation (10%).
To document (in English) your source code (no Augustsson’s code).
The documentation should explain your solution.

(iv) Oral explanation of your solution (20%).

4 Requirements (20%)
i) The programming lab may either be solved on your own, or jointly
with one other student taking the course.

ii) You are to use GHC 8.10.7 or 9.0.1.

iii) Start your programming lab by running the following command:2 .

$ stack new church-encoding

iv) The implementation of the λ-calculus should be in a file/module called


church-encoding/src/LambdaCalculus.hs.

v) The implementation of the Church numerals and the combinators (1)


should be in a file/module called church-encoding/src/ChurchEncoding.hs.

vi) The tests should be in a file called union/test/Spec.hs.

vii) Add the GHC options -Wall and -Wmissing-local-signatures to


the library and tests sections in the package.yaml file.

viii) To add a README.md file in English containing at least the following


information:

• Your name.
• Operating system version used.
• stack version used.
• GHC version used.
• Versions used of the QuickCheck and quickcheck-instances lib-
raries.
2
See https://docs.haskellstack.org/en/stable/README/#quick-start-guide for
instructions.

3
5 Clean Code (10%)
Before submitting your programming lab, which includes your README.md
file, clean it up:
• Does not have long lines (at most 80 columns).
• Has an uniformly indentation (we recommend two characters).
• Has a consistent layout.
• Has type signatures for everything (included where-clauses and let-
definitions).
• Has good comments.
• Has spell-checked comments.
• Has no junk (unused code, commented code, unnecessary code).
• Has no overly complicated function definitions.
• Does not contain any repetitive code.
• Has no unnecessary spaces at the end of a line, or empty lines at the
end of a file.

6 Delivery
i) To create a private repository in GitHub. The name of the repository
must be cm0081-20212-lab2.
ii) Share the repository with your lecturer (user @asr).
iii) Do not include unnecessary files in the repository.

7 From the coordination


El control de versiones no es solamente un herramienta que facilitará la
comunicación entre los miembros del grupo y la administración de los cam-
bios al código. El control de versiones también ayudará al profesor y al mon-
itor a llevar un control sobre el desarrollo de la práctica. Se espera que las
diferentes registros dentro del control de versiones sean cambios graduales.
En caso contrario, se procederá a realizar un escrutinio con el objetivo de
evitar fraudes.

4
References
Barendregt, Henk and Barendsen, Erik (2000). Introduction to Lambda Cal-
culus. Revisited edition, Mar. 2000 (cit. on p. 1).
Paulson, Lawrence C. (2000). Foundations of Functional Programming. Lec-
ture notes. url: http : / / www . cl . cam . ac . uk / ~lp15/ (visited on
10/06/2020) (cit. on p. 1).

You might also like