Division and fixed pred
This commit is contained in:
35
lib/base.tri
35
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
|
||||
-- ---------------------------------------------------------------------------
|
||||
|
||||
65
test/Spec.hs
65
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
|
||||
|
||||
Reference in New Issue
Block a user