@@ -10,9 +10,12 @@ import Prelude.Classes
10%access public
10%access public
11%default total
11%default total
13+||| Unary natural numbers
13−%elim data Nat
14+%elim data Nat =
17-- name hints for interactive editing
20-- name hints for interactive editing
18%name Nat k,j,i,n,m
21%name Nat k,j,i,n,m
@@ -33,14 +36,19 @@ isSucc (S n) = True
33-- Basic arithmetic functions
36-- Basic arithmetic functions
34--------------------------------------------------------------------------------
37--------------------------------------------------------------------------------
39+||| Add two natural numbers.
40+||| @ n the number to case-split on
41+||| @ m the other number
36−total plus : Nat -> Nat -> Nat
42+total plus : (n, m : Nat) -> Nat
37plus Z right = right
43plus Z right = right
38plus (S left) right = S (plus left right)
44plus (S left) right = S (plus left right)
46+||| Multiply natural numbers
40total mult : Nat -> Nat -> Nat
47total mult : Nat -> Nat -> Nat
41mult Z right = Z
48mult Z right = Z
42mult (S left) right = plus right $ mult left right
49mult (S left) right = plus right $ mult left right
51+||| Convert an Integer to a Nat, mapping negative numbers to 0
44fromIntegerNat : Integer -> Nat
52fromIntegerNat : Integer -> Nat
45fromIntegerNat 0 = Z
53fromIntegerNat 0 = Z
46fromIntegerNat n =
54fromIntegerNat n =
@@ -49,15 +57,18 @@ fromIntegerNat n =
60+||| Convert a Nat to an Integer
52toIntegerNat : Nat -> Integer
61toIntegerNat : Nat -> Integer
53toIntegerNat Z = 0
62toIntegerNat Z = 0
54toIntegerNat (S k) = 1 + toIntegerNat k
63toIntegerNat (S k) = 1 + toIntegerNat k
65+||| Subtract natural numbers. If the second number is larger than the first, return 0.
56total minus : Nat -> Nat -> Nat
66total minus : Nat -> Nat -> Nat
57minus Z right = Z
67minus Z right = Z
58minus left Z = left
68minus left Z = left
59minus (S left) (S right) = minus left right
69minus (S left) (S right) = minus left right
71+||| Exponentiation of natural numbers
61total power : Nat -> Nat -> Nat
72total power : Nat -> Nat -> Nat
62power base Z = S Z
73power base Z = S Z
63power base (S exp) = mult base $ power base exp
74power base (S exp) = mult base $ power base exp
@@ -74,33 +85,46 @@ hyper (S pn) a (S pb) = hyper pn a (hyper (S pn) a pb)
74-- Comparisons
85-- Comparisons
75--------------------------------------------------------------------------------
86--------------------------------------------------------------------------------
88+||| Proofs that `n` is less than or equal to `m`
89+||| @ n the smaller number
90+||| @ m the larger number
77−data LTE : Nat -> Nat -> Type where
91+data LTE : (n, m : Nat) -> Type where
92+ ||| Zero is the smallest Nat
78 lteZero : LTE Z right
93 lteZero : LTE Z right
94+ ||| If n <= m, then n + 1 <= m + 1
79 lteSucc : LTE left right -> LTE (S left) (S right)
95 lteSucc : LTE left right -> LTE (S left) (S right)
97+||| Greater than or equal to
81total GTE : Nat -> Nat -> Type
98total GTE : Nat -> Nat -> Type
82GTE left right = LTE right left
99GTE left right = LTE right left
84total LT : Nat -> Nat -> Type
102total LT : Nat -> Nat -> Type
85LT left right = LTE (S left) right
103LT left right = LTE (S left) right
105+||| Strict greater than
87total GT : Nat -> Nat -> Type
106total GT : Nat -> Nat -> Type
88GT left right = LT right left
107GT left right = LT right left
109+||| Boolean test than one Nat is less than or equal to another
90total lte : Nat -> Nat -> Bool
110total lte : Nat -> Nat -> Bool
91lte Z right = True
111lte Z right = True
92lte left Z = False
112lte left Z = False
93lte (S left) (S right) = lte left right
113lte (S left) (S right) = lte left right
115+||| Boolean test than one Nat is greater than or equal to another
95total gte : Nat -> Nat -> Bool
116total gte : Nat -> Nat -> Bool
96gte left right = lte right left
117gte left right = lte right left
119+||| Boolean test than one Nat is strictly less than another
98total lt : Nat -> Nat -> Bool
120total lt : Nat -> Nat -> Bool
99lt left right = lte (S left) right
121lt left right = lte (S left) right
123+||| Boolean test than one Nat is strictly greater than another
101total gt : Nat -> Nat -> Bool
124total gt : Nat -> Nat -> Bool
102gt left right = lt right left
125gt left right = lt right left
127+||| Find the least of two natural numbers
104total minimum : Nat -> Nat -> Nat
128total minimum : Nat -> Nat -> Nat
105minimum left right =
129minimum left right =
106 if lte left right then
130 if lte left right then
@@ -108,6 +132,7 @@ minimum left right =
135+||| Find the greatest of two natural numbers
111total maximum : Nat -> Nat -> Nat
136total maximum : Nat -> Nat -> Nat
112maximum left right =
137maximum left right =
113 if lte left right then
138 if lte left right then
@@ -148,9 +173,11 @@ instance MinBound Nat where
148instance Cast Integer Nat where
173instance Cast Integer Nat where
149 cast = fromInteger
174 cast = fromInteger
176+||| A wrapper for Nat that specifies the semigroup and monad instances that use (+)
151record Multiplicative : Type where
177record Multiplicative : Type where
152 getMultiplicative : Nat -> Multiplicative
178 getMultiplicative : Nat -> Multiplicative
180+||| A wrapper for Nat that specifies the semigroup and monad instances that use (*)
154record Additive : Type where
181record Additive : Type where
155 getAdditive : Nat -> Additive
182 getAdditive : Nat -> Additive
@@ -210,6 +237,7 @@ instance Cast Nat Int where
210-- Auxilliary notions
237-- Auxilliary notions
211--------------------------------------------------------------------------------
238--------------------------------------------------------------------------------
240+||| The predecessor of a natural number. `pred Z` is `Z`.
213total pred : Nat -> Nat
241total pred : Nat -> Nat
214pred Z = Z
242pred Z = Z
215pred (S n) = n
243pred (S n) = n
@@ -218,11 +246,13 @@ pred (S n) = n
218-- Fibonacci and factorial
246-- Fibonacci and factorial
219--------------------------------------------------------------------------------
247--------------------------------------------------------------------------------
249+||| Fibonacci numbers
221total fib : Nat -> Nat
250total fib : Nat -> Nat
223fib (S Z) = S Z
252fib (S Z) = S Z
224fib (S (S n)) = fib (S n) + fib n
253fib (S (S n)) = fib (S n) + fib n
255+||| Factorial function
226total fact : Nat -> Nat
256total fact : Nat -> Nat
227fact Z = S Z
257fact Z = S Z
228fact (S n) = (S n) * fact n
258fact (S n) = (S n) * fact n
@@ -283,10 +313,13 @@ lcm x y = divNat (x * y) (gcd x y)
283--------------------------------------------------------------------------------
313--------------------------------------------------------------------------------
317+||| S preserves equality
286total eqSucc : (left : Nat) -> (right : Nat) -> (p : left = right) ->
318total eqSucc : (left : Nat) -> (right : Nat) -> (p : left = right) ->
287 S left = S right
319 S left = S right
288eqSucc left _ refl = refl
320eqSucc left _ refl = refl
290total succInjective : (left : Nat) -> (right : Nat) -> (p : S left = S right) ->
323total succInjective : (left : Nat) -> (right : Nat) -> (p : S left = S right) ->
291 left = right
324 left = right
292succInjective left _ refl = refl
325succInjective left _ refl = refl