2 | module Pipeline.Equality
4 | import Control.Category
5 | import public Data.List.Quantifiers
25 | Category Equal where
27 | (.) Refl Refl = Refl
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} ->
35 | (fd : innerObj -> outerObj) ->
36 | (ts : CongPipeline (S n) innerObj) ->
37 | CongPipeline m outerObj ->
39 | (check : S n + m = sum) =>
40 | CongPipeline sum outerObj
43 | head : {0 n : Nat} -> (ts : CongPipeline (S n) o) -> o
45 | head (AddNest f ts _) = f (head ts)
48 | last : {0 n : Nat} -> (ts : CongPipeline (S n) o) -> o
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)
57 | record NestCong (outerObj : Type) where
60 | fd : inner -> outerObj
62 | ps : CongPipeline (S plen) inner
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
76 | (|=) : o -> CongPipeline n o -> CongPipeline (S n) o
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
85 | ToList : {0 n : Nat} -> {0 ty : Type} -> CongPipeline n ty -> List Type
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)
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
101 | Prove : CongPipeline n ty -> Type
102 | Prove x = HList (ToList x)
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)
124 | %ambiguity_depth 15
126 | test1 : (a, b, c, d : Nat) -> CongPipeline ?
Nat
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))
137 | testImpl1 : {a, b, c, d : Nat} -> Prove (Equality.test1 a b c d)
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)
147 | testRun1 : {a, b, c, d : Nat} -> a + (b + (c + d)) = d + (c + (b + a))
148 | testRun1 = runProof (test1 a b c d) testImpl1
150 | test2 : (a, b, c, d : Nat) -> CongPipeline ?
Nat
152 | [ Cong (a +) [Cong (b +) [ c + d
155 | , Cong (+ a) [ (d + c) + b
157 | , Cong (d +) [ (c + b) + a
161 | test2Impl : {a, b, c, d : Nat} -> Prove (Equality.test2 a b c d)
163 | [ plusCommutative {}
164 | , plusCommutative {}
165 | , plusCommutative {}
166 | , sym (plusAssociative {})
167 | , sym (plusAssociative {})
168 | , sym (plusAssociative {})
171 | testRun2 : {a, b, c, d : Nat} -> a + (b + (c + d)) = d + (c + (b + a))
172 | testRun2 = runProof (test2 a b c d) test2Impl
174 | test3 : (a, b, c, d : Nat) -> CongPipeline ?
Nat
176 | Cong (a +) [Cong (b +) [ c + d
179 | >| Cong (+ a) [ (d + c) + b
181 | >| Cong (d +) [ (c + b) + a
184 | testRun3 : {a, b, c, d : Nat} -> a + (b + (c + d)) = d + (c + (b + a))
185 | testRun3 = runProof (test3 a b c d) test2Impl