haskell - Haskell 为替代的 Either 数据类型定义 Functor 实例
问题描述
通过 Typeclassopedia 获得一些使用类型类的路由。想要替代Either
的实例Functor
,但即使检查Either
作为实例的定义也Functor
让我陷入困境。有这个,但不会编译。
data Alt a b = Success a | Failure b deriving (Show, Eq, Ord)
instance Functor (Alt a) where
fmap _ (Failure a) = Failure a
fmap f (Success x) = Success (f x)
• Couldn't match expected type ‘a1’ with actual type ‘a’
‘a1’ is a rigid type variable bound by
the type signature for:
fmap :: forall a1 b. (a1 -> b) -> Alt a a1 -> Alt a b
at Brenty_tcop.hs:25:3-6
‘a’ is a rigid type variable bound by
the instance declaration
at Brenty_tcop.hs:24:10-24
• In the first argument of ‘f’, namely ‘x’
In the first argument of ‘Success’, namely ‘(f x)’
In the expression: Success (f x)
• Relevant bindings include
x :: a (bound at Brenty_tcop.hs:26:19)
f :: a1 -> b (bound at Brenty_tcop.hs:26:8)
fmap :: (a1 -> b) -> Alt a a1 -> Alt a b
(bound at Brenty_tcop.hs:25:3)
|
26 | fmap f (Success x) = Success (f x)
解决方案
正如@chepner 在评论中所说,如果您切换类型参数的顺序,您的代码将编译,
data Alt b a = Success a | Failure b
或者切换实例的含义,Functor
以便它映射并单独Failure
离开。Success
instance Functor (Alt a) where
fmap f (Success x) = Success x
fmap f (Failure x) = Failure (f x)
基本上,Functor
类型类只知道如何映射类型的最后一个类型参数。所以我们必须重新调整,以便我们将函数f
应用于最后一个类型参数的出现。
为什么你只能映射最右边的参数是一个非常深刻和有趣的问题。要理解这一点,您必须了解kind ,这是 Haskell 类型系统的一个高级特性。
在某种意义上,您可以将种类视为类型的“下一级”。类型对值进行分类;种类对类型进行分类。"foo"
a也是如此String
,并且String
是一个类型。在 Haskell 中,“类型”发音为*
。
-- :t in ghci asks for the type of a value-level expression
ghci> :t "foo"
"foo" :: String
-- :k asks for the kind of a type-level expression
ghci> :k String
String :: *
所有普通类型——那些可以有值的类型——都有一种*
. 所以String :: *
, Int :: *
,Bool :: *
等
当您开始考虑参数化类型时,事情会变得有趣。Maybe
本身不是一个类型——你不能有 type 的值Maybe
,但你可以有Maybe Int
,Maybe String
等等。Maybe
一种函数也是如此——它将一个类型作为参数并产生一个类型。(Maybe
是一个类型构造函数,使用技术术语。)
-- Maybe is a function...
ghci> :k Maybe
Maybe :: * -> *
-- and you can apply it to an argument to get a type
ghci> :k Maybe Int
Maybe Int :: *
Alt
是一个双参数类型函数。类型函数在 Haskell 中是柯里化的,就像常规的值函数一样,所以Alt
有一个类型* -> * -> *
(实际上是* -> (* -> *)
)。
ghci> :k Alt
Alt :: * -> * -> *
现在,Functor
是一个高阶类型的函数。它接受一个参数f
,它本身就是一个类型函数。Functor
其本身不是有效的类型类约束,但Functor f
它是。
ghci> :k Functor
Functor :: (* -> *) -> Constraint
这意味着Maybe
它本身,带有一种* -> *
,是Functor
类型函数的有效参数。但Int :: *
不是,也不是Maybe Int :: *
,也不是Alt :: * -> * -> *
。错误消息告诉您类型不匹配:
ghci> :k Functor Int
<interactive>:1:9: error:
• Expected kind ‘* -> *’, but ‘Int’ has kind ‘*’
• In the first argument of ‘Functor’, namely ‘Int’
In the type ‘Functor Int’
ghci> :k Functor Alt
<interactive>:1:9: error:
• Expecting one more argument to ‘Alt’
Expected kind ‘* -> *’, but ‘Alt’ has kind ‘* -> * -> *’
• In the first argument of ‘Functor’, namely ‘Alt’
In the type ‘Functor Alt’
种类系统可以防止您形成无效类型,就像类型系统如何防止您写入无效值一样。如果没有 kind 系统,并且我们被允许编写instance Functor Alt
,它将为 生成以下(无意义的)类型fmap
:
-- `Alt a` is not a valid type, because its second argument is missing!
fmap :: (a -> b) -> Alt a -> Alt b
所以我们需要Alt :: * -> * -> *
变成某种东西* -> *
,才能有一个有效的论据Functor
。Alt
是一个柯里化类型函数,所以如果我们给它一个类型参数,我们将得到一个类型函数!
ghci> :k Functor (Alt Int)
Functor (Alt Int) :: Constraint
这就是为什么instance
声明说instance Functor (Alt x)
- 它需要给出Alt
一个参数(在这种情况下,参数可以是任何类型x
,只要它的类型是*
)。现在我们有了fmap :: (a -> b) -> Alt x a -> Alt x b
,这是一个有效的类型表达式。
所以一般来说,创建一个Functor
实例的方法是首先给你的类型提供参数,直到它只剩下一个参数。这就是为什么Functor
只知道如何映射最右边的类型参数。作为练习,您可以尝试定义一个Functor
映射倒数第二个类型参数的类。
这是一个很大的话题,所以希望我没有走得太快。不马上理解种类也没关系——我试了好几次!如果您希望我进一步解释任何内容,请在评论中告诉我。
推荐阅读
- python - Python遍历字典列表
- java - Spring Integration 如何从多个 ftp 服务器和目录中获取文件?
- exception - While 循环使用 ESPNOW 引发异常
- java - 基于 Gradle 的子项目在构建过程中抛出`Could not find or load main class worker.org.gradle.process.internal.worker.GradleWorkerMain`
- c# - 需要使用 C# 在现有 XML 文件下添加新项目
- powershell - Powershell 脚本分析 MFA
- excel - 使用 VBA 创建 Excel 元数据以在 sharepoint 中进行筛选
- java - JUNIT 在 quarkus 中运行测试用例时不使用 src/test/resources
- laravel - Laravel 迁移与人员/人员表名混合
- python - 如何搜索目录中的图像并将它们保存到另一个文件夹中?