现有量化类型无法在类型类上下文中推断

高飞22
{-# LANGUAGE ExistentialQuantification, DeriveDataTypeable #-}
import Data.Typeable;

data EnumBox = forall s. (Enum s, Show s) => EB s
           deriving Typeable

instance Show EnumBox where
  show (EB s) = "EB " ++ show s

这行得通。但是,如果我想为EnumBox添加类Enum的实例,例如:

instance Enum EnumBox where
  succ (EB s) = succ s

它失败,并显示以下消息:

Could not deduce (s ~ EnumBox)
from the context (Enum s, Show s)
  bound by a pattern with constructor
             EB :: forall s. (Enum s, Show s) => s -> EnumBox,
           in an equation for `succ'
  at typeclass.hs:11:9-12
  `s' is a rigid type variable bound by
      a pattern with constructor
        EB :: forall s. (Enum s, Show s) => s -> EnumBox,
      in an equation for `succ'
      at typeclass.hs:11:9
In the first argument of `succ', namely `s'
In the expression: succ s
In an equation for `succ': succ (EB s) = succ s

为什么可以演绎第一场演出而不能演绎第二场成功?

丹尼尔·格拉泽(Daniel Gratzer)

您唯一的问题是succ类型

succ :: Enum a => a -> a

所以你需要

succ (EB s) = EB . succ $ s

只是再次装箱。

你可能还会想要

instance Enum EnumBox where
    toEnum = EB
    fromEnum (EB i) = fromEnum i

由于这是完整性的最低定义,因为

succ = toEnum . succ . fromEnum

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法在当前上下文中推断闭包类型

Alamofire无法在当前上下文中推断闭包类型

Swift 3.0:无法在当前上下文中推断闭包类型,PromiseKit

类成员上下文中的ʻauto`返回类型

Swift无法从上下文推断类型

联合类型无法在孤立的上下文中工作

将文件转换为Swift 3:无法在当前上下文中推断闭包类型

在上下文中解析类型

无法创建类型的常量值在此上下文中仅支持原始类型或枚举类型

无法创建类型“?”的常量值。在此上下文中仅支持原始类型或枚举类型

在类上下文中使用“ this”类型的Typescript条件类型的问题

在Spring上下文中没有找到类型类... myPackageHere ...的bean,也没有多个

具有上下文绑定的类型类隐式

带有期望的 Linq:此上下文中仅支持原始类型或枚举类型

使用自己的类名在推论上下文中解析类型

无法在当前上下文 RxSwift 中推断闭包参数的类型

LINQ查询错误:无法创建类型的常量值。在此上下文中仅支持原始类型或枚举类型

实体框架:无法创建**类型的常量。在此上下文中仅支持原始类型或枚举类型

Asp.Net-无法创建类型的常量值。在此上下文中仅支持原始类型或枚举类型

无法创建类型为“ xxx”的常量。在此上下文中仅支持原始类型或枚举类型

无法创建类型'x'的常量值。在此上下文中仅支持原始类型或枚举类型

原始类型上下文中的钻石算子

布尔上下文中的bool和int类型

在静态上下文中使用泛型类型

在类型依赖的上下文中使用constexpr

在呼叫类型推导的上下文中,“推导的A”的含义

“参数”是“类型”,在给定的上下文中无效

在静态上下文中使用实例方法时,为什么javac会发出“错误:类中的方法无法应用于给定类型”?

关联类型有界,但是“错误:必须在此上下文中知道此值的类型”