Search code examples
haskellcontinuation-passinghigher-rank-typesscott-encoding

How to use a Reader type encoded with continuation passing style


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?


Solution

  • reader f = Reader (\k -> k f) -- where is f's argument?
    

    We don't have to pass a second argument now. This is very similar to the regular data Reader e a = Reader (e->a) and reader f = Reader f which do not involve a second argument.

    runReader (reader id) -- what is the 2nd argument?
    

    The general equation is

    runReader (reader f) k = k f
    

    so,

    runReader (reader f) id x = id f x = f x
    

    You can use f = id (which acts like ask in the real reader monad), but don't confuse it with the other id which is provided as a continuation k, and which recovers the original f.