on master help sign in
Division and modulus for Nat, plus associated utilities

This adds some trivial lemmas to Prelude.Nat, as well as the following contrib modules: * Data.Nat: contains division and modulus for Nat, using well-founded recursion * Control.WellFounded: contains low-level operations for well-founded recursion

commit30364c71896d7a29bc74bf7663bdd6db4f7a0e8d authorDavid Raymond Christiansen <david@davidchristiansen.dk> dateTue 31 Mar 2015, 13:28 (12 years ago) parente9ea90d
4 files changed+271
libs/contrib/Control/WellFounded.idr+83 libs/contrib/Data/Nat.idr+172 libs/contrib/contrib.ipkg+2 libs/prelude/Prelude/Nat.idr+14
libs/contrib/Control/WellFounded.idrnew file+83
@@ -0,0 +1,83 @@
1+||| Well-founded recursion.
2+|||
3+||| This is to let us implement some functions that don't have trivial
4+||| recursion patterns over datatypes, but instead are using some
5+||| other metric of size.
6+module Control.WellFounded
7+
8+%default total
9+
10+
11+||| Accessibility: some element `x` is accessible if all `y` such that
12+||| `rel y x` are themselves accessible.
13+|||
14+||| @ a the type of elements
15+||| @ rel a relation on a
16+||| @ x the acessible element
17+data Accessible : (rel : a -> a -> Type) -> (x : a) -> Type where
18+ ||| Accessibility
19+ |||
20+ ||| @ x the accessible element
21+ ||| @ acc' a demonstration that all smaller elements are also accessible
22+ Access : (acc' : (y : a) -> rel y x -> Accessible rel y) ->
23+ Accessible rel x
24+
25+||| A relation `rel` on `a` is well-founded if all elements of `a` are
26+||| acessible.
27+|||
28+||| @ rel the well-founded relation
29+class WellFounded (rel : a -> a -> Type) where
30+ wellFounded : (x : _) -> Accessible rel x
31+
32+
33+||| A recursor over accessibility.
34+|||
35+||| This allows us to recurse over the subset of some type that is
36+||| accessible according to some relation.
37+|||
38+||| @ rel the well-founded relation
39+||| @ step how to take steps on reducing arguments
40+||| @ z the starting point
41+accRec : {rel : a -> a -> Type} ->
42+ (step : (x : a) -> ((y : a) -> rel y x -> b) -> b) ->
43+ (z : a) -> Accessible rel z -> b
44+accRec step z (Access f) =
45+ step z $ \y, lt => accRec step y (f y lt)
46+
47+||| An induction principle for accessibility.
48+|||
49+||| This allows us to recurse over the subset of some type that is
50+||| accessible according to some relation, producing a dependent type
51+||| as a result.
52+|||
53+||| @ rel the well-founded relation
54+||| @ step how to take steps on reducing arguments
55+||| @ z the starting point
56+accInd : {rel : a -> a -> Type} -> {P : a -> Type} ->
57+ (step : (x : a) -> ((y : a) -> rel y x -> P y) -> P x) ->
58+ (z : a) -> Accessible rel z -> P z
59+accInd step z (Access f) =
60+ step z $ \y, lt => accInd step y (f y lt)
61+
62+
63+||| Use well-foundedness of a relation to write terminating operations.
64+|||
65+||| @ rel a well-founded relation
66+wfRec : WellFounded rel =>
67+ (step : (x : a) -> ((y : a) -> rel y x -> b) -> b) ->
68+ a -> b
69+wfRec {rel} step x = accRec step x (wellFounded {rel} x)
70+
71+
72+||| Use well-foundedness of a relation to write proofs
73+|||
74+||| @ rel a well-founded relation
75+||| @ P the motive for the induction
76+||| @ step the induction step: take an element and its accessibility,
77+||| and give back a demonstration of P for that element,
78+||| potentially using accessibility
79+wfInd : WellFounded rel => {P : a -> Type} ->
80+ (step : (x : a) -> ((y : a) -> rel y x -> P y) -> P x) ->
81+ (x : a) -> P x
82+wfInd {rel} step x = accInd step x (wellFounded {rel} x)
83+
libs/contrib/Data/Nat.idrnew file+172
@@ -0,0 +1,172 @@
1+||| Extra utilities for working with Nats
2+module Data.Nat
3+
4+import Data.Fin
5+
6+import Control.WellFounded
7+
8+%default total
9+
10+||| A strict less-than relation on `Nat`.
11+|||
12+||| @ n the smaller number
13+||| @ m the larger number
14+data LT' : (n,m : Nat) -> Type where
15+ ||| n < 1 + n
16+ LTSucc : LT' n (S n)
17+ ||| n < m implies that n < m + 1
18+ LTStep : LT' n m -> LT' n (S m)
19+
20+%name LT' lt, lt'
21+
22+||| Nothing is strictly less than zero
23+instance Uninhabited (LT' n 0) where
24+ uninhabited LTSucc impossible
25+
26+||| Zero is less than any non-zero number.
27+LTZeroLeast : LT' Z (S n)
28+LTZeroLeast {n = Z} = LTSucc
29+LTZeroLeast {n = S n} = LTStep LTZeroLeast
30+
31+||| If n < m, then 1 + n < 1 + m
32+ltSuccSucc : LT' n m -> LT' (S n) (S m)
33+ltSuccSucc LTSucc = LTSucc
34+ltSuccSucc (LTStep lt) = LTStep $ ltSuccSucc lt
35+
36+||| If n + 1 < m, then n < m
37+lteToLt' : LTE (S n) m -> LT' n m
38+lteToLt' {n = Z} (LTESucc x) = LTZeroLeast
39+lteToLt' {n = S k} (LTESucc x) = ltSuccSucc $ lteToLt' x
40+
41+||| Convert from LT' to LTE
42+ltToLTE : LT' n m -> LTE (S n) m
43+ltToLTE LTSucc = lteRefl
44+ltToLTE (LTStep lt) = lteSucc $ ltToLTE lt
45+
46+||| Subtraction gives a result that is actually smaller.
47+minusLT' : (x,y : Nat) -> x - y `LT'` S x
48+minusLT' Z y = LTSucc
49+minusLT' (S k) Z = LTSucc
50+minusLT' (S k) (S j) = LTStep (minusLT' k j)
51+
52+||| Strict less-than is well-founded, with the cascade stopping at
53+||| zero (because there's nothing less than zero). This can't be done
54+||| for LTE, because that one doesn't stop at zero (because `LTE 0 0`
55+||| is inhabited).
56+instance WellFounded LT' where
57+ wellFounded x = Access (acc x)
58+ where
59+ ||| Show accessibility by induction on the structure of the LT' witness
60+ acc : (x, y : Nat) -> LT' y x -> Accessible LT' y
61+ -- Zero is vacuously accessible: there's nothing smaller to check
62+ acc Z y lt = absurd lt
63+ -- If the element being accessed is one smaller, we're done
64+ acc (S y) y LTSucc = Access (acc y)
65+ -- If the element is more than one smaller, we need to go further
66+ acc (S k) y (LTStep smaller) = acc k y smaller
67+
68+||| The result of division on natural numbers.
69+|||
70+||| @ dividend the dividend
71+||| @ divisor the divisor
72+data DivMod : (dividend, divisor : Nat) -> Type where
73+ ||| The result of division, with a quotient and a remainder.
74+ |||
75+ ||| @ dividend the dividend
76+ ||| @ divisor the divisor
77+ ||| @ quotient the quotient
78+ ||| @ remainder the remainder (bounded by the divisor)
79+ ||| @ ok the fact that this is, in fact, a result of division
80+ DivModRes : {dividend, divisor : Nat} ->
81+ (quotient : Nat) -> (remainder : Fin divisor) ->
82+ (ok : dividend = finToNat remainder + quotient * divisor) ->
83+ DivMod dividend divisor
84+
85+
86+||| Any natural number can be a `Fin` where the bound is itself plus some difference.
87+private
88+toFin : (x : Nat) -> (diff : Nat) -> Fin (plus x (S diff))
89+toFin Z diff = FZ
90+toFin (S k) diff = FS $ toFin k diff
91+
92+||| Converting to a `Fin` and back to `Nat` preserves the input. This is a correctness proof for `toFin`.
93+private
94+toFinAndBack : (x : Nat) -> (diff : Nat) -> finToNat (toFin x diff) = x
95+toFinAndBack Z diff = Refl
96+toFinAndBack (S k) diff = cong (toFinAndBack k diff)
97+
98+||| This property is not the case.
99+|||
100+||| Use this to ensure properties in implicit arguments.
101+|||
102+||| @ p the property that doesn't hold
103+Nope : Dec p -> Type
104+Nope (Yes _) = Void
105+Nope (No _) = ()
106+
107+||| The accessibilty predicate used for division.
108+private
109+accPlusLT' : LT' (S j) (S (plus k (S j)))
110+accPlusLT' {j = Z} {k = Z} = LTSucc
111+accPlusLT' {j = Z} {k = S k} = LTStep (accPlusLT' {j = Z} {k = k})
112+accPlusLT' {j = S j} {k = k} = rewrite sym $ plusSuccRightSucc k (S j) in
113+ ltSuccSucc accPlusLT'
114+
115+
116+||| Division and modulus on `Nat`, inspired by the definition in the
117+||| Agda standard library.
118+|||
119+||| This uses well-founded recursion on `LT'`.
120+|||
121+||| @ dividend the dividend
122+||| @ divisor the divisor
123+||| @ nonzero division by zero is nonsense
124+total -- yay!
125+divMod : (dividend, divisor : Nat) ->
126+ {auto nonzero : Nope (decEq divisor Z)} ->
127+ DivMod dividend divisor
128+divMod _ Z {nonzero} = absurd nonzero
129+divMod Z (S k) = DivModRes 0 FZ Refl
130+divMod (S j) (S k) {nonzero} = wfInd {P=P} stepDiv (S j) (S k) nonzero
131+ where
132+ ||| The goal passed to the accessibility eliminator.
133+ |||
134+ ||| This is responsible for generating the goal in the
135+ ||| well-founded fixed point.
136+ |||
137+ ||| @ dividend the dividend to recurse over
138+ P : (dividend : Nat) -> Type
139+ P dividend = (divisor : Nat) -> (nonzero : Nope (decEq divisor Z)) -> DivMod dividend divisor
140+
141+
142+ ||| Well-founded recursion needs a recursion operator.
143+ |||
144+ ||| @ dividend the dividend we are recursing over
145+ ||| @ rec the recursive call provided by the well-founded induction operator
146+ stepDiv : (dividend : Nat) -> (rec : (d' : Nat) -> LT' d' dividend -> P d') -> P dividend
147+ -- We need not consider division by zero
148+ stepDiv dividend rec Z nonzero = absurd nonzero
149+ -- Dividing zero by anyting else gives 0, with 0 remainder
150+ stepDiv Z rec (S k) nonzero = DivModRes 0 0 Refl
151+ -- To divide two non-zero values, we must know which is larger
152+ stepDiv (S j) rec (S k) nonzero with (cmp j k)
153+ -- n / n = 1 remainder 0
154+ stepDiv (S j) rec (S j) nonzero | CmpEQ =
155+ DivModRes 1 0 (sym $ cong (plusZeroRightNeutral j))
156+ -- if n < m, then m = n + r, and the quotient is 0 with remainder r
157+ stepDiv (S j) rec (S (plus j (S diff))) nonzero | CmpLT diff =
158+ DivModRes 0 (toFin (S j) diff) $
159+ rewrite toFinAndBack j diff
160+ in sym $ cong (plusZeroRightNeutral j)
161+ -- if n > m, then n = m + d, and the quotient is 1 + ((n - m) / m) with the same remainder
162+ stepDiv (S (plus k (S diff))) rec (S k) nonzero | CmpGT diff =
163+ let res = rec (S diff) accPlusLT' (S k) ()
164+ in case res of
165+ DivModRes quotient remainder ok =>
166+ DivModRes (S quotient) remainder $
167+ rewrite plusAssociative (finToNat remainder) (S k) (mult quotient (S k)) in
168+ rewrite plusCommutative (finToNat remainder) (S k) in
169+ rewrite sym $ plusAssociative (S k) (finToNat remainder) (mult quotient (S k)) in
170+ rewrite ok in Refl
171+
172+
libs/contrib/contrib.ipkg+2
@@ -3,9 +3,11 @@ package contrib
33opts = "--nobasepkgs --total -i ../prelude -i ../base"
44modules = Control.Algebra,
55 Control.Isomorphism.Primitives,
6+ Control.WellFounded,
67 Classes.Verified,
78 Data.Fun, Data.Rel,
89 Data.Hash, Data.Matrix,
10+ Data.Nat,
911 Data.ZZ, Data.Sign,
1012 Data.BoundedList,
1113 Data.Heap,
libs/prelude/Prelude/Nat.idr+14
@@ -99,6 +99,9 @@ data LTE : (n, m : Nat) -> Type where
9999 ||| If n <= m, then n + 1 <= m + 1
100100 LTESucc : LTE left right -> LTE (S left) (S right)
101101
102+instance Uninhabited (LTE (S n) Z) where
103+ uninhabited LTEZero impossible
104+
102105||| Greater than or equal to
103106total GTE : Nat -> Nat -> Type
104107GTE left right = LTE right left
@@ -127,6 +130,17 @@ isLTE (S k) (S j) with (isLTE k j)
127130 isLTE (S k) (S j) | (Yes prf) = Yes (LTESucc prf)
128131 isLTE (S k) (S j) | (No contra) = No (contra . fromLteSucc)
129132
133+||| `LTE` is reflexive.
134+lteRefl : LTE n n
135+lteRefl {n = Z} = LTEZero
136+lteRefl {n = S k} = LTESucc lteRefl
137+
138+||| n < m implies n < m + 1
139+lteSucc : LTE n m -> LTE n (S m)
140+lteSucc LTEZero = LTEZero
141+lteSucc (LTESucc x) = LTESucc (lteSucc x)
142+
143+
130144||| Boolean test than one Nat is less than or equal to another
131145total lte : Nat -> Nat -> Bool
132146lte Z right = True