diff options
-rw-r--r-- | bruijn.cabal | 1 | ||||
-rw-r--r-- | std/Box.bruijn | 41 | ||||
-rw-r--r-- | std/Option.bruijn | 2 |
3 files changed, 43 insertions, 1 deletions
diff --git a/bruijn.cabal b/bruijn.cabal index aacd4b5..3717789 100644 --- a/bruijn.cabal +++ b/bruijn.cabal @@ -18,6 +18,7 @@ extra-source-files: readme.md data-files: config + std/Box.bruijn std/Char.bruijn std/Combinator.bruijn std/Float.bruijn diff --git a/std/Box.bruijn b/std/Box.bruijn new file mode 100644 index 0000000..3e3cbfa --- /dev/null +++ b/std/Box.bruijn @@ -0,0 +1,41 @@ +# MIT License, Copyright (c) 2023 Marvin Borner +# a box can store a single item very efficiently +# similar structure to std/Option + +:import std/Logic . +:import std/Combinator . + +# a empty box +empty true ⧗ (Box a) + +# returns true if the box is empty +empty? [0 true [false]] ⧗ (Box a) → Boolean + +:test (empty? empty) (true) + +# builds a box out of a value +box [[[0 2]]] ⧗ a → (Box a) + +# returns true if the box is set +set? [0 false [true]] ⧗ (Box a) → Boolean + +:test (set? (box [0])) (true) +:test (set? empty) (false) + +# sets the value of a empty box, ignores argument if already set +store! [[empty? 1 (box 0) 1]] ⧗ (Box a) → a → (Box a) + +:test (store! (box [[0]]) [[1]]) (box [[0]]) +:test (store! empty [[1]]) (box [[1]]) + +# sets/overrides the value of a box +set! [[(box 0)]] ⧗ (Box a) → a → (Box a) + +:test (set! (box [[0]]) [[1]]) (box [[1]]) +:test (set! empty [[1]]) (box [[1]]) + +# extracts value from a box or returns first argument if none +get [[0 1 i]] ⧗ a → (Box b) → c + +:test (get [[0]] (box [[1]])) ([[1]]) +:test (get [[0]] empty) ([[0]]) diff --git a/std/Option.bruijn b/std/Option.bruijn index c0d57b4..fafe173 100644 --- a/std/Option.bruijn +++ b/std/Option.bruijn @@ -7,7 +7,7 @@ none true ⧗ (Option a) # encapsulates value in option -some [[[0 2]]] ⧗ (Option a) +some [[[0 2]]] ⧗ a → (Option a) # checks whether option is none none? [0 true [false]] ⧗ (Option a) → Boolean |