0 | module Pipeline.Functor
2 | import Control.Category
4 | import Data.List.Quantifiers
6 | import Pipeline.Basic
11 | record FunctorData (outerObj : Type) (outerArr : Rel outerObj) where
14 | 0 nestedArr : Rel nestedObj
15 | {auto nestedCat : Category nestedArr}
16 | mapObj : nestedObj -> outerObj
17 | mapHom : {0 x, y : nestedObj} -> nestedArr x y -> outerArr (mapObj x) (mapObj y)
20 | Endofunctor : Category arr => FunctorData o arr
29 | data FunctorialPipeline : (size : Nat) -> (0 obj : Type) -> (0 arr : Rel obj) -> Type where
30 | Nil : FunctorialPipeline Z o arr
31 | (::) : o -> FunctorialPipeline n o arr -> FunctorialPipeline (S n) o arr
32 | AddNest : {0 outerObj : Type} -> {0 outerArr : Rel outerObj} ->
34 | (fd : FunctorData outerObj outerArr) ->
35 | (ts : FunctorialPipeline (S n) fd.nestedObj fd.nestedArr) ->
36 | FunctorialPipeline m outerObj outerArr ->
38 | (check : S n + m = sum) =>
39 | FunctorialPipeline sum outerObj outerArr
42 | record NestFunctor (outerObj : Type) (outerArr : Rel outerObj) where
43 | constructor MkNested
44 | fd : FunctorData outerObj outerArr
46 | ps : FunctorialPipeline (S plen) fd.nestedObj fd.nestedArr
49 | head : {0 n : Nat} -> (ts : FunctorialPipeline (S n) o arr) -> o
51 | head (AddNest x ts xs) = x.mapObj (head ts)
54 | last : {0 n : Nat} -> (ts : FunctorialPipeline (S n) o arr) -> o
56 | last (x :: n@(y :: z)) = last n
57 | last (x :: (AddNest fd ts y)) = assert_total $
Functor.last (AddNest fd ts y)
58 | last (AddNest fd ts []) = fd.mapObj (last ts)
59 | last (AddNest fd ts n@(x :: y)) = last n
60 | last (AddNest fd ts (AddNest x y z)) = assert_total $
last (AddNest x y z)
64 | FuncTy : {0 n : Nat} -> (f : Type -> Type) -> Functor f => (fp : FunctorialPipeline (S n) Type Fn) -> NestFunctor Type Fn
65 | FuncTy f = MkNested (Func @{Set} Type Fn f map)
68 | (::) : (np : NestFunctor outerObj outerArr) ->
69 | FunctorialPipeline m outerObj outerArr ->
70 | FunctorialPipeline (S np.plen + m) outerObj outerArr
71 | (::) np x = AddNest np.fd np.ps x
78 | data List0 : Type -> Type where
80 | (::) : (0 _ : a) -> List0 a -> List0 a
83 | (++) : List0 a -> List0 a -> List0 a
85 | (++) (x :: xs) ys = x :: (xs ++ ys)
88 | appendNilRightNeutral : (xs : List0 a) -> xs ++ [] = xs
89 | appendNilRightNeutral [] = Refl
90 | appendNilRightNeutral (x :: y) = cong (x ::) (appendNilRightNeutral y)
93 | namespace HListErased
95 | data HList0 : List0 a -> Type where
97 | (::) : (x : a) -> HList0 xs -> HList0 (a :: xs)
100 | splitAt : (xs : List0 a) -> HList0 (xs ++ ys) -> (HList0 xs, HList0 ys)
101 | splitAt [] x = ([], x)
102 | splitAt (x :: xs) (y :: ys) = let r = splitAt xs ys in (y :: fst r, snd r)
105 | concat : HList0 xs -> HList0 ys -> HList0 (xs ++ ys)
107 | concat (x :: z) y = x :: concat z y
110 | combineRest : {0 obj : Type} -> {0 arr : Rel obj} -> (end : obj) -> (x : FunctorialPipeline m obj arr) -> List0 Type
111 | combineRest end [] = []
112 | combineRest end (Functor.(::) x y) = [arr end x]
113 | combineRest end (AddNest x y z) =
114 | arr end (head (AddNest x y z)) :: combineRest (x.mapObj (last y)) z
117 | ToList : {0 n : Nat} -> {0 obj : Type} -> {0 arr : Rel obj} -> FunctorialPipeline n obj arr -> List0 Type
119 | ToList (Functor.(::) x []) = []
120 | ToList (Functor.(::) x (Functor.(::) y z))
121 | = arr x y :: ToList (y :: z)
122 | ToList (Functor.(::) x (AddNest fd ts y))
123 | = arr x (fd.mapObj (head ts)) :: ToList ts ++ combineRest (fd.mapObj (last ts)) y ++ ToList y
124 | ToList (AddNest fd ts x) = ToList ts ++ combineRest (fd.mapObj (last ts)) x ++ ToList x
127 | Impl : {0 n : Nat} -> {0 obj : Type} -> {0 arr : Rel obj} -> FunctorialPipeline n obj arr -> Type
128 | Impl = HList0 . ToList
130 | %ambiguity_depth 15
135 | prfToList : {0 outerObj : Type} -> {0 outerArr : Rel outerObj} ->
136 | {n , m, sum : Nat} ->
137 | {fd : FunctorData outerObj outerArr} ->
138 | {ts : FunctorialPipeline (S m) fd.nestedObj fd.nestedArr} ->
139 | {x : FunctorialPipeline n outerObj outerArr} ->
141 | c1 = Functor.ToList ts ++ Functor.combineRest (fd.mapObj (Functor.last ts)) x ++ Functor.ToList x
142 | in ToList (AddNest {sum = S m + n} fd ts x) === c1
143 | prfToList {n = 0, m = 0} = ?a
144 | prfToList {n = 0, m = (S k)} = ?b
145 | prfToList {n = (S k), m = 0} = ?c
146 | prfToList {n = (S k), m = (S j)} = ?d
148 | headAll : HList0 (x :: xs) -> x
149 | headAll (x :: _) = x
151 | tailAll : HList0 (x :: xs) -> HList0 xs
152 | tailAll (_ :: xs) = xs
156 | {0 n : Nat} -> {0 obj : Type} -> {0 arr : Rel obj} -> (cat : Category arr) =>
157 | (fp : FunctorialPipeline (S n) obj arr) -> Impl fp -> arr (head fp) (last fp)
158 | runFPipeline {n = 0} (Functor.(::) y []) [] = id
159 | runFPipeline {n = S n} (Functor.(::) y (Functor.(::) z w)) (x :: xs) = runFPipeline (z :: w) xs . x
160 | runFPipeline {n = (S n1 + n2) } (Functor.(::) x (AddNest {n = n1, m = n2, check = Refl, sum = (S n1 + n2)} fd ts y)) (ix :: xs)
161 | = let rec = runFPipeline (AddNest {n = n1, m = n2, check = Refl, sum = S n1 + n2} fd ts y)
162 | (replace {p = HList0} (sym (prfToList {sum = S n1 + n2} )) xs)
164 | runFPipeline {n = n1 + 0} (AddNest {n = n1, m = 0, check = Refl, sum = S n1 + 0} fd ts []) x =
165 | let qq = (replace {p = HList0} ((prfToList {fd, ts, x = [], sum = S n1 + 0})) x)
166 | rec = assert_total $
runFPipeline ts @{fd.nestedCat} (replace {p = HList0} (appendNilRightNeutral (ToList ts)) qq)
168 | runFPipeline {n = n1 + S n2 } (AddNest {n = n1, m = S n2, check = Refl, sum = S n1 + S n2} fd ts (Functor.(::) y ys)) x with (replace {p = HList0} (prfToList {fd, ts, x = Functor.(::) y ys, n = S n2, m = n1, sum = S n1 + S n2}) x)
169 | runFPipeline {n = n1 + S n2} (AddNest {n = n1, m = S n2, check = Refl, sum = S n1 + S n2} fd ts (Functor.(::) y ys)) x | with_pat = let
170 | (p1, p2) = splitAt (ToList ?
) with_pat
171 | rec1 = assert_total $
runFPipeline @{cat} (y :: ys) (tailAll {x = arr (fd .mapObj (last ts)) y, xs = ToList (y :: ys)} p2)
172 | rec2 = fd.mapHom (assert_total $
runFPipeline ts @{fd.nestedCat} p1)
173 | in rec1 . (headAll p2) . rec2
174 | runFPipeline {n = n1 + S (m1 + m2) } (AddNest {n = n1, m = S (m1 + m2), check = Refl, sum = S n1 + S (m1 + m2)} fd ts (AddNest {n = m1, m = m2, check = Refl} gd gs yy)) x with (replace {p = HList0} (prfToList {fd, ts, x = AddNest gd gs yy, n = S (m1 + m2), m = n1, sum = S n1 + S (m1 + m2)}) x)
175 | runFPipeline {n = n1 + S (m1 + m2)} (AddNest {n = n1, m = S (m1 + m2), check = Refl, sum = S n1 + S (m1 + m2)} fd ts (AddNest {n = m1, m = m2, check = Refl} gd gs yy)) x | with_pat = let
176 | (p1, p2) = splitAt (ToList ts) with_pat
177 | (q1, q2) = splitAt ?
(tailAll p2)
178 | rec1 = assert_total $
runFPipeline @{cat} (AddNest {n = m1, m = m2, check = Refl} gd gs yy) q2
179 | rec2 = fd.mapHom (assert_total $
runFPipeline ts @{fd.nestedCat} p1)
180 | in rec1 . (headAll p2) . rec2