我找不到将加法定义为重复递增的方法,尽管这在无类型语言中是可能的。这是我的代码:
{-# LANGUAGE RankNTypes #-}
type Church = forall a . (a -> a) -> (a -> a)
zero :: Church
zero = \f -> id
inc :: Church -> Church
inc n = \f -> f . n f
-- This version of addition works
add1 :: Church -> Church -> Church
add1 n m = \f -> n f . m f
-- This version gives me a compilation error
add2 :: Church -> Church -> Church
add2 n m = n inc m
我得到的编译错误add2
是
Couldn't match type `forall a1. (a1 -> a1) -> a1 -> a1'
with `(a -> a) -> a -> a'
Expected type: ((a -> a) -> a -> a) -> (a -> a) -> a -> a
Actual type: Church -> (a -> a) -> a -> a
In the first argument of `n', namely `inc'
In the expression: n inc m
In an equation for `add2': add2 n m = n inc m
为什么这是一个错误?不是Church
那个的同义词((a->a) -> a -> a)
吗?