|
| 1 | +{-# LANGUAGE TypeFamilies #-} |
| 2 | + |
| 3 | +module Control.CSD where |
| 4 | + |
| 5 | +import Control.Concurrent.Async |
| 6 | +import Data.Kind |
| 7 | +import Control.Arrow |
| 8 | + |
| 9 | +infix 0 ≃ |
| 10 | +infixr 1 >>> |
| 11 | +infixr 3 *** |
| 12 | + |
| 13 | +------------------------------------------------------------------------------- |
| 14 | +-- * Site Configurations |
| 15 | + |
| 16 | +data Local (a :: Type) |
| 17 | + |
| 18 | +-- Configuration equality |
| 19 | +data a ≃ b where |
| 20 | + -- conjunction rules |
| 21 | + Swap :: (a, b) ≃ (b, a) |
| 22 | + AssocL :: (a, (b, c)) ≃ ((a, b), c) |
| 23 | + AssocR :: ((a, b), c) ≃ (a, (b, c)) |
| 24 | + CongL :: a ≃ b -> (ctx, a) ≃ (ctx, b) |
| 25 | + CongR :: a ≃ b -> (a, ctx) ≃ (b, ctx) |
| 26 | + |
| 27 | + -- Disjunction rules |
| 28 | + Idem :: Either a a ≃ a |
| 29 | + |
| 30 | + Trans :: a ≃ b -> b ≃ c -> a ≃ c |
| 31 | + |
| 32 | +------------------------------------------------------------------------------- |
| 33 | +-- * CSDs |
| 34 | + |
| 35 | +data CSD f a b where |
| 36 | + -- sequence composition |
| 37 | + Perf :: f a b -> CSD f (Local a) (Local b) |
| 38 | + Seq :: CSD f a b -> CSD f b c -> CSD f a c |
| 39 | + |
| 40 | + -- parallel composition |
| 41 | + Par :: CSD f a c -> CSD f b d -> CSD f (a, b) (c, d) |
| 42 | + Fork :: CSD f (Local (a, b)) (Local a, Local b) |
| 43 | + Join :: CSD f (Local a, Local b) (Local (a, b)) |
| 44 | + Perm :: a ≃ b -> CSD f a b |
| 45 | + |
| 46 | + -- Conditional execution |
| 47 | + Split :: CSD f (Local (Either a b)) (Either (Local a) (Local b)) |
| 48 | + Notify :: CSD f (Either a b, c) (Either (a, c) (b, c)) |
| 49 | + Branch :: CSD f a c -> CSD f b d -> CSD f (Either a b) (Either c d) |
| 50 | + |
| 51 | +noop :: (Arrow f) => CSD f (Local a) (Local a) |
| 52 | +noop = Perf (arr id) |
| 53 | + |
| 54 | +perf :: (Monad m) => (a -> m b) -> CSD (Kleisli m) (Local a) (Local b) |
| 55 | +perf f = Perf (Kleisli f) |
| 56 | + |
| 57 | +-- the following operators are named after arrow operators share the same behavior |
| 58 | + |
| 59 | +(>>>) :: CSD f a b -> CSD f b c -> CSD f a c |
| 60 | +a >>> b = Seq a b |
| 61 | + |
| 62 | +(***) :: CSD f a c -> CSD f b d -> CSD f (a, b) (c, d) |
| 63 | +a *** b = Par a b |
| 64 | + |
| 65 | +------------------------------------------------------------------------------- |
| 66 | +-- An interpreations to `Async` |
| 67 | + |
| 68 | +-- There should be a more general way to interpret CSDs, and the `Async`ed |
| 69 | +-- interpretation is an instance of it |
| 70 | + |
| 71 | +type family Asynced cfg where |
| 72 | + Asynced (a, b) = (Asynced a, Asynced b) |
| 73 | + Asynced (Either a b) = (Either (Asynced a) (Asynced b)) |
| 74 | + Asynced (Local a) = Async a |
| 75 | + |
| 76 | +interpAsynced :: (forall a b. f a b -> a -> IO b) -> CSD f a b -> Asynced a -> IO (Asynced b) |
| 77 | +interpAsynced hdl (Perf eff) a = do |
| 78 | + async $ do |
| 79 | + a' <- wait a |
| 80 | + hdl eff a' |
| 81 | +interpAsynced hdl (Seq f g) a = do |
| 82 | + b <- interpAsynced hdl f a |
| 83 | + interpAsynced hdl g b |
| 84 | +interpAsynced hdl (Par f g) (a, b) = do |
| 85 | + c <- interpAsynced hdl f a |
| 86 | + d <- interpAsynced hdl g b |
| 87 | + return (c, d) |
| 88 | +interpAsynced _ Fork ab = do |
| 89 | + a' <- async $ do |
| 90 | + (a, _) <- wait ab |
| 91 | + return a |
| 92 | + b' <- async $ do |
| 93 | + (_, b) <- wait ab |
| 94 | + return b |
| 95 | + return (a', b') |
| 96 | +interpAsynced _ Join (a, b) = do |
| 97 | + async $ do |
| 98 | + a' <- wait a |
| 99 | + b' <- wait b |
| 100 | + return (a', b') |
| 101 | +interpAsynced _ Split input = do |
| 102 | + i <- wait input |
| 103 | + case i of |
| 104 | + (Left x) -> Left <$> async (return x) |
| 105 | + (Right y) -> Right <$> async (return y) |
| 106 | +interpAsynced _ Notify (input1, input2) = do |
| 107 | + case input1 of |
| 108 | + (Left x) -> return (Left (x, input2)) |
| 109 | + (Right y) -> return (Right (y, input2)) |
| 110 | +interpAsynced hdl (Branch f g) input = do |
| 111 | + case input of |
| 112 | + (Left x) -> Left <$> interpAsynced hdl f x |
| 113 | + (Right y) -> Right <$> interpAsynced hdl g y |
| 114 | +-- structural rules |
| 115 | +interpAsynced _ (Perm Swap) (a, b) = return (b, a) |
| 116 | +interpAsynced hdl (Perm (CongL e)) (ctx, a) = (ctx,) <$> interpAsynced hdl (Perm e) a |
| 117 | +interpAsynced hdl (Perm (CongR e)) (a, ctx) = (,ctx) <$> interpAsynced hdl (Perm e) a |
| 118 | +interpAsynced _ (Perm AssocL) (a, (b, c)) = return ((a, b), c) |
| 119 | +interpAsynced _ (Perm AssocR) ((a, b), c) = return (a, (b, c)) |
| 120 | +interpAsynced _ (Perm Idem) (Left a) = return a |
| 121 | +interpAsynced _ (Perm Idem) (Right a) = return a |
| 122 | +interpAsynced hdl (Perm (Trans e1 e2)) input = interpAsynced hdl (Perm e1) input >>= interpAsynced hdl (Perm e2) |
0 commit comments