Church encoding: Difference between revisions

Content deleted Content added
alonzo church
Church numerals: -- added lambdas to lambda terms for clarity
Line 7:
==Church numerals==
 
A Church numeral is the representation of a [[natural number]] as a [[function (mathematics)|function]] under Church encoding. The [[higher-order function]] that represents natural number <tt>''n''</tt> is a function that maps aany other function <tt>''F''</tt> to its <tt>''n''</tt>-fold [[function composition|composition]] <tt>''F''&nbsp;<smallsup>o''n''</smallsup>&nbsp; = ''F''&nbsp;<small> ο ''F'' o</small>&nbsp; ...&nbsp;<small> o</small>&nbsp; ''F''</tt>.
 
===Definition===
Church numerals <tt>&lt;'''0''&gt;', &lt;'''1''&gt;', &lt;'''2''&gt;', ...</tt>, are defined as follows in the [[lambda calculus]]:
: <code style="text-decoration:overline">0</code> &equiv; <code>&lambda;f.&lambda;x. x</code>
: <tt>&lt;''0''&gt; := &lambda;''fx''.''x''</tt>
: <tt>&lt;'''1''' &gtequiv; := <code>&lambda;''fx''f.''fx''&lambda;x. f x</ttcode>
: <tt>&lt;'''2''' &gtequiv; := <code>&lambda;''fx''f.''&lambda;x. f'' (''fx''f x)</ttcode>
: <tt>&lt;'''3''' &gtequiv; := <code>&lambda;''fx''f.''&lambda;x. f'' (''f'' (''fx''f x))</ttcode>
: ...
: <tt>&lt;'''n''' &gtequiv; := <code>&lambda;''fx''f.''&lambda;x. f''<sup>''n''</sup>'' x''</ttcode>
: ...
 
