Division and fixed pred

This commit is contained in:
2026-08-30 09:56:22 -05:00
parent 079643e2b7
commit dfcaf14404
2 changed files with 88 additions and 12 deletions

View File

@@ -178,18 +178,15 @@ andLazy? = (a bK :
pred = y (self : triage pred = y (self : triage
0 0
(_ : 0) 0
(bit rest : (bit rest :
matchBool ifLazy
(matchBool bit
(_ : matchBool
(t t rest)
0 0
(pair 0 rest) rest)
(equal? rest 0)) (_ : t (t t) (self rest))))
(matchBool
0
(pair 1 (self rest))
(equal? rest 0))
bit))
isZero? = triage true (_ : false) (_ _ : false) isZero? = triage true (_ : false) (_ _ : false)
@@ -231,6 +228,24 @@ mul = y (self a b :
(_ : 0) (_ : 0)
(_ : add a (self a (pred b)))) (_ : add a (self a (pred b))))
div = y (self a b :
ifLazy
(isZero? b)
(_ : 0)
(_ : ifLazy
(lt? a b)
(_ : 0)
(_ : succ (self (sub a b) b))))
mod = y (self a b :
ifLazy
(isZero? b)
(_ : 0)
(_ : ifLazy
(lt? a b)
(_ : a)
(_ : self (sub a b) b)))
-- --------------------------------------------------------------------------- -- ---------------------------------------------------------------------------
-- Result combinators -- Result combinators
-- --------------------------------------------------------------------------- -- ---------------------------------------------------------------------------

View File

@@ -80,6 +80,7 @@ tests = testGroup "Tricu Tests"
--, parser --, parser
--, simpleEvaluation --, simpleEvaluation
--, lambdas --, lambdas
, arithmetic
, providedLibraries , providedLibraries
--, maybeTests --, maybeTests
--, fileEval --, fileEval
@@ -1193,8 +1194,8 @@ providedLibraries = testGroup "Library Tests"
result env @?= ofString "" result env @?= ofString ""
] ]
arithmeticTests :: TestTree arithmetic :: TestTree
arithmeticTests = testGroup "Arithmetic Tests" arithmetic = testGroup "Arithmetic Tests"
[ testCase "isZero? on 0" $ do [ testCase "isZero? on 0" $ do
let input = "isZero? 0" let input = "isZero? 0"
env = evalTricu allTestLibsEnv (parseTricu input) env = evalTricu allTestLibsEnv (parseTricu input)
@@ -1374,6 +1375,66 @@ arithmeticTests = testGroup "Arithmetic Tests"
let input = "isZero? (add 0 0)" let input = "isZero? (add 0 0)"
env = evalTricu allTestLibsEnv (parseTricu input) env = evalTricu allTestLibsEnv (parseTricu input)
result env @?= trueT result env @?= trueT
, testCase "div 10 3 = 3" $ do
let input = "div 10 3"
env = evalTricu allTestLibsEnv (parseTricu input)
result env @?= ofNumber 3
, testCase "div 12 4 = 3 (exact)" $ do
let input = "div 12 4"
env = evalTricu allTestLibsEnv (parseTricu input)
result env @?= ofNumber 3
, testCase "div 3 5 = 0 (divisor larger)" $ do
let input = "div 3 5"
env = evalTricu allTestLibsEnv (parseTricu input)
result env @?= ofNumber 0
, testCase "div 7 1 = 7 (identity)" $ do
let input = "div 7 1"
env = evalTricu allTestLibsEnv (parseTricu input)
result env @?= ofNumber 7
, testCase "div 0 5 = 0" $ do
let input = "div 0 5"
env = evalTricu allTestLibsEnv (parseTricu input)
result env @?= ofNumber 0
, testCase "div 5 0 = 0 (div by zero)" $ do
let input = "div 5 0"
env = evalTricu allTestLibsEnv (parseTricu input)
result env @?= ofNumber 0
, testCase "mod 10 3 = 1" $ do
let input = "mod 10 3"
env = evalTricu allTestLibsEnv (parseTricu input)
result env @?= ofNumber 1
, testCase "mod 12 4 = 0 (exact)" $ do
let input = "mod 12 4"
env = evalTricu allTestLibsEnv (parseTricu input)
result env @?= ofNumber 0
, testCase "mod 3 5 = 3 (divisor larger)" $ do
let input = "mod 3 5"
env = evalTricu allTestLibsEnv (parseTricu input)
result env @?= ofNumber 3
, testCase "mod 7 1 = 0" $ do
let input = "mod 7 1"
env = evalTricu allTestLibsEnv (parseTricu input)
result env @?= ofNumber 0
, testCase "mod 5 0 = 0 (mod by zero)" $ do
let input = "mod 5 0"
env = evalTricu allTestLibsEnv (parseTricu input)
result env @?= ofNumber 0
, testCase "div mod consistency" $ do
let input = "equal? (add (mul 3 7) 4) 25"
env = evalTricu allTestLibsEnv (parseTricu input)
result env @?= trueT
] ]
fileEval :: TestTree fileEval :: TestTree