@@ -2,12 +2,16 @@ module prelude.algebra
3import builtins
3import builtins
5%access public
10%access public
7−-- XXX: change?
12+--------------------------------------------------------------------------------
8−infix 6 <->
13+-- A modest class hierarchy
9−infix 6 <+>
14+--------------------------------------------------------------------------------
12-- Sets equipped with a single binary operation that is associative. Must
16-- Sets equipped with a single binary operation that is associative. Must
13-- satisfy the following laws:
17-- satisfy the following laws:
@@ -16,6 +20,9 @@ infix 6 <*>
16class Semigroup a where
20class Semigroup a where
17 (<+>) : a -> a -> a
21 (<+>) : a -> a -> a
23+class Semigroup a => VerifiedSemigroup a where
24+ semigroupOpIsAssociative : (l, c, r : a) -> l <+> (c <+> r) = (l <+> c) <+> r
19-- Sets equipped with a single binary operation that is associative, along with
26-- Sets equipped with a single binary operation that is associative, along with
20-- a neutral element for that binary operation. Must satisfy the following
27-- a neutral element for that binary operation. Must satisfy the following
@@ -27,6 +34,10 @@ class Semigroup a where
27class Semigroup a => Monoid a where
34class Semigroup a => Monoid a where
28 neutral : a
35 neutral : a
37+class (VerifiedSemigroup a, Monoid a) => VerifiedMonoid a where
38+ monoidNeutralIsNeutralL : (l : a) -> l <+> neutral = l
39+ monoidNeutralIsNeutralR : (r : a) -> neutral <+> r = r
30-- Sets equipped with a single binary operation that is associative, along with
41-- Sets equipped with a single binary operation that is associative, along with
31-- a neutral element for that binary operation and inverses for all elements.
42-- a neutral element for that binary operation and inverses for all elements.
32-- Must satisfy the following laws:
43-- Must satisfy the following laws:
@@ -41,6 +52,10 @@ class Semigroup a => Monoid a where
41class Monoid a => Group a where
52class Monoid a => Group a where
42 inverse : a -> a
53 inverse : a -> a
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
44(<->) : Group a => a -> a -> a
59(<->) : Group a => a -> a -> a
45(<->) left right = left <+> (inverse right)
60(<->) left right = left <+> (inverse right)
@@ -59,6 +74,9 @@ class Monoid a => Group a where
59-- forall a, inverse a <+> a == neutral
74-- forall a, inverse a <+> a == neutral
60class Group a => AbelianGroup a where { }
75class Group a => AbelianGroup a where { }
77+class (VerifiedGroup a, AbelianGroup a) => VerifiedAbelianGroup a where
78+ abelianGroupOpIsCommutative : (l, r : a) -> l <+> r = r <+> l
62-- Sets equipped with two binary operations, one associative and commutative
80-- Sets equipped with two binary operations, one associative and commutative
63-- supplied with a neutral element, and the other associative, with
81-- supplied with a neutral element, and the other associative, with
64-- distributivity laws relating the two operations. Must satisfy the following
82-- distributivity laws relating the two operations. Must satisfy the following
@@ -81,6 +99,11 @@ class Group a => AbelianGroup a where { }
81class AbelianGroup a => Ring a where
99class AbelianGroup a => Ring a where
82 (<*>) : a -> a -> a
100 (<*>) : a -> a -> a
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)
84-- Sets equipped with two binary operations, one associative and commutative
107-- Sets equipped with two binary operations, one associative and commutative
85-- supplied with a neutral element, and the other associative supplied with a
108-- supplied with a neutral element, and the other associative supplied with a
86-- neutral element, with distributivity laws relating the two operations. Must
109-- neutral element, with distributivity laws relating the two operations. Must
@@ -106,6 +129,10 @@ class AbelianGroup a => Ring a where
106class Ring a => RingWithUnity a where
129class Ring a => RingWithUnity a where
107 unity : a
130 unity : a
132+class (VerifiedRing a, RingWithUnity a) => VerifiedRingWithUnity a where
133+ ringWithUnityIsUnityL : (l : a) -> l <*> unity = l
134+ ringWithUnityIsUnityR : (r : a) -> unity <*> r = r
109-- Sets equipped with a binary operation that is commutative, associative and
136-- Sets equipped with a binary operation that is commutative, associative and
110-- idempotent. Must satisfy the following laws:
137-- idempotent. Must satisfy the following laws:
111-- Associativity of join:
138-- Associativity of join:
@@ -118,6 +145,11 @@ class Ring a => RingWithUnity a where
118class JoinSemilattice a where
145class JoinSemilattice a where
119 join : a -> a -> a
146 join : a -> a -> a
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
121-- Sets equipped with a binary operation that is commutative, associative and
153-- Sets equipped with a binary operation that is commutative, associative and
122-- idempotent. Must satisfy the following laws:
154-- idempotent. Must satisfy the following laws:
123-- Associativity of meet:
155-- Associativity of meet:
@@ -130,6 +162,11 @@ class JoinSemilattice a where
130class MeetSemilattice a where
162class MeetSemilattice a where
131 meet : a -> a -> a
163 meet : a -> a -> a
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
133-- Sets equipped with a binary operation that is commutative, associative and
170-- Sets equipped with a binary operation that is commutative, associative and
134-- idempotent and supplied with a neutral element. Must satisfy the following
171-- idempotent and supplied with a neutral element. Must satisfy the following
@@ -139,13 +176,16 @@ class MeetSemilattice a where
139-- forall a b, join a b == join b a
176-- forall a b, join a b == join b a
140-- Idempotency of join:
177-- Idempotency of join:
141-- forall a, join a a == a
178-- forall a, join a a == a
142−-- Neutral for join:
179+-- Bottom:
143-- forall a, join a bottom == bottom
180-- forall a, join a bottom == bottom
144-- Join semilattices capture the notion of sets with a "least upper bound"
181-- Join semilattices capture the notion of sets with a "least upper bound"
145-- equipped with a "bottom" element.
182-- equipped with a "bottom" element.
146class JoinSemilattice a => BoundedJoinSemilattice a where
183class JoinSemilattice a => BoundedJoinSemilattice a where
147 bottom : a
184 bottom : a
186+class (VerifiedJoinSemilattice a, BoundedJoinSemilattice a) => VerifiedBoundedJoinSemilattice a where
187+ boundedJoinSemilatticeBottomIsBottom : (e : a) -> join e bottom = bottom
149-- Sets equipped with a binary operation that is commutative, associative and
189-- Sets equipped with a binary operation that is commutative, associative and
150-- idempotent and supplied with a neutral element. Must satisfy the following
190-- idempotent and supplied with a neutral element. Must satisfy the following
@@ -155,13 +195,16 @@ class JoinSemilattice a => BoundedJoinSemilattice a where
155-- forall a b, meet a b == meet b a
195-- forall a b, meet a b == meet b a
156-- Idempotency of meet:
196-- Idempotency of meet:
157-- forall a, meet a a == a
197-- forall a, meet a a == a
158−-- Neutral for meet:
198+-- Top:
159-- forall a, meet a top == top
199-- forall a, meet a top == top
160-- Meet semilattices capture the notion of sets with a "greatest lower bound"
200-- Meet semilattices capture the notion of sets with a "greatest lower bound"
161-- equipped with a "top" element.
201-- equipped with a "top" element.
162class MeetSemilattice a => BoundedMeetSemilattice a where
202class MeetSemilattice a => BoundedMeetSemilattice a where
205+class (VerifiedMeetSemilattice a, BoundedMeetSemilattice a) => VerifiedBoundedMeetSemilattice a where
206+ boundedMeetSemilatticeTopIsTop : (e : a) -> meet e top = top
165-- Sets equipped with two binary operations that are both commutative,
208-- Sets equipped with two binary operations that are both commutative,
166-- associative and idempotent, along with absorbtion laws for relating the two
209-- associative and idempotent, along with absorbtion laws for relating the two
167-- binary operations. Must satisfy the following:
210-- binary operations. Must satisfy the following:
@@ -179,6 +222,10 @@ class MeetSemilattice a => BoundedMeetSemilattice a where
179-- forall a b, join a (meet a b) == a
222-- forall a b, join a (meet a b) == a
180class (JoinSemilattice a, MeetSemilattice a) => Lattice a where { }
223class (JoinSemilattice a, MeetSemilattice a) => Lattice a where { }
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
182-- Sets equipped with two binary operations that are both commutative,
229-- Sets equipped with two binary operations that are both commutative,
183-- associative and idempotent and supplied with neutral elements, along with
230-- associative and idempotent and supplied with neutral elements, along with
184-- absorbtion laws for relating the two binary operations. Must satisfy the
231-- absorbtion laws for relating the two binary operations. Must satisfy the
@@ -199,6 +246,9 @@ class (JoinSemilattice a, MeetSemilattice a) => Lattice a where { }
199-- forall a, meet a top == top
246-- forall a, meet a top == top
200-- forall a, join a bottom == bottom
247-- forall a, join a bottom == bottom
201class (BoundedJoinSemilattice a, BoundedMeetSemilattice a) => BoundedLattice a where { }
248class (BoundedJoinSemilattice a, BoundedMeetSemilattice a) => BoundedLattice a where { }
250+class (VerifiedBoundedJoinSemilattice a, VerifiedBoundedMeetSemilattice a, VerifiedLattice a) => VerifiedBoundedLattice a where { }
203-- XXX todo:
253-- XXX todo:
204-- Fields and vector spaces.
254-- Fields and vector spaces.