Skip to content

Commit

Permalink
Implement builtin type Map, and its corresponding functions, as well …
Browse files Browse the repository at this point in the history
…as the Maybe type
  • Loading branch information
In-Veritas committed Dec 7, 2024
1 parent e195a55 commit f97fa76
Show file tree
Hide file tree
Showing 44 changed files with 177 additions and 288 deletions.
1 change: 0 additions & 1 deletion examples/example_fun.bend
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ bad_nums : Any = (+ id 1)

# Write new data types like this
type Option = (Some val) | None
type Bool = True | False

# You can have pattern matching on definitions
# Use `*` to ignore a pattern
Expand Down
4 changes: 0 additions & 4 deletions examples/parallel_and.bend
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
# This program allocates a tree with True at the leaves then parallel ANDs them.
type Bool:
True
False

def and(a: Bool, b: Bool) -> Bool:
match a:
case Bool/True:
Expand Down
5 changes: 5 additions & 0 deletions src/fun/builtins.bend
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ def Tree/reverse(tree: Tree(T)) -> Tree(T):
case Tree/Node:
return ![tree.right, tree.left]

# BOOL Impl
type Bool:
True
False


# MAYBE Impl

Expand Down
4 changes: 0 additions & 4 deletions tests/builtin_Map_tests.bend
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
type Bool:
True
False

# Returns values from a map
def test1() -> (u24, u24):
m = {1: 1, 2: 2, 3: 3}
Expand Down
10 changes: 3 additions & 7 deletions tests/golden_tests/compile_file/mismatched_ask_statements.bend
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
type Bool:
T
F

def main:
with IO:
match _ = Bool/T:
case Bool/T:
match _ = Bool/True:
case Bool/False:
x <- wrap(0)
case Bool/F:
case Bool/False:
x = wrap(0)
return wrap(x)
1 change: 0 additions & 1 deletion tests/golden_tests/compile_file_o_all/list_merge_sort.bend
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
type Bool = True | False
type List_ = (Cons head tail) | Nil

If Bool/True then else = then
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
type Maybe = (Some val) | None

