Skip to content

Instantly share code, notes, and snippets.

@dminuoso

dminuoso/f.hs Secret

Created March 7, 2022 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dminuoso/be53ec068306073650802d157ea02e5a to your computer and use it in GitHub Desktop.
Save dminuoso/be53ec068306073650802d157ea02e5a to your computer and use it in GitHub Desktop.
-- | Proceed only if there are no errors. Will error out otherwise.
(>|>) :: Comp e a -> Comp e b -> Comp e b
f >|> g = condemn f >> g
-- | Proceed only if there are no errors. Will error out otherwise.
(>|>=) :: Comp e a -> (a -> Comp e b) -> Comp e b
x >|>= f = condemn x >>= f
-- | Sequence only if there are no errors, skips the other action otherwise.
--
-- This is useful for skipping semantic checks that depend on invariants asserted
-- by an earlier pass.
(>?>) :: Comp e a -> Comp e a -> Comp e a
l >?> r = do
m <- l
e <- askErrors
case e of
[] -> r
_s -> pure m
condemn :: Comp e a -> Comp e a
condemn act = act <* cut
cut :: Comp e ()
cut = do
e <- askErrors
case e of
[] -> pure ()
_xs -> throwIO BailOut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment