From dfcaf14404085898345781f09b0ee4576c7f9059 Mon Sep 17 00:00:00 2001 From: James Eversole Date: Sun, 30 Aug 2026 09:56:22 -0500 Subject: [PATCH] Division and fixed pred --- lib/base.tri | 35 ++++++++++++++++++++-------- test/Spec.hs | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 88 insertions(+), 12 deletions(-) diff --git a/lib/base.tri b/lib/base.tri index 7955361..620f7a4 100644 --- a/lib/base.tri +++ b/lib/base.tri @@ -178,18 +178,15 @@ andLazy? = (a bK : pred = y (self : triage 0 - (_ : 0) + 0 (bit rest : - matchBool - (matchBool + ifLazy + bit + (_ : matchBool + (t t rest) 0 - (pair 0 rest) - (equal? rest 0)) - (matchBool - 0 - (pair 1 (self rest)) - (equal? rest 0)) - bit)) + rest) + (_ : t (t t) (self rest)))) isZero? = triage true (_ : false) (_ _ : false) @@ -231,6 +228,24 @@ mul = y (self a b : (_ : 0) (_ : 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 -- --------------------------------------------------------------------------- diff --git a/test/Spec.hs b/test/Spec.hs index 5a50522..db8d068 100644 --- a/test/Spec.hs +++ b/test/Spec.hs @@ -80,6 +80,7 @@ tests = testGroup "Tricu Tests" --, parser --, simpleEvaluation --, lambdas + , arithmetic , providedLibraries --, maybeTests --, fileEval @@ -1193,8 +1194,8 @@ providedLibraries = testGroup "Library Tests" result env @?= ofString "" ] -arithmeticTests :: TestTree -arithmeticTests = testGroup "Arithmetic Tests" +arithmetic :: TestTree +arithmetic = testGroup "Arithmetic Tests" [ testCase "isZero? on 0" $ do let input = "isZero? 0" env = evalTricu allTestLibsEnv (parseTricu input) @@ -1374,6 +1375,66 @@ arithmeticTests = testGroup "Arithmetic Tests" let input = "isZero? (add 0 0)" env = evalTricu allTestLibsEnv (parseTricu input) 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