我正在尝试实现Abstract Syntax Graphs, as described by Andres Loeh and Bruno C. d. S. Oliveira的形式。在大多数情况下,我似乎正确地理解了事情。但是,当我尝试将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来定义两个顶点以及它们之间的墙,并且全部绑定(bind)。在letrec的主体中,我们可以只返回墙绑定(bind)。我们首先将墙壁移入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)
)
_)
关于haskell - 为什么在此GADT上进行模式匹配似乎会在类型检查器中引入歧义?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27694624/