2008/09/20

Definition of bind operator for State monad?

instance Monad (State s) where 
return a = State $ \s -> (a,s)
(State x) >>= f = State $ \s -> let (v,s') = x s in runState (f v) s'


instance
Monad (State s) where
return a = State $ \s -> (a, s)
m >>= k = State $ \s -> let
(a, s') = runState m s
in runState (k a) s

Hmm, how and why are they different? The latter one makes sense to me, but I cannot get the part to apply x to s in the former one.

Update:

I got it. I just did not notice that the former used pattern matching against to deconstruct the first argument (State x), where as the latter used field selector (runState). Note that the State monad is declared as follows:
newtype State s a = State { runState :: s -> (a, s) }

Labels:

2007/12/08

Installing GHC 6.8.1 on Mac OS X Leopard (x86)

As reported in glasgow-haskell-users, you need to be a bit careful to use this configuration.

Although I took care of those libgmp and readline dependency thingies, following error was persistent when I tried to build it:

... snipped
installPackage: Multiple files with extension buildinfo
make[1]: *** [install.library.Cabal] Error 1
make: *** [install] Error 2

Under libraries/Cabal directory there was a file named buildinfo2.buildinfo, but no other file with .buildinfo extension... yes, actually there was, it was ._buildinfo2.buildinfo. After removing this file the build was successful.

It was likely that this offensive file snuck into there during preparing the archive. I expanded the archive using tar command on the command line, but I should have used the Archive Utility, which handles those dot-files correctly.

Anyhow, that's how I wasted my precious four nights :-(

Labels: , ,

2007/12/03

The Mysterious ⊥

Searching the symbol ⊥, which is used to represent 'bottom' in Haskell, with Google gave me no answer. I mean, no alternative suggestions, nothing.

Weird.

Labels: ,

2007/11/23

Parser in Haskell

The first chapter of the book is mostly about writing a parser in Miranda (the code is in Haskell, though).

It does not use monads. You can see how the implementation can be refined with monads if you compare it with a monadic parser of today.

Labels: ,

2007/11/20

A little Haskeller

Started reading Implementing functional languages: a tutorial.

I'm not much interested in writing a compiler (I wish I could, though), but this tutorial is useful for me. After all, a compiler implementation needs almost every kind of topics. And Simon Peyton Jones is a great author - you can see that from his other papers.

This book was written for Miranda, an ancestor of Haskell. That's okay, but the code in text is in Miranda while sample code seems to be in Haskell. They have many in common and I can easily guess how the description in text part should be fixed, still it's annoying.

I read this book (the dead tree version available from CafePress) while commuting on the train and unable to do exercises. Good excuse to get Nokia N810 or iRex iLiad?

Labels: ,