monad在单纯的函数式编程中使用IO有哪些选择? [英] What are the alternative of monads to use IO in pure functional programming?

查看:116
本文介绍了monad在单纯的函数式编程中使用IO有哪些选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单子被描述为处理IO的haskell解决方案。我想知道是否还有其他方法可以处理纯函数式语言中的IO。

解决方案


在纯功能语言中,I / O是否有替代品?

我知道文献中有两种选择: / p>


  • 一个是所谓的线性类型系统。我们的想法是线性类型的值必须正确使用:您不能忽略它,并且您不能使用它两次。有了这个想法,你可以给世界的状态一个抽象类型(例如, World ),并且使它成为线性的。如果我用星号标记线性类型,那么下面是一些I / O操作的类型:

      getChar :: World * - > (Char,World *)
    putChar :: Char - >世界* - >世界*

    等等。编译器会确保你永远不会复制世界,然后它可以安排编译更新世界的代码,这是安全的,因为只有一个副本。



    Clean 语言中输入的唯一性基于线性。



    这个系统有几个优点;特别是,它没有强制monad做的事件的总次序。它也倾向于避免在Haskell中看到的 IO sin bin,其中 all 有效计算被放入 IO中

  • 和Clean,并基于这样一个想法,即交互式程序是从(可能是无限的)请求序列到(可能是无限的)响应序列序列的函数。这个被称为对话的系统纯粹是编程。没有人会错过它,它没有什么特别推荐它。它的缺点在中引入了monadic I / O( Wadler和Peyton Jones撰写的命令式函数式编程。本文还提到了一个基于延续的I / O系统,该系统由耶鲁哈斯克尔小组引入,但是很短暂。

monads are described as the haskell solution to deal with IO. I was wondering if there were other ways to deal with IO in pure functional language.

解决方案

What alternatives are there to monads for I/O in a pure functional language?

I'm aware of two alternatives in the literature:

  • One is a so-called linear type system. The idea is that a value of linear type must be used exactly one time: you can't ignore it, and you can't use it twice. With this idea in mind, you give the state of the world an abstract type (e.g., World), and you make it linear. If I mark linear types with a star, then here are the types of some I/O operations:

    getChar :: World* -> (Char, World*)
    putChar :: Char -> World* -> World*
    

    and so on. The compiler arranges to make sure you never copy the world, and then it can arrange to compile code that updates the world in place, which is safe because there is only one copy.

    The uniqueness typing in the language Clean is based on linearity.

    This system has a couple of advantages; in particular, it doesn't enforce the total ordering on events that monads do. It also tends to avoid the "IO sin bin" you see in Haskell where all effectful computations are tossed into the IO monad and they all get totally ordered whether you want total order or not.

  • The other system I'm aware of predates monads and Clean and is based on the idea that an interactive program is a function from a (possibly infinite) sequence of requests to a (possibly infinite) sequence of responses. This system, which was called "dialogs", was pure hell to program. Nobody misses it, and it had nothing in particular to recommend it. Its faults are enumerated nicely in the paper that introduced monadic I/O (Imperative Functional Programming) by Wadler and Peyton Jones. This paper also mentions an I/O system based on continuations which was introduced by the Yale Haskell group but which was short-lived.

这篇关于monad在单纯的函数式编程中使用IO有哪些选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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