0 | module Pipeline.Functor
  1 |
  2 | import Control.Category
  3 | import Data.List
  4 | import Data.List.Quantifiers
  5 | import Control.Set
  6 | import Pipeline.Basic
  7 |
  8 | %default total
  9 |
 10 | public export
 11 | record FunctorData (outerObj : Type) (outerArr : Rel outerObj) where
 12 |   constructor Func
 13 |   0 nestedObj : Type
 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)
 18 |
 19 | public export
 20 | Endofunctor : Category arr => FunctorData o arr
 21 | Endofunctor = Func
 22 |   { nestedObj = o
 23 |   , nestedArr = arr
 24 |   , mapObj = id
 25 |   , mapHom = id
 26 |   }
 27 |
 28 | public export
 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} ->
 33 |             {0 m, n : Nat} ->
 34 |             (fd : FunctorData outerObj outerArr) ->
 35 |             (ts : FunctorialPipeline (S n) fd.nestedObj fd.nestedArr) ->
 36 |             FunctorialPipeline m outerObj outerArr ->
 37 |             {0 sum : Nat} ->
 38 |             (check : S n + m = sum) =>
 39 |             FunctorialPipeline sum outerObj outerArr
 40 |
 41 | public export
 42 | record NestFunctor (outerObj : Type) (outerArr : Rel outerObj) where
 43 |   constructor MkNested
 44 |   fd : FunctorData outerObj outerArr
 45 |   {0 plen : Nat}
 46 |   ps : FunctorialPipeline (S plen) fd.nestedObj fd.nestedArr
 47 |
 48 | public export
 49 | head : {0 n : Nat} -> (ts : FunctorialPipeline (S n) o arr) -> o
 50 | head (x :: y) = x
 51 | head (AddNest x ts xs) = x.mapObj (head ts)
 52 |
 53 | public export
 54 | last : {0 n : Nat} -> (ts : FunctorialPipeline (S n) o arr) -> o
 55 | last (x :: []) = x
 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)
 61 |
 62 | namespace AddFunc
 63 |   public export
 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)
 66 |
 67 |   public export
 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
 72 |
 73 | namespace Erased
 74 |   -- Essentially a Nat at runtime, we need this because
 75 |   -- we need to know the type of the morphisms when compiling
 76 |   -- but we don't want to keep them around at runtime
 77 |   public export
 78 |   data List0 : Type -> Type where
 79 |     Nil : List0 a
 80 |     (::) : (0 _ : a) -> List0 a -> List0 a
 81 |
 82 |   public export
 83 |   (++) : List0 a -> List0 a -> List0 a
 84 |   (++) [] xs = xs
 85 |   (++) (x :: xs) ys = x :: (xs ++ ys)
 86 |
 87 |   export
 88 |   appendNilRightNeutral : (xs : List0 a) -> xs ++ [] = xs
 89 |   appendNilRightNeutral [] = Refl
 90 |   appendNilRightNeutral (x :: y) = cong (x ::) (appendNilRightNeutral y)
 91 |
 92 |
 93 | namespace HListErased
 94 |   public export
 95 |   data HList0 : List0 a -> Type where
 96 |     Nil : HList0 []
 97 |     (::) : (x : a) -> HList0 xs -> HList0 (a :: xs)
 98 |
 99 |   export
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)
103 |
104 |   export
105 |   concat : HList0 xs -> HList0 ys -> HList0 (xs ++ ys)
106 |   concat [] y = y
107 |   concat (x :: z) y = x :: concat z y
108 |
109 | public export
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
115 |
116 | public export
117 | ToList : {0 n : Nat} -> {0 obj : Type} -> {0 arr : Rel obj} -> FunctorialPipeline n obj arr -> List0 Type
118 | ToList [] = []
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
125 |
126 | public export 0
127 | Impl : {0 n : Nat} -> {0 obj : Type} -> {0 arr : Rel obj} -> FunctorialPipeline n obj arr -> Type
128 | Impl = HList0 . ToList
129 |
130 | %ambiguity_depth 15
131 |
132 | %hide Prelude.(.)
133 |
134 | 0
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} ->
140 |             let c1 = List Type
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
147 |
148 | headAll : HList0 (x :: xs) -> x
149 | headAll (x :: _) = x
150 |
151 | tailAll : HList0 (x :: xs) -> HList0 xs
152 | tailAll (_ :: xs) = xs
153 |
154 | export
155 | runFPipeline :
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)
163 |     in rec . ix
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)
167 |   in fd.mapHom rec
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
181 |