That is, the natural number <tt>''n''</tt> is represented by the Church numeral <tt>&lt;'''n''&gt;</tt>', which is a lambda-term with thehas property that for any lambda-terms ''F'' and ''X'',
 
: '''n''' <code>''F'' ''X''</code> [[Lambda calculus#&beta;-reduction|=<sub>&beta;</sub>]] <code>''F''<sup>''n''</sup> ''X'' </code>
: <tt> &lt;''n''&gt; ''F X'' </tt>
: <tt> = (&lambda;''fx''.''f''<sup>''n''</sup>x) ''F X'' </tt>
: <tt> = (&lambda;''x''.''F''<sup>''n''</sup>x) ''X'' </tt>
: <tt> = ''F''<sup>''n''</sup> ''X'' </tt>
 
The notation here assumes the usual convention that unless otherwise indicated by parentheses, association is to the ''left'' on the right side of the "<tt>.</tt>", and to the ''right'' on the left side (where implied &lambda;'s are also omitted). Thus, <tt>&lambda;''xyz''.''abc''</tt> abbreviates <tt>&lambda;''x''.(&lambda;''y''.(&lambda;''z''.((''ab'')''c'')))</tt>.
 
===Computation with Church numerals===
In the lambda calculus, numeric functions are representable by corresponding functions of Church numerals, as in the following examples:
 
:The <tt>successor Successorfunction := &lambda;''nfx'succ'.''f(''(x''nfx)='') </tt> x''+1:
has the property that for any Church numeral &lt;''n''&gt;,
: <tt> Successor &lt;''n''&gt; </tt>
: <tt> = (&lambda;''nfx''.''f''(''nfx'')) &lt;''n''&gt; </tt>
: <tt> = &lambda;''fx''.''f''(&lt;''n''&gt;''fx'') </tt>
: <tt> = &lambda;''fx''.''f''(''f''<sup>''n''</sup>''x'')</tt>
: <tt> = &lambda;''fx''.''f''<sup>''n+1''</sup>''x'' </tt>
: <tt> = &lt;''n+1''&gt; </tt>
 
Similarly,
: <tt> Add := &lambda;''mnfx''.''mf''(''nfx'') </tt>
has the property that for any Church numerals <tt>&lt;''m''&gt;</tt>,<tt>&lt;''n''&gt;</tt>,
: <tt> Add &lt;''m''&gt; &lt;''n''&gt; </tt>
: <tt> = (&lambda;''mnfx''.''mf''(''nfx'')) &lt;''m''&gt; &lt;''n''&gt; </tt>
: <tt> = &lambda;''fx''.&lt;''m''&gt;''f''(&lt;''n''&gt;''fx'') </tt>
: <tt> = &lambda;''fx''.''f''<sup>''m''</sup>(''f''<sup>''n''</sup>''x'') </tt>
: <tt> = &lambda;''fx''.''f''<sup>''m+n''</sup>''x'' </tt>
: <tt> = &lt;''m+n''&gt;</tt>
 
Likewise,
: <tt> Predecessor := &lambda;''nfx''.''n''(&lambda;''gh''.''h''(''gf''))(&lambda;''u''.''x'')(&lambda;''u''.''u'')</tt>
: <tt> Multiply := &lambda;''mnfx''.''m''(''nf'')''x'' </tt>
: <tt> Exponentiate := &lambda;''mnfx''.''nmfx'' </tt>
have the expected properties
: <tt> Predecessor &lt;''n''&gt; = &lt;''n-1''&gt; (''n'' &gt; 0) </tt>
: <tt> Multiply &lt;''m''&gt; &lt;''n''&gt; = &lt;''m''*''n''&gt;
: <tt> Exponentiate &lt;''m''&gt; &lt;''n''&gt; = &lt;''m''<sup>''n''</sup>&gt;
 
: '''succ''' &equiv; <code>&lambda;n.&lambda;f.&lambda;x. f (n f x)</code>
In similar fashion, ''all [[recursive function]]s'' are representable in the lambda calculus (are "&lambda;-definable") using Church numerals.
 
Note that <code>n f x</code> =<sub>&beta;</sub> <code>f<sup>''n''</sup> x</code>, so <code>f (n f x)</code> =<sub>&beta;</sub> <code>f (f<sup>''n''</sup> x)</code> =<sub>&beta;</sub> <code>f<sup>''n+1''</sup> x</code>.
===Church numerals in other functional programming languages===
In [[Haskell programming language|Haskell]], a [[function (programming)|function]] that returns a particular Church numeral might be
 
The addition function add(''m'',''n'')=m+n iterates <code>f</code> ''n'' times (starting with <code>x</code>) and uses that result as the base case for ''m'' iterations of <code>x</code>.
church 0 = \ f x -> x
church n = c
where
c f x = c' f (f x)
where
c' = church (n - 1)
 
: '''add''' &equiv; <code>&lambda;m.&lambda;n.&lambda;f.&lambda;x. m f (n f x)</code>
The transformation from a Church numeral to an ordinary numeral might be
 
The multiplication function mult(''m'',''n'')=m*n
unchurch n = n (+1) 0
 
: '''mult''' &equiv; <code>&lambda;m.&lambda;n.&lambda;f.&lambda;x. m (n f) x</code>
Thus the (+1) function would be applied to an initial value of 0 ''n'' times, yielding the ordinary numeral for ''n''. (By "ordinary numeral" is meant a numeral in the default representation, e.g. decimal, of whatever computer language is being used.)
 
The exponentiation function exp(''m'',''n'')=m<sup>n</sup>
In [[Scheme programming language|Scheme]], one simple expression of the Church numerals is as follows:
 
: '''exp''' &equiv; <code>&lambda;m.&lambda;n.&lambda;f.&lambda;x. n m f x</code>
(define (church n)
(if (= n 0)
(lambda (f x) x)
(lambda (f x) ((church (- n 1)) f (f x))))))
 
The predecessor function pred(''x'')=0 if ''x''=0, ''x''-1 otherwise.
This set of Church numerals can be transformed to ordinary numerals as follows:
 
: '''pred''' &equiv; <code>&lambda;n.&lambda;f.&lambda;x. n (&lambda;g.&lambda;h. h (g f))(&lambda;u. x)(&lambda;u. u)</code>
(define add1 (lambda (x) (+ 1 x)))
(define unchurch (lambda (c) ((c add1 0)))
 
Then given (define two (church 2)), (unchurch two) returns the value 2.
 
===Church numerals in other functional programming languages===
There are other mappings -- the Church numerals will map onto any countable set. Here is a mapping onto the powers of 2.
The functions described above can be implemented in most functional programming languages (subject to type constraints) by direct translation of lambda terms.
Since most real-world languages have support for machine-native integers, the ''church'' and ''unchurch'' functions (given here in [[Haskell programming language|Haskell]]) can make explicit the correspondence between nonnegative integers and lambda terms:
 
:<code>church :: Integer -> (a -> a) -> a -> a
(define times2 (lambda (x) (* 2 x)))
:<code>church 0 = \s f-> x\z -> xz</code>
(define unchurchpoweroftwo (lambda (c) (c times2 1)))
:<code>church n = \s -> \z -> s (church (n-1) s z)</code>
 
:<code>unchurch :: ((a -> a) -> a -> a) -> Integer
Then unchurchpoweroftwo three returns 8, the third power of 2.
:<code>unchurch n = n (\x -> x + 1) 0</code>
 
Addition: (define (add (lambda m n) (lambda (f x) (m f (n f x)))))
 
Implementations of these conversions in other languages are similar.
(define seven (church 7))
(define eight (church 8))
(unchurch (add seven eight))
==> 15
 
==Church booleans==