Copyright | © Frank Jung 2020 |
---|---|
License | GPL-3 |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
Cps
Description
Example from Wikibooks Continuation Passing Style
This uses the Cont
Monad from the
transformers package.
Using callCC
is better than using return as argument it returns a suspended
computation.
A function of type a -> b
would become a -> (b -> r) -> r
in CPS, where
b -> r
is the continuation.
Synopsis
- newtype CPS a = CPS {
- runCPS :: forall r. (a -> r) -> r
- toCPS :: a -> forall r. (a -> r) -> r
- fromCPS :: (forall r. (a -> r) -> r) -> a
- addCont :: Int -> Int -> Cont r Int
- addOne :: Int -> Int
- pythagorasCont :: Int -> Int -> Cont r Int
- releaseString :: String
- releaseStringCPS :: CPS String
- squareCont :: Int -> Cont r Int
- withOS :: (String -> r) -> r
- withTimestamp :: (Int -> r) -> r
- withVersionNumber :: (Double -> r) -> r
Types
Type CPS for Continuation-passing style.
See CpsSpec.hs for usage.
Custom continuation passing style (CPS)
toCPS :: a -> forall r. (a -> r) -> r Source #
Custom continuation passing style.
The following example uses a custom continuation-passing style (CPS) to demonstrate the general concept.
This converts any value into a suspended computation.
flip ($) :: b -> (b -> c) -> c
Longhand this is:
toCPS a = k -> k a
Or more simply as:
toCPS a k = k a
CPS Function Examples
addCont :: Int -> Int -> Cont r Int Source #
Continuation passing style (CPS) examples.
The following uses the Cont
Monad from the transformers
package.
Continuation for add function.
releaseString :: String Source #
Return version, timestamp and OS as pyramid of doom.
withTimestamp :: (Int -> r) -> r Source #
Returns (fixed) timestamp.
withVersionNumber :: (Double -> r) -> r Source #
Returns (fixed) version.