Kriens-0.1.0.1: Category for Continuation Passing Style

Copyright(c) Matteo Provenzano 2015
LicenseBSD-style (see the LICENSE file in the distribution)
Maintainermatteo.provenzano@alephdue.com
Stabilityexperimental
Portabilityportable
Safe HaskellSafe
LanguageHaskell2010

Control.Category.Cont

Contents

Description

 

Synopsis

Documentation

data Cont f g Source

A type for the Continuation category. In the Continuation category:

  • object are functions f :: a -> b, g :: c -> d
  • arrows are functions t :: (a -> b) -> (c -> d)

Instances

Category * Cont Source 
Monoid a => Monoid (Cont t (f -> a)) Source 

forget :: Cont (a -> a) (b -> c) -> b -> c Source

Forgets the continuation.

withCont :: (b -> c) -> Cont (a -> b) (a -> c) Source

Apply a function to the continuation.

lift :: Monad m => Cont (a -> b) (a -> m b) Source

Lift the continuation into a Monad.

cont :: (f -> g) -> Cont f g Source

Creates a continuation

Example: Using Cont category

ContT can be used to add continuation handling to other monads. Here is an example how to combine it with IO monad:

import Prelude hiding (id, (.))
import Control.Category
import Control.Category.Cont
withPassword pwd = cont $ \f x -> do
    putStrLn "Enter the secret password:"
    pass <- getLine
    if pass == pwd then
        f x
    else
        return "you are not authorized to execute this action."
greet = cont $ \f x -> f $ "hello to " ++ x
secureGreet = forget $ (withPassword "secret") . lift . greet
verySecureGreet = forget $ (withPassword "secret") . (withPassword "verySecret") . lift . greet

Action withPassword requests user to enter a string. If the string matches the password the input is handed to the continuation. lift is used to inject the pure code into the IO monad.