通过IO了解Haskell中的纯函数 [英] Understanding pure functions in Haskell with IO

查看:79
本文介绍了通过IO了解Haskell中的纯函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出Haskell (根据 Rein Heinrich 评论)f:

Given a Haskell value (edit per Rein Heinrich's comment) f:

f :: IO Int
f = ... -- ignoring its implementation

引用使用Idris进行类型驱动的开发",

Quoting "Type-Driven Development with Idris,"

纯函数的关键属性是相同的输入始终会产生相同的结果.此属性称为参照透明性

The key property of a pure function is that the same inputs always produce the same result. This property is known as referential transparency

f以及Haskell中的所有IO ...函数是否是纯函数?在我看来,它们不是因为lookInDatabase :: IO DBThing不会总是返回相同的值,因为:

Is f, and, namely all IO ... functions in Haskell, pure? It seems to me that they are not since, lookInDatabase :: IO DBThing, won't always return the same value since:

  • 在t = 0时,数据库可能已关闭
  • 在t = 1时,数据库可能已启动,并且会出现return MyDbThing

简而言之,f(通常是IO ...函数)是否纯净?如果是,那么鉴于我试图用我的t=...示例来反驳f的功能纯度,请更正我的错误理解.

In short, is f (and IO ... functions in general) pure? If yes, then please correct my incorrect understanding given my attempt to disprove the functional purity of f with my t=... examples.

推荐答案

IO从概念上讲实际上是一种独立的语言.这是Haskell RTS(运行时系统)的语言.它在Haskell中作为(相对简单的)嵌入式DSL实现,其脚本"的类型为IO a.

IO is really a separate language, conceptually. It's the language of the Haskell RTS (runtime system). It's implemented in Haskell as a (relatively simple) embedded DSL whose "scripts" have the type IO a.

因此,返回类型为IO a的值的Haskell函数实际上不是在运行时执行的函数-执行的是IO a值本身.因此,这些函数实际上 是纯函数,但是它们的返回值表示是非纯计算.

So Haskell functions that return values of type IO a, are actually not the functions that are being executed at runtime — what gets executed is the IO a value itself. So these functions actually are pure but their return values represent non-pure computations.

从语言设计的角度来看,IO是一种非常优雅的技巧,可以将非纯丑陋完全隔离开,同时又可以将其紧密地集成到其纯净的环境中,而无需使用特殊的外壳.换句话说,设计不能解决由不纯的IO引起的问题,但它至少在不影响代码纯净部分的情况下做得很好.

From a language design point of view, IO is a really elegant hack to keep the non-pure ugliness completely isolated away while at the same integrating it tightly into its pure surroundings, without resorting to special casing. In other words, the design does not solve the problems caused by impure IO but it does a great job of at least not affecting the pure parts of your code.

下一步是研究FRP-使用FRP,您可以使包含IO的层变得更薄,并将更多的非纯逻辑转移到纯逻辑中.

The next step would be to look into FRP — with FRP you can make the layer that contains IO even thinner and move even more of non-pure logic into pure logic.

您可能还想阅读John Backus关于函数式编程,冯·诺依曼体系结构的局限性等主题的著作.如果您对纯净度与IO之间的关系感兴趣,那么Conal Elliott也是google的名字.

You might also want to read John Backus' writings on the topic of Function Programming, the limitations of the Von Neumann architecture etc. Conal Elliott is also a name to google if you're interested in the relationship between purity and IO.

P.S.还值得注意的是,尽管IO严重依赖monad来解决延迟评估的一个方面,并且由于monad是构建嵌入式DSL的一种非常好的方法(其中IO只是一个示例),所以monad的通用性要比IO,因此请不要在同一个上下文中过多考虑IO和monad-它们是两个独立的事物,两者可能存在而又彼此不存在.

P.S. also worth noting is that while IO is heavily reliant on monads to work around an aspect of lazy evaluation, and because monads are a very nice way of structuring embedded DSLs (of which IO is just a single example), monads are much more general than IO, so try not to think about IO and monads in the same context too much — they are two separate things and both could exist without the other.

这篇关于通过IO了解Haskell中的纯函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