Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return Result<T, E> from IO functions #407

Merged
merged 17 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 26 additions & 54 deletions examples/demo_io/main.bend
Original file line number Diff line number Diff line change
@@ -1,63 +1,35 @@
test-io = 1

type IO_T:
Done { magic, expr }
Call { magic, func, argm, cont }
def unwrap(res):
match res:
case Result/Ok:
return res.val
case Result/Err:
return res.val

def IO_T/MAGIC:
return (0xD0CA11, 0xFF1FF1)
def open():
return call("OPEN", ("./LICENSE", "r"))

def IO_T/bind(a, b):
match a:
case IO_T/Done:
return (undefer(b))(a.expr)
case IO_T/Call:
return IO_T/Call(IO_T/MAGIC, a.func, a.argm, lambda x: IO_T/bind(a.cont(x), b))
def read(f):
return call("READ", (f, 47))

def call_io(func, argm):
return IO_T/Call(IO_T/MAGIC, func, argm, lambda x: IO_T/Done(IO_T/MAGIC, x))
def print(bytes):
with IO:
* <- call("WRITE", (1, bytes))
* <- call("WRITE", (1, "\n"))

def read_input():
with IO_T:
* <- call_io("WRITE", (1, "What is your name?\n"))
return call_io("READ", (0, 10))
return wrap(*)

def write_to_file():
with IO_T:
fp <- call_io("OPEN", ("testing.txt", "w"))
input <- read_input()
* <- call_io("WRITE", (fp, input))
* <- call_io("WRITE", (fp, "\n"))

return call_io("CLOSE", fp)

def read_from_file():
with IO_T:
fp <- call_io("OPEN", ("testing.txt", "r"))
bytes <- call_io("READ", (fp, 5))
* <- call_io("WRITE", (1, bytes))
* <- call_io("SEEK", (fp, 2, 0))
bytes <- call_io("READ", (fp, 5))
* <- call_io("WRITE", (1, bytes))

return call_io("CLOSE", fp)

def write:
return "WRITE"

def one:
return 1

def newline:
return "\n"

def one_newline_pair:
return (one, newline)
def close(f):
return call("CLOSE", f)

def main():
with IO_T:
* <- write_to_file()
* <- read_from_file()
* <- call_io(write, one_newline_pair)

return 42
with IO:
f <- open()
f = unwrap(f)
bytes <- read(f)
bytes = unwrap(bytes)
* <- print(bytes)
res <- close(f)

return wrap(res)
250 changes: 62 additions & 188 deletions examples/demo_io/main.hvm
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
@IO_T/Call = (a (b (c (d ((@IO_T/Call/tag (a (b (c (d e))))) e)))))
@IO/Call = (a (b (c (d ((@IO/Call/tag (a (b (c (d e))))) e)))))

@IO_T/Call/tag = 1
@IO/Call/tag = 1

@IO_T/Done = (a (b ((@IO_T/Done/tag (a (b c))) c)))
@IO/Done = (a (b ((@IO/Done/tag (a (b c))) c)))

@IO_T/Done/tag = 0
@IO/Done/tag = 0

@IO_T/MAGIC = (13683217 16719857)
@IO/MAGIC = (13683217 16719857)

@IO_T/bind = ((@IO_T/bind__C2 a) a)
@IO/bind = ((@IO/bind__C2 a) a)

@IO_T/bind__C0 = (* (b (a c)))
@IO/bind__C0 = (* (b (a c)))
& @undefer ~ (a (b c))

@IO_T/bind__C1 = (* (* (a (b ((c d) (e g))))))
& @IO_T/Call ~ (@IO_T/MAGIC (a (b ((c f) g))))
& @IO_T/bind ~ (d (e f))
@IO/bind__C1 = (* (* (a (b ((c d) (e g))))))
& @IO/Call ~ (@IO/MAGIC (a (b ((c f) g))))
& @IO/bind ~ (d (e f))

@IO_T/bind__C2 = (?((@IO_T/bind__C0 @IO_T/bind__C1) a) a)
@IO/bind__C2 = (?((@IO/bind__C0 @IO/bind__C1) a) a)

@IO/wrap = a
& @IO/Done ~ (@IO/MAGIC a)

@String/Cons = (a (b ((@String/Cons/tag (a (b c))) c)))

Expand All @@ -27,215 +30,86 @@

@String/Nil/tag = 0

@call_io = (a (b c))
& @IO_T/Call ~ (@IO_T/MAGIC (a (b (@call_io__C0 c))))

@call_io__C0 = a
& @IO_T/Done ~ (@IO_T/MAGIC a)

@main = g
& @IO_T/bind ~ (@write_to_file ((((* e) f) f) g))
& @IO_T/bind ~ (@read_from_file ((((* c) d) d) e))
& @IO_T/bind ~ (a ((((* 42) b) b) c))
& @call_io ~ (@write (@one_newline_pair a))

@newline = a
& @String/Cons ~ (10 (@String/Nil a))

@one = 1
@call = (a (b c))
& @IO/Call ~ (@IO/MAGIC (a (b (@call__C0 c))))

