Sat Feb 18 10:07:32 EST 2012

Reader monad and order

I'm trying to capture the idea of "context dependent state
transformation".  Something doesn't quite add up here...

There seem to be a couple of ways to formulate this.  Let c be
context, s be state and R be a Reader monad, which is a
context-dependent computation:  R a b  ==  a -> b

( Concretely: c is "world state" of an animation, i.e. read-only or
"stiff" background info like current time, and s is "object state" of
an animation, meaning the animation's dynamics = state of its
equations of motion. )

   A)  c -> s -> s    or    R c (s -> s)

   B)  s -> c -> s    or    s -> R c s

   C)  (s,c) -> s

Where R is a reader monad.

The computations I want to fit in a framework have initial s and c
available at the same time, so C) is the type that corresponds best to
reality.  Why is there an ambiguity when trying to write this as a
Reader monad?  Which of A) or B) is the correct/appropirate one?  Is
the Reader monad the appropriate model?  Is this a Co-Monad?  (EDIT:
The answer seems to be Yes[1]).

Something I ran into before while trying to capture state machines /
state space models is that the following correspondences are not
really bijective.  How to make that "really" precise?

    (a,b) -> c   <=>    a -> b -> c
    (a,b) -> c   <=>    b -> a -> c

EDIT: Above isn't expressed well.  According to [1] these really are
the same.  I just changed the animation types from

    m (s -> s)

    s -> m s

It seems that while this doesn't make a difference for the Reader
monad, for other monads it does.  I.e. I could use a state monad to
thread the RNG state without trouble.

[1] http://comonad.com/reader/2008/kan-extensions-ii/