@@ -2,7 +2,9 @@ module Interfaces.Verified
3import Control.Algebra
3import Control.Algebra
4import Control.Algebra.Lattice
4import Control.Algebra.Lattice
5+import Control.Algebra.NumericImplementations
5import Control.Algebra.VectorSpace
6import Control.Algebra.VectorSpace
7%access public export
9%access public export
@@ -19,6 +21,14 @@ interface Functor f => VerifiedFunctor (f : Type -> Type) where
19 (g1 : a -> b) -> (g2 : b -> c) ->
21 (g1 : a -> b) -> (g2 : b -> c) ->
20 map (g2 . g1) x = (map g2 . map g1) x
22 map (g2 . g1) x = (map g2 . map g1) x
24+-- TODO VerifiedFunctor instances for: List, Stream, Either e
26+VerifiedFunctor Maybe where
27+ functorIdentity Nothing = Refl
28+ functorIdentity (Just x) = Refl
29+ functorComposition Nothing g1 g2 = Refl
30+ functorComposition (Just x) g1 g2 = Refl
22interface (Applicative f, VerifiedFunctor f) => VerifiedApplicative (f : Type -> Type) where
32interface (Applicative f, VerifiedFunctor f) => VerifiedApplicative (f : Type -> Type) where
23 applicativeMap : (x : f a) -> (g : a -> b) ->
33 applicativeMap : (x : f a) -> (g : a -> b) ->
24 map g x = pure g <*> x
34 map g x = pure g <*> x
@@ -30,6 +40,25 @@ interface (Applicative f, VerifiedFunctor f) => VerifiedApplicative (f : Type ->
30 applicativeInterchange : (x : a) -> (g : f (a -> b)) ->
40 applicativeInterchange : (x : a) -> (g : f (a -> b)) ->
31 g <*> pure x = pure (\g' : (a -> b) => g' x) <*> g
41 g <*> pure x = pure (\g' : (a -> b) => g' x) <*> g
43+VerifiedApplicative Maybe where
44+ applicativeMap Nothing g = Refl
45+ applicativeMap (Just x) g = Refl
46+ applicativeIdentity Nothing = Refl
47+ applicativeIdentity (Just x) = Refl
48+ applicativeComposition Nothing Nothing Nothing = Refl
49+ applicativeComposition Nothing Nothing (Just x) = Refl
50+ applicativeComposition Nothing (Just x) Nothing = Refl
51+ applicativeComposition Nothing (Just x) (Just y) = Refl
52+ applicativeComposition (Just x) Nothing Nothing = Refl
53+ applicativeComposition (Just x) Nothing (Just y) = Refl
54+ applicativeComposition (Just x) (Just y) Nothing = Refl
55+ applicativeComposition (Just x) (Just y) (Just z) = Refl
56+ applicativeHomomorphism x g = Refl
57+ applicativeInterchange x Nothing = Refl
58+ applicativeInterchange x (Just y) = Refl
60+-- TODO VerifiedApplicative instances for: List, Stream, Either e
33interface (Monad m, VerifiedApplicative m) => VerifiedMonad (m : Type -> Type) where
62interface (Monad m, VerifiedApplicative m) => VerifiedMonad (m : Type -> Type) where
34 monadApplicative : (mf : m (a -> b)) -> (mx : m a) ->
63 monadApplicative : (mf : m (a -> b)) -> (mx : m a) ->
35 mf <*> mx = mf >>= \f =>
64 mf <*> mx = mf >>= \f =>
@@ -40,6 +69,19 @@ interface (Monad m, VerifiedApplicative m) => VerifiedMonad (m : Type -> Type) w
40 monadAssociativity : (mx : m a) -> (f : a -> m b) -> (g : b -> m c) ->
69 monadAssociativity : (mx : m a) -> (f : a -> m b) -> (g : b -> m c) ->
41 (mx >>= f) >>= g = mx >>= (\x => f x >>= g)
70 (mx >>= f) >>= g = mx >>= (\x => f x >>= g)
72+-- The believe_me is there because idris won't seem to unify (pure x = Just x) with Refl
73+VerifiedMonad Maybe where
74+ monadApplicative Nothing Nothing = Refl
75+ monadApplicative Nothing (Just x) = Refl
76+ monadApplicative (Just x) Nothing = Refl
77+ monadApplicative (Just x) (Just y) = Refl
78+ monadLeftIdentity x f = Refl
79+ monadRightIdentity Nothing = Refl
80+ monadRightIdentity (Just x) = (believe_me (Refl {A = Maybe a} {x = Just x}))
81+ monadAssociativity Nothing f g = Refl
82+ monadAssociativity (Just x) f g = Refl
84+-- TODO VerifiedMonad instances for: List, Stream, Either e
44interface Semigroup a => VerifiedSemigroup a where
86interface Semigroup a => VerifiedSemigroup a where
45 total semigroupOpIsAssociative : (l, c, r : a) -> l <+> (c <+> r) = (l <+> c) <+> r
87 total semigroupOpIsAssociative : (l, c, r : a) -> l <+> (c <+> r) = (l <+> c) <+> r
@@ -47,42 +89,99 @@ interface Semigroup a => VerifiedSemigroup a where
47implementation VerifiedSemigroup (List a) where
89implementation VerifiedSemigroup (List a) where
48 semigroupOpIsAssociative = appendAssociative
90 semigroupOpIsAssociative = appendAssociative
50−--implementation VerifiedSemigroup Nat where
92+[PlusNatSemiV] VerifiedSemigroup Nat using PlusNatSemi where
51−-- semigroupOpIsAssociative = plusAssociative
93+ semigroupOpIsAssociative = plusAssociative
95+[MultNatSemiV] VerifiedSemigroup Nat using MultNatSemi where
96+ semigroupOpIsAssociative = multAssociative
98+[PlusZZSemiV] VerifiedSemigroup ZZ using PlusZZSemi where
99+ semigroupOpIsAssociative = plusAssociativeZ
101+[MultZZSemiV] VerifiedSemigroup ZZ using MultZZSemi where
102+ semigroupOpIsAssociative = multAssociativeZ
104+-- TODO Verified versions of...
105+-- (Semigroup m, Semigroup n) => Semigroup (m, n) where
106+-- (a, b) <+> (c, d) = (a <+> c, b <+> d)
108+-- Semigroup (Maybe a)
110+-- [collectJust] Semigroup a => Semigroup (Maybe a)
112+-- libs/base/Data/Morphisms.idr: implementation Monoid (Endomorphism a)
54interface (VerifiedSemigroup a, Monoid a) => VerifiedMonoid a where
114interface (VerifiedSemigroup a, Monoid a) => VerifiedMonoid a where
55 total monoidNeutralIsNeutralL : (l : a) -> l <+> Algebra.neutral = l
115 total monoidNeutralIsNeutralL : (l : a) -> l <+> Algebra.neutral = l
56 total monoidNeutralIsNeutralR : (r : a) -> Algebra.neutral <+> r = r
116 total monoidNeutralIsNeutralR : (r : a) -> Algebra.neutral <+> r = r
58−-- implementation VerifiedMonoid Nat where
118+[PlusNatMonoidV] VerifiedMonoid Nat using PlusNatSemiV, PlusNatMonoid where
59−-- monoidNeutralIsNeutralL = plusZeroRightNeutral
119+ monoidNeutralIsNeutralL = plusZeroRightNeutral
60−-- monoidNeutralIsNeutralR = plusZeroLeftNeutral
120+ monoidNeutralIsNeutralR = plusZeroLeftNeutral
122+[MultNatMonoidV] VerifiedMonoid Nat using MultNatSemiV, MultNatMonoid where
123+ monoidNeutralIsNeutralL = multOneRightNeutral
124+ monoidNeutralIsNeutralR = multOneLeftNeutral
126+[PlusZZMonoidV] VerifiedMonoid ZZ using PlusZZSemiV, PlusZZMonoid where
127+ monoidNeutralIsNeutralL = plusZeroRightNeutralZ
128+ monoidNeutralIsNeutralR = plusZeroLeftNeutralZ
130+[MultZZMonoidV] VerifiedMonoid ZZ using MultZZSemiV, MultZZMonoid where
131+ monoidNeutralIsNeutralL = multOneRightNeutralZ
132+ monoidNeutralIsNeutralR = multOneLeftNeutralZ
62implementation VerifiedMonoid (List a) where
134implementation VerifiedMonoid (List a) where
63 monoidNeutralIsNeutralL = appendNilRightNeutral
135 monoidNeutralIsNeutralL = appendNilRightNeutral
64 monoidNeutralIsNeutralR xs = Refl
136 monoidNeutralIsNeutralR xs = Refl
138+-- TODO Verified versions of...
139+-- (Monoid m, Monoid n) => Monoid (m, n) where
140+-- neutral = (neutral, neutral)
66interface (VerifiedMonoid a, Group a) => VerifiedGroup a where
144interface (VerifiedMonoid a, Group a) => VerifiedGroup a where
67 total groupInverseIsInverseL : (l : a) -> l <+> inverse l = Algebra.neutral
145 total groupInverseIsInverseL : (l : a) -> l <+> inverse l = Algebra.neutral
68 total groupInverseIsInverseR : (r : a) -> inverse r <+> r = Algebra.neutral
146 total groupInverseIsInverseR : (r : a) -> inverse r <+> r = Algebra.neutral
148+VerifiedGroup ZZ using PlusZZMonoidV where
149+ groupInverseIsInverseL k = rewrite sym $ multCommutativeZ (NegS 0) k in
150+ rewrite multNegLeftZ 0 k in
151+ rewrite multOneLeftNeutralZ k in
152+ plusNegateInverseLZ k
153+ groupInverseIsInverseR k = rewrite sym $ multCommutativeZ (NegS 0) k in
154+ rewrite multNegLeftZ 0 k in
155+ rewrite multOneLeftNeutralZ k in
156+ plusNegateInverseRZ k
70interface (VerifiedGroup a, AbelianGroup a) => VerifiedAbelianGroup a where
158interface (VerifiedGroup a, AbelianGroup a) => VerifiedAbelianGroup a where
71 total abelianGroupOpIsCommutative : (l, r : a) -> l <+> r = r <+> l
159 total abelianGroupOpIsCommutative : (l, r : a) -> l <+> r = r <+> l
161+VerifiedAbelianGroup ZZ where
162+ abelianGroupOpIsCommutative = plusCommutativeZ
73interface (VerifiedAbelianGroup a, Ring a) => VerifiedRing a where
164interface (VerifiedAbelianGroup a, Ring a) => VerifiedRing a where
74 total ringOpIsAssociative : (l, c, r : a) -> l <.> (c <.> r) = (l <.> c) <.> r
165 total ringOpIsAssociative : (l, c, r : a) -> l <.> (c <.> r) = (l <.> c) <.> r
75 total ringOpIsDistributiveL : (l, c, r : a) -> l <.> (c <+> r) = (l <.> c) <+> (l <.> r)
166 total ringOpIsDistributiveL : (l, c, r : a) -> l <.> (c <+> r) = (l <.> c) <+> (l <.> r)
76 total ringOpIsDistributiveR : (l, c, r : a) -> (l <+> c) <.> r = (l <.> r) <+> (c <.> r)
167 total ringOpIsDistributiveR : (l, c, r : a) -> (l <+> c) <.> r = (l <.> r) <+> (c <.> r)
169+VerifiedRing ZZ where
170+ ringOpIsAssociative = multAssociativeZ
171+ ringOpIsDistributiveL = multDistributesOverPlusRightZ
172+ ringOpIsDistributiveR = multDistributesOverPlusLeftZ
78interface (VerifiedRing a, RingWithUnity a) => VerifiedRingWithUnity a where
174interface (VerifiedRing a, RingWithUnity a) => VerifiedRingWithUnity a where
79 total ringWithUnityIsUnityL : (l : a) -> l <.> Algebra.unity = l
175 total ringWithUnityIsUnityL : (l : a) -> l <.> Algebra.unity = l
80 total ringWithUnityIsUnityR : (r : a) -> Algebra.unity <.> r = r
176 total ringWithUnityIsUnityR : (r : a) -> Algebra.unity <.> r = r
82−--interface (VerifiedRingWithUnity a, Field a) => VerifiedField a where
178+VerifiedRingWithUnity ZZ where
83−-- total fieldInverseIsInverseL : (l : a) -> (notId : Not (l = neutral)) -> l <.> (inverseM l notId) = Algebra.unity
179+ ringWithUnityIsUnityL = multOneRightNeutralZ
84−-- total fieldInverseIsInverseR : (r : a) -> (notId : Not (r = neutral)) -> (inverseM r notId) <.> r = Algebra.unity
180+ ringWithUnityIsUnityR = multOneLeftNeutralZ
182+--interface (VerifiedRingWithUnity a, Field a) => VerifiedField a where
183+-- total fieldInverseIsInverseL : (l : a) -> (notId : Not (l = Algebra.neutral)) -> l <.> (inverseM l notId) = Algebra.unity
184+-- total fieldInverseIsInverseR : (r : a) -> (notId : Not (r = Algebra.neutral)) -> (inverseM r notId) <.> r = Algebra.unity
87interface JoinSemilattice a => VerifiedJoinSemilattice a where
186interface JoinSemilattice a => VerifiedJoinSemilattice a where
88 total joinSemilatticeJoinIsAssociative : (l, c, r : a) -> join l (join c r) = join (join l c) r
187 total joinSemilatticeJoinIsAssociative : (l, c, r : a) -> join l (join c r) = join (join l c) r