Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the alternative of monads to use IO in pure functional programming?

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.

like image 886
Cheick Avatar asked Jan 28 '10 21:01

Cheick


People also ask

Why do we need IO monad?

IO Monad is simply a Monad which: Allows you to safely manipulate effects. Transform the effects into data and further manipulate it before it actually gets evaluated.

Are monads pure?

Monads are not considered pure or impure. They're totally unrelated concepts. Your title is kind of like asking how verbs are considered delicious. "Monad" refers to a particular pattern of composition that can be implemented on types with certain higher-kinded type constructors.

What are the different classes of monads?

Common monadsNondeterminism using List monad to represent carrying multiple values. State using State monad. Read-only environment using Reader monad. I/O using IO monad.

What are monads used for?

A monad is an algebraic structure in category theory, and in Haskell it is used to describe computations as sequences of steps, and to handle side effects such as state and IO. Monads are abstract, and they have many useful concrete instances. Monads provide a way to structure a program.


2 Answers

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.

like image 200
Norman Ramsey Avatar answered Sep 23 '22 06:09

Norman Ramsey


Besides linear types, there's also effect system.

like image 26
Wei Hu Avatar answered Sep 19 '22 06:09

Wei Hu