on master help sign in
Merge pull request #10 from dpmulligan/master

Promised fix to nat.idr file, and minor change to algebra.idr

commit7cca84f04a04610e0ef5aece6121acb6cf064fd9 authorEdwin Brady <eb@cs.st-andrews.ac.uk> dateWed 15 Feb 2012, 04:20 (15 years ago) parent0432a705521977
2 files changed+143−83
lib/prelude/algebra.idr+56−6 lib/prelude/nat.idr+87−77
lib/prelude/algebra.idr+56−6
@@ -2,12 +2,16 @@ module prelude.algebra
22
33import builtins
44
5+-- XXX: change?
6+infixl 6 <->
7+infixl 6 <+>
8+infixl 6 <*>
9+
510%access public
611
7−-- XXX: change?
8−infix 6 <->
9−infix 6 <+>
10−infix 6 <*>
12+--------------------------------------------------------------------------------
13+-- A modest class hierarchy
14+--------------------------------------------------------------------------------
1115
1216-- Sets equipped with a single binary operation that is associative. Must
1317-- satisfy the following laws:
@@ -16,6 +20,9 @@ infix 6 <*>
1620class Semigroup a where
1721 (<+>) : a -> a -> a
1822
23+class Semigroup a => VerifiedSemigroup a where
24+ semigroupOpIsAssociative : (l, c, r : a) -> l <+> (c <+> r) = (l <+> c) <+> r
25+
1926-- Sets equipped with a single binary operation that is associative, along with
2027-- a neutral element for that binary operation. Must satisfy the following
2128-- laws:
@@ -27,6 +34,10 @@ class Semigroup a where
2734class Semigroup a => Monoid a where
2835 neutral : a
2936
37+class (VerifiedSemigroup a, Monoid a) => VerifiedMonoid a where
38+ monoidNeutralIsNeutralL : (l : a) -> l <+> neutral = l
39+ monoidNeutralIsNeutralR : (r : a) -> neutral <+> r = r
40+
3041-- Sets equipped with a single binary operation that is associative, along with
3142-- a neutral element for that binary operation and inverses for all elements.
3243-- Must satisfy the following laws:
@@ -41,6 +52,10 @@ class Semigroup a => Monoid a where
4152class Monoid a => Group a where
4253 inverse : a -> a
4354
55+class (VerifiedMonoid a, Group a) => VerifiedGroup a where
56+ groupInverseIsInverseL : (l : a) -> l <+> inverse l = neutral
57+ groupInverseIsInverseR : (r : a) -> inverse r <+> r = neutral
58+
4459(<->) : Group a => a -> a -> a
4560(<->) left right = left <+> (inverse right)
4661
@@ -59,6 +74,9 @@ class Monoid a => Group a where
5974-- forall a, inverse a <+> a == neutral
6075class Group a => AbelianGroup a where { }
6176
77+class (VerifiedGroup a, AbelianGroup a) => VerifiedAbelianGroup a where
78+ abelianGroupOpIsCommutative : (l, r : a) -> l <+> r = r <+> l
79+
6280-- Sets equipped with two binary operations, one associative and commutative
6381-- supplied with a neutral element, and the other associative, with
6482-- distributivity laws relating the two operations. Must satisfy the following
@@ -81,6 +99,11 @@ class Group a => AbelianGroup a where { }
8199class AbelianGroup a => Ring a where
82100 (<*>) : a -> a -> a
83101
102+class (VerifiedAbelianGroup a, Ring a) => VerifiedRing a where
103+ ringOpIsAssociative : (l, c, r : a) -> l <*> (c <*> r) = (l <*> c) <*> r
104+ ringOpIsDistributiveL : (l, c, r : a) -> l <*> (c <+> r) = (l <*> c) <+> (l <*> r)
105+ ringOpIsDistributiveR : (l, c, r : a) -> (l <+> c) <*> r = (l <*> r) <+> (l <*> c)
106+
84107-- Sets equipped with two binary operations, one associative and commutative
85108-- supplied with a neutral element, and the other associative supplied with a
86109-- neutral element, with distributivity laws relating the two operations. Must
@@ -106,6 +129,10 @@ class AbelianGroup a => Ring a where
106129class Ring a => RingWithUnity a where
107130 unity : a
108131
132+class (VerifiedRing a, RingWithUnity a) => VerifiedRingWithUnity a where
133+ ringWithUnityIsUnityL : (l : a) -> l <*> unity = l
134+ ringWithUnityIsUnityR : (r : a) -> unity <*> r = r
135+
109136-- Sets equipped with a binary operation that is commutative, associative and
110137-- idempotent. Must satisfy the following laws:
111138-- Associativity of join:
@@ -118,6 +145,11 @@ class Ring a => RingWithUnity a where
118145class JoinSemilattice a where
119146 join : a -> a -> a
120147
148+class JoinSemilattice a => VerifiedJoinSemilattice a where
149+ joinSemilatticeJoinIsAssociative : (l, c, r : a) -> join l (join c r) = join (join l c) r
150+ joinSemilatticeJoinIsCommutative : (l, r : a) -> join l r = join r l
151+ joinSemilatticeJoinIsIdempotent : (e : a) -> join e e = e
152+
121153-- Sets equipped with a binary operation that is commutative, associative and
122154-- idempotent. Must satisfy the following laws:
123155-- Associativity of meet:
@@ -130,6 +162,11 @@ class JoinSemilattice a where
130162class MeetSemilattice a where
131163 meet : a -> a -> a
132164
165+class MeetSemilattice a => VerifiedMeetSemilattice a where
166+ meetSemilatticeMeetIsAssociative : (l, c, r : a) -> meet l (meet c r) = meet (meet l c) r
167+ meetSemilatticeMeetIsCommutative : (l, r : a) -> meet l r = meet r l
168+ meetSemilatticeMeetIsIdempotent : (e : a) -> meet e e = e
169+
133170-- Sets equipped with a binary operation that is commutative, associative and
134171-- idempotent and supplied with a neutral element. Must satisfy the following
135172-- laws:
@@ -139,13 +176,16 @@ class MeetSemilattice a where
139176-- forall a b, join a b == join b a
140177-- Idempotency of join:
141178-- forall a, join a a == a
142−-- Neutral for join:
179+-- Bottom:
143180-- forall a, join a bottom == bottom
144181-- Join semilattices capture the notion of sets with a "least upper bound"
145182-- equipped with a "bottom" element.
146183class JoinSemilattice a => BoundedJoinSemilattice a where
147184 bottom : a
148185
186+class (VerifiedJoinSemilattice a, BoundedJoinSemilattice a) => VerifiedBoundedJoinSemilattice a where
187+ boundedJoinSemilatticeBottomIsBottom : (e : a) -> join e bottom = bottom
188+
149189-- Sets equipped with a binary operation that is commutative, associative and
150190-- idempotent and supplied with a neutral element. Must satisfy the following
151191-- laws:
@@ -155,13 +195,16 @@ class JoinSemilattice a => BoundedJoinSemilattice a where
155195-- forall a b, meet a b == meet b a
156196-- Idempotency of meet:
157197-- forall a, meet a a == a
158−-- Neutral for meet:
198+-- Top:
159199-- forall a, meet a top == top
160200-- Meet semilattices capture the notion of sets with a "greatest lower bound"
161201-- equipped with a "top" element.
162202class MeetSemilattice a => BoundedMeetSemilattice a where
163203 top : a
164204
205+class (VerifiedMeetSemilattice a, BoundedMeetSemilattice a) => VerifiedBoundedMeetSemilattice a where
206+ boundedMeetSemilatticeTopIsTop : (e : a) -> meet e top = top
207+
165208-- Sets equipped with two binary operations that are both commutative,
166209-- associative and idempotent, along with absorbtion laws for relating the two
167210-- binary operations. Must satisfy the following:
@@ -179,6 +222,10 @@ class MeetSemilattice a => BoundedMeetSemilattice a where
179222-- forall a b, join a (meet a b) == a
180223class (JoinSemilattice a, MeetSemilattice a) => Lattice a where { }
181224
225+class (VerifiedJoinSemilattice a, VerifiedMeetSemilattice a) => VerifiedLattice a where
226+ latticeMeetAbsorbsJoin : (l, r : a) -> meet l (join l r) = l
227+ latticeJoinAbsorbsMeet : (l, r : a) -> join l (meet l r) = l
228+
182229-- Sets equipped with two binary operations that are both commutative,
183230-- associative and idempotent and supplied with neutral elements, along with
184231-- absorbtion laws for relating the two binary operations. Must satisfy the
@@ -199,6 +246,9 @@ class (JoinSemilattice a, MeetSemilattice a) => Lattice a where { }
199246-- forall a, meet a top == top
200247-- forall a, join a bottom == bottom
201248class (BoundedJoinSemilattice a, BoundedMeetSemilattice a) => BoundedLattice a where { }
249+
250+class (VerifiedBoundedJoinSemilattice a, VerifiedBoundedMeetSemilattice a, VerifiedLattice a) => VerifiedBoundedLattice a where { }
251+
202252
203253-- XXX todo:
204254-- Fields and vector spaces.
lib/prelude/nat.idr+87−77
@@ -44,6 +44,51 @@ total power : Nat -> Nat -> Nat
4444power base O = S O
4545power base (S exp) = mult base $ power base exp
4646
47+--------------------------------------------------------------------------------
48+-- Comparisons
49+--------------------------------------------------------------------------------
50+
51+data LTE : Nat -> Nat -> Set where
52+ lteZero : LTE O right
53+ lteSucc : LTE left right -> LTE (S left) (S right)
54+
55+total GTE : Nat -> Nat -> Set
56+GTE left right = LTE right left
57+
58+total LT : Nat -> Nat -> Set
59+LT left right = LTE (S left) right
60+
61+total GT : Nat -> Nat -> Set
62+GT left right = LT right left
63+
64+total lte : Nat -> Nat -> Bool
65+lte O right = True
66+lte left O = False
67+lte (S left) (S right) = lte left right
68+
69+total gte : Nat -> Nat -> Bool
70+gte left right = lte right left
71+
72+total lt : Nat -> Nat -> Bool
73+lt left right = lte (S left) right
74+
75+total gt : Nat -> Nat -> Bool
76+gt left right = lt right left
77+
78+total minimum : Nat -> Nat -> Nat
79+minimum left right =
80+ if lte left right then
81+ left
82+ else
83+ right
84+
85+total maximum : Nat -> Nat -> Nat
86+maximum left right =
87+ if lte left right then
88+ right
89+ else
90+ left
91+
4792--------------------------------------------------------------------------------
4893-- Type class instances
4994--------------------------------------------------------------------------------
@@ -85,35 +130,48 @@ record Multiplicative : Set where
85130record Additive : Set where
86131 getAdditive : Nat -> Additive
87132
88−-- XXX: infix operators don't seem to be being exported correctly from
89−-- algebra.idr?
90−--instance Monoid Multiplicative where
91−-- neutral = getMultiplicative $ S O
92−-- left <+> right = getMultiplicative $ left' * right'
93−-- where
94−-- left' : Nat
95−-- left' =
96−-- case left of
97−-- getMultiplicative m => m
98−
99−-- right' : Nat
100−-- right' =
101−-- case right of
102−-- getMultiplicative m => m
103−
104−--instance Monoid Additive where
105−-- neutral = getAdditive $ O
106−-- left <+> right = getAdditive $ left' + right'
107−-- where
108−-- left' : Nat
109−-- left' =
110−-- case left of
111−-- getAdditive m => m
112−
113−-- right' : Nat
114−-- right' =
115−-- case right of
116−-- getAdditive m => m
133+instance Semigroup Multiplicative where
134+ (<+>) left right = getMultiplicative $ left' * right'
135+ where
136+ left' : Nat
137+ left' =
138+ case left of
139+ getMultiplicative m => m
140+
141+ right' : Nat
142+ right' =
143+ case right of
144+ getMultiplicative m => m
145+
146+instance Semigroup Additive where
147+ left <+> right = getAdditive $ left' + right'
148+ where
149+ left' : Nat
150+ left' =
151+ case left of
152+ getAdditive m => m
153+
154+ right' : Nat
155+ right' =
156+ case right of
157+ getAdditive m => m
158+
159+instance Monoid Multiplicative where
160+ neutral = getMultiplicative $ S O
161+
162+instance Monoid Additive where
163+ neutral = getAdditive O
164+
165+instance MeetSemilattice Nat where
166+ meet = minimum
167+
168+instance JoinSemilattice Nat where
169+ join = maximum
170+
171+instance Lattice Nat where { }
172+
173+instance BoundedJoinSemilattice Nat where
174+ bottom = O
117175
118176--------------------------------------------------------------------------------
119177-- Auxilliary notions
@@ -136,51 +194,6 @@ fib (S (S n)) = fib (S n) + fib n
136194-- GCD and LCM
137195--------------------------------------------------------------------------------
138196
139−--------------------------------------------------------------------------------
140−-- Comparisons
141−--------------------------------------------------------------------------------
142−
143−data LTE : Nat -> Nat -> Set where
144− lteZero : LTE O right
145− lteSucc : LTE left right -> LTE (S left) (S right)
146−
147−total GTE : Nat -> Nat -> Set
148−GTE left right = LTE right left
149−
150−total LT : Nat -> Nat -> Set
151−LT left right = LTE (S left) right
152−
153−total GT : Nat -> Nat -> Set
154−GT left right = LT right left
155−
156−total lte : Nat -> Nat -> Bool
157−lte O right = True
158−lte left O = False
159−lte (S left) (S right) = lte left right
160−
161−total gte : Nat -> Nat -> Bool
162−gte left right = lte right left
163−
164−total lt : Nat -> Nat -> Bool
165−lt left right = lte (S left) right
166−
167−total gt : Nat -> Nat -> Bool
168−gt left right = lt right left
169−
170−total minimum : Nat -> Nat -> Nat
171−minimum left right =
172− if lte left right then
173− left
174− else
175− right
176−
177−total maximum : Nat -> Nat -> Nat
178−maximum left right =
179− if lte left right then
180− right
181− else
182− left
183−
184197--------------------------------------------------------------------------------
185198-- Division and modulus
186199--------------------------------------------------------------------------------
@@ -712,9 +725,6 @@ plusZeroRightNeutralStepCase = proof {
712725 trivial;
713726}
714727
715−
716−---------- Proofs ----------
717−
718728maximumCommutativeStepCase = proof {
719729 intros;
720730 rewrite (boolElimSuccSucc (lte left right) right left);