[英]Why does pattern matching on this GADT seem to introduce ambiguity in the type checker?
我正在尝试实现一种抽象语法图的形式,如Andres Loeh和Bruno C. d所述。 S.奥利维拉 。 在大多数情况下,我似乎正确地理解了事情。 但是,当我尝试将letrec引入我的语法时,出现了一些问题。 我认为通过此小代码示例更容易进行工作:
首先,有一些前奏:
{-# LANGUAGE GADTs #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeOperators #-}
import Control.Applicative
infixr 8 :::
data TList :: (k -> *) -> [k] -> * where
TNil :: TList f '[]
(:::) :: f t -> TList f ts -> TList f (t ': ts)
tmap :: (forall a. f a -> g a) -> TList f as -> TList g as
tmap f (TNil) = TNil
tmap f (x ::: xs) = f x ::: tmap f xs
ttraverse :: Applicative i => (forall a. f a -> i (g a)) -> TList f xs -> i (TList g xs)
ttraverse f TNil = pure TNil
ttraverse f (x ::: xs) = (:::) <$> f x <*> ttraverse f xs
现在,我可以为我的语言定义语法。 在这种情况下,我试图描述视频游戏中的二维级别。 我有顶点(平面中的点)和墙(顶点之间的线段)。
data WallId
data VertexId
data LevelExpr :: (* -> *) -> * -> * where
Let
:: (TList f ts -> TList (LevelExpr f) ts)
-> (TList f ts -> LevelExpr f t)
-> LevelExpr f t
Var :: f t -> LevelExpr f t
Vertex :: (Float, Float) -> LevelExpr f VertexId
Wall
:: LevelExpr f VertexId
-> LevelExpr f VertexId
-> LevelExpr f WallId
在PHOAS之后,我们使用更高等级的类型来对f
的选择施加参数:
newtype Level t = Level (forall f. LevelExpr f t)
最后,让我们为letrec
引入一些语法糖,该糖将自动用Var
标记所有内容,如本文所述:
letrec :: (TList (LevelExpr f) ts -> TList (LevelExpr f) ts)
-> (TList (LevelExpr f) ts -> LevelExpr f t)
-> LevelExpr f t
letrec es e =
Let (\xs -> es (tmap Var xs))
(\xs -> e (tmap Var xs))
我们现在可以用这种语言编写一些程序。 这是一个简单的表达式,引入了两个顶点并在它们之间定义了墙。
testExpr :: Level WallId
testExpr =
Level (letrec (\ts ->
Vertex (0,0) :::
Vertex (10,10) :::
TNil)
(\(v1 ::: v2 ::: _) ->
Wall v1 v2))
这样很好。 一个等效的表达式是使用letrec来定义两个顶点以及它们之间的墙,并且全部绑定。 在letrec的主体中,我们可以只返回墙绑定。 我们首先将墙移入letrec,然后添加一些孔以查看GHC知道的内容:
testExprLetrec :: Level WallId
testExprLetrec =
Level (letrec (\ts ->
Vertex (0,0) :::
Vertex (10,10) :::
Wall _ _ :::
TNil)
_)
GHC通知我们:
y-u-no-infer.hs:74:25:
Found hole `_' with type: LevelExpr f VertexId
Where: `f' is a rigid type variable bound by
a type expected by the context: LevelExpr f WallId
at y-u-no-infer.hs:71:3
Relevant bindings include
ts :: TList (LevelExpr f) '[VertexId, VertexId, WallId]
(bound at y-u-no-infer.hs:71:19)
testExprLetrec :: Level WallId (bound at y-u-no-infer.hs:70:1)
In the first argument of `Wall', namely `_'
In the first argument of `(:::)', namely `Wall _ _'
In the second argument of `(:::)', namely `Wall _ _ ::: TNil'
好的,GHC知道ts
包含两个VertexId
和一个WallId
。 我们应该能够在ts
上进行模式匹配以提取每个表达式。
testExprLetrec2 :: Level WallId
testExprLetrec2 =
Level (letrec (\ts@(v1 ::: v2 ::: _) ->
Vertex (0,0) :::
Vertex (10,10) :::
Wall v1 v2 :::
TNil)
_)
当我尝试输入此内容时,系统会提示我
y-u-no-infer.hs:109:20:
Could not deduce (t ~ VertexId)
from the context (ts0 ~ (t : ts))
bound by a pattern with constructor
::: :: forall (k :: BOX) (f :: k -> *) (t :: k) (ts :: [k]).
f t -> TList f ts -> TList f (t : ts),
in a lambda abstraction
at y-u-no-infer.hs:108:23-37
or from (ts ~ (t1 : ts1))
bound by a pattern with constructor
::: :: forall (k :: BOX) (f :: k -> *) (t :: k) (ts :: [k]).
f t -> TList f ts -> TList f (t : ts),
in a lambda abstraction
at y-u-no-infer.hs:108:23-31
`t' is a rigid type variable bound by
a pattern with constructor
::: :: forall (k :: BOX) (f :: k -> *) (t :: k) (ts :: [k]).
f t -> TList f ts -> TList f (t : ts),
in a lambda abstraction
at y-u-no-infer.hs:108:23
Expected type: TList (LevelExpr f) ts0
Actual type: TList (LevelExpr f) '[VertexId, VertexId, WallId]
Relevant bindings include
v1 :: LevelExpr f t (bound at y-u-no-infer.hs:108:23)
In the expression:
Vertex (0, 0) ::: Vertex (10, 10) ::: Wall v1 v2 ::: TNil
In the first argument of `letrec', namely
`(\ ts@(v1 ::: v2 ::: _)
-> Vertex (0, 0) ::: Vertex (10, 10) ::: Wall v1 v2 ::: TNil)'
In the first argument of `Level', namely
`(letrec
(\ ts@(v1 ::: v2 ::: _)
-> Vertex (0, 0) ::: Vertex (10, 10) ::: Wall v1 v2 ::: TNil)
_)'
为什么GHC现在期望TList (LevelExpr f) ts0
,而以前是ts0 ~ '[VertexId, VertexId, WallId]
?
GADT无法可靠地进行类型推断。 您可以通过提供简单的类型注释来修复代码:
testExprLetrec2 :: Level WallId
testExprLetrec2 =
Level (letrec ((\ts@(v1 ::: v2 ::: _
:: TList (LevelExpr f) '[VertexId, VertexId, WallId]) ->
Vertex (0,0) :::
Vertex (10,10) :::
Wall _ _ :::
TNil)
)
_)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.