forked from Soonad/Moonad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IO.fm
38 lines (32 loc) · 1021 Bytes
/
IO.fm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// The IO type allows representing side-effective computations.
// You can run a term of type `IO(Unit)` using `fmcio my_term`.
IO: Type -> Type
(A)
io<P : IO(A) -> Type> ->
(end: (val: A) -> P(IO.end<A>(val))) ->
(log: (str: String) -> (nxt: Unit -> IO(A)) -> P(IO.log<A>(str)(nxt))) ->
(get: (nxt: String -> IO(A)) -> P(IO.get<A>(nxt))) ->
P(io)
IO.end: <A: Type> -> A -> IO(A)
<A> (x)
<P> (end) () ()
end(x)
IO.get: <A: Type> -> (String -> IO(A)) -> IO(A)
<A> (nxt)
<P> () () (get)
get(nxt)
IO.log: <A: Type> -> String -> (Unit -> IO(A)) -> IO(A)
<A> (str) (nxt)
<P> () (log) ()
log(str)(nxt)
IO.query(str: String): IO(String)
IO.log<String>(str, () IO.get<String>(IO.end<String>))
IO.print(str: String): IO(Unit)
use skip = IO.log<Unit>(str)
IO.end<Unit>(Unit.new)
IO.bind<A: Type, B: Type>(a: IO(A), f: A -> IO(B)) : IO(B)
case a
: () IO(B);
| (val) f(val);
| (str, nxt) IO.log<B>(str, (x) IO.bind<A,B>(nxt(x), f));
| (nxt) IO.get<B>((x) IO.bind<A,B>(nxt(x), f));