Floquet theory: Difference between revisions
en>Kilom691 ref corrected |
en>Policron m Added an external link (EOM). |
||
Line 1: | Line 1: | ||
In [[mathematics]], '''Church encoding''' is a means of representing data and operators in the [[lambda calculus]]. The data and operators form a mathematical structure which is [[embedding|embedded]] in the lambda calculus. The '''Church numerals''' are a representation of the natural numbers using lambda notation. The method is named for [[Alonzo Church]], who first encoded data in the lambda calculus this way. | |||
Terms that are usually considered primitive in other notations (such as integers, booleans, pairs, lists, and tagged unions) are mapped to [[higher-order function]]s under Church encoding. The [[Church-Turing thesis]] asserts that any computable operator (and its operands) can be represented under Church encoding. In the [[Lambda calculus|untyped lambda calculus]] the only primitive data type is the function. The Church-Turing thesis is that lambda calculus is [[Turing complete]]. | |||
The Church encoding is not intended as a practical implementation of primitive data types. Its use is to show that other primitives data types are not required to represent any calculation. The completeness is representational. Additional functions are needed to translate the representation into common data types, for display to people. It is not possible in general to decide if two functions are extrinsically equal due to the [[Lambda calculus#Undecidability of equivalence|undecidability of equivalence]] from [[Church's theorem]]. The translation may apply the function in some way to retrieve the value it represents, or look up its value as a literal lambda term. | |||
==Church numerals== | |||
Church numerals are the representations of [[natural number]]s under Church encoding. The [[higher-order function]] that represents natural number <math>n</math> is a function that maps any function <math>f</math> to its ''n''-fold [[function composition|composition]]. In simpler terms, the "value" of the numeral is equivalent to the number of times the function encapsulates its argument. | |||
: <math>f^n = \underbrace{f \circ f \circ \cdots \circ f}_{n\text{ times}}.\,</math> | |||
All Church numerals are functions that take two parameters. Church numerals '''0''', '''1''', '''2''', ..., are defined as follows in the [[lambda calculus]]: | |||
Then starting with zero, being not applying the function, then one being applying the function, ... | |||
{| class="wikitable" | |||
|- | |||
! Number !! Function definition !! Lambda expression | |||
|- | |||
| 0 || <math>0\ f\ x = x </math> || <math>0 = \lambda f.\lambda x.x </math> | |||
|- | |||
| 1 || <math>1\ f\ x = f\ x </math> || <math>1 = \lambda f.\lambda x.f\ x </math> | |||
|- | |||
| 2 || <math>2\ f\ x = f\ (f\ x) </math> || <math>2 = \lambda f.\lambda x.f\ (f\ x) </math> | |||
|- | |||
| 3 || <math>3\ f\ x = f\ (f\ (f\ x)) </math> || <math>3 = \lambda f.\lambda x.f\ (f\ (f\ x)) </math> | |||
|- | |||
| colspan='3' align='center' style='border-left: 1px solid white; border-right: 1px solid white' | ... | |||
|- | |||
| n || <math>n\ f\ x = f^n\ x </math> || <math>n = \lambda f.\lambda x.f^n\ x </math> | |||
|} | |||
The Church numeral '''3''' represents the action of applying any given function three times to a value. The supplied function is first applied to a supplied parameter and then successively to its own result. The end result is not the numeral 3 (unless the supplied parameter happens to be 0 and the function is a successor function). The function itself, and not its end result, is the Church numeral '''3'''. The Church numeral '''3''' means simply to do anything three times. It is an [[Ostensive definition|ostensive]] demonstration of what is meant by "three times". | |||
===Calculation with Church numerals=== | |||
[[Arithmetic]] operations on numbers may be represented by functions on Church numerals. These functions may be defined in [[Lambda calculus|lambda calculus]], or implemented in most functional programming languages (see [[Lambda lifting#Conversion without lifting|converting lambda expressions to functions]]). | |||
The addition function <math>\operatorname{plus}(m, n)= m+n</math> uses the identity <math>f^{(m+n)}(x)=f^m(f^n(x))</math>. | |||
: <math>\operatorname{plus} \equiv \lambda m.\lambda n.\lambda f.\lambda x. m\ f\ (n\ f\ x)</math> | |||
The successor function <math>\operatorname{succ}(n)=n+1</math> is [[Beta reduction#.CE.B2-reduction|β-equivalent]] to <math>(\operatorname{plus}\ 1)</math>. | |||
: <math>\operatorname{succ} \equiv \lambda n.\lambda f.\lambda x. f\ (n\ f\ x)</math> | |||
The multiplication function <math>\operatorname{mult}(m, n) = m*n</math> uses the identity <math>f^{(m*n)}(x) = (f^n)^m(x)</math>. | |||
: <math>\operatorname{mult} \equiv \lambda m.\lambda n.\lambda f. m\ (n\ f)</math> | |||
The exponentiation function <math>\operatorname{exp}(m, n) = m^n</math> is given by the definition of a Church numerals; <math>n\ f\ x = f^n\ x </math>. In the definition substitute <math> f \to m, x \to f</math> to get <math>n\ m\ f = m^n\ f </math> and, | |||
: <math>\operatorname{exp}\ m\ n = m^n = n\ m </math> | |||
which gives the lambda expression, | |||
: <math>\operatorname{exp} \equiv \lambda m.\lambda n. n\ m</math> | |||
The <math>\operatorname{pred}(n)</math> function is more difficult to understand. | |||
: <math>\operatorname{pred} \equiv \lambda n.\lambda f.\lambda x. n\ (\lambda g.\lambda h. h\ (g\ f))\ (\lambda u. x)\ (\lambda u. u)</math> | |||
A Church numeral applies a function ''n'' times. The predecessor function must return a function that applies it's parameter ''n - 1'' times. This is achieved by building a container around ''f'' and ''x'', which is initialized in a way that omits the application of the function the first time. See [[#Derivation of predecessor function|predecessor]] for a more detailed explanation. | |||
The subtraction function can be written based on the predecessor function. | |||
: <math>\operatorname{minus} \equiv \lambda m.\lambda n. (n \operatorname{pred})\ m</math> | |||
=== Table of functions on Church numerals === | |||
{| class="wikitable" | |||
|- | |||
! Function !! Algebra !! Identity !! Function definition | |||
! colspan="2" | Lambda expressions | |||
|- | |||
| [[Successor function|Successor]] || <math>n + 1</math> || <math>f^{n+1}\ x = f (f^n x) </math> || <math>\operatorname{succ}\ n\ f\ x = f\ (n\ f\ x) </math> || <math>\lambda n.\lambda f.\lambda x.f\ (n\ f\ x) </math> || ... | |||
|- | |||
| [[Addition]] || <math>m + n</math> || <math>f^{m+n}\ x = f^m (f^n x) </math> || <math>\operatorname{plus}\ m\ n\ f\ x = m\ f\ (n\ f\ x) </math> || <math> \lambda m.\lambda n.\lambda f.\lambda x.m\ f\ (n\ f\ x) </math> || <math>\lambda m.\lambda n.n \operatorname{succ} m</math> | |||
|- | |||
| [[Multiplication]] || <math>m * n </math> || <math> f^{m*n}\ x = (f^m)^n\ x </math> ||<math>\operatorname{multiply}\ m\ n\ f\ x = m\ (n\ f) \ x </math> || <math>\lambda m.\lambda n.\lambda f.\lambda x.m\ (n\ f) \ x </math> || <math>\lambda m.\lambda n.\lambda f.m\ (n\ f) </math> | |||
|- | |||
| [[Exponentiation]] || <math>m^n </math> || <math> n\ m\ f = m^n\ f </math><ref name="Church numeral definition">This formula is the definition of a Church numeral n with f -> m, x -> f.</ref> ||<math>\operatorname{exp} \ m\ n\ f\ x = (n\ m)\ f\ x </math> || <math> \lambda m.\lambda n.\lambda f.\lambda x.(n\ m)\ f\ x </math> || <math> \lambda m.\lambda n.n\ m </math> | |||
|- | |||
| [[#Derivation of predecessor function|Predecessor]]* || <math>n - 1</math> || <math>\operatorname{inc}^n \operatorname{con} = \operatorname{val} (f^{n-1} x) </math> || <math>\operatorname{pred}</math> ... | |||
| colspan="2" | | |||
<math>\lambda n.\lambda f.\lambda x.n\ (\lambda g.\lambda h.h\ (g\ f))\ (\lambda u.x)\ (\lambda u.u) </math> | |||
|- | |||
| [[Subtraction]]* || <math> m - n </math> || <math>f^{m-n}\ x = (f^{-1})^n (f^{m} x)</math> || <math>\operatorname{minus}\ m\ n = (n \operatorname{pred})\ m</math> || ... || <math>\lambda m.\lambda n.n \operatorname{pred} m</math> | |||
|} | |||
<nowiki>* </nowiki>Note that in the Church encoding, | |||
* <math> \operatorname{pred}(0) = 0 </math> | |||
* <math> m < n \to m - n = 0 </math> | |||
===Translation with other representations=== | |||
Most real-world languages have support for machine-native integers; the ''church'' and ''unchurch'' functions convert between nonnegative integers and their corresponding church numerals. The functions are given here in [[Haskell (programming language)|Haskell]], where the <code>\</code> corresponds to the λ of Lambda calculus. Implementations in other languages are similar. | |||
<syntaxhighlight lang="haskell"> | |||
type Church a = (a -> a) -> (a -> a) | |||
church :: Integer -> Church Integer | |||
church 0 = \f -> \x -> x | |||
church n = \f -> \x -> f (church (n-1) f x) | |||
unchurch :: Church Integer -> Integer | |||
unchurch n = n (\x -> x + 1) 0 | |||
</syntaxhighlight> | |||
==Church Booleans== | |||
''Church Booleans'' are the Church encoding of the Boolean values ''true'' and ''false.'' Some programming languages use these as an implementation model for Boolean arithmetic; examples are [[Smalltalk]] and [[Pico (programming language)|Pico]]. | |||
Boolean logic may be considered as a choice. The Church encoding of ''true'' and ''false'' are functions of two parameters; | |||
* ''true'' chooses the first parameter. | |||
* ''false'' chooses the second parameter. | |||
The two definitions are known as Church Booleans; | |||
: <math>\operatorname{true} \equiv \lambda a.\lambda b.a</math> | |||
: <math>\operatorname{false} \equiv \lambda a.\lambda b.b</math> | |||
This definition allows predicates (i.e. functions returning [[Truth value|logical values]]) to directly act as if-clauses. A function returning a Boolean, which is then applied to two parameters, returns either the first or the second parameter; | |||
: <math>\operatorname{predicate}\ x\ \operatorname{then-clause}\ \operatorname{else-clause} </math> | |||
evaluates to ''then-clause'' if ''predicate'' x evaluates to ''true'', and to ''else-clause'' if ''predicate'' x evaluates to ''false''. | |||
Because ''true'' and ''false'' choose the first or second parameter they may be combined to provide logic operators, | |||
: <math>\operatorname{and} = \lambda p.\lambda q.p\ q\ p</math> | |||
: <math>\operatorname{or} = \lambda p.\lambda q.p\ p\ q</math> | |||
: <math>\operatorname{not}_1 = \lambda p.\lambda a.\lambda b.p\ b\ a</math> - This is only a correct implementation if the evaluation strategy is applicative order. | |||
: <math>\operatorname{not}_2 = \lambda p.p\ (\lambda a.\lambda b. b)\ (\lambda a.\lambda b. a) = p \operatorname{false} \operatorname{true}</math> - This is only a correct implementation if the evaluation strategy is normal order. | |||
: <math>\operatorname{xor} = \lambda a.\lambda b.a\ (\operatorname{not}\ b)\ b</math> | |||
: <math>\operatorname{if} = \lambda p.\lambda a.\lambda b.p\ a\ b</math> | |||
Some examples: | |||
: <math>\operatorname{and} \operatorname{true} \operatorname{false} = (\lambda p.\lambda q.p\ q\ p)\ \operatorname{true}\ \operatorname{false} = \operatorname{true} \operatorname{false} \operatorname{true} = (\lambda a.\lambda b.a) \operatorname{false} \operatorname{true} = \operatorname{false} </math> | |||
:<math>\operatorname{or} \operatorname{true} \operatorname{false} = (\lambda p.\lambda q.p\ p\ q)\ (\lambda a.\lambda b.a)\ (\lambda a.\lambda b.b) = (\lambda a.\lambda b.a)\ (\lambda a.\lambda b.a)\ (\lambda a.\lambda b.b) = (\lambda a.\lambda b.a) = \operatorname{true} </math> | |||
:<math>\operatorname{not1}\ \operatorname{true} = (\lambda p.\lambda a.\lambda b.p\ b\ a) (\lambda a.\lambda b.a) = \lambda a.\lambda b.(\lambda a.\lambda b.a)\ b\ a = \lambda a.\lambda b.(\lambda x.b)\ a = \lambda a.\lambda b.b = \operatorname{false} </math> | |||
:<math>\operatorname{not2}\ \operatorname{true} = (\lambda p.p\ (\lambda a.\lambda b. b) (\lambda a.\lambda b. a)) (\lambda a.\lambda b. a) = (\lambda a.\lambda b. a) (\lambda a.\lambda b. b) (\lambda a.\lambda b. a) = (\lambda b. (\lambda a.\lambda b. b))\ (\lambda a.\lambda b. a) = \lambda a.\lambda b.b = \operatorname{false} </math> | |||
== Predicates == | |||
A ''predicate'' is a function that returns a Boolean value. The most fundamental predicate is <math>\operatorname{IsZero}</math>, which returns <math>\operatorname{true}</math> if its argument is the Church numeral <math>0</math>, and <math>\operatorname{false}</math> if its argument is any other Church numeral: | |||
: <math>\operatorname{IsZero} = \lambda n.n\ (\lambda x.\operatorname{false})\ \operatorname{true}</math> | |||
The following predicate tests whether the first argument is less-than-or-equal-to the second: | |||
: <math>\operatorname{LEQ} = \lambda m.\lambda n.\operatorname{IsZero}\ (\operatorname{minus}\ m\ n)</math>, | |||
Because of the identity, | |||
: <math> x = y \equiv (x <= y \and y <= x) </math> | |||
The test for equality may be implemented as, | |||
: <math>\operatorname{EQ} = \lambda m.\lambda n.\operatorname{and}\ (\operatorname{LEQ}\ m\ n)\ (\operatorname{LEQ}\ n\ m) </math> | |||
==Church pairs== | |||
{{see also|Cons}} | |||
Church pairs are the Church encoding of the [[cons|pair]] (two-tuple) type. The pair is represented as a function that takes a function argument. When given its argument it will apply the argument to the two components of the pair. The definition in [[lambda calculus]] is, | |||
: <math>\operatorname{pair} \equiv \lambda x.\lambda y.\lambda z.z\ x\ y </math> | |||
: <math>\operatorname{first} \equiv \lambda p.p\ (\lambda x.\lambda y.x) </math> | |||
: <math>\operatorname{second} \equiv \lambda p.p\ (\lambda x.\lambda y.y) </math> | |||
For example, | |||
: <math> \operatorname{first}\ (\operatorname{pair}\ a\ b) </math> | |||
:: <math> = (\lambda p.p\ (\lambda x.\lambda y.x))\ ((\lambda x.\lambda y.\lambda z.z\ x\ y)\ a\ b) </math> | |||
:: <math> = (\lambda p.p\ (\lambda x.\lambda y.x))\ (\lambda z.z\ a\ b) </math> | |||
:: <math> = (\lambda z.z\ a\ b)\ (\lambda x.\lambda y.x) </math> | |||
:: <math> = (\lambda x.\lambda y.x)\ a\ b = a </math> | |||
==List encodings== | |||
An ([[Immutable object|immutable]]) [[list (computing)|list]] is constructed from list nodes. The basic operations on the list are; | |||
{| class="wikitable" | |||
|- | |||
! Function !! Description | |||
|- | |||
| ''nil'' || Construct an empty list. | |||
|- | |||
| ''isnil'' || Test if list is empty. | |||
|- | |||
| ''cons'' || Prepend a given value to a (possibly empty) list. | |||
|- | |||
| ''head'' || Get the first element of the list. | |||
|- | |||
| ''tail'' || Get the rest of the list. | |||
|} | |||
Three different representations of lists are given. | |||
* Build each list node from two pairs (to allow for empty lists). | |||
* Build each list node from one pair. | |||
* Represent the list using the [[Fold (higher-order function)|right fold function]]. | |||
=== Two pairs as a list node === | |||
A nonempty list can implemented by a Church pair; | |||
* ''First'' contains the head. | |||
* ''Second'' contains the tail. | |||
However this does not give a representation of the empty list, because there is no "null" pointer. To represent null, the pair may be wrapped in another pair, giving free values, | |||
* ''First'' - Is the null pointer (empty list). | |||
* ''Second.First'' contains the head. | |||
* ''Second.Second'' contains the tail. | |||
Using this idea the basic list operations can be defined like this:<ref>{{cite book | |||
| last = Pierce | |||
| first = Benjamin C. | |||
| authorlink = Benjamin C. Pierce | |||
| title = [[Types and Programming Languages]] | |||
| publisher = [[MIT Press]] | |||
| year = 2002 | |||
| pages = 500 | |||
| isbn = 978-0-262-16209-8}}</ref> | |||
{| class="wikitable" | |||
|- | |||
! Expression !! Description | |||
|- | |||
| <math> \operatorname{nil} \equiv \operatorname{pair}\ \operatorname{true}\ \operatorname{true} </math> | |||
| The first element of the pair is ''true'' meaning the list is null. | |||
|- | |||
| <math> \operatorname{isnil} \equiv \operatorname{first} </math> | |||
| Retrieve the null (or empty list) indicator. | |||
|- | |||
| <math> \operatorname{cons} \equiv \lambda h.\lambda t.\operatorname{pair} \operatorname{false}\ (\operatorname{pair} h\ t) </math> | |||
| Create a list node, which is not null, and give it a head ''h'' and a tail ''t''. | |||
|- | |||
| <math> \operatorname{head} \equiv \lambda z.\operatorname{first}\ (\operatorname{second} z) </math> | |||
| ''second.first'' is the head. | |||
|- | |||
| <math> \operatorname{tail} \equiv \lambda z.\operatorname{second}\ (\operatorname{second} z) </math> | |||
| ''second.second'' is the tail. | |||
|} | |||
In a ''nil'' node ''second'' is never accessed, provided that '''head''' and '''tail''' are only applied to nonempty lists. | |||
=== One pair as a list node === | |||
Alternatively, define <ref>John Tromp, Binary Lambda Calculus and Combinatory Logic, in ''Randomness And Complexity, from Leibniz To Chaitin'', ed. Cristian S. Calude, World Scientific Publishing Company, October 2008. [http://www.cwi.nl/~tromp/cl/LC.pdf (pdf version)]</ref> | |||
: <math>\operatorname{cons} \equiv \operatorname{pair} </math> | |||
: <math>\operatorname{head} \equiv \operatorname{first} </math> | |||
: <math>\operatorname{tail} \equiv \operatorname{second} </math> | |||
: <math>\operatorname{nil} \equiv \operatorname{false} </math> | |||
: <math>\operatorname{isnil} \equiv \lambda l.l (\lambda h.\lambda t.\lambda d.\operatorname{false}) \operatorname{true} </math> | |||
where the last definition is a special case of the general | |||
: <math>\operatorname{process-list} \equiv \lambda l.l (\lambda h.\lambda t.\lambda d. \operatorname{head-and-tail-clause}) \operatorname{nil-clause} </math> | |||
=== Represent the list using ''right fold'' === | |||
As an alternative to the encoding using Church pairs, a list can be encoded by identifying it with its [[Fold (higher-order function)|right fold function]]. For example, a list of three elements x, y and z can be encoded by a higher-order function which when applied to a combinator c and a value n returns c x (c y (c z n)). | |||
: <math>\operatorname{nil} \equiv \lambda c.\lambda n.n</math> | |||
: <math>\operatorname{isnil} \equiv \lambda l.l\ (\lambda h.\lambda t.\operatorname{false})\ \operatorname{true}</math> | |||
: <math>\operatorname{cons} \equiv \lambda h.\lambda t.\lambda c.\lambda n.c\ h\ (t\ c\ n)</math> | |||
: <math>\operatorname{head} \equiv \lambda l.l\ (\lambda h.\lambda t.h)\ \operatorname{false}</math> | |||
: <math>\operatorname{tail} \equiv \lambda l. \operatorname{first}\ (l\ (\lambda x.\lambda p.\operatorname{pair}\ (\operatorname{second}\ p)\ (\operatorname{cons}\ x\ (\operatorname{second}\ p)))\ (\operatorname{pair}\ \operatorname{nil}\ \operatorname{nil})) </math> | |||
== Derivation of predecessor function == | |||
The predecessor function used in the Church Encoding is, | |||
<math>\operatorname{pred}(n) = \begin{cases} 0 & \mbox{if }n=0, \\ n-1 & \mbox{otherwise}\end{cases}</math>. | |||
To build the predecessor we need a way of applying the function 1 less time. A numeral ''n'' applies the function ''f'' ''n'' times to ''x''. The predecessor function must use the numeral ''n'' to apply the function ''n-1'' times. | |||
Before implementing the predecessor function, here is a scheme that wraps the value in a container function. We will define new functions to use in place of ''f'' and ''x'', called ''inc'' and ''init''. The container function is called ''value''. The left hand side of the table shows a numeral ''n'' applied to ''inc'' and ''init''. | |||
{| class="wikitable" | |||
|- | |||
! Number || Using init !! Using const | |||
|- | |||
| 0 | |||
| <math> \operatorname{init} = \operatorname{value}\ x </math> | |||
| | |||
|- | |||
| 1 | |||
| <math> \operatorname{inc}\ \operatorname{init} = \operatorname{value}\ (f\ x) </math> | |||
| <math> \operatorname{inc}\ \operatorname{const} = \operatorname{value}\ x </math> | |||
|- | |||
| 2 | |||
| <math> \operatorname{inc}\ (\operatorname{inc}\ \operatorname{init}) = \operatorname{value}\ (f\ (f\ x)) </math> | |||
| <math> \operatorname{inc}\ (\operatorname{inc}\ \operatorname{const}) = \operatorname{value}\ (f\ x) </math> | |||
|- | |||
| 3 | |||
| <math> \operatorname{inc}\ (\operatorname{inc}\ (\operatorname{inc}\ \operatorname{init})) = \operatorname{value}\ (f\ (f\ (f\ x))) </math> | |||
| <math> \operatorname{inc}\ (\operatorname{inc}\ (\operatorname{inc}\ \operatorname{const})) = \operatorname{value}\ (f\ (f\ x)) </math> | |||
|- | |||
| colspan='3' align='center' style='border-left: 1px solid white; border-right: 1px solid white' | ... | |||
|- | |||
| n | |||
| <math> n \operatorname{inc}\ \operatorname{init} = \operatorname{value}\ (f^n\ x) = \operatorname{value}\ (n\ f\ x) </math> | |||
| <math> n \operatorname{inc}\ \operatorname{const} = \operatorname{value}\ (f^{n-1}\ x) = \operatorname{value}\ ((n-1)\ f\ x) </math> | |||
|} | |||
The general recurrence rule is, | |||
:<math> \operatorname{inc}\ (\operatorname{value}\ v) = \operatorname{value}\ (f\ v)</math> | |||
If there is also a function to retrieve the value from the container (called ''extract''), | |||
:<math> \operatorname{extract}\ (\operatorname{value}\ v) = v</math> | |||
Then ''extract'' may be used to define the ''samenum'' function as, | |||
: <math>\operatorname{samenum} = \lambda n.\lambda f.\lambda x.\operatorname{extract}\ (n \operatorname{inc} \operatorname{init}) = \lambda n.\lambda f.\lambda x.\operatorname{extract}\ (\operatorname{value}\ (n\ f\ x)) = \lambda n.\lambda f.\lambda x.n\ f\ x = \lambda n.n</math> | |||
The ''samenum'' function is not intrinsically useful. What we want is the initial value to skip an application of ''f''. Call this new initial container ''const''. The right hand side of the above table shows the expansions of ''n'' ''inc'' ''const''. Then by replacing ''init'' with ''const'' in the expression for the ''same'' function we get the predecessor function, | |||
: <math>\operatorname{pred} = \lambda n.\lambda f.\lambda x.\operatorname{extract}\ (n \operatorname{inc} \operatorname{const}) = \lambda n.\lambda f.\lambda x.\operatorname{extract}\ (\operatorname{value}\ ((n-1)\ f\ x)) = \lambda n.\lambda f.\lambda x.(n-1)\ f\ x = \lambda n.(n-1)</math> | |||
As explained below the functions ''inc'', ''init'', ''const'', ''value'' and ''extract'' may be defined as, | |||
{| class="wikitable" | |||
|- | |||
! <math> \operatorname{value} </math> | |||
! <math> \operatorname{extract}\ k </math> | |||
! <math> \operatorname{inc} </math> | |||
! <math> \operatorname{init} </math> | |||
! <math> \operatorname{const} </math> | |||
|- | |||
| <math> \lambda v.(\lambda h.h\ v) </math> | |||
| <math> k\ \lambda u.u </math> | |||
| <math> \lambda g.\lambda h.h\ (g\ f) </math> | |||
| <math> \lambda h.h\ x </math> | |||
| <math> \lambda u.x </math> | |||
|} | |||
Which gives the lambda expression for ''pred'' as, | |||
: <math>\operatorname{pred} = \lambda n.\lambda f.\lambda x.n\ (\lambda g.\lambda h.h\ (g\ f))\ (\lambda u.x)\ (\lambda u.u) </math> | |||
{| class="wikitable" | |||
|- | |||
| align='top' | | |||
=== Value container === | |||
The value container applies a function to its value. It is defined by, | |||
:<math> \operatorname{value}\ v\ h = h\ v </math> | |||
so, | |||
:<math> \operatorname{value} = \lambda v.(\lambda h.h\ v) </math> | |||
=== Inc === | |||
The ''inc'' function should take a value containing ''v'', and return a new value containing ''f v''. | |||
:<math> \operatorname{inc}\ (\operatorname{value}\ v) = \operatorname{value}\ (f\ v)</math> | |||
Letting g be the value container, | |||
:<math> g = \operatorname{value}\ v</math> | |||
then, | |||
:<math> g\ f = \operatorname{value}\ v\ f = f\ v</math> | |||
so, | |||
:<math> \operatorname{inc}\ g = \operatorname{value}\ (g\ f) </math> | |||
:<math> \operatorname{inc} = \lambda g.\lambda h.h\ (g\ f) </math> | |||
| align='top' | | |||
=== Extract === | |||
The value may be extracted by applying the identity function, | |||
:<math> I = \lambda u.u </math> | |||
Using ''I'', | |||
:<math> \operatorname{value}\ v\ I = v </math> | |||
so, | |||
:<math> \operatorname{extract}\ k = k\ I </math> | |||
=== Const === | |||
To implement ''pred'' the ''init'' function is replaced with the ''const'' that does not apply ''f''. We need ''const'' to satisfy, | |||
:<math> \operatorname{inc}\ \operatorname{const} = \operatorname{value}\ x </math> | |||
:<math> \lambda h.h\ (\operatorname{const}\ f) = \lambda h.h\ x </math> | |||
Which is satisfied if, | |||
:<math> \operatorname{const}\ f = x </math> | |||
Or as a lambda expression, | |||
:<math> \operatorname{const} = \lambda u.x </math> | |||
|} | |||
==See also== | |||
*[[Lambda calculus]] | |||
*[[System F]] for Church numerals in a typed calculus | |||
*[[Mogensen–Scott encoding]] | |||
==References== | |||
*[http://www.cs.uiowa.edu/~astump/papers/archon.pdf Directly Reflective Meta-Programming] | |||
*[http://www.cs.rice.edu/~javaplt/311/Readings/supplemental.pdf Church numerals and booleans explained] by Robert Cartwright at [[Rice University]] | |||
*[http://www.archive.org/download/TheoreticalFoundationsForPracticaltotallyFunctionalProgramming/33429551_PHD_totalthesis.pdf Theoretical Foundations For Practical 'Totally Functional Programming'] (Chapters 2 and 5) All about Church and other similar encodings, including how to derive them and operations on them, from first principles | |||
*[http://www.csse.monash.edu.au/~lloyd/tildeFP/Lambda/Examples/const-int/ Some interactive examples of Church numerals] | |||
<references/> | |||
[[Category:Lambda calculus]] |
Revision as of 07:06, 18 January 2014
In mathematics, Church encoding is a means of representing data and operators in the lambda calculus. The data and operators form a mathematical structure which is embedded in the lambda calculus. The Church numerals are a representation of the natural numbers using lambda notation. The method is named for Alonzo Church, who first encoded data in the lambda calculus this way.
Terms that are usually considered primitive in other notations (such as integers, booleans, pairs, lists, and tagged unions) are mapped to higher-order functions under Church encoding. The Church-Turing thesis asserts that any computable operator (and its operands) can be represented under Church encoding. In the untyped lambda calculus the only primitive data type is the function. The Church-Turing thesis is that lambda calculus is Turing complete.
The Church encoding is not intended as a practical implementation of primitive data types. Its use is to show that other primitives data types are not required to represent any calculation. The completeness is representational. Additional functions are needed to translate the representation into common data types, for display to people. It is not possible in general to decide if two functions are extrinsically equal due to the undecidability of equivalence from Church's theorem. The translation may apply the function in some way to retrieve the value it represents, or look up its value as a literal lambda term.
Church numerals
Church numerals are the representations of natural numbers under Church encoding. The higher-order function that represents natural number is a function that maps any function to its n-fold composition. In simpler terms, the "value" of the numeral is equivalent to the number of times the function encapsulates its argument.
All Church numerals are functions that take two parameters. Church numerals 0, 1, 2, ..., are defined as follows in the lambda calculus:
Then starting with zero, being not applying the function, then one being applying the function, ...
Number | Function definition | Lambda expression |
---|---|---|
0 | ||
1 | ||
2 | ||
3 | ||
... | ||
n |
The Church numeral 3 represents the action of applying any given function three times to a value. The supplied function is first applied to a supplied parameter and then successively to its own result. The end result is not the numeral 3 (unless the supplied parameter happens to be 0 and the function is a successor function). The function itself, and not its end result, is the Church numeral 3. The Church numeral 3 means simply to do anything three times. It is an ostensive demonstration of what is meant by "three times".
Calculation with Church numerals
Arithmetic operations on numbers may be represented by functions on Church numerals. These functions may be defined in lambda calculus, or implemented in most functional programming languages (see converting lambda expressions to functions).
The addition function uses the identity .
The successor function is β-equivalent to .
The multiplication function uses the identity .
The exponentiation function is given by the definition of a Church numerals; . In the definition substitute to get and,
which gives the lambda expression,
The function is more difficult to understand.
A Church numeral applies a function n times. The predecessor function must return a function that applies it's parameter n - 1 times. This is achieved by building a container around f and x, which is initialized in a way that omits the application of the function the first time. See predecessor for a more detailed explanation.
The subtraction function can be written based on the predecessor function.
Table of functions on Church numerals
Function | Algebra | Identity | Function definition | Lambda expressions | |
---|---|---|---|---|---|
Successor | ... | ||||
Addition | |||||
Multiplication | |||||
Exponentiation | [1] | ||||
Predecessor* | ... | ||||
Subtraction* | ... |
* Note that in the Church encoding,
Translation with other representations
Most real-world languages have support for machine-native integers; the church and unchurch functions convert between nonnegative integers and their corresponding church numerals. The functions are given here in Haskell, where the \
corresponds to the λ of Lambda calculus. Implementations in other languages are similar.
type Church a = (a -> a) -> (a -> a)
church :: Integer -> Church Integer
church 0 = \f -> \x -> x
church n = \f -> \x -> f (church (n-1) f x)
unchurch :: Church Integer -> Integer
unchurch n = n (\x -> x + 1) 0
Church Booleans
Church Booleans are the Church encoding of the Boolean values true and false. Some programming languages use these as an implementation model for Boolean arithmetic; examples are Smalltalk and Pico.
Boolean logic may be considered as a choice. The Church encoding of true and false are functions of two parameters;
- true chooses the first parameter.
- false chooses the second parameter.
The two definitions are known as Church Booleans;
This definition allows predicates (i.e. functions returning logical values) to directly act as if-clauses. A function returning a Boolean, which is then applied to two parameters, returns either the first or the second parameter;
evaluates to then-clause if predicate x evaluates to true, and to else-clause if predicate x evaluates to false.
Because true and false choose the first or second parameter they may be combined to provide logic operators,
- - This is only a correct implementation if the evaluation strategy is applicative order.
- - This is only a correct implementation if the evaluation strategy is normal order.
Some examples:
Predicates
A predicate is a function that returns a Boolean value. The most fundamental predicate is , which returns if its argument is the Church numeral , and if its argument is any other Church numeral:
The following predicate tests whether the first argument is less-than-or-equal-to the second:
Because of the identity,
The test for equality may be implemented as,
Church pairs
DTZ's public sale group in Singapore auctions all forms of residential, workplace and retail properties, outlets, homes, lodges, boarding homes, industrial buildings and development websites. Auctions are at present held as soon as a month.
We will not only get you a property at a rock-backside price but also in an space that you've got longed for. You simply must chill out back after giving us the accountability. We will assure you 100% satisfaction. Since we now have been working in the Singapore actual property market for a very long time, we know the place you may get the best property at the right price. You will also be extremely benefited by choosing us, as we may even let you know about the precise time to invest in the Singapore actual property market.
The Hexacube is offering new ec launch singapore business property for sale Singapore investors want to contemplate. Residents of the realm will likely appreciate that they'll customize the business area that they wish to purchase as properly. This venture represents one of the crucial expansive buildings offered in Singapore up to now. Many investors will possible want to try how they will customise the property that they do determine to buy by means of here. This location has offered folks the prospect that they should understand extra about how this course of can work as well.
Singapore has been beckoning to traders ever since the value of properties in Singapore started sky rocketing just a few years again. Many businesses have their places of work in Singapore and prefer to own their own workplace area within the country once they decide to have a everlasting office. Rentals in Singapore in the corporate sector can make sense for some time until a business has discovered a agency footing. Finding Commercial Property Singapore takes a variety of time and effort but might be very rewarding in the long term.
is changing into a rising pattern among Singaporeans as the standard of living is increasing over time and more Singaporeans have abundance of capital to invest on properties. Investing in the personal properties in Singapore I would like to applaud you for arising with such a book which covers the secrets and techniques and tips of among the profitable Singapore property buyers. I believe many novice investors will profit quite a bit from studying and making use of some of the tips shared by the gurus." – Woo Chee Hoe Special bonus for consumers of Secrets of Singapore Property Gurus Actually, I can't consider one other resource on the market that teaches you all the points above about Singapore property at such a low value. Can you? Condominium For Sale (D09) – Yong An Park For Lease
In 12 months 2013, c ommercial retails, shoebox residences and mass market properties continued to be the celebrities of the property market. Models are snapped up in report time and at document breaking prices. Builders are having fun with overwhelming demand and patrons need more. We feel that these segments of the property market are booming is a repercussion of the property cooling measures no.6 and no. 7. With additional buyer's stamp responsibility imposed on residential properties, buyers change their focus to commercial and industrial properties. I imagine every property purchasers need their property funding to understand in value.
Church pairs are the Church encoding of the pair (two-tuple) type. The pair is represented as a function that takes a function argument. When given its argument it will apply the argument to the two components of the pair. The definition in lambda calculus is,
For example,
List encodings
An (immutable) list is constructed from list nodes. The basic operations on the list are;
Function | Description |
---|---|
nil | Construct an empty list. |
isnil | Test if list is empty. |
cons | Prepend a given value to a (possibly empty) list. |
head | Get the first element of the list. |
tail | Get the rest of the list. |
Three different representations of lists are given.
- Build each list node from two pairs (to allow for empty lists).
- Build each list node from one pair.
- Represent the list using the right fold function.
Two pairs as a list node
A nonempty list can implemented by a Church pair;
- First contains the head.
- Second contains the tail.
However this does not give a representation of the empty list, because there is no "null" pointer. To represent null, the pair may be wrapped in another pair, giving free values,
- First - Is the null pointer (empty list).
- Second.First contains the head.
- Second.Second contains the tail.
Using this idea the basic list operations can be defined like this:[2]
In a nil node second is never accessed, provided that head and tail are only applied to nonempty lists.
One pair as a list node
Alternatively, define [3]
where the last definition is a special case of the general
Represent the list using right fold
As an alternative to the encoding using Church pairs, a list can be encoded by identifying it with its right fold function. For example, a list of three elements x, y and z can be encoded by a higher-order function which when applied to a combinator c and a value n returns c x (c y (c z n)).
Derivation of predecessor function
The predecessor function used in the Church Encoding is,
To build the predecessor we need a way of applying the function 1 less time. A numeral n applies the function f n times to x. The predecessor function must use the numeral n to apply the function n-1 times.
Before implementing the predecessor function, here is a scheme that wraps the value in a container function. We will define new functions to use in place of f and x, called inc and init. The container function is called value. The left hand side of the table shows a numeral n applied to inc and init.
Number | Using init | Using const |
---|---|---|
0 | ||
1 | ||
2 | ||
3 | ||
... | ||
n |
The general recurrence rule is,
If there is also a function to retrieve the value from the container (called extract),
Then extract may be used to define the samenum function as,
The samenum function is not intrinsically useful. What we want is the initial value to skip an application of f. Call this new initial container const. The right hand side of the above table shows the expansions of n inc const. Then by replacing init with const in the expression for the same function we get the predecessor function,
As explained below the functions inc, init, const, value and extract may be defined as,
Which gives the lambda expression for pred as,
Value containerThe value container applies a function to its value. It is defined by, so, IncThe inc function should take a value containing v, and return a new value containing f v. Letting g be the value container, then, so, |
ExtractThe value may be extracted by applying the identity function, Using I, so, ConstTo implement pred the init function is replaced with the const that does not apply f. We need const to satisfy, Which is satisfied if, Or as a lambda expression, |
See also
- Lambda calculus
- System F for Church numerals in a typed calculus
- Mogensen–Scott encoding
References
- Directly Reflective Meta-Programming
- Church numerals and booleans explained by Robert Cartwright at Rice University
- Theoretical Foundations For Practical 'Totally Functional Programming' (Chapters 2 and 5) All about Church and other similar encodings, including how to derive them and operations on them, from first principles
- Some interactive examples of Church numerals
- ↑ This formula is the definition of a Church numeral n with f -> m, x -> f.
- ↑ 20 year-old Real Estate Agent Rusty from Saint-Paul, has hobbies and interests which includes monopoly, property developers in singapore and poker. Will soon undertake a contiki trip that may include going to the Lower Valley of the Omo.
My blog: http://www.primaboinca.com/view_profile.php?userid=5889534 - ↑ John Tromp, Binary Lambda Calculus and Combinatory Logic, in Randomness And Complexity, from Leibniz To Chaitin, ed. Cristian S. Calude, World Scientific Publishing Company, October 2008. (pdf version)