Comprised of two operations: function definition and application. Has three syntactic forms:
- Variable:
x - Abstraction:
\x -> E - Application:
E1(function),E2(argument)
Theory
-Reduction
Function calls
- Reducible Expression (redex):
(\x -> E1) E2 - Reduction Rule:
(\x -> E1) E2 -> E1[x := E2]E1[x := E2]meansE1with all free occurrences ofxreplaced withE2, as long as no free variables ofE2get captured (undefined otherwise)
- Repeatedly apply -reduction until normal form with no further redexes (or loop forever)
-Conversion
Renaming formals
\x -> E =a> \y -> E[x := y]- Ensures no accidental capture of free variables
Capture-Avoiding Substitution
- When doing
E[x := E2], we ensure no free variable inE2gets bound accidentally inE - If conflict arises, -convert the abstractionโs bound variable first
let Definitions
let NAME = Eis just substitutingEwhereverNAMEappears
Example:
let ID = \x -> x
ID apple
=d> (\x -> x) apple
=b> appleChurch Numerals
- Encoding natural numbers:
ZERO = \f x -> xONE = \f x -> f xTWO = \f x -> f (f x)
- General form: integer
nrepresented as\f x -> f^n(x) - Arithmetic
- Increment:
INC = \n f x -> f (n f x) - Addition:
ADD = \n m -> n INC m - Multiplication:
MULT = \n m -> n (ADD m) ZERO
- Increment:
Examples
Booleans:
TRUE = \x y -> xFALSE = \x y -> yITE = \b x y -> b x y(if then else) Pairs:PAIR = \x y -> \b -> ITE b x yFST p = p TRUE,SND p = p FALSERecursion (Fix-point Combinator):FIX = \stp -> (\x -> stp (x x)) (\x -> stp (x x))- Allows for self-application that emulates recursion
Example:
let STEP = \rec -> \n -> ITE (ISZ n) ZERO (ADD n (rec (DEC n)))
let SUM = FIX STEP
-- SUM 3 -> 0 + 1 + 2 + 3