Current section
Files
Jump to
Current section
Files
src/test.erl
-module(test).
-compile([no_auto_import, nowarn_unused_vars]).
-export([yield/2, mprompt/2, prompt/2, main/0]).
-export_type([ctrl/1]).
-type ctrl(FJH) :: {pure, FJH} |
{yield,
binary(),
fun((fun((FJH) -> ctrl(FJH))) -> ctrl(FJH)),
fun((FJH) -> ctrl(FJH))}.
-spec yield(binary(), fun((fun((FKE) -> ctrl(FKE))) -> ctrl(FKE))) -> ctrl(FKE).
yield(Marker, Op) ->
{yield, Marker, Op, fun(Field@0) -> {pure, Field@0} end}.
-spec mprompt(binary(), ctrl(FKJ)) -> ctrl(FKJ).
mprompt(Marker, Ctl) ->
case Ctl of
{pure, X} ->
{pure, X};
{yield, M, Op, Cont} ->
Cont@1 = fun(X@1) -> mprompt(Marker, Cont(X@1)) end,
case Marker =:= M of
true ->
Op(Cont@1);
false ->
{yield, M, Op, Cont@1}
end
end.
-spec prompt(binary(), fun(() -> ctrl(FKM))) -> ctrl(FKM).
prompt(Marker, Action) ->
mprompt(Marker, Action()).
-spec bind(ctrl(FJN), fun((FJN) -> ctrl(FJN))) -> ctrl(FJN).
bind(Ctl, F) ->
case Ctl of
{pure, X} ->
F(X);
{yield, M, Op, Cont} ->
{yield, M, Op, kcompose(F, Cont)}
end.
-spec kcompose(fun((FJN) -> ctrl(FJN)), fun((FJN) -> ctrl(FJN))) -> fun((FJN) -> ctrl(FJN)).
kcompose(G, F) ->
fun(X) -> _pipe = F(X),
bind(_pipe, G) end.
-spec run() -> ctrl(integer()).
run() ->
prompt(
<<"read"/utf8>>,
fun() ->
bind(
{pure, 5},
fun(A) ->
bind(
yield(<<"read"/utf8>>, fun(K) -> K(2) end),
fun(B) -> {pure, A + B} end
)
end
)
end
).
-spec main() -> nil.
main() ->
_pipe = run(),
gleam@io:debug(_pipe),
gleam@io:println(<<"Hello from deli!"/utf8>>).