在Haskell中,我们有IOmonad来处理副作用,但是,它无法表达跟踪副作用,你真的不知道实际发生了什么类型的副作用:
main :: IO ()
Run Code Online (Sandbox Code Playgroud)
在PureScript中,我们有Effmonad,你可以根据类型签名知道发生了什么类型的副作用:
main :: forall e. Eff (fs :: FS, trace :: Trace, process :: Process | e) Unit
Run Code Online (Sandbox Code Playgroud)
这里显然该main函数使用了文件系统,跟踪消息到控制台,并能够处理当前进程,我们有一个特定的模块Control.Monad.Eff来处理副作用,以及子模块,如Control.Monad.Eff.Random和Control.Monad.Eff.Console.
以下为例:
module RandomExample where
import Prelude
import Control.Monad.Eff
import Control.Monad.Eff.Random (random)
import Control.Monad.Eff.Console (print)
printRandom :: forall e. Eff (console :: CONSOLE, random :: RANDOM | e) Unit
printRandom = do
n <- random
print n
Run Code Online (Sandbox Code Playgroud)
这比仅仅使用"嘿,这里发生了副作用,就是这样,你不需要知道了!" .我正在浏览网页,我没有看到一个完整的monad跟踪副作用.
在Haskell中是否有一个特定的monad Eff,用于跟踪副作用?
提前致谢.
有几个库为 Haskell 定义了类似的效果系统。
我已经使用过一些extensible-effects,并且发现添加受限效果IO(例如STDIO,FileIO效果)非常容易。缺乏编译器支持使得使用起来稍微不太好。
如果您想尝试一下,您可以在框架的现有效果中找到灵感extensible-effects:http://hackage.haskell.org/packages/#cat:Effect
似乎有一个版本extensible-effects不用于Typeable跟踪效果:http ://hackage.haskell.org/package/effin 。这应该会让编写新效果变得更好。
| 归档时间: |
|
| 查看次数: |
282 次 |
| 最近记录: |