dhall-nix-lib/Integer/multiply.dhall

37 lines
1 KiB
Text

--| `multiply m n` computes `m * n`.
let nonPositive = ./nonPositive.dhall
let multiplyNonNegative =
λ(x : Integer) →
λ(y : Integer) →
Natural/toInteger (Integer/clamp x * Integer/clamp y)
let multiply
: Integer → Integer → Integer
= λ(m : Integer) →
λ(n : Integer) →
if nonPositive m
then if nonPositive n
then multiplyNonNegative (Integer/negate m) (Integer/negate n)
else Integer/negate (multiplyNonNegative (Integer/negate m) n)
else if nonPositive n
then Integer/negate (multiplyNonNegative m (Integer/negate n))
else multiplyNonNegative m n
let example0 = assert : multiply +3 +5 ≡ +15
let example1 = assert : multiply -3 +5 ≡ -15
let example2 = assert : multiply -3 -5 ≡ +15
let example3 = assert : multiply +0 +5 ≡ +0
let example4 = assert : multiply +5 +0 ≡ +0
let example5 = assert : multiply +0 +0 ≡ +0
let example6 = assert : multiply +1 +5 ≡ +5
let example7 = assert : multiply +3 -1 ≡ -3
in multiply