我一直在阅读Haskell wiki上的type arithmetic页面,并且在类型系统中嵌入的lambda微积分部分有点麻烦。从那一节中,我收集到以下代码不适用于GHC/GHCi - 显然GHC不应该能够确定g的类型签名。Haskell类型级lambda微积分错误(或缺少)
{-# OPTIONS -fglasgow-exts #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE UndecidableInstances #-}
data X
data App t u
data Lam t
class Subst s t u | s t -> u
instance Subst X u u
instance (Subst s u s', Subst t u t') => Subst (App s t) u (App s' t')
instance Subst (Lam t) u (Lam t)
class Apply s t u | s t -> u
instance (Subst s t u, Eval u u') => Apply (Lam s) t u'
class Eval t u | t -> u
instance Eval X X
instance Eval (Lam t) (Lam t)
instance (Eval s s', Apply s' t u) => Eval (App s t) u
f :: Eval (App (Lam X) X) u => u
f = undefined
g :: Eval (App (Lam (App X X)) (Lam (App X X))) u => u
g = undefined
注意,我不得不添加FlexibleContexts和UndecidableInstances,因为给定的代码似乎并不没有这些扩展进行编译。然而,当我跑这跟GHCI(8.0.2版本),我得到如下结果:
*Main> :t f
f :: X
*Main> :t g
g :: u
,这是特别奇怪,我,因为U型尚未在任何地方定义。这是上述两种语言扩展的结果,它们是相互作用的,还是格拉斯哥外部语言?如果是这样,怎么样?
这当然很有趣。我原以为GHC会继续评估这个表达,但我想不是。这是由于UndecidableInstances,然后呢?另外,当我尝试运行备用文件时,ghci声称我需要FlexibleInstances和AllowAmbiguousTypes。 – InThisStyle10s6p
@InThisStyle10s6p什么是“this”中的“这是由于UndecidableInstances',然后?”? –
@ InThisStyle10s6p re:其他扩展,是的,我只是修复了备用文件。在我的狂妄之中,我并没有真正测试我的断言,但我现在这样做了。 –