Lambda Calculus
-
<Exp> ::= <ident> | ( <Exp> ) | <Exp> <Exp> | --application λ<ident>.<Exp> --abstraction - -- Syntax of the λ-calculus --
- -- Syntax of the λ-calculus --
The syntax of the λ-calculus is very simple, comprising just four kinds of expression but surprisingly it is sufficient to define any computable function. Constants including integers, booleans, conditiional, declarations and so on can all be defined using just the syntax above, as can be demonstrated by examples. Because of this it is often convenient to extend the syntax with options for constants (0, 1, 2, ..., +, -, *, /, =, ≠, ≤, <, ≥, >, true, false, ∧, ∨, ¬, nil, cons, null, hd, tl, if...then...else..., let {rec}...in... etc.) but, if that is done, it is only a convenience and does not increase the power of the language. Note that an abstraction defines an anonymous function.
Fixed-Point Operator, Y
- The pure λ-calculus appears to lack recursion, or equivalently iteration, but recursive functions can in fact be defined by using the fixed-point operator Y, e.g.,
let Y = lambda G. (lambda g. G(g g)) (lambda g. G(g g))
in let F = lambda f. lambda n. if n<=0 then 1 else n*f(n-1)
in Y F 10- or even
(lambda G. (lambda g. G(g g)) (lambda g. G(g g)))
(lambda f. lambda n. if n<=0 then 1 else n*f(n-1)) 10
Consequently recursive definitions, let rec x=f(x), are allowed for convenience. Note that Y works even if the end result is a data structure such as posInts: