- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有以下代码:
{-# LANGUAGE GADTs, DeriveDataTypeable, StandaloneDeriving #-}
import Data.Typeable
class Eq t => OnlyEq t
class (Eq t, Typeable t) => BothEqAndTypeable t
data Wrapper a where
Wrap :: BothEqAndTypeable a => a -> Wrapper a
deriving instance Eq (Wrapper a)
deriving instance Typeable1 Wrapper
然后,以下实例声明将起作用,没有对 t
的约束:
instance OnlyEq (Wrapper t)
并执行我期望的操作。
<小时/>但是以下实例声明不起作用:
instance BothEqAndTypeable (Wrapper t)
自从 GHC - 我使用 7.6.1 - 提示:
No instance for (Typeable t)
arising from the superclasses of an instance declaration
Possible fix:
add (Typeable t) to the context of the instance declaration
In the instance declaration for `BothEqAndTypeable (Wrapper t)'
当然,将
Typeable t
添加到上下文中是可行的。但添加以下实例也是如此:
instance Typeable (Wrapper t) where
typeOf (Wrap x) = typeOf1 (Wrap x) `mkAppTy` typeOf x
有没有办法让 GHC 为我编写后一个实例?如果是这样,怎么办?如果没有,为什么不呢?
我希望 GHC 能够从 Wrap
构造函数的上下文中提取 Typeable
约束,就像它对 Eq
所做的那样> 约束。我认为我的问题归结为这样一个事实:GHC 明确禁止编写 派生实例 Typeable (Wrapper t)
和标准 (Typeable1 s, Typeable a) => Typeable (s a)
实例无法“查看”s a
来查找可键入 a
字典。
最佳答案
I was hoping GHC would be able to pull the
Typeable
constraint from the context on theWrap
constructor
如果它有一个 Wrap
构造函数,它可以从中提取 Typeable
约束。
但它没有 Wrap
构造函数。
不同之处在于 Eq
实例使用该值,因此它是一个 Wrap some
,其中 Wrap
构造函数使 >Eq
可用包装类型字典,一切都很好,或者是 ⊥
,然后一切也都很好,评估 x == y
触底.
注意派生
instance Eq (Wrapper a)
类型变量a
没有有Eq
约束吗?
Prelude DerivT> (undefined :: Wrapper (Int -> Int)) == undefined
*** Exception: Prelude.undefined
Prelude DerivT> (undefined :: (Int -> Int)) == undefined
<interactive>:3:29:
No instance for (Eq (Int -> Int)) arising from a use of `=='
Possible fix: add an instance declaration for (Eq (Int -> Int))
In the expression: (undefined :: Int -> Int) == undefined
In an equation for `it':
it = (undefined :: Int -> Int) == undefined
但是 Typeable
实例不得使用该值,因此如果提供的值不是 Wrap Something
,则不会触底。
因此派生的实例 Typeable1 Wrapper
提供了
instance Typeable t => Typeable (Wrapper t)
但不是无约束的
instance Typeable (Wrapper t)
并且该不受约束的实例无法通过 GHC 导出。
因此你必须提供一个约束
instance Typeable t => BothEqAndTypeable (Wrapper t)
或者无约束
instance Typeable (Wrapper t)
你自己。
关于haskell - 如何让 GHC 为上下文中具有 Typeable 的 GADT 生成 Data.Typeable 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15531420/
假设我有以下代码: {-# LANGUAGE GADTs, DeriveDataTypeable, StandaloneDeriving #-} import Data.Typeable class
我正在尝试为关联的数据系列派生Typeable,如下所示: {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE TypeFamilies #-} modu
如何为 Typeable 编写实际的实例声明?例如,假设我想为 Char 编写一个实例,其中每个字符都有不同的类型,例如 instance Typeable Char where typeOf
[...] wants to find all the places in a legacy codebase that a variable foo is used in the condition
我大致想要实现以下目标: f :: a -> a g :: b -> b h :: Typeable t => t -> Maybe t h x = case x of (x is of type
例如,假设我们有以下数据结构: data Foo = Bool Bool | Int Int | Double Double 现在,有没有更简单的方法来做到这一点: foo :: Typeable a
所以这是我的对象系统传奇的延续(part 1,part 2)。 这部分基本上归结为以下内容。 {-# LANGUAGE ExistentialQuantification #-} {-# LANGUA
通用编程时间! 如果我有一个函数: f :: a1 -> a2 -> a3 -> ... -> an 和一个值 v :: aX -- where 1 a2 -> a3 -> a4 -> a5)
自 GHC 7.8 起,Typeable 是多类的。查看内置 Typeable 实例列表 in the documentation ,我注意到一些有趣的事情: Typeable ((* -> *) -
我遇到过对 Haskell 的 Data.Typeable 的引用,但我不清楚为什么要在代码中使用它。 它解决了什么问题以及如何解决? 最佳答案 Data.Typeable 是一种众所周知的方法(参见
我正在玩 Haskell 的 Data和 Typeable ,并且我一直试图在上下文中没有可用类型变量的情况下获取函数的参数。 让我澄清一下我的意思。只要我像下面这样对类型变量 a 进行量化,我就可以
有人能给我指出一组在 Haskell 中为 GADT 定义 Typeable 或 Typeable1 实例的好示例吗? 或者,有人可以向我展示如何为以下 GADT 定义 Typeable(手动)。 d
我有一些这样的类型: data Currency = USD | EUR deriving (Show, Typeable) data Money :: Currency
我正在尝试为 (':) 派生一个 Typeable 实例 我正在使用 DataKinds 和 [*]。 我启用了 TypeOperators、StandaloneDeriving 和 PolyKind
我有 data D t = ... data SomeStuff = forall a (Typeable a, ...) => SomeStuff a 在某些时候,我得到了一个 SomeStuff,
我试图使用 Shapeless Typeable 来使对 Java 库的访问更加类型安全,但遇到了障碍。我不确定我是否滥用了该库,是否必须手动提供类型类实例,或者是否有其他问题。 此代码演示了我的问题
我是一名优秀的程序员,十分优秀!