-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Applicative/Arrow for resource estimation and progress tracking. -- -- This module contains a writer-like Applicative for giving monoidal -- annotations to underlying computations. The annotations are available -- before running the computations. It also allows tagging different -- parts of a computation as separate steps, so that progress -- notifications can be emitted during execution. Optional steps are -- allowed. @package plan-applicative @version 2.0.0.1 -- | Prefer using the main module. If you manipulate the internals of -- Plan to add fake steps, bad things might happen. module Control.Plan.Core -- | A computation that takes inputs of type i and produces -- outputs of type o working in the underlying monad m. -- The Applicative instance cares only about the outputs, the -- Arrow instance cares about both inputs and outputs. -- -- Parts of the computation can be labeled as steps with tags of type -- s. -- -- Computations can have monoidal resource annotations of type -- w. -- -- The structure of steps and the monoidal annotations can be inspected -- before executing the Plan. data Plan s w m i o Plan :: (Steps s w) -> (Star (Stream (Of Tick') m) i o) -> Plan s w m i o -- | A Forest of steps tags of type s interspersed with -- monoidal annotations of type w. data Steps s w Steps :: !(Seq (w, s, Mandatoriness, Steps s w)) -> w -> Steps s w data Mandatoriness Skippable :: Mandatoriness Mandatory :: Mandatoriness -- | bifoldMap allows extracting the steps and the annotations -- together. -- | A catamorphism on Steps, that "destroys" the Steps value -- from the leaves upwards. -- -- Unlike foldMap or bifoldMap, it allows a more structured -- analysis of the annotations, by preserving their relationship with the -- hierarchy of steps. foldSteps :: ([(w, s, Mandatoriness, r)] -> w -> r) -> Steps s w -> r foldSteps' :: (Seq (w, s, Mandatoriness, r) -> w -> r) -> Steps s w -> r -- | Adapt the Step value inside a Plan without extracting -- it. bimapSteps :: (s -> s') -> (w -> w') -> Plan s w m i o -> Plan s' w' m i o -- | Use a lens setter to "zoom" the monoidal annotations of a Plan -- into a wider monoidal context. zoomSteps :: Monoid w' => ((w -> Identity w) -> w' -> Identity w') -> Plan s w m i o -> Plan s w' m i o -- | Change the underlying monad of a Plan. hoistPlan :: Monad m => (forall x. m x -> n x) -> Plan s w m i o -> Plan s w n i o data Tick' Skipped' :: Tick' Started' :: Tick' Finished' :: Tick' -- | Inspect a plan without executing it. getSteps :: Plan s w m i o -> Steps s w -- | Decorate each step tag with its mandatoriness. Useful in combination -- with toForest. mandatoriness :: Steps s w -> Steps (Mandatoriness, s) w -- | Declare a step by wrapping an existing plan (which may contain -- substeps). step :: (Monoid w, Monad m) => s -> Plan s w m i o -> Plan s w m i o -- | Declare an optional step by wrapping an existing arrow plan. The step -- will only be executed when the input is Just. -- -- This function only makes sense when using the Arrow instance of -- Plan, because for Applicatives an effect cannot depend -- on previously obtained values. -- --
--   >>> :{
--       let example :: Plan String () IO () ()
--           example = proc () -> do 
--               i <- step "reading" (plan (readMaybe @Int <$> getLine)) -< () 
--               skippable "writing" (kplan print) -< i
--       in  putStr . drawForest . fmap (fmap show) . toForest . mandatoriness . getSteps $ example
--       :}
--   (Mandatory,"reading")
--   
--   (Skippable,"writing")
--   
skippable :: (Monoid w, Monad m) => s -> Plan s w m i o -> Plan s w m (Maybe i) () -- | Declare a monoidal annotation. The annotation can be later inspected -- without having to run the Plan. -- -- Usually the annotations will represent resources that the Plan -- is expected to require. foretell :: (Monad m) => w -> Plan s w m i () -- | Lift a monadic action to a Plan. The input type remains -- polymorphic. plan :: (Monoid w, Monad m) => m o -> Plan s w m i o -- | Lift an IO action to a Plan. The input type remains -- polymorphic. planIO :: (Monoid w, MonadIO m) => IO o -> Plan s w m i o -- | Lift a Kleisli arrow to a Plan. kplan :: (Monoid w, Monad m) => (i -> m o) -> Plan s w m i o -- | Lift a Kleisli arrow working in IO to a Plan. kplanIO :: (Monoid w, MonadIO m) => (i -> IO o) -> Plan s w m i o zipSteps' :: Forest a -> Steps r w -> Maybe (Steps (a, r) w) -- | Pair each step tag s inside a Plan with the -- corresponding element of the Forest. -- -- If the forest doesn't have the same structure as the steps, the -- function fails with Nothing. -- -- This function can be useful to annotate each step tag with some -- information, for example the time duration of the step in a previous -- execution of the plan. See Timeline, instants, and -- toForest. zipSteps :: Forest s' -> Plan s w m i o -> Maybe (Plan (s', s) w m i o) -- | A given step might not have been reached yet. It it has been reached, -- either it has been skipped at a certain time, or started at a certain -- time. If if has been started, maybe it has already finished, too. -- -- This function can be used in combination with toForest and -- drawForest to render the state of each step for a Tick. completedness :: Tick s t -> Tick (Maybe (Either t (t, Maybe t)), s) t contextCompletedness :: (t -> (Either t (t, Maybe t))) -> Context s t -> Context (Maybe (Either t (t, Maybe t)), s) t adapt :: Timeline (Either t (t, t), s) t -> Timeline (Maybe (Either t (t, Maybe t)), s) t progressCompletedness :: t -> Progress s t -> (Progress (Maybe (Either t (t, Maybe t)), s) t, Maybe (Either t (t, Maybe t))) -- | Forget that there is a plan, get the underlying monadic action. unliftPlan :: Monad m => Plan s w m () o -> m o -- | Forget that there is a plan, get the underlying Kleisli arrow. unliftKPlan :: Monad m => Plan s w m i o -> i -> m o -- | A Forest of steps tags of type s interspersed with -- measurements of type t. data Timeline s t Timeline :: !(Seq (t, s, Either (Forest s) (Timeline s t))) -> t -> Timeline s t -- | Timelines always have at least one measurement. extract -- gives the final measurement. -- | Decorate each step tag with either the time the step was skipped, or -- the time it was started and finished. Useful in combination with -- toForest. instants :: Timeline s t -> Timeline (Either t (t, t), s) t -- | A catamorphism on Timelines, that "destroys" the -- Timeline value from the leaves upwards. foldTimeline :: ([(t, s, Either (Forest s) r)] -> t -> r) -> Timeline s t -> r foldTimeline' :: (Seq (t, c, Either (Forest c) r) -> t -> r) -> Timeline c t -> r -- | Represents how far we are along a sequence of sibling steps. -- -- For the already completed steps, a Timeline of measurements is -- provided. extract for the Timeline returns the starting -- measurement of the current step. data Context s t Context :: Timeline s t -> s -> Forest s -> Context s t [completed] :: Context s t -> Timeline s t [current] :: Context s t -> s [pending] :: Context s t -> Forest s -- | Represents some kind of progress through the Steps of a -- Plan while the plan executes. -- -- The ascending list of contexts provides the current position of the -- execution along the hierarchy of steps. -- -- If the plan only has a linear sequence of steps, the list will have -- only one Context. data Tick s t Tick :: (NonEmpty (Context s t)) -> (Progress s t) -> Tick s t -- | The execution of a Plan can make progress by skipping a step, -- starting a step, or finishing a step. data Progress s t -- | Provides the substeps that were skipped. Skipped :: (Forest s) -> Progress s t -- | Provides the substeps that will be executed next. Started :: (Forest s) -> Progress s t -- | Provides a Timeline of measurements for the completed substeps. -- extract for the Timeline gives the finishing measurement -- for the current step. Finished :: (Timeline s t) -> Progress s t -- | Specify a monadic callback for processing each Tick update. onTick :: Monad m => (tick -> m ()) -> Stream (Of tick) m r -> m r -- | Runs a plan that doesn't need input. It returns a Stream of -- Tick updates that are emitted every time the execution advances -- through the Steps. -- -- For each Tick update, a monadic measurement of type t -- is taken. Usually the measurement consists in getting the current -- time. -- -- When the execution finishes, a Timeline with the measurements -- for each Tick is returned, along with the result value. -- -- Even if the plan didn't have any steps, the Timeline will -- contain a measurement taken when the computation finished. runPlan :: Monad m => m t -> Plan s w m () o -> Stream (Of (Tick s t)) m (Timeline s t, o) -- | Like runPlan, but for Arrow-like Plans that take -- inputs. runKPlan :: Monad m => m t -> Plan s w m i o -> i -> Stream (Of (Tick s t)) m (Timeline s t, o) data RunState s t RunState :: !(Seq (t, s, Either (Forest s) (Timeline s t))) -> !(Forest s) -> ![Context s t] -> RunState s t -- | Instances of Sylvan are Forests with nodes of type -- n, interspersed with annotations of type a, and -- perhaps some other extra information. -- -- They must satisfy -- --
--   bifoldMap f (\_ -> mempty) s == foldMap (foldMap f) (toForest s)
--   
class (Bitraversable l) => Sylvan l -- | Forget about the annotations and return the underlying Forest. toForest :: Sylvan l => l n a -> Forest n -- | toForest forgets about the annotations and returns a -- Forest of step tags. -- | toForest forgets about the measurements and returns a -- Forest of step tags. -- | A Forest is a Sylvan for which no annotations exist. instance (GHC.Show.Show s, GHC.Show.Show t) => GHC.Show.Show (Control.Plan.Core.Tick s t) instance (GHC.Classes.Eq s, GHC.Classes.Eq t) => GHC.Classes.Eq (Control.Plan.Core.Tick s t) instance Data.Traversable.Traversable (Control.Plan.Core.Tick s) instance Data.Foldable.Foldable (Control.Plan.Core.Tick s) instance GHC.Base.Functor (Control.Plan.Core.Tick s) instance (GHC.Show.Show t, GHC.Show.Show s) => GHC.Show.Show (Control.Plan.Core.Progress s t) instance (GHC.Classes.Eq t, GHC.Classes.Eq s) => GHC.Classes.Eq (Control.Plan.Core.Progress s t) instance Data.Traversable.Traversable (Control.Plan.Core.Progress s) instance Data.Foldable.Foldable (Control.Plan.Core.Progress s) instance GHC.Base.Functor (Control.Plan.Core.Progress s) instance (GHC.Show.Show t, GHC.Show.Show s) => GHC.Show.Show (Control.Plan.Core.Context s t) instance (GHC.Classes.Eq t, GHC.Classes.Eq s) => GHC.Classes.Eq (Control.Plan.Core.Context s t) instance Data.Traversable.Traversable (Control.Plan.Core.Context s) instance Data.Foldable.Foldable (Control.Plan.Core.Context s) instance GHC.Base.Functor (Control.Plan.Core.Context s) instance (GHC.Show.Show s, GHC.Show.Show t) => GHC.Show.Show (Control.Plan.Core.Timeline s t) instance (GHC.Classes.Eq s, GHC.Classes.Eq t) => GHC.Classes.Eq (Control.Plan.Core.Timeline s t) instance Data.Traversable.Traversable (Control.Plan.Core.Timeline s) instance Data.Foldable.Foldable (Control.Plan.Core.Timeline s) instance GHC.Base.Functor (Control.Plan.Core.Timeline s) instance GHC.Base.Monad m => GHC.Base.Functor (Control.Plan.Core.Plan s w m i) instance GHC.Show.Show Control.Plan.Core.Tick' instance GHC.Enum.Enum Control.Plan.Core.Tick' instance GHC.Classes.Ord Control.Plan.Core.Tick' instance GHC.Classes.Eq Control.Plan.Core.Tick' instance (GHC.Show.Show s, GHC.Show.Show w) => GHC.Show.Show (Control.Plan.Core.Steps s w) instance (GHC.Classes.Eq s, GHC.Classes.Eq w) => GHC.Classes.Eq (Control.Plan.Core.Steps s w) instance Data.Traversable.Traversable (Control.Plan.Core.Steps s) instance Data.Foldable.Foldable (Control.Plan.Core.Steps s) instance GHC.Base.Functor (Control.Plan.Core.Steps s) instance GHC.Classes.Ord Control.Plan.Core.Mandatoriness instance GHC.Classes.Eq Control.Plan.Core.Mandatoriness instance GHC.Show.Show Control.Plan.Core.Mandatoriness instance (GHC.Base.Monoid w, GHC.Base.Monad m) => GHC.Base.Applicative (Control.Plan.Core.Plan s w m i) instance (GHC.Base.Monoid w, GHC.Base.Monad m) => Control.Category.Category (Control.Plan.Core.Plan s w m) instance (GHC.Base.Monoid w, GHC.Base.Monad m) => Control.Arrow.Arrow (Control.Plan.Core.Plan s w m) instance (GHC.Base.Monoid w, GHC.Base.Monad m) => Data.Profunctor.Unsafe.Profunctor (Control.Plan.Core.Plan s w m) instance Data.Bifunctor.Bifunctor Control.Plan.Core.Steps instance Data.Bifoldable.Bifoldable Control.Plan.Core.Steps instance Data.Bitraversable.Bitraversable Control.Plan.Core.Steps instance GHC.Base.Monoid w => GHC.Base.Monoid (Control.Plan.Core.Steps s w) instance Data.Bifunctor.Bifunctor Control.Plan.Core.Timeline instance Data.Bifoldable.Bifoldable Control.Plan.Core.Timeline instance Data.Bitraversable.Bitraversable Control.Plan.Core.Timeline instance Control.Comonad.Comonad (Control.Plan.Core.Timeline s) instance Data.Bifunctor.Bifunctor Control.Plan.Core.Context instance Data.Bifunctor.Bifunctor Control.Plan.Core.Tick instance Data.Bifoldable.Bifoldable Control.Plan.Core.Tick instance Data.Bitraversable.Bitraversable Control.Plan.Core.Tick instance Control.Plan.Core.Sylvan Control.Plan.Core.Tick instance Control.Plan.Core.Sylvan Control.Plan.Core.Progress instance Data.Bifunctor.Bifunctor Control.Plan.Core.Progress instance Data.Bifoldable.Bifoldable Control.Plan.Core.Progress instance Data.Bitraversable.Bitraversable Control.Plan.Core.Progress instance Control.Plan.Core.Sylvan Control.Plan.Core.Steps instance Control.Plan.Core.Sylvan Control.Plan.Core.Timeline instance Control.Plan.Core.Sylvan (Data.Bifunctor.Clown.Clown (Data.Functor.Compose.Compose [] Data.Tree.Tree)) -- | This module exports the Plan Applicative. -- --
--   >>> :{
--       let example :: Plan String [Int] IO () ()
--           example = 
--               step "a" (step "b" (foretell [1] *> plan (threadDelay 1e6)) 
--                         *>
--                         step "c" (foretell [2] *> plan (threadDelay 1e6))) 
--               *>
--               step "d" (step "e" (foretell [3] *> plan (threadDelay 1e6)) 
--                         *>
--                         step "f" (foretell [4] *> plan (threadDelay 1e6)))
--       in 
--       bifoldMap id (foldMap Prelude.show) (getSteps example)
--       :}
--   "ab1c2de3f4"
--   
-- -- Some possible use cases: -- -- module Control.Plan -- | A computation that takes inputs of type i and produces -- outputs of type o working in the underlying monad m. -- The Applicative instance cares only about the outputs, the -- Arrow instance cares about both inputs and outputs. -- -- Parts of the computation can be labeled as steps with tags of type -- s. -- -- Computations can have monoidal resource annotations of type -- w. -- -- The structure of steps and the monoidal annotations can be inspected -- before executing the Plan. data Plan s w m i o -- | Lift a monadic action to a Plan. The input type remains -- polymorphic. plan :: (Monoid w, Monad m) => m o -> Plan s w m i o -- | Lift an IO action to a Plan. The input type remains -- polymorphic. planIO :: (Monoid w, MonadIO m) => IO o -> Plan s w m i o -- | Lift a Kleisli arrow to a Plan. kplan :: (Monoid w, Monad m) => (i -> m o) -> Plan s w m i o -- | Lift a Kleisli arrow working in IO to a Plan. kplanIO :: (Monoid w, MonadIO m) => (i -> IO o) -> Plan s w m i o -- | Declare a step by wrapping an existing plan (which may contain -- substeps). step :: (Monoid w, Monad m) => s -> Plan s w m i o -> Plan s w m i o -- | Declare an optional step by wrapping an existing arrow plan. The step -- will only be executed when the input is Just. -- -- This function only makes sense when using the Arrow instance of -- Plan, because for Applicatives an effect cannot depend -- on previously obtained values. -- --
--   >>> :{
--       let example :: Plan String () IO () ()
--           example = proc () -> do 
--               i <- step "reading" (plan (readMaybe @Int <$> getLine)) -< () 
--               skippable "writing" (kplan print) -< i
--       in  putStr . drawForest . fmap (fmap show) . toForest . mandatoriness . getSteps $ example
--       :}
--   (Mandatory,"reading")
--   
--   (Skippable,"writing")
--   
skippable :: (Monoid w, Monad m) => s -> Plan s w m i o -> Plan s w m (Maybe i) () -- | Declare a monoidal annotation. The annotation can be later inspected -- without having to run the Plan. -- -- Usually the annotations will represent resources that the Plan -- is expected to require. foretell :: (Monad m) => w -> Plan s w m i () -- | Inspect a plan without executing it. getSteps :: Plan s w m i o -> Steps s w -- | A Forest of steps tags of type s interspersed with -- monoidal annotations of type w. data Steps s w -- | Decorate each step tag with its mandatoriness. Useful in combination -- with toForest. mandatoriness :: Steps s w -> Steps (Mandatoriness, s) w data Mandatoriness Skippable :: Mandatoriness Mandatory :: Mandatoriness -- | A catamorphism on Steps, that "destroys" the Steps value -- from the leaves upwards. -- -- Unlike foldMap or bifoldMap, it allows a more structured -- analysis of the annotations, by preserving their relationship with the -- hierarchy of steps. foldSteps :: ([(w, s, Mandatoriness, r)] -> w -> r) -> Steps s w -> r -- | Adapt the Step value inside a Plan without extracting -- it. bimapSteps :: (s -> s') -> (w -> w') -> Plan s w m i o -> Plan s' w' m i o -- | Use a lens setter to "zoom" the monoidal annotations of a Plan -- into a wider monoidal context. zoomSteps :: Monoid w' => ((w -> Identity w) -> w' -> Identity w') -> Plan s w m i o -> Plan s w' m i o -- | Pair each step tag s inside a Plan with the -- corresponding element of the Forest. -- -- If the forest doesn't have the same structure as the steps, the -- function fails with Nothing. -- -- This function can be useful to annotate each step tag with some -- information, for example the time duration of the step in a previous -- execution of the plan. See Timeline, instants, and -- toForest. zipSteps :: Forest s' -> Plan s w m i o -> Maybe (Plan (s', s) w m i o) -- | Change the underlying monad of a Plan. hoistPlan :: Monad m => (forall x. m x -> n x) -> Plan s w m i o -> Plan s w n i o -- | Forget that there is a plan, get the underlying monadic action. unliftPlan :: Monad m => Plan s w m () o -> m o -- | Runs a plan that doesn't need input. It returns a Stream of -- Tick updates that are emitted every time the execution advances -- through the Steps. -- -- For each Tick update, a monadic measurement of type t -- is taken. Usually the measurement consists in getting the current -- time. -- -- When the execution finishes, a Timeline with the measurements -- for each Tick is returned, along with the result value. -- -- Even if the plan didn't have any steps, the Timeline will -- contain a measurement taken when the computation finished. runPlan :: Monad m => m t -> Plan s w m () o -> Stream (Of (Tick s t)) m (Timeline s t, o) -- | Specify a monadic callback for processing each Tick update. onTick :: Monad m => (tick -> m ()) -> Stream (Of tick) m r -> m r -- | Represents some kind of progress through the Steps of a -- Plan while the plan executes. -- -- The ascending list of contexts provides the current position of the -- execution along the hierarchy of steps. -- -- If the plan only has a linear sequence of steps, the list will have -- only one Context. data Tick s t Tick :: (NonEmpty (Context s t)) -> (Progress s t) -> Tick s t -- | A given step might not have been reached yet. It it has been reached, -- either it has been skipped at a certain time, or started at a certain -- time. If if has been started, maybe it has already finished, too. -- -- This function can be used in combination with toForest and -- drawForest to render the state of each step for a Tick. completedness :: Tick s t -> Tick (Maybe (Either t (t, Maybe t)), s) t -- | Represents how far we are along a sequence of sibling steps. -- -- For the already completed steps, a Timeline of measurements is -- provided. extract for the Timeline returns the starting -- measurement of the current step. data Context s t Context :: Timeline s t -> s -> Forest s -> Context s t [completed] :: Context s t -> Timeline s t [current] :: Context s t -> s [pending] :: Context s t -> Forest s -- | The execution of a Plan can make progress by skipping a step, -- starting a step, or finishing a step. data Progress s t -- | Provides the substeps that were skipped. Skipped :: (Forest s) -> Progress s t -- | Provides the substeps that will be executed next. Started :: (Forest s) -> Progress s t -- | Provides a Timeline of measurements for the completed substeps. -- extract for the Timeline gives the finishing measurement -- for the current step. Finished :: (Timeline s t) -> Progress s t -- | A Forest of steps tags of type s interspersed with -- measurements of type t. data Timeline s t -- | Decorate each step tag with either the time the step was skipped, or -- the time it was started and finished. Useful in combination with -- toForest. instants :: Timeline s t -> Timeline (Either t (t, t), s) t -- | A catamorphism on Timelines, that "destroys" the -- Timeline value from the leaves upwards. foldTimeline :: ([(t, s, Either (Forest s) r)] -> t -> r) -> Timeline s t -> r -- | Forget that there is a plan, get the underlying Kleisli arrow. unliftKPlan :: Monad m => Plan s w m i o -> i -> m o -- | Like runPlan, but for Arrow-like Plans that take -- inputs. runKPlan :: Monad m => m t -> Plan s w m i o -> i -> Stream (Of (Tick s t)) m (Timeline s t, o) -- | Instances of Sylvan are Forests with nodes of type -- n, interspersed with annotations of type a, and -- perhaps some other extra information. -- -- They must satisfy -- --
--   bifoldMap f (\_ -> mempty) s == foldMap (foldMap f) (toForest s)
--   
class (Bitraversable l) => Sylvan l -- | Forget about the annotations and return the underlying Forest. toForest :: Sylvan l => l n a -> Forest n -- | Map over both arguments at the same time. -- --
--   bimap f g ≡ first f . second g
--   
bimap :: Bifunctor p => forall a b c d. (a -> b) -> (c -> d) -> p a c -> p b d -- | Combines the elements of a structure, given ways of mapping them to a -- common monoid. -- --
--   bifoldMap f g ≡ bifoldr (mappend . f) (mappend . g) mempty
--   
bifoldMap :: Bifoldable p => forall m a b. Monoid m => (a -> m) -> (b -> m) -> p a b -> m -- | Evaluates the relevant functions at each element in the structure, -- running the action, and builds a new structure with the same shape, -- using the elements produced from sequencing the actions. -- --
--   bitraverse f g ≡ bisequenceA . bimap f g
--   
-- -- For a version that ignores the results, see bitraverse_. bitraverse :: Bitraversable t => forall (f :: * -> *) a c b d. Applicative f => (a -> f c) -> (b -> f d) -> t a b -> f (t c d) -- |
--   extract . fmap f = f . extract
--   
extract :: Comonad w => forall a. w a -> a -- | Lift a monad morphism from m to n into a monad -- morphism from (t m) to (t n) hoist :: MFunctor t => forall (m :: * -> *) (n :: * -> *) b. Monad m => (forall a. m a -> n a) -> t m b -> t n b -- | Reduce a stream, performing its actions but ignoring its elements. -- --
--   >>> rest <- S.effects $ S.splitAt 2 $ each [1..5]
--   
--   >>> S.print rest
--   3
--   4
--   5
--       'effects' should be understood together with 'copy' and is subject to the rules
--   
-- --
--   S.effects . S.copy       = id
--   hoist S.effects . S.copy = id
--   
-- -- The similar effects and copy operations in -- Data.ByteString.Streaming obey the same rules. effects :: Monad m => Stream (Of a) m r -> m r