0 | module Stellar.API.Kleene
3 | import Stellar.API.Morphism
5 | %hide Prelude.Ops.infixl.(|>)
9 | data StarShp : API -> Type where
11 | More : Ex c (StarShp c) -> StarShp c
14 | data StarPos : (c : API) -> StarShp c -> Type where
15 | StarU : StarPos c Done
16 | StarM : (x : c.response x1) -> StarPos c (x2 x) -> StarPos c (More (MkEx x1 x2))
19 | (.star) : API -> API
20 | (.star) c = (!>) (StarShp c) (StarPos c)
24 | Kleene c = (!>) (StarShp c) (StarPos c)
27 | mapStarShp : (a =&> b) -> StarShp a -> StarShp b
28 | mapStarShp x Done = Done
29 | mapStarShp x (More p1) =
30 | let xx = x.continuation p1.req
31 | in More (MkEx xx.π1 (\y => assert_total $
mapStarShp x (p1.cont (xx.π2 y))))
34 | mapStarPos : (mor : a =&> b) -> (x : StarShp a) -> StarPos b (mapStarShp mor x) -> StarPos a x
35 | mapStarPos y Done z = StarU
36 | mapStarPos y (More (MkEx p1 p2)) (StarM z1 z2) = ?fixLater
41 | map_kleene : a =&> b -> a.star =&> b.star
42 | map_kleene mor = !! \x => mapStarShp mor x ## mapStarPos mor x
45 | join_star : forall a. StarShp a.star -> StarShp a
46 | join_star Done = Done
47 | join_star (More (MkEx Done p2)) = Done
48 | join_star (More (MkEx (More p2) p3)) = More p2
51 | single : a.message -> StarShp a
52 | single x = More (MkEx x (\_ => Done))
55 | pure_kleene : a =&> a.star
57 | !! \x => single {a} x ## \(StarM y1 y2) => y1
60 | unit_kleene : (End).star =&> End
61 | unit_kleene = !! \x => () ## bwd x
64 | bwd : (x : StarShp End) -> Unit -> StarPos End x
66 | bwd (More (MkEx () p2)) () = StarM () (bwd (p2 ()) ())
69 | kleeneCounit : a =&> End -> a.star =&> End
70 | kleeneCounit c = map_kleene c |&> unit_kleene
73 | runStar : a =&> b.star -> b =&> End -> a =&> End
74 | runStar m1 m2 = m1 |&> map_kleene m2 |&> unit_kleene