main = @maybe
match maybe {
Maybe/None: 0
Maybe/Some: match maybe.val {
Maybe/Some: match maybe.value {
Maybe/None: 0
}
}
6 changes: 2 additions & 4 deletions tests/golden_tests/compile_file_o_all/non_exhaustive_and.bend
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
type Bool = T | F

Bool.and Bool/T Bool/T = Bool/T
Bool.and Bool/F Bool/F = Bool/F
Bool.and Bool/True Bool/True = Bool/True
Bool.and Bool/False Bool/False = Bool/False

Main = *
10 changes: 3 additions & 7 deletions tests/golden_tests/desugar_file/ask_branch.bend
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
type Bool:
T
F

def main:
with IO:
match _ = Bool/T:
case Bool/T:
match _ = Bool/True:
case Bool/True:
x <- wrap(0)
case Bool/F:
case Bool/False:
x <- wrap(0)
return wrap(x)
7 changes: 3 additions & 4 deletions tests/golden_tests/encode_pattern_match/and3.bend
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
type Bool = T | F

And (Bool/T, Bool/T, Bool/T) = Bool/T
And * = Bool/F
And (Bool/True, Bool/True, Bool/True) = Bool/True
And * = Bool/False

main = (And (Bool/F, Bool/T, Bool/F))
main = (And (Bool/False, Bool/True, Bool/False))
8 changes: 3 additions & 5 deletions tests/golden_tests/encode_pattern_match/bool_tup.bend
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
type Bool = T | F
foo (Bool/True, x) = x
foo * = Bool/False

foo (Bool/T, x) = x
foo * = Bool/F

main = (foo (Bool/F, Bool/T))
main = (foo (Bool/False, Bool/True))
2 changes: 0 additions & 2 deletions tests/golden_tests/encode_pattern_match/common.bend
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ type List_
= (Cons x xs)
| Nil

type Bool = True | False

type Light = Red | Yellow | Green

type Direction
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
type Either = (Left value) | (Right value)
type Bool = True | False

Foo (Either/Left Bool/False) (Either/Left Bool/False) = 1
Foo (Either/Left Bool/False) (Either/Left Bool/True) = 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
type Bool = True | False
type List_ = (Cons head tail) | Nil

If Bool/True then else = then
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
type Maybe = None | (Some val)

main = (match x = (Maybe/Some 1) {
Maybe/None: @$x *
Maybe/Some: x.val
Maybe/Some: x.value
} $x)
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
type Maybe = None | (Some val)

Foo = @$x match x = (Maybe/Some 1) {
Maybe/None: $x
Maybe/Some: x.val
Expand Down
2 changes: 0 additions & 2 deletions tests/golden_tests/encode_pattern_match/var_only.bend
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
type Bool = False | True

(Foo a b) = λf (f a)
(Foo a b) = b

Expand Down
2 changes: 0 additions & 2 deletions tests/golden_tests/mutual_recursion/odd_even.bend
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
type Bool = True | False

(if_ 0 then else) = else
(if_ _ then else) = then

Expand Down
4 changes: 0 additions & 4 deletions tests/golden_tests/parse_file/imp_program.bend
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
type Point:
Point { x, y }

type Bool:
True
False


def symbols():
x = { `x`: 5, 2: 3 };
Expand Down
1 change: 0 additions & 1 deletion tests/golden_tests/run_file/example.bend
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

# Write new data types like this
type Option = (Some val) | None
type Bool = True | False

# You can have pattern matching on definitions
# Use `*` to ignore a pattern
Expand Down
10 changes: 3 additions & 7 deletions tests/golden_tests/run_file/filter_bool_id.bend
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
type Bool:
T
F

def filter(f, ls):
match ls:
case List/Nil:
return List/Nil
case List/Cons:
match f(ls.head):
case Bool/T:
case Bool/True:
return List/Cons(ls.head, filter(f, ls.tail))
case Bool/F:
case Bool/False:
return filter(f, ls.tail)

def main:
return filter(lambda x: x, [Bool/T])
return filter(lambda x: x, [Bool/True])
4 changes: 0 additions & 4 deletions tests/golden_tests/run_file/mixed_syntax.bend
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
type Bool:
True
False

type MyTree = (node ~lft ~rgt) | (leaf val)

def tree_xor(tree):
Expand Down
2 changes: 0 additions & 2 deletions tests/golden_tests/run_file/unbound_wrap.bend
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
type Maybe = (Some x) | None

Maybe/bind val nxt = match val {
Maybe/Some: (nxt val.x)
Maybe/None: Maybe/None
Expand Down
6 changes: 2 additions & 4 deletions tests/golden_tests/run_file/unscoped_never_used.bend
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
type Bool = T | F

main = @x match x {
Bool/T : @$x *
Bool/F : @x *
Bool/True : @$x *
Bool/False : @x *
}
4 changes: 1 addition & 3 deletions tests/golden_tests/simplify_matches/double_unwrap_maybe.bend
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# We want to make sure that the default value is not mistakenly erased in the first level of flattening.
type Maybe = (Some x) | None

(DoubleUnwrap (Maybe/Some (Maybe/Some x)) *) = x
(DoubleUnwrap (Maybe/Some (Maybe/Some val)) *) = val
(DoubleUnwrap * x) = x

Main = (DoubleUnwrap (Maybe/Some Maybe/None) 5)
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ input_file: tests/golden_tests/compile_file_o_all/non_exhaustive_and.bend
Errors:
In tests/golden_tests/compile_file_o_all/non_exhaustive_and.bend :
In definition 'Bool.and':
Non-exhaustive pattern matching rule. Constructor 'Bool/F' of type 'Bool' not covered
Non-exhaustive pattern matching rule. Constructor 'Bool/False' of type 'Bool' not covered
20 changes: 7 additions & 13 deletions tests/snapshots/desugar_file__ask_branch.bend.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ undefer: (((a -> a) -> b) -> b)
(undefer) = λa (a λb b)

unchecked main: Any
(main) = (IO/bind (Bool/T λa switch a { 0: (IO/wrap 0); _: λ* (IO/wrap 0); }) λb (b λc λd (c d) IO/wrap))
(main) = (IO/bind (Bool/True λa switch a { 0: (IO/wrap 0); _: λ* (IO/wrap 0); }) λb (b λc λd (c d) IO/wrap))

Bool/True/tag: u24
(Bool/True/tag) = 0

Bool/True: Bool
(Bool/True) = λa (a Bool/True/tag)

IO/Done/tag: u24
(IO/Done/tag) = 0
Expand All @@ -29,18 +35,6 @@ IO/Call/tag: u24
IO/Call: ((u24, u24) -> String -> Any -> ((Result Any (IOError Any)) -> (IO a)) -> (IO a))
(IO/Call) = λa λb λc λd λe (e IO/Call/tag a b c d)

Bool/T/tag: u24
(Bool/T/tag) = 0

Bool/T: Bool
(Bool/T) = λa (a Bool/T/tag)

Bool/F/tag: u24
(Bool/F/tag) = 1

Bool/F: Bool
(Bool/F) = λa (a Bool/F/tag)

IO/bind__C0: _
(IO/bind__C0) = λ* λa λb (undefer b a)

Expand Down
54 changes: 36 additions & 18 deletions tests/snapshots/desugar_file__mapper_syntax.bend.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
source: tests/golden_tests.rs
input_file: tests/golden_tests/desugar_file/mapper_syntax.bend
---
Maybe/unwrap: ((Maybe a) -> a)
(Maybe/unwrap) = λa (a Maybe/unwrap__C0)

Map/empty: (Map a)
(Map/empty) = Map/Leaf

Expand All @@ -20,10 +23,22 @@ unreachable: Any
unchecked main: Any
(main) = let (c, d) = (Map/get (Map/map (Map/map (Map/set (Map/set Map/empty 0 3) 1 4) 1 λa (+ a 1)) 1 λb (* b 2)) 1); let (e, *) = (Map/get d 0); ((λf (+ f 1) 1), c, e)

Maybe/Some/tag: u24
(Maybe/Some/tag) = 0

Maybe/Some: (a -> (Maybe a))
(Maybe/Some) = λa λb (b Maybe/Some/tag a)

Maybe/None/tag: u24
(Maybe/None/tag) = 1

Maybe/None: (Maybe a)
(Maybe/None) = λa (a Maybe/None/tag)

Map/Node/tag: u24
(Map/Node/tag) = 0

Map/Node: (a -> (Map a) -> (Map a) -> (Map a))
Map/Node: ((Maybe a) -> (Map a) -> (Map a) -> (Map a))
(Map/Node) = λa λb λc λd (d Map/Node/tag a b c)

Map/Leaf/tag: u24
Expand All @@ -33,16 +48,16 @@ Map/Leaf: (Map a)
(Map/Leaf) = λa (a Map/Leaf/tag)

Map/get__C0: _
(Map/get__C0) = λa λb λc λd let (e, f) = (Map/get c (/ a 2)); (e, (Map/Node b f d))
(Map/get__C0) = λa λb λc λd let (e, f) = (Map/get d (/ a 2)); (e, (Map/Node b f c))

Map/get__C1: _
(Map/get__C1) = λ* λa λb λc λd let (e, f) = (Map/get d (/ a 2)); (e, (Map/Node b c f))
(Map/get__C1) = λ* λa λb λc λd let (e, f) = (Map/get c (/ a 2)); (e, (Map/Node b f d))

Map/get__C2: _
(Map/get__C2) = λa let {b c} = a; λd λe λf (switch (% b 2) { 0: Map/get__C0; _: Map/get__C1; } c d e f)
(Map/get__C2) = λa let {b c} = a; λd λe λf (switch (== (% b 2) 0) { 0: Map/get__C0; _: Map/get__C1; } c d e f)

Map/get__C3: _
(Map/get__C3) = λ* λ* λa let {b c} = a; λd λe (b, (Map/Node c d e))
(Map/get__C3) = λ* λ* λa let {b c} = a; λd λe ((Maybe/unwrap b), (Map/Node c d e))

Map/get__C4: _
(Map/get__C4) = λa λb λc λd let {e f} = d; (switch (== 0 e) { 0: Map/get__C2; _: Map/get__C3; } f a b c)
Expand All @@ -51,52 +66,55 @@ Map/get__C5: _
(Map/get__C5) = λa switch a { 0: Map/get__C4; _: λ* λ* (unreachable, Map/Leaf); }

Map/map__C0: _
(Map/map__C0) = λa λb λc λd λe (Map/Node c (Map/map d (/ a 2) b) e)
(Map/map__C0) = λa λb λc λd λe (Map/Node c d (Map/map e (/ a 2) b))

Map/map__C1: _
(Map/map__C1) = λ* λa λb λc λd λe (Map/Node c d (Map/map e (/ a 2) b))
(Map/map__C1) = λ* λa λb λc λd λe (Map/Node c (Map/map d (/ a 2) b) e)

Map/map__C2: _
(Map/map__C2) = λa let {b c} = a; λd λe λf λg (switch (% b 2) { 0: Map/map__C0; _: Map/map__C1; } c d e f g)
(Map/map__C2) = λa let {b c} = a; λd λe λf λg (switch (== (% b 2) 0) { 0: Map/map__C0; _: Map/map__C1; } c d e f g)

Map/map__C3: _
(Map/map__C3) = λ* λ* λa λb λc λd (Map/Node (a b) c d)
(Map/map__C3) = λ* λ* λa λb λc λd (Map/Node (Maybe/Some (a (Maybe/unwrap b))) c d)

Map/map__C4: _
(Map/map__C4) = λa λb λc λd let {e f} = d; λg (switch (== 0 e) { 0: Map/map__C2; _: Map/map__C3; } f g a b c)

Map/map__C5: _
(Map/map__C5) = λa switch a { 0: Map/map__C4; _: λ* λ* λ* Map/Leaf; }
(Map/map__C5) = λa switch a { 0: Map/map__C4; _: λ* λ* λ* unreachable; }

Map/set__C0: _
(Map/set__C0) = λa λb λc λd λe (Map/Node c (Map/set d (/ a 2) b) e)
(Map/set__C0) = λa λb λc λd λe (Map/Node c d (Map/set e (/ a 2) b))

Map/set__C1: _
(Map/set__C1) = λ* λa λb λc λd λe (Map/Node c d (Map/set e (/ a 2) b))
(Map/set__C1) = λ* λa λb λc λd λe (Map/Node c (Map/set d (/ a 2) b) e)

Map/set__C10: _
(Map/set__C10) = λa switch a { 0: Map/set__C8; _: Map/set__C9; }

Map/set__C2: _
(Map/set__C2) = λa let {b c} = a; λd λe λf λg (switch (% b 2) { 0: Map/set__C0; _: Map/set__C1; } c d e f g)
(Map/set__C2) = λa let {b c} = a; λd λe λf λg (switch (== (% b 2) 0) { 0: Map/set__C0; _: Map/set__C1; } c d e f g)

Map/set__C3: _
(Map/set__C3) = λ* λ* λa λ* λb λc (Map/Node a b c)
(Map/set__C3) = λ* λ* λa λ* λb λc (Map/Node (Maybe/Some a) b c)

Map/set__C4: _
(Map/set__C4) = λa λb (Map/Node unreachable (Map/set Map/Leaf (/ a 2) b) Map/Leaf)
(Map/set__C4) = λa λb (Map/Node Maybe/None Map/Leaf (Map/set Map/Leaf (/ a 2) b))

Map/set__C5: _
(Map/set__C5) = λ* λa λb (Map/Node unreachable Map/Leaf (Map/set Map/Leaf (/ a 2) b))
(Map/set__C5) = λ* λa λb (Map/Node Maybe/None (Map/set Map/Leaf (/ a 2) b) Map/Leaf)

Map/set__C6: _
(Map/set__C6) = λa let {b c} = a; λd (switch (% b 2) { 0: Map/set__C4; _: Map/set__C5; } c d)
(Map/set__C6) = λa let {b c} = a; λd (switch (== (% b 2) 0) { 0: Map/set__C4; _: Map/set__C5; } c d)

Map/set__C7: _
(Map/set__C7) = λ* λ* λa (Map/Node a Map/Leaf Map/Leaf)
(Map/set__C7) = λ* λ* λa (Map/Node (Maybe/Some a) Map/Leaf Map/Leaf)

Map/set__C8: _
(Map/set__C8) = λa λb λc λd let {e f} = d; λg (switch (== 0 e) { 0: Map/set__C2; _: Map/set__C3; } f g a b c)

Map/set__C9: _
(Map/set__C9) = λ* λa let {b c} = a; λd (switch (== 0 b) { 0: Map/set__C6; _: Map/set__C7; } c d)

Maybe/unwrap__C0: _
(Maybe/unwrap__C0) = λa switch a { 0: λb b; _: λ* unreachable; }
Loading

0 comments on commit f97fa76

Please sign in to comment.