试图抽象掉类型类,但类型变量逃逸了
Trying to abstract away typeclass, but a type variable escapes
我有一些 类 和它们的实例。该示例显示了一些无意义的 类。它们的确切性质并不重要。
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
class Foo a where
foo :: a -> Int
class Bar a where
bar :: a -> String
instance Foo Int where
foo x = x
instance Foo String where
foo x = length x
instance Bar Int where
bar x = show x
instance Bar String where
bar x = x
好的,现在我想创建一些存在类型,将这些 类 隐藏在某些数据类型外观后面,这样我就不必处理约束。 (我知道存在类型被认为是一种反模式,请不要向我解释这个)。
data TFoo = forall a. Foo a => TFoo a
instance Foo TFoo where
foo (TFoo x) = foo x
data TBar = forall a. Bar a => TBar a
instance Bar TBar where
bar (TBar x) = bar x
显然里面有一些样板。我想把它抽象出来。
{-# LANGUAGE ConstraintKinds #-}
data Obj cls = forall o. (cls o) => Obj o
所以我没有几种存在类型,只有一种,由类型类参数化。到目前为止一切顺利。
现在如何对 Obj a
执行操作?明显的尝试
op f (Obj a) = f a
失败,因为类型变量可能会转义。
existential.hs:31:18: error:
• Couldn't match expected type ‘o -> p1’ with actual type ‘p’
because type variable ‘o’ would escape its scope
This (rigid, skolem) type variable is bound by
a pattern with constructor:
Obj :: forall (cls :: * -> Constraint) o. cls o => o -> Obj cls,
in an equation for ‘call’
at existential.hs:31:9-13
• In the expression: f k
In an equation for ‘call’: call f (Obj k) = f k
• Relevant bindings include
k :: o (bound at existential.hs:31:13)
f :: p (bound at existential.hs:31:6)
call :: p -> Obj cls -> p1 (bound at existential.hs:31:1)
|
31 | call f (Obj k) = f k
| ^^^
Failed, no modules loaded.
我有点明白为什么会这样了。但是对于 call foo
和 call bar
等真正的调用,类型变量不会转义。我可以说服编译器吗?也许我能以某种方式表达 u -> v where v does not mention u
类型(实际上应该是 f
的类型)?如果没有,还有什么其他方法可以处理这种情况?我想我可以用 TemplateHaskell 生成一些东西,但我仍然无法理解它。
您的代码运行良好;编译器只需要一些关于它的类型的帮助。
Obj
隐藏了其内容的类型,这意味着 op
的参数 f
必须是多态的(也就是说,它不能仔细检查其参数)。开启 RankNTypes
:
op :: (forall a. cls a => a -> r) -> Obj cls -> r
op f (Obj x) = f x
您必须提供完整的类型签名,因为 GHC 无法推断更高级别的类型。
像这样对 class 进行存在量化是 usually not the best way 设计给定程序。
我有一些 类 和它们的实例。该示例显示了一些无意义的 类。它们的确切性质并不重要。
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
class Foo a where
foo :: a -> Int
class Bar a where
bar :: a -> String
instance Foo Int where
foo x = x
instance Foo String where
foo x = length x
instance Bar Int where
bar x = show x
instance Bar String where
bar x = x
好的,现在我想创建一些存在类型,将这些 类 隐藏在某些数据类型外观后面,这样我就不必处理约束。 (我知道存在类型被认为是一种反模式,请不要向我解释这个)。
data TFoo = forall a. Foo a => TFoo a
instance Foo TFoo where
foo (TFoo x) = foo x
data TBar = forall a. Bar a => TBar a
instance Bar TBar where
bar (TBar x) = bar x
显然里面有一些样板。我想把它抽象出来。
{-# LANGUAGE ConstraintKinds #-}
data Obj cls = forall o. (cls o) => Obj o
所以我没有几种存在类型,只有一种,由类型类参数化。到目前为止一切顺利。
现在如何对 Obj a
执行操作?明显的尝试
op f (Obj a) = f a
失败,因为类型变量可能会转义。
existential.hs:31:18: error:
• Couldn't match expected type ‘o -> p1’ with actual type ‘p’
because type variable ‘o’ would escape its scope
This (rigid, skolem) type variable is bound by
a pattern with constructor:
Obj :: forall (cls :: * -> Constraint) o. cls o => o -> Obj cls,
in an equation for ‘call’
at existential.hs:31:9-13
• In the expression: f k
In an equation for ‘call’: call f (Obj k) = f k
• Relevant bindings include
k :: o (bound at existential.hs:31:13)
f :: p (bound at existential.hs:31:6)
call :: p -> Obj cls -> p1 (bound at existential.hs:31:1)
|
31 | call f (Obj k) = f k
| ^^^
Failed, no modules loaded.
我有点明白为什么会这样了。但是对于 call foo
和 call bar
等真正的调用,类型变量不会转义。我可以说服编译器吗?也许我能以某种方式表达 u -> v where v does not mention u
类型(实际上应该是 f
的类型)?如果没有,还有什么其他方法可以处理这种情况?我想我可以用 TemplateHaskell 生成一些东西,但我仍然无法理解它。
您的代码运行良好;编译器只需要一些关于它的类型的帮助。
Obj
隐藏了其内容的类型,这意味着 op
的参数 f
必须是多态的(也就是说,它不能仔细检查其参数)。开启 RankNTypes
:
op :: (forall a. cls a => a -> r) -> Obj cls -> r
op f (Obj x) = f x
您必须提供完整的类型签名,因为 GHC 无法推断更高级别的类型。
像这样对 class 进行存在量化是 usually not the best way 设计给定程序。