0 | ||| Equality pipelines hardcoded to avoid as much instance search as possible
  1 |
  2 | module Pipeline.Equality
  3 |
  4 | import Control.Category
  5 | import public Data.List.Quantifiers
  6 | import Data.Nat
  7 |
  8 | %default total
  9 | -- public export
 10 | -- ProofSpec : Nat -> Type -> Type
 11 | -- ProofSpec n = Vect (2 + n)
 12 | --
 13 | -- public export
 14 | -- ProofSteps : ProofSpec n a -> Type
 15 | -- ProofSteps = All (uncurry (Equal {a, b = a})) . pairUp
 16 | --
 17 | -- public export
 18 | -- Prove : (spec : ProofSpec n a) -> ProofSteps spec -> Vect.head spec === Vect.last spec
 19 | -- Prove [x, y] [p] = p
 20 | -- Prove (y :: (z :: (v :: xs))) (x :: (w :: s)) =
 21 | --   trans x (Prove (z :: v :: xs) (w :: s))
 22 |
 23 | -- In case you want to use the generic version we provide a category instance
 24 | public export
 25 | Category Equal where
 26 |   id = Refl
 27 |   (.) Refl Refl = Refl
 28 |
 29 | public export
 30 | data CongPipeline : (size : Nat) -> (obj : Type) -> Type where
 31 |   Nil : CongPipeline Z o
 32 |   (::) : o -> CongPipeline n o -> CongPipeline (S n) o
 33 |   AddNest : {0 innerObj, outerObj : Type} ->
 34 |             {0 m, n : Nat} ->
 35 |             (fd : innerObj -> outerObj) ->
 36 |             (ts : CongPipeline (S n) innerObj) ->
 37 |             CongPipeline m outerObj ->
 38 |             {0 sum : Nat} ->
 39 |             (check : S n + m = sum) =>
 40 |             CongPipeline sum outerObj
 41 |
 42 | public export
 43 | head : {0 n : Nat} -> (ts : CongPipeline (S n) o) -> o
 44 | head (x :: y) = x
 45 | head (AddNest f ts _) = f (head ts)
 46 |
 47 | public export
 48 | last : {0 n : Nat} -> (ts : CongPipeline (S n) o) -> o
 49 | last (x :: []) = x
 50 | last (x :: n@(y :: z)) = last n
 51 | last (x :: (AddNest fd ts y)) = assert_total $ Equality.last (AddNest fd ts y)
 52 | last (AddNest f ts []) = f (last ts)
 53 | last (AddNest fd ts n@(x :: y)) = last n
 54 | last (AddNest fd ts (AddNest x y z)) = assert_total $ last (AddNest x y z)
 55 |
 56 | public export
 57 | record NestCong (outerObj : Type) where
 58 |   constructor Cong
 59 |   {0 inner : Type}
 60 |   fd : inner -> outerObj
 61 |   {0 plen : Nat}
 62 |   ps : CongPipeline (S plen) inner
 63 |
 64 | namespace AddFunc
 65 |
 66 |   public export
 67 |   (::) : (np : NestCong outerObj) ->
 68 |          CongPipeline m outerObj ->
 69 |          CongPipeline (S np.plen + m) outerObj
 70 |   (::) np x = AddNest {m} np.fd np.ps x
 71 |
 72 | export infixr 0 |<
 73 | export infixr 0 >|
 74 | export infixr 0 |=
 75 | public export
 76 | (|=) : o -> CongPipeline n o -> CongPipeline (S n) o
 77 | (|=) = (::)
 78 | public export
 79 | (>|) : (np : NestCong outerObj) ->
 80 |        CongPipeline m outerObj ->
 81 |        CongPipeline (S np.plen + m) outerObj
 82 | (>|) np x = AddNest {m} np.fd np.ps x
 83 |
 84 | public export
 85 | ToList : {0 n : Nat} -> {0 ty : Type} -> CongPipeline n ty -> List Type
 86 |
 87 | public export
 88 | combineRest : {0 ty : Type} -> (end : ty) -> (x : CongPipeline m ty) -> List Type
 89 | combineRest end [] = []
 90 | combineRest end (Equality.(::) x y) = (end === x) :: ToList (x :: y)
 91 | combineRest end (AddNest f y z) =
 92 |     (end === head (AddNest f y z)) :: ToList (AddNest f y z)
 93 |
 94 | ToList [] = []
 95 | ToList (Equality.(::) x []) = []
 96 | ToList (Equality.(::) x (Equality.(::) y z)) = (x === y) :: ToList (y :: z)
 97 | ToList (Equality.(::) x (AddNest fd ts y)) = (x === fd (head ts)) :: ToList ts ++ combineRest (fd (last ts)) y
 98 | ToList (AddNest fd ts x) = ToList ts ++ combineRest (fd (last ts)) x
 99 |
