我一直在閱讀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:其他擴展,是的,我只是修復了備用文件。在我的狂妄之中,我並沒有真正測試我的斷言,但我現在這樣做了。 –