此程序在GHC下正确编译和运行:
type Church a = (a -> a) -> a -> a
ch :: Int -> Church a
ch 0 _ = id
ch n f = f . ch (n-1) f
unch :: Church Int -> Int
unch n = n (+1) 0
suc :: Church a -> Church a
suc n f = f . n f
pre :: Church ((a -> a) -> a) -> Church a
pre n f a = n s z id
where s g h = h (g f)
z = const a
main :: IO ()
main = do let seven = ch 7
eight = suc seven
six = pre seven
print (unch eight)
print (unch six)但是,在使用Frege编译时,我得到了以下错误:
E /home/xgp/work/flab/src/main/frege/flab/fold.fr:23: type error in expression seven
type is : Int
expected: (t1→t1)→t1
E /home/xgp/work/flab/src/main/frege/flab/fold.fr:23: type error in expression seven
type is : (t1→t1)→t1
expected: Int
E /home/xgp/work/flab/src/main/frege/flab/fold.fr:23: type error in expression seven
type is : (t1→t1)→t1
expected: Int
E /home/xgp/work/flab/src/main/frege/flab/fold.fr:23: type error in
expression seven
type is apparently Int
used as function为什么?是否可以修改程序以通过Frege下的编译?
发布于 2016-03-08 17:34:06
这是一种罕见的情况,在这种情况下,泛化类型的let绑定变量确实会产生影响。
问题是,弗雷格在这方面的表现就像GHC一样,关于语用-XMonoLocalBinds的详细信息,请参阅此处:https://github.com/Frege/frege/wiki/GHC-Language-Options-vs.-Frege#Let-Generalization和guide/other-type-extensions.html#typing-binds (还有一个链接到SPJ的一篇论文,其中解释了理由)。
简而言之,这意味着所有未注释的让绑定验证都将具有一个单一类型,并且不能在不同的类型中使用。要恢复多态性,需要显式类型签名。
要使您的程序编译,只需用
seven :: Church a关于print/println:前者不刷新输出。所以你在REPL里有:
frege> print 'a'
IO ()
frege> print 'b'
IO ()
frege> println "dammit!"
abdammit!
IO ()https://stackoverflow.com/questions/35858527
复制相似问题