是否可以在Clojure中做Haskell的IO monad? [英] Is it possible to do the IO monad from Haskell in Clojure?

查看:132
本文介绍了是否可以在Clojure中做Haskell的IO monad?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过 algo.monads floluitten 文档。我还通过 Jim Duey Konrad Hinsen Leonardo Borges

I've had a look at the algo.monads and fluokitten documentation. I've also read through monad blog entries by Jim Duey, Konrad Hinsen and Leonardo Borges.

我能找到的最近的是 Konrad Hinsen的库Monadic IO流 - 但这似乎没有'实现monad接口'(为了更好的措辞)

The closest I can find is Konrad Hinsen's library Monadic IO streams - but this doesn't appear to 'implement the monad interface' (for want of a better phrasing)

这是使用ST在Haskell中的示例

This is example using ST in Haskell

oneST :: ST s Int -- note that this works correctly for any s
oneST = do var <- newSTRef 0
           modifySTRef var (+1)
           readSTRef var

one :: Int
one = runST oneST

我的问题是:可以做来自Haskell的IO Monad 在Clojure?您能提供一个示例吗?

My question is: Is it possible to do the IO Monad from Haskell in Clojure? Could you provide an example?

推荐答案

简单:
将IO视为monad变压器,授予特殊权限以处理副作用。

  • Trivially: Think of IO as a monad transformer that grants the special permission of working with side effects. Then any monad in Clojure is an IO monad, as performing side effects is not a privileged operation in Clojure.

难以置信的:Clojure是一个非常有趣的操作是图灵完成,所以你可以实现所有的Haskell,包括在Clojure的IO monad。

Fatuously: Clojure is Turing-complete, so you could implement all of Haskell, including the IO monad in Clojure. Haskell is Turing-complete, so you could implement all of Clojure in Haskell and expose the IO monad.


  • 哲学上: IO monad的本质是在与类型系统集成时保持纯度,与Clojure杂质和动态分型。任何尝试在IO monad鞋拔都不符合Clojure的哲学或未能捕获IO monad的基本点。

  • Philosophically: The essence of the IO monad, preserving purity while integrating with the type system, is incompatible with Clojure's impurity and dynamic typing. Any attempt to shoehorn in the IO monad would either be at odds with Clojure's philosophy or fail to capture the essential point of having an IO monad.

  • 部分:问题中链接到的monadic-意图用于algo.monads库或其前身。 monadic接口是状态monad。 Monadic-stream提供了一些监控的IO monadic函数来处理它。这不会阻止您使用任何其他具有副作用的功能,并且没有类型系统集成IO,没有系统的方式来说哪个是哪个。这不是IO monad;它只是做了一些类似于IO monad的事情。

  • Partially: The monadic-io-streams library linked to in the question is intended for use with the algo.monads library, or its predecessor. The monadic interface is the state monad. Monadic-io-streams provides some jailed IO monadic functions to work with it. This does not prevent you from using any other functions with side effects, and without a type system integrating IO there is no systematic way to say which is which. This is not the IO monad; it just does a few things similar to the IO monad. This is interesting but of dubious utility.

有一天有兴趣在 Typed Clojure 。如果副作用被添加到类型系统,那么可能需要以结构化的方式将它们隔离以用于某些目的,并给出在类型化Clojure中存在类似IO monad的东西的原因。

Someday: There is interest in Typed Clojure. If side effects are added to the type system, then it may become desirable to isolate them in a structured manner for some purposes and give reason for the existence of something like an IO monad in Typed Clojure.

这篇关于是否可以在Clojure中做Haskell的IO monad?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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