@one_newline_pair = (@one @newline)
@call__C0 = a
& @IO/Done ~ (@IO/MAGIC a)

@read_from_file = a
& @IO_T/bind ~ (@read_from_file__C11 (@read_from_file__C10 a))

@read_from_file__C0 = (f (* g))
& @call_io ~ (e (f g))
@close = f
& @call ~ (e f)
& @String/Cons ~ (67 (d e))
& @String/Cons ~ (76 (c d))
& @String/Cons ~ (79 (b c))
& @String/Cons ~ (83 (a b))
& @String/Cons ~ (69 (@String/Nil a))

@read_from_file__C1 = e
& @String/Cons ~ (87 (d e))
& @String/Cons ~ (82 (c d))
& @String/Cons ~ (73 (b c))
& @String/Cons ~ (84 (a b))
& @String/Cons ~ (69 (@String/Nil a))

@read_from_file__C10 = ((@read_from_file__C9 a) a)

@read_from_file__C11 = q
& @call_io ~ (d ((o p) q))
@main = w
& @IO/bind ~ (@open ((((s (a u)) (@IO/wrap v)) v) w))
& @IO/bind ~ (c ((((n (o (d q))) (r (s t))) t) u))
& @unwrap ~ (a {b r})
& @read ~ (b c)
& @IO/bind ~ (f ((((g (k (* m))) (n (o p))) p) q))
& @print ~ (e f)
& @unwrap ~ (d e)
& @IO/bind ~ (h ((((i i) (k l)) l) m))
& @close ~ (g h)

@open = o
& @call ~ (d ((m n) o))
& @String/Cons ~ (79 (c d))
& @String/Cons ~ (80 (b c))
& @String/Cons ~ (69 (a b))
& @String/Cons ~ (78 (@String/Nil a))
& @String/Cons ~ (116 (n o))
& @String/Cons ~ (101 (m n))
& @String/Cons ~ (115 (l m))
& @String/Cons ~ (116 (k l))
& @String/Cons ~ (105 (j k))
& @String/Cons ~ (110 (i j))
& @String/Cons ~ (103 (h i))
& @String/Cons ~ (46 (g h))
& @String/Cons ~ (116 (f g))
& @String/Cons ~ (120 (e f))
& @String/Cons ~ (116 (@String/Nil e))
& @String/Cons ~ (114 (@String/Nil p))

@read_from_file__C2 = (c (a e))
& @IO_T/bind ~ (b (((@read_from_file__C0 (c d)) d) e))
& @call_io ~ (@read_from_file__C1 ((1 a) b))

@read_from_file__C3 = d
& @String/Cons ~ (82 (c d))
& @String/Cons ~ (69 (b c))
& @String/Cons ~ (65 (a b))
& @String/Cons ~ (68 (@String/Nil a))

@read_from_file__C4 = ({a c} (* e))
& @IO_T/bind ~ (b (((@read_from_file__C2 (c d)) d) e))
& @call_io ~ (@read_from_file__C3 ((a 5) b))

@read_from_file__C5 = d
& @String/Cons ~ (83 (c d))
& @String/Cons ~ (69 (b c))
& @String/Cons ~ (69 (a b))
& @String/Cons ~ (75 (@String/Nil a))

@read_from_file__C6 = ({a c} (* e))
& @IO_T/bind ~ (b (((@read_from_file__C4 (c d)) d) e))
& @call_io ~ (@read_from_file__C5 ((a (2 0)) b))

@read_from_file__C7 = e
& @String/Cons ~ (46 (l m))
& @String/Cons ~ (47 (k l))
& @String/Cons ~ (76 (j k))
& @String/Cons ~ (73 (i j))
& @String/Cons ~ (67 (h i))
& @String/Cons ~ (69 (g h))
& @String/Cons ~ (78 (f g))
& @String/Cons ~ (83 (e f))
& @String/Cons ~ (69 (@String/Nil e))
& @String/Cons ~ (114 (@String/Nil n))

@print = (f h)
& @IO/bind ~ (g (@print__C3 h))
& @call ~ (e ((1 f) g))
& @String/Cons ~ (87 (d e))
& @String/Cons ~ (82 (c d))
& @String/Cons ~ (73 (b c))
& @String/Cons ~ (84 (a b))
& @String/Cons ~ (69 (@String/Nil a))

@read_from_file__C8 = (c (a e))
& @IO_T/bind ~ (b (((@read_from_file__C6 (c d)) d) e))
& @call_io ~ (@read_from_file__C7 ((1 a) b))

@read_from_file__C9 = ({e g} i)
& @IO_T/bind ~ (f (((@read_from_file__C8 (g h)) h) i))
& @call_io ~ (d ((e 5) f))
& @String/Cons ~ (82 (c d))
& @String/Cons ~ (69 (b c))
& @String/Cons ~ (65 (a b))
& @String/Cons ~ (68 (@String/Nil a))

@read_input = a
& @IO_T/bind ~ (@read_input__C2 (@read_input__C1 a))
@print__C0 = ((* a) (* a))

