Skip to content

Commit

Permalink
Add non-zero restriction to gcd
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianhjr authored and Ahmad Salim Al-Sibahi committed Dec 5, 2016
1 parent 327a423 commit d89f3fa
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions libs/prelude/Prelude/Nat.idr
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ hyper (S pn) a (S pb) = hyper pn a (hyper (S pn) a pb)
-- Comparisons
--------------------------------------------------------------------------------

||| Proofs that `n` or `m` is not equal to Z
data NotBothZero : (n, m : Nat) -> Type where
LeftIsNotZero : NotBothZero (S n) m
RightIsNotZero : NotBothZero n (S m)


||| Proofs that `n` is less than or equal to `m`
||| @ n the smaller number
||| @ m the larger number
Expand Down Expand Up @@ -386,16 +392,15 @@ log2 (S n) = log2NZ (S n) SIsNotZ
--------------------------------------------------------------------------------
-- GCD and LCM
--------------------------------------------------------------------------------
partial
gcd : Nat -> Nat -> Nat
gcd a Z = a
gcd a b = assert_total (gcd b (a `modNat` b))
gcd : (a: Nat) -> (b: Nat) -> .{auto ok: NotBothZero a b} -> Nat
gcd a Z = a
gcd Z b = b
gcd a (S b) = assert_total $ gcd (S b) (modNatNZ a (S b) SIsNotZ)

partial
lcm : Nat -> Nat -> Nat
lcm _ Z = Z
lcm Z _ = Z
lcm x y = divNat (x * y) (gcd x y)
lcm _ Z = Z
lcm Z _ = Z
lcm a (S b) = assert_total $ divNat (a * (S b)) (gcd a (S b))


--------------------------------------------------------------------------------
Expand Down

0 comments on commit d89f3fa

Please sign in to comment.