Haskell 为什么赢了';GHC减少我的家庭类型?
这是一个非类型的lambda演算,它的项由自由变量索引。我正在使用该库处理类型级别字符串的单例值Haskell 为什么赢了';GHC减少我的家庭类型?,haskell,lambda-calculus,gadt,type-families,Haskell,Lambda Calculus,Gadt,Type Families,这是一个非类型的lambda演算,它的项由自由变量索引。我正在使用该库处理类型级别字符串的单例值 {-# LANGUAGE DataKinds #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE PolyKinds #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeOperators #-} {-# LANGUAGE UndecidableInstances #-} import Data.Singletons i
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
import Data.Singletons
import Data.Singletons.TypeLits
data Expr (free :: [Symbol]) where
Var :: Sing a -> Expr '[a]
Lam :: Sing a -> Expr as -> Expr (Remove a as)
App :: Expr free1 -> Expr free2 -> Expr (Union free1 free2)
Var
引入自由变量。lambda抽象绑定一个在主体中显示为自由的变量(如果有匹配的变量)。应用程序将表达式两部分的自由变量连接起来,删除重复项(因此xy
的自由变量是x
和y
,而xx
的自由变量只是x
)。我写下了这些类型的族:
type family Remove x xs where
Remove x '[] = '[]
Remove x (x ': xs) = Remove x xs
Remove x (y ': xs) = y ': Remove x xs
type family Union xs ys where
Union xs ys = Nub (xs :++ ys)
type family xs :++ ys where
'[] :++ ys = ys
(x ': xs) :++ ys = x ': (xs :++ ys)
type family Nub xs where
Nub xs = Nub' '[] xs
type family Nub' seen xs where
Nub' seen '[] = '[]
Nub' seen (x ': xs) = If (Elem x seen) (Nub' seen xs) (Nub' (x ': seen) (x ': xs))
type family If c t f where
If True t f = t
If False t f = f
type family Elem x xs where
Elem x '[] = False
Elem x (x ': xs) = True
Elem x (y ': xs) = Elem x xs
我在交互式提示下对此进行了测试:
ghci> :t Var (sing :: Sing "x")
Var (sing :: Sing "x") :: Expr '["x"] -- good
ghci> :t (Lam (sing :: Sing "x") (Var (sing :: Sing "x")))
(Lam (sing :: Sing "x") (Var (sing :: Sing "x")))
:: Expr (Remove "x" '["x"]) -- not so good
我惊讶地发现,标识函数的类型\x。x
是Expr(删除“x”[“x”])
,而不是Expr'[]
。GHC似乎不愿意评估类型族Remove
。
我做了更多的实验,了解到我的类型家庭本身没有问题-GHC很乐意在这种情况下减少它:
ghci> :t (Proxy :: Proxy (Remove "x" '["x"]))
(Proxy :: Proxy (Remove "x" '["x"])) :: Proxy '[]
那么:当我查询GADT的类型时,GHC为什么不将
删除“x”[/x”]
为'[/code>?通常情况下,类型检查器何时计算类型族?有什么启发法可以用来避免对它的行为感到惊讶吗?它是有效的。GHC似乎只是懒惰
λ *Main > :t (Lam (Proxy :: Proxy "x") (Var (Proxy :: Proxy "x")))
(Lam (Proxy :: Proxy "x") (Var (Proxy :: Proxy "x")))
:: Expr (Remove "x" '["x"])
λ *Main > :t (Lam (Proxy :: Proxy "x") (Var (Proxy :: Proxy "x"))) :: Expr '[]
(Lam (Proxy :: Proxy "x") (Var (Proxy :: Proxy "x"))) :: Expr '[]
:: Expr '[]
λ *Main > :t (Lam (Proxy :: Proxy "x") (Var (Proxy :: Proxy "x"))) :: Expr '["x"]
<interactive>:1:2:
Couldn't match type ‘'[]’ with ‘'["x"]’
Expected type: Expr '["x"]
Actual type: Expr (Remove "x" '["x"])
In the expression:
(Lam (Proxy :: Proxy "x") (Var (Proxy :: Proxy "x"))) ::
Expr '["x"]
我读过“为什么GHC不能减少我的家庭?”这听起来很残酷。@JoachimBreitner即使是最好的编译器也不能做你想让他们做的一切,我怀疑删除中的重叠定义是基于。您可能需要一个约束,以证明类型是不相等的,因此删除“x”[“x”]
似乎与'[]
相统一,但不规范化,至少在交互提示下不规范化。知道为什么吗?“GHC似乎只是懒惰”当然是真的,但我希望得到一个更具启发性的解释!我不是100%肯定。有一节是关于GADT如何让事情变得更难的。所以,也许不是懒惰,而是小心(有人也能理解为:)。
{-# LANGUAGE TypeOperators, DataKinds, TypeFamilies, GADTs #-}
import Data.Proxy
import GHC.TypeLits
type family Remove (x :: Symbol) (xs :: [Symbol]) where
Remove x '[] = '[]
Remove x (x ': xs) = Remove x xs
Remove x (y ': xs) = y ': Remove x xs
data Expr (free :: [Symbol]) where
Var :: KnownSymbol a => Proxy a -> Expr '[a]
Lam :: KnownSymbol a => Proxy a -> Expr as -> Expr (Remove a as)
-- App :: Expr free1 -> Expr free2 -> Expr (Union free1 free2)