diff options
Diffstat (limited to 'std/Number.bruijn')
-rw-r--r-- | std/Number.bruijn | 184 |
1 files changed, 97 insertions, 87 deletions
diff --git a/std/Number.bruijn b/std/Number.bruijn index 2a4ca25..f603d88 100644 --- a/std/Number.bruijn +++ b/std/Number.bruijn @@ -25,43 +25,43 @@ trit-zero [[[0]]] # returns whether a trit is zero trit-zero? [0 F F T] -:test trit-neg? trit-neg = T -:test trit-neg? trit-pos = F -:test trit-neg? trit-zero = F -:test trit-pos? trit-neg = F -:test trit-pos? trit-pos = T -:test trit-pos? trit-zero = F -:test trit-zero? trit-neg = F -:test trit-zero? trit-pos = F -:test trit-zero? trit-zero = T +:test (trit-neg? trit-neg) (T) +:test (trit-neg? trit-pos) (F) +:test (trit-neg? trit-zero) (F) +:test (trit-pos? trit-neg) (F) +:test (trit-pos? trit-pos) (T) +:test (trit-pos? trit-zero) (F) +:test (trit-zero? trit-neg) (F) +:test (trit-zero? trit-pos) (F) +:test (trit-zero? trit-zero) (T) # shifts a negative trit into a balanced ternary number up-neg [[[[[2 (4 3 2 1 0)]]]]] -:test up-neg +0 = -1 -:test up-neg -1 = -4 -:test up-neg +42 = +125 +:test (up-neg +0) (-1) +:test (up-neg -1) (-4) +:test (up-neg +42) (+125) # shifts a positive trit into a balanced ternary number up-pos [[[[[1 (4 3 2 1 0)]]]]] -:test up-pos +0 = +1 -:test up-pos -1 = -2 -:test up-pos +42 = +127 +:test (up-pos +0) (+1) +:test (up-pos -1) (-2) +:test (up-pos +42) (+127) # shifts a zero trit into a balanced ternary number up-zero [[[[[0 (4 3 2 1 0)]]]]] -:test up-zero +0 = [[[[0 3]]]] -:test up-zero +1 = +3 -:test up-zero +42 = +126 +:test (up-zero +0) ([[[[0 3]]]]) +:test (up-zero +1) (+3) +:test (up-zero +42) (+126) # shifts a specified trit into a balanced ternary number up [[[[[[5 2 1 0 (4 3 2 1 0)]]]]]] -:test up trit-neg +42 = up-neg +42 -:test up trit-pos +42 = up-pos +42 -:test up trit-zero +42 = up-zero +42 +:test (up trit-neg +42) (up-neg +42) +:test (up trit-pos +42) (up-pos +42) +:test (up trit-zero +42) (up-zero +42) # shifts the least significant trit out - basically div by 3 down [snd (0 z neg pos zero)] @@ -73,9 +73,9 @@ down [snd (0 z neg pos zero)] # negates a balanced ternary number negate [[[[[4 3 1 2 0]]]]] -:test negate +0 = +0 -:test negate -1 = +1 -:test negate +42 = -42 +:test (negate +0) (+0) +:test (negate -1) (+1) +:test (negate +42) (-42) # converts a balanced ternary number to a list of trits list! [0 z neg pos zero] @@ -93,17 +93,17 @@ strip [fst (0 z neg pos zero)] pos [0 [[pair (up-pos 1) F]]] zero [0 [[pair (0 +0 (up-zero 1)) 0]]] -:test strip [[[[0 3]]]] = +0 -:test strip [[[[2 (0 (0 (0 (0 3))))]]]] = -1 -:test strip +42 = +42 +:test (strip [[[[0 3]]]]) (+0) +:test (strip [[[[2 (0 (0 (0 (0 3))))]]]]) (-1) +:test (strip +42) (+42) # extracts least significant trit from balanced ternary numbers lst [0 trit-zero [trit-neg] [trit-pos] [trit-zero]] -:test lst +0 = trit-zero -:test lst -1 = trit-neg -:test lst +1 = trit-pos -:test lst +42 = trit-zero +:test (lst +0) (trit-zero) +:test (lst -1) (trit-neg) +:test (lst +1) (trit-pos) +:test (lst +42) (trit-zero) # extracts most significant trit from balanced ternary numbers # TODO: Find a more elegant way to do this @@ -113,34 +113,34 @@ lst [0 trit-zero [trit-neg] [trit-pos] [trit-zero]] # TODO: Fix list import loop mst [trit-zero] -:test mst +0 = trit-zero -:test mst -1 = trit-neg -:test mst +1 = trit-pos -:test mst +42 = trit-pos +:test (mst +0) (trit-zero) +:test (mst -1) (trit-neg) +:test (mst +1) (trit-pos) +:test (mst +42) (trit-pos) # returns whether balanced ternary number is negative negative? [trit-neg? (mst 0)] -:test negative? +0 = F -:test negative? -1 = T -:test negative? +1 = F -:test negative? +42 = F +:test (negative? +0) (F) +:test (negative? -1) (T) +:test (negative? +1) (F) +:test (negative? +42) (F) # returns whether balanced ternary number is positive positive? [trit-pos? (mst 0)] -:test positive? +0 = F -:test positive? -1 = F -:test positive? +1 = T -:test positive? +42 = T +:test (positive? +0) (F) +:test (positive? -1) (F) +:test (positive? +1) (T) +:test (positive? +42) (T) # checks whether balanced ternary number is zero zero? [0 T [F] [F] I] -:test zero? +0 = T -:test zero? -1 = F -:test zero? +1 = F -:test zero? +42 = F +:test (zero? +0) (T) +:test (zero? -1) (F) +:test (zero? +1) (F) +:test (zero? +42) (F) # converts the normal balanced ternary representation into abstract # -> the abstract representation is used in add/sub/mul @@ -150,18 +150,18 @@ abstract! [0 z neg pos zero] pos [[[[[1 4]]]]] zero [[[[[0 4]]]]] -:test abstract! -3 = [[[[0 [[[[2 [[[[3]]]]]]]]]]]] -:test abstract! +0 = [[[[3]]]] -:test abstract! +3 = [[[[0 [[[[1 [[[[3]]]]]]]]]]]] +:test (abstract! -3) ([[[[0 [[[[2 [[[[3]]]]]]]]]]]]) +:test (abstract! +0) ([[[[3]]]]) +:test (abstract! +3) ([[[[0 [[[[1 [[[[3]]]]]]]]]]]]) # converts the abstracted balanced ternary representation back to normal # using ω to solve recursion normal! ω rec rec [[0 +0 [up-neg ([3 3 0] 0)] [up-pos ([3 3 0] 0)] [up-zero ([3 3 0] 0)]]] -:test normal! [[[[3]]]] = +0 -:test normal! (abstract! +42) = +42 -:test normal! (abstract! -42) = -42 +:test (normal! [[[[3]]]]) (+0) +:test (normal! (abstract! +42)) (+42) +:test (normal! (abstract! -42)) (-42) # checks whether two balanced ternary numbers are equal # -> ignores leading 0s! @@ -172,14 +172,16 @@ eq? [[abs 1 (abstract! 0)]] zero [[0 (1 0) [F] [F] [2 0]]] abs [0 z neg pos zero] -:test eq? -42 -42 = T -:test eq? -1 -1 = T -:test eq? -1 +0 = F -:test eq? +0 +0 = T -:test eq? +1 +0 = F -:test eq? +1 +1 = T -:test eq? +42 +42 = T -:test eq? [[[[(1 (0 (0 (0 (0 3)))))]]]] +1 = T +(=?) eq? + +:test (-42 =? -42) (T) +:test (-1 =? -1) (T) +:test (-1 =? +0) (F) +:test (+0 =? +0) (T) +:test (+1 =? +0) (F) +:test (+1 =? +1) (T) +:test (+42 =? +42) (T) +:test ([[[[(1 (0 (0 (0 (0 3)))))]]]] =? +1) (T) # I believe Mogensen's Paper has an error in its inc/dec/add/mul/eq definitions. # They use 3 instead of 2 abstractions in the functions, also we use switched @@ -196,11 +198,11 @@ inc [snd (0 z neg pos zero)] # adds +1 to a balanced ternary number and strips leading 0s sinc [strip (inc 0)] -:test eq? (inc -42) -41 = T -:test eq? (inc -1) +0 = T -:test eq? (inc +0) +1 = T -:test eq? (inc (inc (inc (inc (inc +0))))) +5 = T -:test eq? (inc +42) +43 = T +:test (eq? (inc -42) -41) (T) +:test (eq? (inc -1) +0) (T) +:test (eq? (inc +0) +1) (T) +:test (eq? (inc (inc (inc (inc (inc +0))))) +5) (T) +:test (eq? (inc +42) +43) (T) # subs +1 from a balanced ternary number (can introduce leading 0s) dec [snd (0 dec-z dec-neg dec-pos dec-zero)] @@ -212,11 +214,11 @@ dec [snd (0 dec-z dec-neg dec-pos dec-zero)] # subs +1 from a balanced ternary number and strips leading 0s sdec [strip (dec 0)] -:test eq? (dec -42) -43 = T -:test eq? (dec +0) -1 = T -:test eq? (dec (dec (dec (dec (dec +5))))) +0 = T -:test eq? (dec +1) +0 = T -:test eq? (dec +42) +41 = T +:test (eq? (dec -42) -43) (T) +:test (eq? (dec +0) -1) (T) +:test (eq? (dec (dec (dec (dec (dec +5))))) +0) (T) +:test (eq? (dec +1) +0) (T) +:test (eq? (dec +42) +41) (T) # adds two balanced ternary numbers (can introduce leading 0s) add [[abs 1 (abstract! 0)]] @@ -232,15 +234,17 @@ add [[abs 1 (abstract! 0)]] z [[0 (dec (normal! 1)) (inc (normal! 1)) (normal! 1)]] abs [c (0 z a-neg a-pos a-zero)] +(+) add + # adds two balanced ternary numbers and strips leading 0s sadd [[strip (add 1 0)]] -:test eq? (add -42 -1) -43 = T -:test eq? (add -5 +6) +1 = T -:test eq? (add -1 +0) -1 = T -:test eq? (add +0 +0) +0 = T -:test eq? (add +1 +2) +3 = T -:test eq? (add +42 +1) +43 = T +:test (eq? (add -42 -1) -43) (T) +:test (eq? (add -5 +6) +1) (T) +:test (eq? (add -1 +0) -1) (T) +:test (eq? (add +0 +0) +0) (T) +:test (eq? (add +1 +2) +3) (T) +:test (eq? (add +42 +1) +43) (T) # subs two balanced ternary numbers (can introduce leading 0s) sub [[add 1 (negate 0)]] @@ -248,28 +252,34 @@ sub [[add 1 (negate 0)]] # subs two balanced ternary numbers and strips leading 0s ssub [[strip (sub 1 0)]] -:test eq? (sub -42 -1) -41 = T -:test eq? (sub -5 +6) -11 = T -:test eq? (sub -1 +0) -1 = T -:test eq? (sub +0 +0) +0 = T -:test eq? (sub +1 +2) -1 = T -:test eq? (sub +42 +1) +41 = T +:test (eq? (sub -42 -1) -41) (T) +:test (eq? (sub -5 +6) -11) (T) +:test (eq? (sub -1 +0) -1) (T) +:test (eq? (sub +0 +0) +0) (T) +:test (eq? (sub +1 +2) -1) (T) +:test (eq? (sub +42 +1) +41) (T) # returns whether number is greater than other number gre? [[negative? (sub 0 1)]] +(>?) gre? + # returns whether number is less than or equal to other number leq? [[not (gre? 1 0)]] +(<=?) leq? + # muls two balanced ternary numbers (can introduce leading 0s) mul [[1 +0 neg pos zero]] neg [sub (up-zero 0) 1] pos [add (up-zero 0) 1] zero [up-zero 0] +(*) mul + smul [[strip (mul 1 0)]] -:test eq? (mul +42 +0) +0 = T -:test eq? (mul -1 +42) -42 = T -:test eq? (mul +3 +11) +33 = T -:test eq? (mul +42 -4) -168 = T +:test (eq? (mul +42 +0) +0) (T) +:test (eq? (mul -1 +42) -42) (T) +:test (eq? (mul +3 +11) +33) (T) +:test (eq? (mul +42 -4) -168) (T) |