教会自然,取幂函数和类型检查

时间:2017-11-30 00:10:43

标签: haskell lambda numbers church-encoding

我对lambda演算中的自然数有一个定义如下,这是我的主要目标。

-- Apply a function n times on x
apply = \f -> \n -> \x -> foldr ($) x $ replicate n f

-- Church numbers
churchZero = \f -> id

churchOne = \f -> \x -> apply f 1 x

churchTwo = \f -> \x -> apply f 2 x

churchNatural = \n -> \f -> \x -> apply f n x

然后,下一步是定义运算符churchSumchurchMulchurchExp

churchSum = \n -> \m -> \f -> \x -> n f (m f x)

churchMul = \n -> \m -> \f -> \x -> n (m f) x

churchExp = \n -> \m -> n m

很好,它有效,前两个函数是“容易”演绎,但指数不是。对我来说至少。为了更多地理解,我对lambda术语进行了beta规范化:
(λf.λx. f(f x))(λf.λx f(f x))以确保有效地取幂是正确的。

beta reduction

所以,我的问题是:如何在不知道指数的情况下推导出这个lambda术语?更重要的是,当类型为λ> churchTwo churchTwo时,Haskell类型的内容会在λ> churchTwo :: (b -> b) -> b -> b上进行检查?在里面做功能的β标准化?

1 个答案:

答案 0 :(得分:3)

你的exp有点倒退:

((\f x -> f$f$f$x) `exp` (\f x -> f$f$x)) (+1) 0 == 8
--      3            ^          2                 = 8???
-- But 2^3 = 8

正确的(-er-ish)版本将是

exp n m = m n
((\f x -> f$f$f$x) `exp` (\f x -> f$f$x)) (+1) 0 == 9
--      3            ^          2                 = 9

因为它保持熟悉的顺序。这并不会影响您定义exp的方式。

指数重复乘法:nm n乘以m次。教会数字表示将函数重复应用于值。因此,churchMul n是一个将数字乘以n的函数,m是重复函数m次的函数,churchOne是基值(乘法的身份)。把它们放在一起,然后简化:

-- n^m is the repeated multiplication of 1 by n, m times
exp n m = m (churchMul n) churchOne
-- expand definitions x2; simplify churchOne
exp n m = m (\o f x -> n (o f) x) (\f x -> f x)
-- eta contract x2
exp n m = m (\o f -> n (o f)) (\f -> f)
-- definition of (.), id
exp n m = m (\o -> n . o) id
-- eta contract
exp n m = m (n .) id
-- eta expand
exp n m f = m (n .) id f
-- Assume m has the type forall b. (b -> b) -> b -> b
-- This assumption may actually be false here, because the implicit type of exp
-- does not require that m, n have that type. The difference is that you could define
-- bogus _ _ _ = 0
-- (which isn't a church numeral) and still pass it to exp, which would no longer
-- act like exponentiation:
-- exp n bogus = bogus (n .) id = const 0
-- which also isn't a church numeral

-- Polymorphic functions like m give rise to theorems that can be derived
-- entirely from their types. I used http://www-ps.iai.uni-bonn.de/cgi-bin/free-theorems-webui.cgi
-- to get this one automatically.
-- Free theorem of the type of m
forall a b (g :: a -> b) (p :: a -> a) (q :: b -> b).
   (forall (x :: a). g (p x) = q (g x)) ->
     (forall (y :: a). g (m p y) = m q (g y))

-- Instantiate g = ($ f), p = (n .), q = n, y = id
  (forall x. (n . x) f = n (x f)) -> (m (n .) id f = m n f)

-- definition of (.)
  (n . x) f = n (x f)
-- so...
  m (n .) id f = m n f
-- transitive property
exp n m f = m n f
-- eta contract
exp n m = m n

以上带有m自由定理的东西实际上是以下参数的严格版本(可能更好地转换为无类型的lambda演算):

-- m, being a valid numeral, is of the form
m f x = f $ f $ ... $ f $ f $ x

m (n .) id = (n .) $ (n .) $ ... $ (n .) $ (n .) $ id
           = (n .) $ (n .) $ ... $ (n .) $ n . id
           = (n .) $ (n .) $ ... $ (n .) $ n
           = (n .) $ (n .) $ ... $ n . n
           ...
           = n . n . ... . n . n
-- so
m n = n . n . ... . n . n = m (n .) id

至于为什么churchTwo churchTwo类型检查,请注意该表达式中的每个匹配项都有不同的类型,因为churchTwo是多态的并描述了整个函数族而不是一个。

-- most general type
churchTwo :: forall b. (b -> b) -> (b -> b)
-- Each occurrence of churchTwo can have a different type, so let's give them
-- different names.
-- I'm using underscores because these variables haven't been solved yet
churchTwo0 :: (_b0 -> _b0) -> (_b0 -> _b0)
churchTwo1 :: (_b1 -> _b1) -> (_b1 -> _b1)
churchTwo0 churchTwo1 :: _
-- Since churchTwo0 is being applied, the whole expression must have the
-- type on the right of the arrow
churchTwo0 churchTwo1 :: _b0 -> _b0
-- Since churchTwo0 is being applied to churchTwo1, the left side of the
-- top level arrow in churchTwo0 must be equal to the type of churchTwo1
(_b0 -> _b0) ~ ((_b1 -> _b1) -> (_b1 -> _b1))
-- Therefore...
(_b0 ~ (_b1 -> _b1))
churchTwo0 churchTwo1 :: (_b1 -> _b1) -> (_b1 -> _b1)
-- That's all the constraints we have, so replace the free variables
-- with universally quantified ones
chuchTwo0 churchTwo1 :: forall b. (b -> b) -> (b -> b)
-- (which is the type of a numeral)