Re:Why Haskell is suited to IO
by yummyfajitas on Monday July 04, 2011 @11:41AM
(#2726804)
Pure means that functions have no side effects - a function is purely a mathematical object defined by it's return values.
For example, f(x) = 2*(x+1) is pure. f(3) == 8, and it is completely safe (in terms of yielding the proper return value) to replace f(3) with 8 anywhere in your program.
Similarly, print world "foo" is referentially transparent - print world_in_which_foo_wasnt_printed "foo" == world_in_which_foo_was_printed. It's completely safe to make the substitution (restOfProgram (print world_in_which_foo_wasnt_printed "foo")) -> (restOfProgram world_in_which_foo_was_printed).
Referential transparency makes it easy to reason about programs. A function of type Int -> Int will never touch a variable of type OutsideWorld, and can always be replaced by it's output.
For those wondering what this has to do with monads, it's fairly simple. A pure program without monads requires a lot of plumbing concerning the world variable:
f: World, Int
world, result <- action1 world 3
world2, result <- action2 world
...
return (worldN, result)
Monads are merely syntactic sugar to suppress the world variable.
For example, f(x) = 2*(x+1) is pure. f(3) == 8, and it is completely safe (in terms of yielding the proper return value) to replace f(3) with 8 anywhere in your program.
Similarly, print world "foo" is referentially transparent - print world_in_which_foo_wasnt_printed "foo" == world_in_which_foo_was_printed. It's completely safe to make the substitution (restOfProgram (print world_in_which_foo_wasnt_printed "foo")) -> (restOfProgram world_in_which_foo_was_printed).
Referential transparency makes it easy to reason about programs. A function of type Int -> Int will never touch a variable of type OutsideWorld, and can always be replaced by it's output.
For those wondering what this has to do with monads, it's fairly simple. A pure program without monads requires a lot of plumbing concerning the world variable:
Monads are merely syntactic sugar to suppress the world variable.[edit: fixed formatting.]