- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有人可以指出为什么 Haskell 中的不纯计算被建模为 monad 吗?
我的意思是 monad 只是一个包含 4 个操作的接口(interface),那么在其中建模副作用的原因是什么?
最佳答案
假设一个函数有副作用。如果我们将它产生的所有效果作为输入和输出参数,那么这个函数对于外界来说是纯粹的。
因此,对于不纯函数
f' :: Int -> Int
我们将现实世界添加到考虑因素中
f :: Int -> RealWorld -> (Int, RealWorld)
-- input some states of the whole world,
-- modify the whole world because of the side effects,
-- then return the new world.
那么f
又是纯的了。我们定义了一个参数化数据类型type IO a = RealWorld -> (a, RealWorld)
,所以我们不需要多次输入RealWorld,直接写
f :: Int -> IO Int
对于程序员来说,直接处理 RealWorld 太危险了——特别是,如果程序员得到了 RealWorld 类型的值,他们可能会尝试复制它,这基本上是不可能的。 (例如,考虑尝试复制整个文件系统。您会将其放在哪里?)因此,我们对 IO 的定义也封装了整个世界的状态。
如果我们不能将这些不纯的函数链接在一起,它们就毫无用处。考虑一下
getLine :: IO String ~ RealWorld -> (String, RealWorld)
getContents :: String -> IO String ~ String -> RealWorld -> (String, RealWorld)
putStrLn :: String -> IO () ~ String -> RealWorld -> ((), RealWorld)
我们想要
如果我们能够访问现实世界的状态,我们会怎么做?
printFile :: RealWorld -> ((), RealWorld)
printFile world0 = let (filename, world1) = getLine world0
(contents, world2) = (getContents filename) world1
in (putStrLn contents) world2 -- results in ((), world3)
我们在这里看到了一种模式。函数的调用方式如下:
...
(<result-of-f>, worldY) = f worldX
(<result-of-g>, worldZ) = g <result-of-f> worldY
...
所以我们可以定义一个运算符~~~
来绑定(bind)它们:
(~~~) :: (IO b) -> (b -> IO c) -> IO c
(~~~) :: (RealWorld -> (b, RealWorld))
-> (b -> RealWorld -> (c, RealWorld))
-> (RealWorld -> (c, RealWorld))
(f ~~~ g) worldX = let (resF, worldY) = f worldX
in g resF worldY
然后我们可以简单地写
printFile = getLine ~~~ getContents ~~~ putStrLn
不接触现实世界。
现在假设我们也想让文件内容大写。大写是一个纯函数
upperCase :: String -> String
但要使其进入现实世界,它必须返回一个IO String
。解除这样的功能很容易:
impureUpperCase :: String -> RealWorld -> (String, RealWorld)
impureUpperCase str world = (upperCase str, world)
这可以概括为:
impurify :: a -> IO a
impurify :: a -> RealWorld -> (a, RealWorld)
impurify a world = (a, world)
这样 impureUpperCase = impurify 。大写
,我们可以写
printUpperCaseFile =
getLine ~~~ getContents ~~~ (impurify . upperCase) ~~~ putStrLn
(注意:通常我们写 getLine ~~~ getContents ~~~ (putStrLn . upperCase)
)
现在让我们看看我们做了什么:
(~~~)::IO b -> (b -> IO c) -> IO c
,它将两个不纯函数链接在一起impurify::a -> IO a
,它将纯值转换为不纯值。现在我们进行识别(>>=) = (~~~)
和return = impurify
,看到了吗?我们有一个单子(monad)。
为了确保它确实是一个 monad,还需要检查一些公理:
返回 a >>= f = f a
impurify a = (\world -> (a, world))
(impurify a ~~~ f) worldX = let (resF, worldY) = (\world -> (a, world )) worldX
in f resF worldY
= let (resF, worldY) = (a, worldX)
in f resF worldY
= f a worldX
f >>= return = f
(f ~~~ impurify) worldX = let (resF, worldY) = f worldX
in impurify resF worldY
= let (resF, worldY) = f worldX
in (resF, worldY)
= f worldX
f >>= (\x -> g x >>= h) = (f >>= g) >>= h
留作练习。
关于haskell - 为什么在 Haskell 中将副作用建模为 monad?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2488646/
我正在寻找可以从 Eclipse 的 UML 图生成代码的工具。该工具应该从 UML 图生成代码,进行逆向工程以查看代码更改,并提供合并选项以防止因任何更改而删除文件。 是否有工具可以做到这一点,或者
我正在寻找有关建模的见解。我有一个关于设计模式和基本类图、序列图和用例的介绍类(class)。 我发现类图作为我编程中的组织工具非常宝贵。到目前为止,用例还算有用。 本学期我正在上课,更深入地学习 U
如何使用 flutter 数据建模技术访问“HH”和“mm”。我一直在尝试按以下方式使用它,但是下面给出了错误。 我的数据模型当前为它的简化版本。 class Week { final Strin
我正在使用 Neo4j 和 PHP。在我的项目中,我有餐厅节点。每个节点都有纬度、经度和分类属性。 我需要返回与用户给定分类相匹配的餐厅节点,其结果按距用户位置的距离排序(即第一个最近的餐厅)。 最简
我希望 POJO 包含一个 Status 字段(只有两个可能的值 SUCCESS、FAILED),并且根据此状态,第二个字段需要为 POJO 对象(如果 Status 为 SUCCESS) 或只是字符
我正在尝试将我的办公室迁移到数据库应用程序中。这是我得到的: (来源:phunkei.de) 公司和个人从客户继承(1:1关系)。这是因为他们都可以是“客户”并收到账单,我想引用订单中的单个表。公司和
我目前有这个数据库结构: 一个条目可以有多个"file"、“文本”和“url”类型的项目。 这些项目中的每一项在文本、网址或文件表(存储数据的位置)中都有一个对应的项目。 我需要一个查询来有效地选择一
下面的代码不应该像“if(condition)”语句一样工作吗,当它在另一个循环中并且“body”有自己的 break 或 continue 语句时,会出现明显的错误行为: for( ; condit
我一直在像这样存储和递增下载计数器: INCRBY downloads: 1 但现在我想要下载:* 按值排序,以便显示热门下载列表。 我觉得我可以更好地存储它。我愚弄了: ZINCRBY downlo
我开始使用图形数据库,在我的团队中,我们已经开始为我们的软件建模图形。当我们尝试“记录”模型以查看数据库的结构时,问题就出现了。对于 SQL 数据库,您只需查看 SQL 模式。 我们花了一些时间阅读
我正在做一个固定效应回归并且遇到了自相关问题,为了解决这个问题,我正在使用预测、lmtest 和 plm 包进行 ARIMA 建模。我的数据是一般面板数据,looks like this ,我正在尝试
我想知道是否有任何工具可以帮助我对 C 应用程序(即函数式编程)进行建模。例如。我目前正在构建一个共享库。但是为了直观地传达我的设计,我需要类似 UML 的东西。我想这样做,以便审查我的设计的人不需要
我正在尝试将 JSON 转换为 GSON ,但我无法建模。谁能给我举个例子。 [ { "id": "1", "name": "lalala",
正如标题所说,在 中建模 optional 参数的最佳方法是什么?斯卡拉 ? 对于 optional 参数,我的意思是执行函数体不需要的值。 要么因为该参数存在默认值,要么根本不需要该参数本身(例如配
我发现 UML 可用于记录 OO 系统的各个方面,尤其是用于整体架构的类图和用于说明特定例程的序列图。我想为我的 clojure 应用程序做同样的事情。我目前对模型驱动开发不感兴趣,只是在交流应用程序
我想知道是否有人知道如何使用 UML 在属性中建模 DateTime 基元类型? 最佳答案 我想这取决于日期的格式......请参阅下面的示例,其中我使用两个整数(分别为日和年)和一个名为“Month
我有一些与我的问题建模相关的问题。我正在研究基于模型的测试的论文项目。还想从专家的角度了解我是否采用正确的方法来建模我的场景。我正在对 Android 应用程序的 UI 进行建模,遍历它们,生成测试用
我正在尝试建立对 CouchDB 以及如何为某些现实世界场景建模数据的理解。我现在已经尽可能多地“按日期获取我的博客文章”;) 给定这样的文件: { "_id": "couch1",
一个模型资源是如何分层的?例如假设一个人有一个“留言板”并且“消息”是一种资源。假设“消息”可以有回复,从而形成一个讨论线程。如何模拟线程的概念? “消息”是否包含它的子项? “线程”是它自己的资源吗
我正在尝试创建一个像通用 POJO 一样工作的对象,因为我必须通过不同的对象传递它,并且我需要一个接口(interface)来访问其属性。 目前,我使用具有 getField 方法的基础对象来完成此操
我是一名优秀的程序员,十分优秀!