@read_input__C0 = (* e)
& @call_io ~ (d ((0 10) e))
& @String/Cons ~ (82 (c d))
& @String/Cons ~ (69 (b c))
& @String/Cons ~ (65 (a b))
& @String/Cons ~ (68 (@String/Nil a))

@read_input__C1 = ((@read_input__C0 a) a)

@read_input__C2 = y
& @call_io ~ (e ((1 x) y))
@print__C1 = g
& @call ~ (e ((1 f) g))
& @String/Cons ~ (87 (d e))
& @String/Cons ~ (82 (c d))
& @String/Cons ~ (73 (b c))
& @String/Cons ~ (84 (a b))
& @String/Cons ~ (69 (@String/Nil a))
& @String/Cons ~ (87 (w x))
& @String/Cons ~ (104 (v w))
& @String/Cons ~ (97 (u v))
& @String/Cons ~ (116 (t u))
& @String/Cons ~ (32 (s t))
& @String/Cons ~ (105 (r s))
& @String/Cons ~ (115 (q r))
& @String/Cons ~ (32 (p q))
& @String/Cons ~ (121 (o p))
& @String/Cons ~ (111 (n o))
& @String/Cons ~ (117 (m n))
& @String/Cons ~ (114 (l m))
& @String/Cons ~ (32 (k l))
& @String/Cons ~ (110 (j k))
& @String/Cons ~ (97 (i j))
& @String/Cons ~ (109 (h i))
& @String/Cons ~ (101 (g h))
& @String/Cons ~ (63 (f g))
& @String/Cons ~ (10 (@String/Nil f))

@test-io = 1
@print__C2 = (a (* c))
& @IO/bind ~ (@print__C1 (((@print__C0 (a b)) b) c))

@undefer = (((a a) b) b)

@write = e
& @String/Cons ~ (87 (d e))
& @String/Cons ~ (82 (c d))
& @String/Cons ~ (73 (b c))
& @String/Cons ~ (84 (a b))
& @String/Cons ~ (69 (@String/Nil a))

@write_to_file = a
& @IO_T/bind ~ (@write_to_file__C8 (@write_to_file__C7 a))

@write_to_file__C0 = (f (* g))
& @call_io ~ (e (f g))
& @String/Cons ~ (67 (d e))
& @String/Cons ~ (76 (c d))
& @String/Cons ~ (79 (b c))
& @String/Cons ~ (83 (a b))
& @String/Cons ~ (69 (@String/Nil a))
@print__C3 = ((@print__C2 (@IO/wrap a)) a)

@write_to_file__C1 = a
& @String/Cons ~ (10 (@String/Nil a))

@write_to_file__C2 = e
& @String/Cons ~ (87 (d e))
@read = (e f)
& @call ~ (d ((e 47) f))
& @String/Cons ~ (82 (c d))
& @String/Cons ~ (73 (b c))
& @String/Cons ~ (84 (a b))
& @String/Cons ~ (69 (@String/Nil a))

@write_to_file__C3 = ({a c} (* e))
& @IO_T/bind ~ (b (((@write_to_file__C0 (c d)) d) e))
& @call_io ~ (@write_to_file__C2 ((a @write_to_file__C1) b))

@write_to_file__C4 = e
& @String/Cons ~ (87 (d e))
& @String/Cons ~ (82 (c d))
& @String/Cons ~ (73 (b c))
& @String/Cons ~ (84 (a b))
& @String/Cons ~ (69 (@String/Nil a))
& @String/Cons ~ (69 (b c))
& @String/Cons ~ (65 (a b))
& @String/Cons ~ (68 (@String/Nil a))

@write_to_file__C5 = ({a d} (b f))
& @IO_T/bind ~ (c (((@write_to_file__C3 (d e)) e) f))
& @call_io ~ (@write_to_file__C4 ((a b) c))
@test-io = 1

@write_to_file__C6 = (a c)
& @IO_T/bind ~ (@read_input (((@write_to_file__C5 (a b)) b) c))
@undefer = (((a a) b) b)

@write_to_file__C7 = ((@write_to_file__C6 a) a)
@unwrap = ((@unwrap__C0 a) a)

@write_to_file__C8 = q
& @call_io ~ (d ((o p) q))
& @String/Cons ~ (79 (c d))
& @String/Cons ~ (80 (b c))
& @String/Cons ~ (69 (a b))
& @String/Cons ~ (78 (@String/Nil a))
& @String/Cons ~ (116 (n o))
& @String/Cons ~ (101 (m n))
& @String/Cons ~ (115 (l m))
& @String/Cons ~ (116 (k l))
& @String/Cons ~ (105 (j k))
& @String/Cons ~ (110 (i j))
& @String/Cons ~ (103 (h i))
& @String/Cons ~ (46 (g h))
& @String/Cons ~ (116 (f g))
& @String/Cons ~ (120 (e f))
& @String/Cons ~ (116 (@String/Nil e))
& @String/Cons ~ (119 (@String/Nil p))
@unwrap__C0 = (?(((a a) (* (b b))) c) c)


Loading
Loading