diff options
author | Marvin Borner | 2022-08-15 15:36:27 +0200 |
---|---|---|
committer | Marvin Borner | 2022-08-15 16:03:47 +0200 |
commit | 008fa86e9e66ca1b4503eb0ce7bbf42c38e3a521 (patch) | |
tree | 40a753467299087575061f8d64cfab062122b291 | |
parent | d757a5756eb6cd8aa470e5f53bbb685c6bd5d0df (diff) |
General std cleanup
-rw-r--r-- | std/List.bruijn | 89 | ||||
-rw-r--r-- | std/Logic.bruijn | 2 | ||||
-rw-r--r-- | std/Number.bruijn | 42 |
3 files changed, 93 insertions, 40 deletions
diff --git a/std/List.bruijn b/std/List.bruijn index d1f6394..37737fd 100644 --- a/std/List.bruijn +++ b/std/List.bruijn @@ -36,7 +36,11 @@ tail P.snd :test (tail ((+1) : ((+2) : empty))) ((+2) : empty) # returns the length of a list in balanced ternary -length Z [[[empty? 0 [2] [3 (N.inc 2) (tail 1)] I]]] (+0) +length Z [[[case-some]]] case-empty + case-some empty? 0 case-end case-inc + case-inc 2 (N.inc 1) (tail 0) + case-end 1 + case-empty (+0) #( length @@ -50,7 +54,11 @@ index [[head (1 tail 0)]] (!!) [[index 0 1]] # reverses a list -reverse Z [[[empty? 0 [2] [3 ((head 1) : 2) (tail 1)] I]]] empty +reverse Z [[[case-some]]] case-empty + case-some empty? 0 case-end case-rev + case-rev 2 ((head 0) : 1) (tail 0) + case-end 1 + case-empty empty :test (reverse ((+1) : ((+2) : ((+3) : empty)))) ((+3) : ((+2) : ((+1) : empty))) @@ -58,79 +66,122 @@ reverse Z [[[empty? 0 [2] [3 ((head 1) : 2) (tail 1)] I]]] empty # TODO: fix for balanced ternary list [0 [[[2 (0 : 1)]]] reverse empty] -# merges two lists -merge Z [[[empty? 1 [1] [(head 2) : (3 (tail 2) 1)] I]]] +# appends two lists +append Z [[[case-some]]] + case-some empty? 1 case-end case-merge + case-merge (head 1) : (2 (tail 1) 0) + case-end 0 -(++) merge +(++) append :test (((+1) : ((+2) : ((+3) : empty))) ++ ((+4) : empty)) ((+1) : ((+2) : ((+3) : ((+4) : empty)))) # maps each element to a function -map Z [[[empty? 0 [empty] [(2 (head 1)) : (3 2 (tail 1))] I]]] +map Z [[[case-some]]] + case-some empty? 0 case-end case-map + case-map (1 (head 0)) : (2 1 (tail 0)) + case-end empty (<$>) map :test (N.inc <$> ((+1) : ((+2) : ((+3) : empty)))) ((+2) : ((+3) : ((+4) : empty))) # applies a left fold on a list -foldl Z [[[[empty? 0 [2] [4 3 (3 2 (head 1)) (tail 1)] I]]]] +foldl Z [[[[case-some]]]] + case-some empty? 0 case-end case-fold + case-fold 3 2 (2 1 (head 0)) (tail 0) + case-end 1 :test (N.eq? (foldl N.add (+0) ((+1) : ((+2) : ((+3) : empty)))) (+6)) (true) :test (N.eq? (foldl N.sub (+6) ((+1) : ((+2) : ((+3) : empty)))) (+0)) (true) # applies a right fold on a list -foldr [[[Z [[empty? 0 [4] [5 (head 1) (2 (tail 1))] I]] 0]]] +foldr [[[Z [[case-some]] case-empty]]] + case-some empty? 0 case-end case-fold + case-fold 4 (head 0) (1 (tail 0)) + case-end 3 + case-empty 0 :test (N.eq? (foldr N.add (+0) ((+1) : ((+2) : ((+3) : empty)))) (+6)) (true) :test (N.eq? (foldr N.sub (+2) ((+1) : ((+2) : ((+3) : empty)))) (+0)) (true) # filters a list based on a predicate -filter Z [[[empty? 0 [empty] [2 (head 1) (cons (head 1)) I (3 2 (tail 1))] I]]] +filter Z [[[case-some]]] + case-some empty? 0 case-end case-filter + case-filter 1 (head 0) (cons (head 0)) I (2 1 (tail 0)) + case-end empty :test (filter N.zero? ((+1) : ((+0) : ((+3) : empty)))) ((+0) : empty) # returns the last element of a list -last Z [[empty? 0 [empty] [empty? (tail 1) (head 1) (2 (tail 1))] I]] +last Z [[case-some]] + case-some empty? 0 case-end case-last + case-last empty? (tail 0) (head 0) (1 (tail 0)) + case-end empty :test (last ((+1) : ((+2) : ((+3) : empty)))) ((+3)) # returns everything but the last element of a list -init Z [[empty? 0 [empty] [empty? (tail 1) empty ((head 1) : (2 (tail 1)))] I]] +init Z [[case-some]] + case-some empty? 0 case-end case-init + case-init empty? (tail 0) empty ((head 0) : (1 (tail 0))) + case-end empty :test (init ((+1) : ((+2) : ((+3) : empty)))) ((+1) : ((+2) : empty)) # zips two lists discarding excess elements -zip Z [[[empty? 1 [empty] [empty? 1 empty (((head 2) : (head 1)) : (3 (tail 2) (tail 1)))] I]]] +zip Z [[[case-some]]] + case-some empty? 1 case-end case-zip + case-zip empty? 0 empty (((head 1) : (head 0)) : (2 (tail 1) (tail 0))) + case-end empty -:test (zip ((+1) : ((+2) : empty)) ((+2) : ((+1) : empty))) ((P.pair (+1) (+2)) : ((P.pair (+2) (+1)) : empty)) +:test (zip ((+1) : ((+2) : empty)) ((+2) : ((+1) : empty))) (((+1) : (+2)) : (((+2) : (+1)) : empty)) # applies pairs of the zipped list as arguments to a function -zip-with Z [[[[empty? 1 [empty] [empty? 1 empty ((3 (head 2) (head 1)) : (4 3 (tail 2) (tail 1)))] I]]]] +zip-with Z [[[[case-some]]]] + case-some empty? 1 case-end case-zip + case-zip empty? 0 empty ((2 (head 1) (head 0)) : (3 2 (tail 1) (tail 0))) + case-end empty :test (zip-with N.add ((+1) : ((+2) : empty)) ((+2) : ((+1) : empty))) ((+3) : ((+3) : empty)) # returns first n elements of a list -take Z [[[empty? 0 [empty] [N.zero? 2 empty ((head 1) : (3 (N.dec 2) (tail 1)))] I]]] +take Z [[[case-some]]] + case-some empty? 0 case-end case-take + case-take N.zero? 1 empty ((head 0) : (2 (N.dec 1) (tail 0))) + case-end empty :test (take (+2) ((+1) : ((+2) : ((+3) : empty)))) ((+1) : ((+2) : empty)) # takes elements while a predicate is satisfied -take-while Z [[[empty? 0 [empty] [2 (head 1) ((head 1) : (3 2 (tail 1))) empty] I]]] +take-while Z [[[case-some]]] + case-some empty? 0 case-end case-take + case-take 1 (head 0) ((head 0) : (2 1 (tail 0))) empty + case-end empty :test (take-while N.zero? ((+0) : ((+0) : ((+1) : empty)))) ((+0) : ((+0) : empty)) # removes first n elements of a list -drop Z [[[empty? 0 [empty] [N.zero? 2 1 (3 (N.dec 2) (tail 1))] I]]] +drop Z [[[case-some]]] + case-some empty? 0 case-end case-drop + case-drop N.zero? 1 0 (2 (N.dec 1) (tail 0)) + case-end empty :test (drop (+2) ((+1) : ((+2) : ((+3) : empty)))) ((+3) : empty) # removes elements while a predicate is satisfied -drop-while Z [[[empty? 0 [empty] [2 (head 1) (3 2 (tail 1)) 1] I]]] +drop-while Z [[[case-some]]] + case-some empty? 0 case-end case-drop + case-drop 1 (head 0) (2 1 (tail 0)) 0 + case-end empty :test (drop-while N.zero? ((+0) : ((+0) : ((+1) : empty)))) ((+1) : empty) # returns a list with n-times a element -repeat Z [[[N.zero? 1 [empty] [P.pair 1 (3 (N.dec 2) 1)] I]]] +repeat Z [[[case-some]]] + case-some N.zero? 1 case-end case-repeat + case-repeat 0 : (2 (N.dec 1) 0) + case-end empty :test (repeat (+5) (+4)) (((+4) : ((+4) : ((+4) : ((+4) : ((+4) : empty)))))) :test (repeat (+1) (+4)) (((+4) : empty)) diff --git a/std/Logic.bruijn b/std/Logic.bruijn index a254715..f4b7470 100644 --- a/std/Logic.bruijn +++ b/std/Logic.bruijn @@ -55,6 +55,8 @@ xnor [[1 0 (not 0)]] :test (xnor false true) (false) :test (xnor false false) (true) +# this function is generally redundant +# I personally just write (exp? case-T case-F) directly if [[[2 1 0]]] (?!) if diff --git a/std/Number.bruijn b/std/Number.bruijn index 26a5d5b..89d04d3 100644 --- a/std/Number.bruijn +++ b/std/Number.bruijn @@ -179,14 +179,14 @@ normal! ω rec :test (normal! (abstract! (-42))) ((-42)) # checks whether two balanced ternary numbers are equal -# constants should be second argument (performance) +# smaller numbers should be second argument (performance) # -> ignores leading 0s! eq? [[abs 1 (abstract! 0)]] - z [zero? (normal! 0)] - neg [[0 false [2 0] [false] [false]]] - pos [[0 false [false] [2 0] [false]]] - zero [[0 (1 0) [false] [false] [2 0]]] abs [0 z neg pos zero] + z [zero? (normal! 0)] + neg [[0 false [2 0] [false] [false]]] + pos [[0 false [false] [2 0] [false]]] + zero [[0 (1 0) [false] [false] [2 0]]] (=?) eq? @@ -241,19 +241,19 @@ sdec strip . dec :test ((--(+42)) =? (+41)) (true) # adds two balanced ternary numbers (can introduce leading 0s) -# constants should be second argument (performance) +# smaller numbers should be second argument (performance) add [[abs 1 (abstract! 0)]] - c [[1 0 trit-zero]] - b-neg2 [1 (^=(3 0 trit-neg)) (^<(3 0 trit-zero)) (^>(3 0 trit-neg))] - b-neg [1 (^>(3 0 trit-neg)) (^=(3 0 trit-zero)) (^<(3 0 trit-zero))] - b-zero [up 1 (3 0 trit-zero)] - b-pos [1 (^=(3 0 trit-zero)) (^<(3 0 trit-pos)) (^>(3 0 trit-zero))] - b-pos2 [1 (^>(3 0 trit-zero)) (^=(3 0 trit-pos)) (^<(3 0 trit-pos))] - a-neg [[[1 (b-neg 1) b-neg2 b-zero b-neg]]] - a-pos [[[1 (b-pos 1) b-zero b-pos2 b-pos]]] - a-zero [[[1 (b-zero 1) b-neg b-pos b-zero]]] - z [[0 (--(normal! 1)) (++(normal! 1)) (normal! 1)]] abs [c (0 z a-neg a-pos a-zero)] + b-neg [1 (^>(3 0 trit-neg)) (^=(3 0 trit-zero)) (^<(3 0 trit-zero))] + b-zero [up 1 (3 0 trit-zero)] + b-pos [1 (^=(3 0 trit-zero)) (^<(3 0 trit-pos)) (^>(3 0 trit-zero))] + a-neg [[[1 (b-neg 1) b-neg' b-zero b-neg]]] + b-neg' [1 (^=(3 0 trit-neg)) (^<(3 0 trit-zero)) (^>(3 0 trit-neg))] + a-pos [[[1 (b-pos 1) b-zero b-pos' b-pos]]] + b-pos' [1 (^>(3 0 trit-zero)) (^=(3 0 trit-pos)) (^<(3 0 trit-pos))] + a-zero [[[1 (b-zero 1) b-neg b-pos b-zero]]] + z [[0 (--(normal! 1)) (++(normal! 1)) (normal! 1)]] + c [[1 0 trit-zero]] (+) add @@ -268,7 +268,7 @@ sadd strip ... add :test (((+42) + (+1)) =? (+43)) (true) # subs two balanced ternary numbers (can introduce leading 0s) -# constants should be second argument (performance) +# smaller numbers should be second argument (performance) sub [[1 + -0]] (-) sub @@ -284,7 +284,7 @@ ssub strip ... sub :test (((+42) - (+1)) =? (+41)) (true) # returns whether number is greater than other number -# constants should be second argument (performance) +# smaller numbers should be second argument (performance) gre? [[positive? (sub 1 0)]] (>?) gre? @@ -294,7 +294,7 @@ gre? [[positive? (sub 1 0)]] :test ((+3) >? (+2)) (true) # returns whether number is less than other number -# constants should be second argument (performance) +# smaller numbers should be second argument (performance) les? [[negative? (sub 1 0)]] (<?) les? @@ -304,7 +304,7 @@ les? [[negative? (sub 1 0)]] :test ((+3) <? (+2)) (false) # returns whether number is less than or equal to other number -# constants should be second argument (performance) +# smaller numbers should be second argument (performance) leq? [[not (gre? 1 0)]] (<=?) leq? @@ -314,7 +314,7 @@ leq? [[not (gre? 1 0)]] :test ((+3) <=? (+2)) (false) # returns whether number is greater than or equal to other number -# constants should be second argument (performance) +# smaller numbers should be second argument (performance) geq? [[not (les? 1 0)]] (>=?) geq? |