0 | module Control.RIO.App
2 | import public Data.List.Quantifiers.Extra
3 | import public Control.RIO
14 | 0 App : (xs : List Type) -> (a : Type) -> Type
15 | App xs a = RIO (HSum xs) a
25 | throw : Has x xs => (err : x) -> App xs a
26 | throw err = fail (inject err)
31 | injectRIO : Has x xs => RIO x a -> App xs a
32 | injectRIO = mapFst inject
37 | injectEither : Has x xs => Either x a -> App xs a
38 | injectEither (Left v) = throw v
39 | injectEither (Right v) = pure v
44 | injectIO : Has x xs => IO (Either x a) -> App xs a
45 | injectIO = injectRIO . liftEitherIO
53 | 0 Handler : (a,x : Type) -> Type
54 | Handler a x = x -> RIO Void a
61 | handle : (prf : Has x xs) => Handler a x -> App xs a -> App (xs - x) a
64 | case map f (decomp @{prf} u) of
68 | hall : (prf : All (Handler a) ts) => HSum ts -> RIO Void a
69 | hall @{h :: t} (Here v) = h v
70 | hall @{h :: t} (There v) = hall v
71 | hall @{[]} x = absurd x
75 | handleAll : (prf : All (Handler a) xs) => App xs a -> RIO Void a
76 | handleAll = catch $
\u => hall u
82 | handleAllDflt : (prf : All (Handler ()) xs) => a -> App xs a -> RIO Void a
83 | handleAllDflt v = handleAll @{mapProperty (($> v) .) prf}
89 | runApp : All (Handler ()) xs -> App xs () -> IO ()
91 | True <- run $
catch (\u => hall @{hs} u $> False) (app $> True)
92 | | False => exitFailure