0 | module Pipeline.Category.Graded
 1 |
 2 | import public Data.Category.Graded
 3 | import public Control.Set
 4 | import public Control.Category
 5 | import public Data.Vect
 6 | import public Data.Vect.Erased
 7 |
 8 | -- This ensures the functor `f` is strong wrt to the monoid `mon`
 9 | export
10 | interface CoprodStrength (mon : Monoid a) (f : a -> Type) | f where
11 |   constructor MkFCoprod
12 |   left : {x, y : _} -> f x -> f (x <+> y)
13 |   right : {x, y : _} -> f y -> f (x <+> y)
14 |
15 | -- The identity functor is strong wrt to Either as a monoid on types
16 | export
17 | CoprodStrength EitherMon (\x => x) using EitherSem where
18 |   left = Left
19 |   right = Right
20 |
21 | -- The OneOf functor is strong wrt to the free monoid on types
22 | export
23 | CoprodStrength AnyMon OneOf using AnySem where
24 |   left = Any.left
25 |   right = Any.right
26 |
27 | -- The following is to avoid clashes between `.` as function composition and `.` as morphism composition
28 | %hide Basics.(.)
29 |
30 | -- Given any monoid and a strong functor, we can build the graded category using the monoid given
31 | -- and GradedCorproduct morphisms indexed by `f` using the grade given by the monoid
32 | public export %hint
33 | MkInst : {0 gr : Type} -> {f : gr -> Type} -> (mon : Monoid gr) => (fc : CoprodStrength mon f) => GradedCat mon (GradedCoproductMonad {e = gr} f)
34 | MkInst {fc = MkFCoprod ll rr} = MkGradedCat Right
35 |   (\m1, m2 => (mapFst ll . m1) @{Set} >=> (mapFst rr . m2) @{Set})
36 |
37 | -- This is merely a fold but we write it inductively explicitly so that it evaluates nicesly when asking for the
38 | -- types of `runGradedCat`
39 | public export
40 | foldGrades : Monoid gr => Vect (S n) gr -> gr
41 | foldGrades (x :: []) = x
42 | foldGrades (x :: (y :: xs)) = x <+> foldGrades (y :: xs)
43 |
44 | public export
45 | zip0 : Vect n a -> Vect0 n b -> Vect0 n (a, b)
46 | zip0 [] [] = []
47 | zip0 (x :: z) (y :: w) = (x, y) :: zip0 z w
48 |
49 | -- The common parameters to run pipelines in a graded category, we parameterised everything by
50 | -- the grade, the objects and the graded morphisms
51 | parameters {0 gr : Type}  {0 obj : Type} {0 arr : gr -> obj -> obj -> Type}
52 |
53 |   -- An implementation needs both the layers of the pipeline, and the grades of each morphism.
54 |   -- For each triple of `source`, `target` and `grade` we build the graded morphism `source -[grade]> target`
55 |   -- as the type of the implementation of the corresponding stage
56 |   public export 0
57 |   ImplGr : forall n. Vect0 (2 + n) obj -> Vect (S n) gr -> Type
58 |   ImplGr layers grades = All0 (\arg => arr (fst arg) (fst (snd arg)) (snd (snd arg))) (zip0 grades (pairUp layers))
59 |
60 |   -- To run a graded pipeline we need evidence that the grade is a monoid, that the category is a graded category
61 |   -- and we need a graded implementation of the pipeline. The resulting morphism is a graded morphism from the
62 |   -- start of the pipeline, to the end, except that all the grades are accumulated using `foldGrades` as a result
63 |   -- of composing each graded morphism from the implementation.
64 |   export
65 |   RunGrCat : forall n. {auto mon : Monoid gr} -> (cat : GradedCat mon arr) ->
66 |              (p : Vect0 (2 + n) obj) ->
67 |              (grades : Vect (S n) gr) ->
68 |              ImplGr p grades -> arr (foldGrades grades) (Head p) (Last p)
69 |   RunGrCat cat [x, y] [gr] [f] = f
70 |   RunGrCat cat (x :: y :: z :: xs) (gr1 :: g :: gs) (f :: c :: cs) =
71 |     let rest = RunGrCat cat (y :: z :: xs) (g :: gs) (c :: cs)
72 |     in (#>) @{cat} {g1 = gr1, x, y, g2 = foldGrades (g :: gs), z = Last (z :: xs)} f rest
73 |