- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一些可以编译的代码:
{-# LANGUAGE ScopedTypeVariables, KindSignatures, GADTs,
FlexibleContexts #-}
module Foo where
data Foo :: (* -> *) where
Foo :: c m zp' -> Foo (c m zp)
f :: forall c m zp d . Foo (c m zp) -> d
f y@(Foo (x :: c m a)) = g x y
g :: c m a -> Foo (c m b) -> d
g = error ""
我在实际代码中需要的关键是让 GHC 相信如果 y
的类型为 Foo (c m zp)
和 x
其类型为 c' m' zp'
,然后是 c' ~ c
和 m' ~ m
。上面的代码实现了这一点,因为我能够调用 g
。
我想以两种正交方式更改此代码,但我似乎无法弄清楚如何使 GHC 通过任一更改来编译代码。
第一个更改:添加 -XPolyKinds
。 GHC 7.8.3 提示:
Foo.hs:10:11:
Could not deduce ((~) (k2 -> k3 -> *) c1 c)
from the context ((~) * (c m zp) (c1 m1 zp1))
bound by a pattern with constructor
Foo :: forall (k :: BOX)
(k :: BOX)
(c :: k -> k -> *)
(m :: k)
(zp' :: k)
(zp :: k).
c m zp' -> Foo (c m zp),
in an equation for ‘f’
at Foo.hs:10:6-21
‘c1’ is a rigid type variable bound by
a pattern with constructor
Foo :: forall (k :: BOX)
(k :: BOX)
(c :: k -> k -> *)
(m :: k)
(zp' :: k)
(zp :: k).
c m zp' -> Foo (c m zp),
in an equation for ‘f’
at Foo.hs:10:6
‘c’ is a rigid type variable bound by
the type signature for f :: Foo (c m zp) -> d
at Foo.hs:9:13
Expected type: c1 m1 zp'
Actual type: c m a
Relevant bindings include
y :: Foo (c m zp) (bound at Foo.hs:10:3)
f :: Foo (c m zp) -> d (bound at Foo.hs:10:1)
In the pattern: x :: c m a
In the pattern: Foo (x :: c m a)
In an equation for ‘f’: f y@(Foo (x :: c m a)) = g x y
Foo.hs:10:11:
Could not deduce ((~) k2 m1 m)
from the context ((~) * (c m zp) (c1 m1 zp1))
...
第二个变化:忘记-XPolyKinds
。相反,我想使用 -XDataKinds
创建新类型并限制 m
的类型:
{-# LANGUAGE ScopedTypeVariables, KindSignatures, GADTs,
FlexibleContexts, DataKinds #-}
module Foo where
data Bar
data Foo :: (* -> *) where
Foo :: c (m :: Bar) zp' -> Foo (c m zp)
f :: forall c m zp d . Foo (c m zp) -> d
f y@(Foo (x :: c m a)) = g x y
g :: c m a -> Foo (c m b) -> d
g = error ""
我遇到类似的错误(无法推论(c1 ~ c)
,无法推论(m1 ~ m)
)。DataKinds
似乎与这里相关:如果我限制 m
具有种类 Constraint
而不是种类 Bar
,则代码编译良好。
我给出了两个如何破坏原始代码的示例,这两个示例都使用更高级的类型。我尝试过使用 case 语句而不是模式保护,我尝试过为 node
提供类型而不是 x
,我常用的技巧在这里不起作用。
我对 x
的类型最终在哪里/它看起来是什么样子并不挑剔,我只需要能够说服 GHC 如果 y
具有输入 Foo (c m zp)
,则 x
对于某些不相关的类型 zp'
具有类型 c m zp'
。
最佳答案
我将原来的问题大大简化为以下内容,无需 {-# LANGUAGE PolyKinds #-}
即可编译,但无法使用 PolyKinds
进行编译。
{-# LANGUAGE ScopedTypeVariables, KindSignatures, GADTs #-}
{-# LANGUAGE PolyKinds #-}
data Pair1 :: (* -> *) where
Pair1 :: Pair1 (c a, c b)
data D p a where
D :: p (a, b) -> D p a -> D p b
f :: forall c z. D Pair1 (c z) -> D Pair1 (c z)
f y@(D Pair1 x) |
(_ :: D Pair1 (c z)) <- y,
(_ :: D Pair1 (c z')) <- x = y
启用PolyKinds
后,编译器错误为
Could not deduce (c1 ~ c)
from the context ((a, c z) ~ (c1 a1, c1 b))
这个错误强烈暗示了我已经怀疑的事情,答案取决于是否 polykinded type application is injective 。如果多类类型应用是单射的,我们可以如下推导出c1 ~ c
。
(a, c z) ~ (c1 a1, c1 b)
(a,) (c z) ~ (c1 a1,) (c1 b) {- switch to prefix notation -}
c z ~ c1 b {- f a ~ g b implies a ~ b -}
c ~ c1 {- f a ~ g b implies f ~ g -}
c1 ~ c {- ~ is reflexive -}
Polykinded type application is injective ,但 ghc 不知道。为了让 ghc 推断类型应用程序是单射的,我们需要提供类型签名,以便编译器知道类型是等效的。
对于您最初的、过于简化的问题版本,我还没有找到足够的注释。当简化处理类型的问题时,将类型简化为本质上的代理有时有点过分,因为它留下的附加类型签名的位置更少。您有found places to attach kind signatures到一个更有意义的问题。
关于haskell - 具有更高种类类型的 GADT 类型推断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27977076/
这三者之间有什么/为什么有区别? GADT(和常规数据类型)只是数据系列的简写吗?具体有什么区别: data GADT a where MkGADT :: Int -> GADT Int dat
我们能否将构造函数没有给定约束的 GADT 转换为具有上述约束的 GADT?我想这样做是因为我想要深度嵌入箭头,并使用(目前)似乎需要 Typeable 的表示来做一些有趣的事情。 (One reas
我试图将 mod-n 计数器表示为将间隔 [0, ..., n-1] 分成两部分: data Counter : ℕ → Set where cut : (i j : ℕ) → Counter (
这是一个示例代码: {-# LANGUAGE GADTs #-} data NumGadt a where NumGadt :: Num a => a -> Int -> String -> Bo
我为表达式创建了一个 GADT。当我对具有约束的构造函数进行模式匹配时,类型检查器无法推断构造函数约束中使用的类型变量的约束。我认为代码和错误消息更清楚。 {-# LANGUAGE GADTs, Mu
背景 我正在使用 Haskell 编写一个红黑树实现 依赖类型并且我在理解为什么下面的代码不起作用时遇到了一些麻烦。作为一种热身练习,我想做的是找到一个给定任意值的子树。不幸的是,我在编译代码并最终继
在他的论文 Generics for the Masses Hinze 回顾了数据类型的编码。 从 Nat 开始 data Nat :: ⋆ where Zero :: Nat Succ
当我尝试创建一个返回 Thing a 的函数时,我目前正与类型检查器发生冲突。 (其中 Thing 是 GADT)。一个最小的人为例子: {-#LANGUAGE GADTs, EmptyDataDec
考虑以下代码 data Foo f where Foo :: Foo Int class DynFoo t where dynFoo :: Foo f -> Foo t instance Dy
假设我正在编写一个 DSL,并希望同时支持幻像类型和错误类型的表达式。我的值(value)类型可能是 {-# LANGUAGE GADTs, DataKinds #-} data Ty = Num |
当answering a question with a suggestion to use GADTs ,评论中出现了一些关于性能的问题。问题涉及类型类 PlotValue : class Plot
我只是在阅读Dependent Types at Work .在参数化类型的介绍中,作者提到在这个声明中 data List (A : Set) : Set where [] : List A
考虑以下代码: data (:+:) f g a = Inl (f a) | Inr (g a) data A data B data Foo l where Foo :: Foo A data
是分机GADT在 Haskell 中破坏多态性,即使在不使用 GADT 的代码中? 这是一个有效但不使用 GADT 的示例 {-# LANGUAGE RankNTypes #-} --{-# LANG
这两个 GADT 声明之间有区别吗? data A a b where ... data A :: * -> * -> * where ... 最佳答案 没有区别。有人可能会认为,在构
我在 haskell 中建立了一个几何库。我不打算发布它,它只是我用来提高我的语言知识的一个项目。 我有一个 Local数据类型,定义如下 data Local a where MkLocal
我正在阅读 GADTs for dummies Haskell Wiki 上的页面,我仍然不明白如何以及为什么应该使用它们。作者举了一个励志的例子: data T a where D1 ::
我正在解析表单的一些语句 v1 = expression1 v2 = expression2 ... 我正在使用 State Monad 并且我的状态应该是一对 (String, Expr a),我真
如何使用广义代数数据类型? haskell wikibook 中给出的示例太短了,无法让我深入了解 GADT 的真正可能性。 最佳答案 我发现“Prompt”monad(来自“MonadPrompt”
我正在使用 learnyouahaskell 来介绍 GADT,并且我对它们可能的用途很感兴趣。据我了解,它们的主要特点是允许显式类型设置。 如: data Users a where GetUs
我是一名优秀的程序员,十分优秀!