by tome · 13y ago · 0 comments
> > Haskell makes your functions pure by default. You have to change the type signature if you want to do IO
> The author seems to be conflating IO and side effects. IO is just one of the many ways that side effects can occur.
Indeed, but that does not refute the OP's statement.
> > Functions that do IO are difficult or impossible to test.
> Is your function creating a file? Start your test, assert the file doesn't exist, call your function, assert your file exists. Done.
Right, and (in my opinion) that is more difficult than testing something that doesn't create a file.
> > boo :: Map Integer String -> String -> Integer
> This syntax is clean but it doesn't tell me anything that
> def boo(map: Map[Integer, String], key: String) : Integer
> doesn't tell me.
Indeed, but again that does not refute the OP's statement which was simply that the syntax is clean.
> > The point is, you can't confidently reason about a function from its signature if IO is involved.
> Sure, you can: the mention of IO tells me this function operates within the IO monad, so I know a lot of operations and laws that apply to it.
You do? Please tell me a law that applies to IO.
> This syntax is clean but it doesn't tell me anything that
> def boo(map: Map[Integer, String], key: String) : Integer
> doesn't tell me.
OP's point was not so much that the Haskell syntax is clean, but more that there is a huge difference between
Map Integer String -> String -> Integer
and
Map Integer String -> String -> IO Integer
The latter is allowed to do IO, while the former is not. There are very few languages that allow you to express this difference and have the compiler enforce it (Haskell being one of them). So "Map Integer String -> String -> Integer" tells you a lot more than "def boo(map: Map[Integer, String], key: String) : Integer" does.
For more information about what exactly types tell you, look up parametricity or read Philip Wadler's "Theorems for free".
Also Fortran being one of them (you can mark functions and subroutines 'pure').
How about three? Right identity, left identity and associativity.
That's for the laws. For the properties, IO is a monad so I can compose any value in the IO monad with other monads (Maybe, ST, etc...).
Actually, I kind of wish IO was split up a bit... I have been meaning to play with rolling some typeclasses (with an IO instance and a testing instance) that wrap up IO of various types, so I define my functions in terms of these and spot what kind of IO various functions might do - I'm not yet sure how much of a win it will be, but it seems like something to explore.
http://www.haskellforall.com/2012/07/purify-code-using-free-...
http://www.haskellforall.com/2012/06/you-could-have-invented...
There's a section on this practice in Real World Haskell: http://book.realworldhaskell.org/read/programming-with-monad...
I would point out that in addition to the mentioned testing use (itself not to be underestimated - QuickCheck is amazing and making more things tractable to QuickCheck is great), there's also some other big potential wins. If I have a function that deals with local files in a monad that's an instance of MonadHandle, I can write an instance for a RemoteIO type that performs file operations on a remote system through a network connection, and suddenly the function that was local also works remotely.