0 | module Pipeline.Basic
 1 |
 2 | import public Data.Vect
 3 | import public Data.Vect.Erased
 4 | import public Data.Vect.Quantifiers
 5 | import Control.Set
 6 | import Debug.Trace
 7 |
 8 | public export
 9 | Pipeline : Nat -> Type
10 | Pipeline length = Vect0 length Type
11 |
12 | public export
13 | Impl : Pipeline (2 + n) -> Type
14 | Impl cs = All0 (uncurry Fn) (pairUp cs)
15 |
16 | export
17 | Run : (p : Pipeline (2 + n)) -> Impl p -> Head p -> Last p
18 | Run [x, y] [f] v = f v
19 | Run (s :: t :: x :: xs) (f :: cont) v = Run (t :: x :: xs) cont (f v)
20 |
21 | export
22 | RunTrace : (p : Pipeline (2 + n)) -> (print : All0 Show p) => Impl p -> Head p -> Last p
23 | RunTrace {print = [p, q]} [x, y] [f] v = traceValBy show (f v)
24 | RunTrace {print = p :: q :: ps} (s :: t :: x :: xs) (f :: cont) v =
25 |   Run (t :: x :: xs) cont (traceValBy show (f v))
26 |
27 | parameters (m : Type -> Type)
28 |
29 |   public export
30 |   ImplM : Pipeline (2 + n) -> Type
31 |   ImplM cs = All0 (uncurry (KleisliFn m)) (pairUp cs)
32 |
33 |   export
34 |   RunM : (mon : Monad m) => (p : Pipeline (2 + n)) -> ImplM p -> Head p -> m (Last p)
35 |   RunM [x, y]  [f] = f
36 |   RunM (x :: y :: z :: xs) (f :: c) = RunM (y :: z :: xs) c <=< f
37 |
38 |   export
39 |   RunTraceM : Monad m => (p : Pipeline (2 + n)) -> (print : All0 Show p) => ImplM p -> Head p -> m (Last p)
40 |   RunTraceM [x, y] {print = [p1, p2]} [f] = f >=> pure . traceValBy show
41 |   RunTraceM (x :: y :: z :: xs) {print = p :: q :: ps} (f :: c) =
42 |     f >=> pure . traceValBy show >=> RunTraceM (y :: z :: xs) c
43 |
44 | data Cmd : Type where
45 |
46 | data Content : Type where
47 |
48 | data Output : Type where
49 |
50 | example : Vect0 ? Type
51 | example =
52 |   [ String
53 |   , IO String
54 |   , IO (Maybe Cmd)
55 |   , Maybe (IO Cmd)
56 |   , Maybe (IO Content)
57 |   , Maybe (IO Output)
58 |   ]
59 |
60 | distIOMaybe : Maybe (IO a) -> IO (Maybe a)
61 | distIOMaybe Nothing = pure Nothing
62 | distIOMaybe (Just x) = map pure x
63 |
64 | runExample : String -> Maybe (IO Output)
65 | runExample = Run example
66 |   [ ?readARg
67 |   , ?parseCmd
68 |   , ?distrib
69 |   , map (>>= ?readContent)
70 |   , map (>>= ?printOutput)
71 |   ]
72 |
73 |