I was under the impression that every value of type a
can be described with a rank-2 polymorphic type newtype Id a = Id {runId :: forall r. (a -> r) -> r }
in continuation passing style. So I derived the following type to define the Reader
accordingly:
newtype Reader e a = Reader {runReader :: forall r. ((e -> a) -> r) -> r}
Then I tried to construct a value of this type and run it:
reader f = Reader (\k -> k f) -- where is f's argument?
runReader (reader id) -- what is the 2nd argument?
Provided that Reader
's encoding in CPS and its type are valid, how would I use it?
We don't have to pass a second argument now. This is very similar to the regular
data Reader e a = Reader (e->a)
andreader f = Reader f
which do not involve a second argument.The general equation is
so,
You can use
f = id
(which acts likeask
in the real reader monad), but don't confuse it with the otherid
which is provided as a continuationk
, and which recovers the originalf
.