This file contains 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
type (_, _) cont = | |
Cont : (('a -> 'r) -> 'r) -> ('r, 'a) cont | |
let runCont : ('r, 'a) cont -> ('a -> 'r) -> 'r | |
= fun (Cont f) k -> f k | |
let runCont' k cf = runCont cf k | |
let return x = Cont((|>) x) |
This file contains 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
type (_, _) cont = | |
Cont : (('a -> 'r) -> 'r) -> ('r, 'a) cont | |
let runCont : ('r, 'a) cont -> ('a -> 'r) -> 'r | |
= fun (Cont f) k -> f k | |
effect ECont : (* arg *) ('r, 'a) cont -> (* ans *) 'a | |
(* mutlti-shot 💩 *) | |
let continue' k x = continue (Obj.clone_continuation k) x |
This file contains 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
interface Get { | |
readonly _tag: 'Get'; | |
readonly _ans: number; | |
} | |
interface Put { | |
readonly _tag: 'Put'; | |
readonly _ans: void; | |
value: number; | |
} |
This file contains 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
import { isNone, Option, none, some } from 'fp-ts/lib/Option'; | |
function runOption<T>(th: () => Iterator<Option<T>, Option<T>, unknown>): Option<T> { | |
const iter = th(); | |
const go = ( | |
{ done, value: v }: IteratorResult<Option<T>, Option<T>>, | |
): Option<T> => (done || isNone(v) ? v : go(iter.next(v.value))); | |
return go(iter.next()); | |
} |
This file contains 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
local eff = require('eff') | |
local inst, perform, handlers = eff.inst, eff.perform, eff.handlers | |
local Defer = inst() | |
local runDefer = function(th) | |
local tasks = {} | |
return handlers{ | |
function(x) | |
for i = #tasks, 1, -1 do | |
tasks[i]() |
This file contains 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
type (_, _) operation = .. | |
type 'a computation = | |
| Return : 'a -> 'a computation | |
| Call : ('arg, 'res) operation * 'arg * ('res -> 'a computation) -> 'a computation | |
type ('a, 'b) handler = { | |
return : 'a -> 'b computation; | |
operations : 'arg 'res. ('arg, 'res) operation -> | |
'arg -> ('res -> 'b computation) -> 'b computation |
This file contains 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
#require "higher";; | |
open Higher | |
type 'f functr = { | |
fmap : 'a 'b. ('a -> 'b) -> ('a, 'f) app -> ('b, 'f) app | |
} | |
type 'f applicative = { | |
app : 'a 'b. ('a -> 'b, 'f) app -> ('a, 'f) app -> ('b, 'f) app; |
This file contains 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
type id = int | |
type scene = { | |
id: id; | |
content: unit -> unit | |
} | |
effect Resume : scene -> unit | |
effect Switch : unit |
This file contains 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
effect Imperative : (('a -> 'b) -> 'b) -> 'a;; | |
let runImperative : (unit -> 'ans) -> 'ans = | |
fun f -> match f () with | |
| x -> x | |
| effect (Imperative fx) k -> | |
(* fx : ('a -> '_weak_b) -> '_weak_b *) | |
(* continue k : 'a -> 'ans *) | |
(* Mutlcire OCaml cannot unify '_weak_b and 'ans *) | |
(Obj.magic fx) @@ continue k |
This file contains 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
-- support functions {{{ | |
-- to find corresponding effect handler | |
local function lookup(h, eff, v) | |
local effh = h[eff] | |
if effh then | |
return function(k) | |
return effh(v, k) | |
end | |
else |