How is IO monad actually implemented?in sense of, what would be the actual implementation of the main
function?
How would I call haskell function (IO) f
Here is an example of how one could implement the IO monad in Java:
package so.io;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import static so.io.IOMonad.*;
import static so.io.ConsoleIO.*;
/**
* This is a type containing no data -- corresponds to () in Haskell.
*/
class Unit {
public final static Unit VALUE = new Unit();
}
/**
* This type represents a function from A to R
*/
interface Function {
public R apply(A argument);
}
/**
* This type represents an action, yielding type R
*/
interface IO {
/**
* Warning! May have arbitrary side-effects!
*/
R unsafePerformIO();
}
/**
* This class, internally impure, provides pure interface for action sequencing (aka Monad)
*/
class IOMonad {
static IO pure(final T value) {
return new IO() {
@Override
public T unsafePerformIO() {
return value;
}
};
}
static IO join(final IO> action) {
return new IO(){
@Override
public T unsafePerformIO() {
return action.unsafePerformIO().unsafePerformIO();
}
};
}
static IO fmap(final Function func, final IO action) {
return new IO(){
@Override
public B unsafePerformIO() {
return func.apply(action.unsafePerformIO());
}
};
}
static IO bind(IO action, Function> func) {
return join(fmap(func, action));
}
}
/**
* This class, internally impure, provides pure interface for interaction with stdin and stdout
*/
class ConsoleIO {
static IO putStrLn(final String line) {
return new IO() {
@Override
public Unit unsafePerformIO() {
System.out.println(line);
return Unit.VALUE;
}
};
};
// Java does not have first-class functions, thus this:
final static Function> putStrLn = new Function>() {
@Override
public IO apply(String argument) {
return putStrLn(argument);
}
};
final static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static IO getLine = new IO() {
@Override
public String unsafePerformIO() {
try {
return in.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
};
}
/**
* The program composed out of IO actions in a purely functional manner.
*/
class Main {
/**
* A variant of bind, which discards the bound value.
*/
static IO bind_(final IO a, final IO b) {
return bind(a, new Function>(){
@Override
public IO apply(Unit argument) {
return b;
}
});
}
/**
* The greeting action -- asks the user for his name and then prints a greeting
*/
final static IO greet =
bind_(putStrLn("Enter your name:"),
bind(getLine, new Function>(){
@Override
public IO apply(String argument) {
return putStrLn("Hello, " + argument + "!");
}
}));
/**
* A simple echo action -- reads a line, prints it back
*/
final static IO echo = bind(getLine, putStrLn);
/**
* A function taking some action and producing the same action run repeatedly forever (modulo stack overflow :D)
*/
static IO loop(final IO action) {
return bind(action, new Function>(){
@Override
public IO apply(Unit argument) {
return loop(action);
}
});
}
/**
* The action corresponding to the whole program
*/
final static IO main = bind_(greet, bind_(putStrLn("Entering the echo loop."),loop(echo)));
}
/**
* The runtime system, doing impure stuff to actually run our program.
*/
public class RTS {
public static void main(String[] args) {
Main.main.unsafePerformIO();
}
}
This is a runtime system implementing interface to the console I/O together with a small purely functional program which greets the user and then runs an echo loop.
One can't implement the unsafe part in Haskell because Haskell is purely functional language. It is always implemented with lower-level facilities.