繁体   English   中英

论弗雷格时期的教会数字计划

[英]On Church numeral program under Frege

该程序在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下的编译?

这是极少数情况之一,其中let绑定变量类型的泛化实际上确实有所不同。

关键是,在这方面,Frege的行为类似于GHC和pragma -XMonoLocalBinds ,详见此处: https//github.com/Frege/frege/wiki/GHC-Language-Options-vs.-Frege#Let-Generalization and这里: https//downloads.haskell.org/~ghc/latest/docs/html/users_guide/other-type-extensions.html#typing-binds (还有一篇指向SPJ论文的链接,解释了理由)

简而言之,这意味着所有未注释的让限定的veriabes将具有单态类型,并且不能用于不同类型。 要恢复多态性,需要显式类型签名。

为了使你的程序编译,只需注释seven绑定即可

seven :: Church a

关于print / println:前一个不刷新输出。 所以你在REPL中:

frege> print 'a'
IO ()
frege> print 'b'
IO ()
frege> println "dammit!"
abdammit!
IO ()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM