Skip to content

Instantly share code, notes, and snippets.

@frasertweedale
Last active May 20, 2020 01:22
Show Gist options
  • Save frasertweedale/82e7229edaa4c85b747bc7cf92e9ac6a to your computer and use it in GitHub Desktop.
Save frasertweedale/82e7229edaa4c85b747bc7cf92e9ac6a to your computer and use it in GitHub Desktop.
workflow library
{-# LANGUAGE KindSignatures #-}
module Workflow where
import Data.Bool (bool)
data Workflow a r
= Decision a (Workflow a r) (Workflow a r)
| Guard a (Workflow a r)
| Activity a (Workflow a r)
| Conclusion a r
-- | Output from guard step. Either stop and return a result,
-- or keep going with the next step.
data Guard r = Stop r | KeepGoing
runWorkflow
:: (Monad m)
=> (a -> m Bool) -- execute decision
-> (a -> m (Guard r)) -- execute guard
-> (a -> m ()) -- execute activity
-> (a -> r -> m ()) -- execute conclusion
-> Workflow a r
-> m r
runWorkflow fd fg fa fc workflow =
let
go = runWorkflow fd fg fa fc
in
case workflow of
Decision desc t f ->
fd desc >>= go . bool f t
Guard desc next ->
fg desc >>= \g -> case g of
KeepGoing -> go next
Stop r -> pure r
Activity desc next ->
fa desc *> go next
Conclusion desc r ->
r <$ fc desc r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment