Skip to content

Instantly share code, notes, and snippets.

View Nymphium's full-sized avatar
⚜️
百合

Nymphium Nymphium

⚜️
百合
View GitHub Profile
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)
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
@Nymphium
Nymphium / main.ts
Created July 22, 2019 00:30
algebraic effects using stricter generator (is too hard or impossible).
interface Get {
readonly _tag: 'Get';
readonly _ans: number;
}
interface Put {
readonly _tag: 'Put';
readonly _ans: void;
value: number;
}
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());
}
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]()
@Nymphium
Nymphium / eff.ml
Last active December 7, 2021 07:35
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
#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;
type id = int
type scene = {
id: id;
content: unit -> unit
}
effect Resume : scene -> unit
effect Switch : unit
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
@Nymphium
Nymphium / n_cps.lua
Last active May 25, 2019 13:08
from algebraic effects to N-Barrelled CPS
-- 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