Haskell类型变量不解决
我有以下代码段,
class LatticeElement a where
next :: a -> a -- next element
class (Ord a, LatticeElement a) => LatticeDate a where
prev :: a -> a -- prev date
data LatticeSlice d v = forall d v. (LatticeDate d, LatticeElement v) => LatticeSlice{date :: d, slice :: v}
map :: (v -> w) -> LatticeSlice d v -> LatticeSlice d w
map f LatticeSlice{date = d, slice = sl} = LatticeSlice{date = d, slice = f sl}
该代码会导致编译错误。现在,据我了解代码,首先,我将latticeElement
作为Typeclass设置,然后将latticedate
作为latticeelement
typecleass,typecleass 要求类型d
是ord
的实例。
现在,我创建latticeslice
,并在声明中读到“ latticeslice
是类型上的数据类型d
和v ,仅适用于所有类型
d
实例化latticedate
和v
实例化latticeElement
MAP
的实例化:
src/Lattice.hs:9:77: error:
• Couldn't match expected type ‘v’ with actual type ‘v1’
‘v1’ is a rigid type variable bound by
a pattern with constructor:
LatticeSlice :: forall d1 v1 d2 v2.
(LatticeDate d2, LatticeElement v2) =>
d2 -> v2 -> LatticeSlice d1 v1,
in an equation for ‘map’
at src/Lattice.hs:9:7-40
‘v’ is a rigid type variable bound by
the type signature for:
Lattice.map :: forall v w d.
(v -> w) -> LatticeSlice d v -> LatticeSlice d w
at src/Lattice.hs:8:1-55
• In the first argument of ‘f’, namely ‘sl’
In the ‘slice’ field of a record
In the expression: LatticeSlice {date = d, slice = f sl}
• Relevant bindings include
sl :: v1 (bound at src/Lattice.hs:9:38)
f :: v -> w (bound at src/Lattice.hs:9:5)
map :: (v -> w) -> LatticeSlice d v -> LatticeSlice d w
(bound at src/Lattice.hs:9:1)
我认为编译器告诉我,它在(v - > w)中没有解决
v
与Latticeslice D V
中的V
相同。但是我不知道为什么要这样做,或者更重要的是,该怎么办!
I have the following snippet of code
class LatticeElement a where
next :: a -> a -- next element
class (Ord a, LatticeElement a) => LatticeDate a where
prev :: a -> a -- prev date
data LatticeSlice d v = forall d v. (LatticeDate d, LatticeElement v) => LatticeSlice{date :: d, slice :: v}
map :: (v -> w) -> LatticeSlice d v -> LatticeSlice d w
map f LatticeSlice{date = d, slice = sl} = LatticeSlice{date = d, slice = f sl}
Which causes a compilation error. Now, as far as I understand the code, first I set up LatticeElement
as a typeclass, then LatticeDate
as a specialisation of the LatticeElement
typecleass, requiring that the type d
is an instance of Ord
.
Now I create LatticeSlice
, with a declaration that I read something like "LatticeSlice
is a data type on types d
and v
, constrained to exist only for all types d
instantiating LatticeDate
and v
instantiating LatticeElement
"
The error comes in the instantiation of map
:
src/Lattice.hs:9:77: error:
• Couldn't match expected type ‘v’ with actual type ‘v1’
‘v1’ is a rigid type variable bound by
a pattern with constructor:
LatticeSlice :: forall d1 v1 d2 v2.
(LatticeDate d2, LatticeElement v2) =>
d2 -> v2 -> LatticeSlice d1 v1,
in an equation for ‘map’
at src/Lattice.hs:9:7-40
‘v’ is a rigid type variable bound by
the type signature for:
Lattice.map :: forall v w d.
(v -> w) -> LatticeSlice d v -> LatticeSlice d w
at src/Lattice.hs:8:1-55
• In the first argument of ‘f’, namely ‘sl’
In the ‘slice’ field of a record
In the expression: LatticeSlice {date = d, slice = f sl}
• Relevant bindings include
sl :: v1 (bound at src/Lattice.hs:9:38)
f :: v -> w (bound at src/Lattice.hs:9:5)
map :: (v -> w) -> LatticeSlice d v -> LatticeSlice d w
(bound at src/Lattice.hs:9:1)
I think that the compiler is telling me that it isn't resolving the v
in (v -> w)
to be the same type as the v
in LatticeSlice d v
. But I don't know why it is doing this, or more to the point, what to do about it!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,
forall dv。
阴影latticeslice
的参数。latticeslice
的类型演示了值构造函数的参数类型
d2
和v2
与类型构造函数的参数无关。 > d1 和v1
:如果删除
forall
量化器量化您的数据类型携带的约束,在这种情况下,我再次建议它!它等同于此GADT:约束您的定义,
MAP
在w
参数中无法参数。仅仅因为v
是latticeElement
并不意味着w
是。因此,我们必须见证f sl :: w
是latticelement w
:这使其与
functor
(>)的基本基础结构不相容(
)适用的
,monad
..)和bifunctor
(biapplicative
,..)。这是最好的版本,如果需要晶格约束,则将它们添加到在
latticeslice
上操作的功能中,请观看Edward Kmett的类型类与世界有关更多信息。
First of all the
forall d v.
shadows the arguments ofLatticeSlice
.The type of
LatticeSlice
demonstrates thisThe argument types of value constructor has
d2
andv2
has nothing to do with the arguments of the type constructord1
andv1
:If you remove the
forall
quantifiers your datatype is carrying around the constraint, in this case I recommend againt it! It is equivalent to this GADT:The constraint encumbers your definitions,
map
can not be parametric in thew
argument. Just becausev
is aLatticeElement
does not meanw
is. Therefore we must witness thatf sl :: w
is aLatticeElement w
:This makes it incompatible with the basic infrastructure like
Functor
(Applicative
,Monad
..) andBifunctor
(Biapplicative
, ..).This is the best version, if you need lattice constraints you add them to the functions that operate on
LatticeSlice
Watch Edward Kmett's Type Classes vs. the World for more information.