我试图在Haskell中实现教堂数字,但我遇到了一个小问题.Haskell抱怨无限类型
发生检查:无法构造无限类型:t =(t - > t1) - >(t1 - > t2) - > t2
当我尝试做减法时.我99%肯定我的lambda演算是有效的(虽然如果不是,请告诉我).我想知道的是,我能做些什么来让haskell与我的功能一起工作.
module Church where
type (Church a) = ((a -> a) -> (a -> a))
makeChurch :: Int -> (Church a)
makeChurch 0 = \f -> \x -> x
makeChurch n = \f -> \x -> f (makeChurch (n-1) f x)
numChurch x = (x succ) 0
showChurch x = show $ numChurch x
succChurch = \n -> \f -> \x -> f (n f x) …Run Code Online (Sandbox Code Playgroud) 在Haskell中隐性函数组合问题的评论中,人们提到制作一个Num实例a -> r,所以我想我会使用函数表示法来表示乘法:
{-# LANGUAGE TypeFamilies #-}
import Control.Applicative
instance Show (a->r) where -- not needed in recent GHC versions
show f = " a function "
instance Eq (a->r) where -- not needed in recent GHC versions
f == g = error "sorry, Haskell, I lied, I can't really compare functions for equality"
instance (Num r,a~r) => Num (a -> r) where
(+) = liftA2 (+)
(-) = liftA2 (-)
(*) = …Run Code Online (Sandbox Code Playgroud)