PreludeNat.agda 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. module PreludeNat where
  2. open import AlonzoPrelude
  3. import PreludeBool as Bool
  4. open Bool
  5. infix 40 _==_ _<_
  6. infixl 60 _+_ _-_
  7. infixl 70 _*_
  8. infixr 80 _^_
  9. infix 100 _!
  10. _+_ : Nat -> Nat -> Nat
  11. zero + m = m
  12. suc n + m = suc (n + m)
  13. _-_ : Nat -> Nat -> Nat
  14. zero - m = zero
  15. suc n - zero = suc n
  16. suc n - suc m = n - m
  17. _*_ : Nat -> Nat -> Nat
  18. zero * m = zero
  19. suc n * m = m + n * m
  20. _^_ : Nat -> Nat -> Nat
  21. n ^ zero = 1
  22. n ^ suc m = n * n ^ m
  23. _! : Nat -> Nat
  24. zero ! = 1
  25. suc n ! = suc n * n !
  26. {-# BUILTIN NATPLUS _+_ #-}
  27. {-# BUILTIN NATMINUS _-_ #-}
  28. {-# BUILTIN NATTIMES _*_ #-}
  29. divSuc : Nat -> Nat -> Nat
  30. divSuc zero _ = zero
  31. divSuc (suc n) m = 1 + divSuc (n - m) m
  32. modSuc : Nat -> Nat -> Nat
  33. modSuc zero _ = zero
  34. modSuc (suc n) m = modSuc (n - m) m
  35. {-# BUILTIN NATDIVSUC divSuc #-}
  36. {-# BUILTIN NATMODSUC modSuc #-}
  37. div : Nat -> Nat -> Nat
  38. div n zero = zero
  39. div n (suc m) = divSuc n m
  40. mod : Nat -> Nat -> Nat
  41. mod n zero = zero
  42. mod n (suc m) = modSuc n m
  43. gcd : Nat -> Nat -> Nat
  44. gcd a 0 = a
  45. gcd a b = gcd b (mod a b)
  46. lcm : Nat -> Nat -> Nat
  47. lcm a b = div (a * b) (gcd a b)
  48. _==_ : Nat -> Nat -> Bool
  49. zero == zero = true
  50. zero == suc _ = false
  51. suc _ == zero = false
  52. suc n == suc m = n == m
  53. _<_ : Nat -> Nat -> Bool
  54. n < zero = false
  55. zero < suc m = true
  56. suc n < suc m = n < m
  57. _≤_ : Nat -> Nat -> Bool
  58. n ≤ m = n < suc m
  59. _>_ = flip _<_
  60. _≥_ = flip _≤_
  61. even : Nat -> Bool
  62. even n = mod n 2 == 0
  63. odd : Nat -> Bool
  64. odd n = mod n 2 == 1
  65. _≡_ : Nat -> Nat -> Set
  66. n ≡ m = IsTrue (n == m)
  67. {-# BUILTIN NATEQUALS _==_ #-}
  68. {-# BUILTIN NATLESS _<_ #-}