0% found this document useful (0 votes)
85 views4 pages

Collected Lambdas

This document provides a collection of functions in the untyped lambda calculus, including common combinators, natural numbers, mathematical operators, booleans, pairs, lists, and other useful functions. It implements numbers, booleans, pairs, lists, and their associated functions using Church encodings. Functions are provided for operations on numbers like addition and multiplication, logical operations on booleans, manipulating pairs and lists, and more.

Uploaded by

Alex Hambasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views4 pages

Collected Lambdas

This document provides a collection of functions in the untyped lambda calculus, including common combinators, natural numbers, mathematical operators, booleans, pairs, lists, and other useful functions. It implements numbers, booleans, pairs, lists, and their associated functions using Church encodings. Functions are provided for operations on numbers like addition and multiplication, logical operations on booleans, manipulating pairs and lists, and more.

Uploaded by

Alex Hambasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

4/6/2017 Collected Lambdas

Collected Lambda Calculus Functions


The following is a small collection of functions in the untyped lambda calculus which I feel are
noteworthy for one reason or another, either by relevance to the foundations of lambda calculus
(such as the combinators and natural numbers) or by utility to people who wish to actively make use
of this Turing tarpit. Some of them are taken from Wikipedia (which tends to be very reliable on
mathematical issues), while others (primarily the list functions) I derived myself.
Unless explicitly noted otherwise, natural numbers, booleans, pairs & lists, and all functions
for dealing with them use the Church encodings of their values. Operations on other types (e.g.,
negative or non-integral numbers) are not defined, and if the reader desires them, he must construct
such types and the operations on them himself (possibly with the use of pairs acting as typed
unions). Additionally, functions for operating on numbers, lists, etc. are only meant for use on those
types; if a value of the wrong type is supplied (e.g., if a list is passed to SUCC) or a non-Church
encoded value is used, the results are undefined.

Common Combinators

K := xy. x X (X (X X)) X X X
S := xyz. (x z) (y z) X (X (X (X X))) X K X (X X) B (B (B W) C) (B B)
I := x. x S K S S K K X X
X := x. x S K also called (iota)
X := x. x K S K
B := xyz. x (y z) S (K S) K function composition
C := xyz. x z y S (S (K (S (K S) K)) S) (K K)
W := xy. x y y S S (K (S K K))
Y := g. (x. g (x x)) (x. g (x x)) S (K (S I I)) (S (S (K S) K) (K (S I I)))
Y := (xy. x y x) (yx. y (x y x)) S S K (S (K (S S (S (S S K)))) K)
:= (xy. y (x x y)) (xy. y (x x y)) called the "Turing fixed-point combinator"
:= x. x x S I I
:=
2 := (x. x x x) (x. x x x)
A fixed point combinator is any function F for which F g g (F g) for all g; examples include Y,
Y, and . Since lambda calculus functions cannot refer to themselves by name, fixed point
combinators are needed to implement recursion. For example, the factorial function can be
implemented using f := gx. ISZERO x 1 (MULT x (g (PRED x))), which takes a function g and a
number x; if x is not zero, it is multiplied by the result of g (PRED x). Defining FACTORIAL :=
Y f (or Y f or f) means that FACTORIAL x Y f x f (Y f) x, and so f is able to recurse on
itself indefinitely.

Natural Numbers

0 := fx. x
1 := fx. f x
2 := fx. f (f x)
3 := fx. f (f (f x))
4 := fx. f (f (f (f x)))
5 := fx. f (f (f (f (f x))))
et cetera

Mathematical Operators
http://jwodder.freeshell.org/lambda.html 1/4
4/6/2017 Collected Lambdas

The successor operator (given a natural number n, calculate n+1):


SUCC := nfx. f (n f x)
The predecessor operator (for all n > 0, calculate n-1; for zero, return zero):
PRED := nfx. n (gh. h (g f)) (u. x) (u. u)
n. n (gk. ISZERO (g 1) k (PLUS (g k) 1)) (v. 0) 0
n. CAR (n (x. PAIR (CDR x) (SUCC (CDR x))) (PAIR 0 0))
Addition:
PLUS := mnfx. n f (m f x)
mn. n SUCC m
Subtraction SUB m n evaluates to m - n if m > n and to zero otherwise:
SUB := mn. n PRED m
Multiplication:
MULT := mnf. m (n f)
mn. m (PLUS n) 0
B
Division DIV a b evaluates to a pair of two numbers, a idiv b and a mod b:
DIV := Y (gqab. LT a b (PAIR q a) (g (SUCC q) (SUB a b) b)) 0
Integer division:
IDIV := ab. CAR (DIV a b)
Modulus:
MOD := ab. CDR (DIV a b)
Exponentiation (EXP a b ab):
EXP := ab. b a C I
Factorial:
FACTORIAL := Y (gx. ISZERO x 1 (MULT x (g (PRED x))))
n. Y (gax. GT x n a (g (MUL a x) (SUCC x))) 1 1
n. n (fax. f (MUL a x) (SUCC x)) K 1 1
Fibonacci numbers FIBONACCI n evaluates to the n-th Fibonacci number:
FIBONACCI := n. n (fab. f b (PLUS a b)) K 0 1
Greatest common divisor/highest common factor:
GCD := (gmn. LEQ m n (g n m) (g m n)) (Y (gxy. ISZERO y x (g y (MOD x y))))

