public exportmapStarFw : (a =%> b) -> StarFw a -> StarFw bmapStarFw x Done = DonemapStarFw x (More p1 p2) = More (x.fwd p1) (\y => assert_total $ mapStarFw x (p2 (x.bwd p1 y)))public exportmapStarUntil : (Maybe • a =%> b) -> StarFw a -> StarFw bmapStarUntil m Done = DonemapStarUntil m (More p1 p2) = More (m.fwd p1) (\y => case m.bwd p1 y of Just x => assert_total $ mapStarUntil m (p2 x) ; Nothing => Done)public exportmapStarBw : (mor : a =%> b) -> (x : StarFw a) -> StarBw b (mapStarFw mor x) -> StarBw a xmapStarBw y Done z = StarUmapStarBw y (More p1 p2) (StarM z1 z2) = StarM (y.bwd p1 z1) (mapStarBw y (p2 (y.bwd p1 z1)) z2)public exportmap_kleene : a =%> b -> Star a =%> Star bmap_kleene mor = mapStarFw mor <! mapStarBw morjoin_star : forall a. StarFw (Star a) -> StarFw ajoin_star Done = Donejoin_star (More Done p2) = Donejoin_star (More (More p1 p2) p3) = More p1 p2public exportsingle : a.req -> StarFw asingle x = More x (\_ => Done)public exportpure : a =%> Star apure = (single {a}) <! (\x, (StarM y1 y2) => y1)public exportunit_kleene : Star I =%> Iunit_kleene = const () <! bwd where bwd : (x : StarFw I) -> Unit -> StarBw I x bwd Done y = StarU bwd (More () p2) () = StarM () (bwd (p2 ()) ())exportShow a.req => Show (StarFw a) where show Done = "done" show (More x f) = "more: " ++ show xnamespace Tensor -- λ x : Container -> μy . I + x ⊗ y data TensorShp : Container -> Type where Done : TensorShp c More : c.req * TensorShp c -> TensorShp c TensorPos : (c : Container) -> TensorShp c -> Type TensorPos c Done = Unit TensorPos c (More (head && tail)) = (c.res head) * (TensorPos c tail) Tensor : Container -> Container Tensor c = (!>) (TensorShp c) (TensorPos c)-- Absord one layed of composition into a Kleene starpublic exportabsorb : a ▷ Star a =%> Star aabsorb = (\(MkEx x y) => More x y) <! \(MkEx x y), (StarM z w) => z ## w-- Decompose one layer of the kleene star into either the unit, or more computationexportdecomposeStar : Star a =%> I + (a ▷ Star a)decomposeStar = peel <! peelBack where peel : StarFw a -> () + Ex a (StarFw a) peel Done = <+ () peel (More x f) = +> MkEx x f peelBack : (x : StarFw a) -> (I + (a ▷ Star a)).res (peel x) -> StarBw a x peelBack Done y = StarU peelBack (More x f) y = StarM y.π1 y.π2exportdecomposeStar' : Star a =%> I + Star adecomposeStar' = decomposeStar |%> identity I ~+~ absorbsplitFwd : ((I + a) ▷ b).req -> b.req + Ex a b.reqsplitFwd (MkEx (<+ x) ex2) = <+ ex2 ()splitFwd (MkEx (+> x) ex2) = +> MkEx x ex2splitBwd : (x : ((I + a) ▷ b).req) -> (b + (a ▷ b)).res (splitFwd {a} {b} x) -> ((I + a) ▷ b).res xsplitBwd (MkEx (<+ x) ex2) y = () ## ysplitBwd (MkEx (+> x) ex2) y = ysplitA : (I + a) ▷ b =%> b + (a ▷ b)splitA = splitFwd <! splitBwdrec : (x : StarFw I) -> StarBw I xrec Done = StarUrec (More x f) = StarM () (rec (f ()))exportallUnits : Star I =%> IallUnits = const () <! \x, y => rec xexportcommuteMaybeStar : Maybe • Star a =%> Star (Maybe • a)commuteMaybeStar = commuteShp <! commutePos where commuteShp : StarFw a -> StarFw ((x : a.req) !> Maybe (a .res x)) commuteShp Done = Done commuteShp (More x f) = More x $ maybe Done (commuteShp . f) commutePos : (x : StarFw a) -> (Star (Maybe • a)).res (commuteShp x) -> Maybe (StarBw a x) commutePos Done StarU = Just StarU commutePos (More x f) (StarM Nothing StarU) = Nothing commutePos (More x f) (StarM (Just y) z) = map (StarM y) (commutePos (f y) z)exportcommuteEitherStar : Either e • Star a =%> Star (Either e • a)commuteEitherStar = commuteShp <! commutePos where commuteShp : StarFw a -> StarFw ((x : a.req) !> Either e (a .res x)) commuteShp Done = Done commuteShp (More x f) = More x $ either (const Done) (commuteShp . f) commutePos : (x : StarFw a) -> (Star (Either e • a)).res (commuteShp x) -> Either e (StarBw a x) commutePos Done StarU = Right StarU commutePos (More x f) (StarM (Left e) StarU) = Left e commutePos (More x f) (StarM (Right y) z) = map (StarM y) (commutePos (f y) z){-coveringrecc : a =%> Star a -> (x : StarFw a) -> StarBw a xrecc m Done = StarUrecc (m1 <! m2) (More x f) = let rec1 = recc (m1 <! m2) (m1 x) xv = m2 x rec1 rec2 = recc (m1 <! m2) (f xv) in StarM xv rec2covering exportloop : a =%> Star a -> Costate (Star a)loop m = const () <! \x, _ => recc m x{-namespace UniversalComp -- λ x : Container -> μy . I + x ▶ y data StarFw : Container -> Type where Done : StarFw c More : Ex c (StarFw c) -> StarFw c StarBw : (c : Container) -> StarFw c -> Type StarBw c Done = Unit StarBw c (More (x1 ## x2)) = (x : c.res x1) -> StarBw c (x2 x) Star : Container -> Container Star c = (!>) (StarFw c) (StarBw c)coveringdata StarFwBuilder : (act : Container -> Type -> Type) -> Container -> Type where Done : StarFwBuilder act c More : {act : Container -> Type -> Type} -> {rest : StarFwBuilder act c} -> (act c (StarFwBuilder act c)) -> StarFwBuilder act c{-StarBuilder : (act : Container -> Type -> Type) -> (bwd : (c : Container) -> {x : Type} -> Act c x) -> Container -> ContainerStarBuilder act c = (!>) (StarFwBuilder act c) StarBwBuilder where StarBwBuilder : StarFwBuilder act c -> Type StarBwBuilder arg = ?hol