Church Numerals: Approaches In Scheme

Church Numerals is a representation of the natural number using lambda notation, defined as follows:

0 ≡ λf.λx. x
1 ≡ λf.λx. f x
2 ≡ λf.λx. f (f x)
3 ≡ λf.λx. f (f (f x))
...
n ≡ λf.λx. fn x
...

That is, n F x == F^n x, where n is a Church numeral. Zero can be represented as follows in Scheme:

(define zero (lambda (f) (lambda (x) x)))

Scheme implementations for calculations with Church Numeral:

  1. Plus: plus(m, n) = m + n (where m,n are both Church numerals) can be inferred from F^(m+n) (x) == F^m(F^n(x)) == m F (n F x), so in lambda notation, plus function can be expressed as λm.λn.λf.λx. m f (n f x). Scheme implementation: 
    (define (plus m n) (lambda (f) (lambda (x) ((m f) ((n f) x)))))

  2. Successor: succ(n) = n + 1 (where n is a Church numeral) can be inferred directly from plus function as plus(n, 1) == F (n F x). That is, two above function are β-equivalent. Scheme implementation:
    (define (succ n) (lambda (f) (lambda (x) (f ((n f) x)))))

  3. Multiply: mult(m, n) = m * n (where m,n are both Church numerals) can be inferred from identical equation F^(m*n) == F^m^n == n (m F), while in lambda notation: λm.λn.λf. n (m f). Scheme implementation:
    (define (mult m n) (lambda (f) (n (m f))))

  4. Exponentiation: exp(m, n) = m^n (where m,n are both Church numerals) comes right from the definition of Church numerals which is, in lambda notation, λm.λn. n m. Scheme implementation:
    (define (exp m n) (n m))

  5. Predecessor: Predecessor function is a bit more complex. pred(n) = n - 1 (where n is a Church numeral and n != 0) works by generating an n-fold composition of functions that each apply their argument g to f. In lambda notation it can be expressed as: λn.λf.λx. n (λg.λh. h (g f)) (λu. x) (λu. u). Scheme implementation:
    (define (pred n) (lambda (f) (lambda (x) (((n (lambda (g) (lambda (h) (h (g f))))) (lambda (u) x)) (lambda (u) u)))))

  6. A Church numeral can be converted to digit by invoking following function:
    (define (to-digit n) ((n (lambda (k) (+ k 1))) 0))

Reference:

    Church encoding in Wikipedia http://en.wikipedia.org/wiki/Church_encoding


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值