Booleans

Given a boolean value b, the expression b t f will evaluate to t if b is true and to f if b is false.
This allows conditional expressions to be written simply as a condition applied directly to the two
possible results without the need for an IF function.

TRUE := xy. x K
FALSE := xy. y 0 x. I K I S K X (X X)
AND := pq. p q p
OR := pq. p p q
XOR := pq. p (NOT q) q
NOT := pab. p b a p. p FALSE TRUE

Numeric Comparison Operators

Test whether a number is zero:


ISZERO := n. n (x. FALSE) TRUE
Less than:
LT := ab. NOT (LEQ b a)
Less than or equal to:

http://jwodder.freeshell.org/lambda.html 2/4
4/6/2017 Collected Lambdas

LEQ := mn. ISZERO (SUB m n)


Equal to:
EQ := mn. AND (LEQ m n) (LEQ n m)
Not equal to:
NEQ := ab. OR (NOT (LEQ a b)) (NOT (LEQ b a))
Greater than or equal to:
GEQ := ab. LEQ b a
Greater than:
GT := ab. NOT (LEQ a b)

Pairs and Lists

Pairs and lists are structured the same way that they are in Lisp and its relatives: a pair is
made up of two components, called the car and the cdr, and a list is either NIL (the empty list) or a
pair whose cdr is another list (and whose car is an element of the enclosing list).

PAIR x y create a pair with a car of x and a cdr of y; also called CONS:
PAIR := xyf. f x y
CAR p get the car of pair p; also called FIRST or HEAD:
CAR := p. p TRUE
CDR p get the cdr of pair p; also called SECOND, TAIL, or REST:
CDR := p. p FALSE
The empty list:
NIL := x. TRUE
NULL p evaluates to TRUE if p is NIL or to FALSE if p is a normal pair (all other types are
undefined):
NULL := p. p (xy. FALSE)

List Functions

Concatenate two lists:


APPEND := Y (gab. NULL a b (PAIR (CAR a) (g (CDR a) b)))
Calculate the length of a list:
LENGTH := Y (gcx. NULL x c (g (SUCC c) (CDR x))) 0
INDEX x i evaluates to the i-th (zero-based) element of list x, assuming that x has at least
i+1 elements:
INDEX := xi. CAR (i CDR x)
Get the last element in a list:
LAST := Y (gx. NULL x NIL (NULL (CDR x) (CAR x) (g (CDR x))))
Get a list without its last element:
TRUNCATE := Y (gx. NULL x NIL (NULL (CDR x) NIL (PAIR (CAR x) (g (CDR x)))))
Reverse a list:
REVERSE := Y (gal. NULL l a (g (PAIR (CAR l) a) (CDR l))) NIL
RANGE s e evaluates to a list of all natural numbers from s up through e, or to NIL when
s > e.
RANGE := se. Y (gc. LEQ c e (PAIR c (g (SUCC c) e)) NIL) s
LIST n a0 a1 ... an-1 evaluates to a0 ... an-1 as a list
LIST := n. n (fax. f (PAIR x a)) REVERSE NIL
APPLY f x passes the elements of the list x to f:
APPLY := Y (gfx. NULL x f (g (f (CAR x)) (CDR x)))
MAP f x maps each element of the list x through f:
MAP := Y (gfx. NULL x NIL (PAIR (f (CAR x)) (g f (CDR x))))
FILTER f x evaluates to a list of all e in the list x for which f e is TRUE (assuming that f
returns only TRUE or FALSE for all elements of x):
http://jwodder.freeshell.org/lambda.html 3/4
4/6/2017 Collected Lambdas

FILTER := Y (gfx. NULL x NIL (f (CAR x) (PAIR (CAR x)) I (g f (CDR x))))
CROSS f l m evaluates to a list of all values of f a b where a is in the list l and b is in the list
m. To obtain the Cartesian cross product of l and m, supply PAIR (or a similar function) for f.
CROSS := flm. FOLD-LEFT APPEND NIL (MAP (x. MAP (f x) m) l)
FOLD-LEFT f e x Apply f a to each element of the list x, where a is the result of the previous
application (or e for the first application) and return the result of the last application:
FOLD-LEFT := Y (gfex. NULL x e (g f (f e (CAR x)) (CDR x)))
FOLD-RIGHT f e x Apply (y. f y a) to each element of the list x in reverse order, where a is
the result of the previous application (or e for the first application) and return the result of the
last application:
FOLD-RIGHT := fex. Y (gy. NULL y e (f (CAR y) (g (CDR y)))) x

Other

GET n i a0 a1 ... an-1 = ai:


GET := ni. i K (SUB n (SUCC i) K)

Sources

Wikipedia: Lambda calculus


Wikipedia: Combinatory logic
Wikipedia: SKI combinator calculus
Wikipedia: Fixed point combinator
Wikipedia: B,C,K,W system

Main Page
$Id: lambda.html,v 1.3 2014/06/23 01:42:31 jwodder Exp jwodder $

http://jwodder.freeshell.org/lambda.html 4/4

You might also like