100 | public export
101 | Prove : CongPipeline n ty -> Type
102 | Prove x = HList (ToList x)
103 |
104 | export
105 | runProof : (spec : CongPipeline (S n) ty) -> (steps : Prove spec) -> head spec === last spec
106 | runProof (Equality.(::) x []) steps = Refl
107 | runProof (Equality.(::) x (Equality.(::) y z)) (w :: v) = trans w (runProof (y :: z) v)
108 | runProof (Equality.(::) x (AddNest fd ts y)) (s1 :: s2)
109 |   = trans s1 $ runProof (AddNest fd ts y) s2
110 | runProof (AddNest fd ts []) steps with (splitAt ? steps)
111 |   runProof (AddNest fd ts []) steps | (p, _) =
112 |     assert_total $ cong fd (runProof ts p)
113 | runProof (AddNest fd ts (Equality.(::) x y)) steps with (splitAt ? steps)
114 |   runProof (AddNest fd ts (Equality.(::) x y)) steps | (p1, p2 :: p3) =
115 |     let q1 = assert_total $ runProof ts p1
116 |         q2 = assert_total $ runProof (x :: y) p3
117 |     in trans (cong fd q1) (trans p2 q2)
118 | runProof (AddNest fd ts (AddNest f x y)) steps with (splitAt ? steps)
119 |   runProof (AddNest fd ts (AddNest f x y)) steps | (p1, p2 :: p3) =
120 |     let q1 = assert_total $ cong fd (runProof ts p1)
121 |         q2 = assert_total $ runProof (AddNest f x y) p3
122 |     in trans q1 (trans p2 q2)
123 |
124 | %ambiguity_depth 15
125 |
126 | test1 : (a, b, c, d : Nat) -> CongPipeline ? Nat
127 | test1 a b c d =
128 |   [ a + (b + (c + d))
129 |   , a + (b + (d + c))
130 |   , a + ((d + c) + b)
131 |   , ((d + c) + b) + a
132 |   , (d + (c + b)) + a
133 |   , d + ((c + b) + a)
134 |   , d + (c + (b + a))
135 |   ]
136 |
137 | testImpl1 : {a, b, c, d : Nat} -> Prove (Equality.test1 a b c d)
138 | testImpl1 =
139 |   [ cong (a +) (cong (b+) (plusCommutative c d))
140 |   , cong (a +) (plusCommutative ? ?)
141 |   , plusCommutative a ((d + c) + b)
142 |   , cong (+ a) (sym $ plusAssociative d c b)
143 |   , (sym $ plusAssociative d (c + b) a)
144 |   , cong (d + ) (sym $ plusAssociative c b a)
145 |   ]
146 |
147 | testRun1 : {a, b, c, d : Nat} -> a + (b + (c + d)) = d + (c + (b + a))
148 | testRun1 = runProof (test1 a b c d) testImpl1
149 |
150 | test2 : (a, b, c, d : Nat) -> CongPipeline ? Nat
151 | test2 a b c d =
152 |   [ Cong (a +) [Cong (b +) [ c + d
153 |                            , d + c]
154 |                , (d + c) + b]
155 |   , Cong (+ a) [ (d + c) + b
156 |                , d + (c + b)]
157 |   , Cong (d +) [ (c + b) + a
158 |                , c + (b + a)]
159 |   ]
160 |
161 | test2Impl : {a, b, c, d : Nat} -> Prove (Equality.test2 a b c d)
162 | test2Impl =
163 |   [ plusCommutative {}
164 |   , plusCommutative {}
165 |   , plusCommutative {}
166 |   , sym (plusAssociative {})
167 |   , sym (plusAssociative {})
168 |   , sym (plusAssociative {})
169 |   ]
170 |
171 | testRun2 : {a, b, c, d : Nat} -> a + (b + (c + d)) = d + (c + (b + a))
172 | testRun2 = runProof (test2 a b c d) test2Impl
173 |
174 | test3 : (a, b, c, d : Nat) -> CongPipeline ? Nat
175 | test3 a b c d =
176 |      Cong (a +) [Cong (b +) [ c + d
177 |                             , d + c]
178 |                 , (d + c) + b]
179 |   >| Cong (+ a) [ (d + c) + b
180 |              , d + (c + b)]
181 |   >| Cong (d +) [ (c + b) + a
182 |                 , c + (b + a)]
183 |   >| Nil
184 | testRun3 : {a, b, c, d : Nat} -> a + (b + (c + d)) = d + (c + (b + a))
185 | testRun3 = runProof (test3 a b c d) test2Impl
186 |
187 | {-
188 | data CEq2 : ty -> sy -> Type where
189 |   (>-) : (0 _ : ty) -> (0 _ : sy) -> CEq2 ty sy
190 |
191 |
192 | data Mid : Type where
193 |   EqPrf : Mid
194 |   CongPrf : (sy -> ty) -> Vect (S n) (TEq sy) -> Mid
195 |
196 | Steps : Vect (2 + n) (TEq ty) -> Type
197 |
198 | data CongEq : (0 _ : sy) -> (0 _ : ty) -> (0 _ : gy) -> Type where
199 |   Cong : (sy -> ty) -> gy -> CongEq sy ty gy
200 |
201 | data CEq2Cong : sy -> ty -> Vect (2 + n) (TEq sy) -> rest -> Type where
202 |   (>=) : CongEq sy ty (Steps xs) -> rest -> CEq2Cong sy ty xs rest
203 |
204 | data END : (0 _ : ty) -> Type where
205 |   QED : (0 ty : _) -> END ty
206 |
207 | data CEq : (0 _ : ty) -> (0 _ : rhd) -> (0 _ : rest) -> Type where
208 |   (-<) : (0 ty : _) -> (0 _ : CEq2 (Equal ty rhs) rest) -> CEq ty rhs rest
209 |   (~<) : (0 ty : _) -> (0 _ : CEq2 (Equal rhs ty) rest) -> CEq ty rhs rest
210 |
211 | data CEqCong : (0 _ : ty) -> (0 _ : rhd) -> (0 _ : rest) -> Type where
212 |   (=<) : (0 ty : _) -> (0 _ : CEq2Cong ty rhs xs rest) -> CEq ty rhs rest
213 |
214 | %hide Prelude.Ops.infix.(>=)
215 | export infixr 0 -<
216 | export infixr 0 =<
217 | export infixr 0 ~<
218 | export infixr 0 >-
219 | export infixr 0 >=
220 |
221 | Steps [B x, B y ]    = CEq x y (END y)
222 | Steps [B x, C {ty, sy} f xs] = CEq x EqPrf (f (HeadType xs)) (CEq (f (HeadType xs)) (CongPrf f xs) (f (LastType xs)) (END (f (LastType xs))))
223 | Steps [C {ty, sy} f xs, B y] = CEq (f (HeadType xs)) (CongPrf f xs) (f (LastType xs)) (CEq (f (LastType xs)) EqPrf y (END y))
224 | Steps [C f xs, C g ys]
225 |   = CEq (f (HeadType xs)) (CongPrf f xs) (f (LastType xs))
226 |   $ CEq (f (LastType xs)) EqPrf (g (HeadType ys))
227 |   $ CEq (g (HeadType ys)) (CongPrf g ys) (g (LastType ys)) (g (LastType ys))
228 | Steps (B x :: y :: z :: ys) = CEq x EqPrf (HeadType (y :: ys)) (Steps (y :: z :: ys))
229 | Steps (C f xs :: y :: z :: ys)
230 |   = CEq (f (HeadType xs)) (CongPrf f xs) (f (LastType xs))
231 |   $ CEq (f (LastType xs)) EqPrf (HeadType (y :: ys))
232 |   $ Steps (y :: z :: ys)
233 |
234 | test : (a, b, c, d : Nat) -> a + (b + (c + d)) === (((a + b) + c) + d)
235 | test a b c d = let
236 |
237 |   test1 : Vect 2 (TEq Nat)
238 |   test1 = [ B (a + b), B (b + a)]
239 |   test1Impl : Steps test1
240 |   test1Impl =  plus a b -< ?beheh_4 >- QED (plus b a)
241 |
242 |   test2 : Vect 2 (TEq Nat)
243 |   test2 = [ B ((a + b) + c) , C (+ c) [ B (a + b), B (b + a)]]
244 |
245 |   test2Impl : Steps test2
246 |   test2Impl = plus (plus a b) c
247 |     -< Refl >-
248 |     plus (plus a b) c
249 |     =< Cong (+ c) ?nowowoow_0 >= QED (plus (plus b a) c)
250 |
251 |   test3 : Vect 2 (TEq Nat)
252 |   test3 = [ C (+ c) [ B (a + b), B (b + a)], B ((a + b) + c)]
253 |
254 |   test3Impl : Steps test3
255 |   test3Impl = plus (plus a b) c =< ?ADgb
256 |
257 |   steps : Vect ? (TEq Nat)
258 |   steps = [ C (a +) [ B (b + (c + d))
259 |                        , B ((b + c) + d)]
260 |           , B ((a + (b + c)) + d)
261 |           , C (+ d)
262 |             [ B (a + (b + c))
263 |             , B ((a + b) + c) ]
264 |           ]
265 |   prove : Steps steps
266 |   -- prove = ?pppw
267 |
268 |   in ?blue
269 |