- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有
data D t = ...
data SomeStuff = forall a (Typeable a, ...) => SomeStuff a
在某些时候,我得到了一个 SomeStuff,我想尝试将其内部 a
转换为 D t
(其中 t
可以any 类型,我只对 D
部分感兴趣)。一些东西(在伪 Haskell 中)看起来像:
case someStuff of
SomeStuff (_ :: a) -> case eqT :: Maybe (a :~: (exists t. D t)) of
Just Refl -> -- proven that `exists t. a ~ D t`
Nothing -> -- nothing to do here
我一直在摆弄 Data.Type.Equality
和 Type.Reflection
(如 App 等),但我无法让它工作。谁有办法做到这一点?
最佳答案
可以利用 Type.Reflection
来测试类型是否为 D _
形式,如下所示。
我们首先给出一些以您的示例为模型的具体定义,以便我们稍后可以测试我们的代码。
{-# LANGUAGE GADTs, RankNTypes, ScopedTypeVariables, TypeOperators, TypeApplications #-}
{-# OPTIONS -Wall #-}
import Type.Reflection
data D t = D1 | D2 -- whatever
data SomeStuff = forall a. (Typeable a, Show a) => SomeStuff a
然后,我们测试 D _
如下:
foo :: SomeStuff -> (forall t. D t -> String) -> String
foo (SomeStuff (x :: a)) f = case typeRep @a of
App d _ | Just HRefl <- eqTypeRep (typeRep @D) d -> f x
_ -> "the SomeStuff argument does not contain a value of type D t for any t"
在上面,我们将 SomeStuff
值和多态函数 f
作为参数。后者需要 D _
形式的参数,此处仅用于说明我们的方法确实有效——如果不需要,可以删除 f
参数那个。
之后,我们采用 takeRep @a
对我们未知类型 a
的(反射)类型表示进行建模。我们检查它是否与模式 App d _
匹配,即类型 a
是否是 d
对我们不关心的东西的应用 _
。如果是这样,我们检查 d
是否确实是我们的 D
类型构造函数的(反射(reflect)表示)。将 eqTypeRepl
的结果与 Just HRefl
进行匹配,使 GHC 假设 a ~ D t
为一些新的类型变量 t
,这就是我们想要的。之后,我们可以调用 f x
确认 GHC 推断出所需的类型。
作为替代方案,我们可以利用 View 模式使我们的代码更紧凑,而不会过多牺牲可读性:
foo :: SomeStuff -> (forall t. D t -> String) -> String
foo (SomeStuff (x :: a)) f = case typeRep @a of
App (eqTypeRep (typeRep @D) -> Just HRefl) _ -> f x
_ -> "the SomeStuff argument does not contain a value of type D t for any t"
关于Haskell:用 Typeable 证明 `exists t. a ~ D t`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67105950/
假设我有以下代码: {-# 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 库的访问更加类型安全,但遇到了障碍。我不确定我是否滥用了该库,是否必须手动提供类型类实例,或者是否有其他问题。 此代码演示了我的问题
我是一名优秀的程序员,十分优秀!