Created
October 5, 2011 12:58
-
-
Save einblicker/1264362 to your computer and use it in GitHub Desktop.
Delimited Continuation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open FSharpx.Continuation | |
//from http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.11.3425 | |
type DelimitedCont<'A,'B>() = | |
let metaCont = ref Unchecked.defaultof<'A -> Cont<'A, 'B>> | |
member private this.Abort (thunk:Cont<'A,'B>):Cont<'A,'B> = | |
cont { | |
let! v = thunk | |
return! !metaCont v | |
} | |
member this.Reset (thunk:Cont<'A,'B>):Cont<'A,'B> = | |
cont { | |
let mc = !metaCont | |
return! callCC <| fun k -> cont { | |
metaCont := (fun v -> cont { | |
metaCont := mc | |
return! k v | |
}) | |
return! this.Abort thunk | |
} | |
} | |
member this.Shift (f:('A -> Cont<'A,'B>) -> Cont<'A,'B>):Cont<'A,'B> = | |
callCC <| fun k -> cont { | |
return! this.Abort <| cont { | |
return! f <| fun v -> this.Reset <| cont { return! k v } | |
} | |
} | |
let d = DelimitedCont() | |
let c = d.Reset <| cont { | |
let! x = d.Shift <| fun k -> cont { | |
let! a = k 2 | |
let! b = k a | |
printfn "back from shift" | |
return b | |
} | |
printfn "back from reset" | |
return x + 10 | |
} | |
c id raise |> printfn "%A" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment