Skip to content

Commit

Permalink
Add McCarthy's conditional combinators (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
bolt12 authored and snowleopard committed Jan 12, 2020
1 parent 32abed1 commit 9824756
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions test/Sketch.hs
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,28 @@ t7 x y z =
((\mbc cd -> maybe (Right Nothing) (\bc -> Left $ fmap ((cd . bc) .)) mbc) <$> y))
((&) <$> z)

------------------------ McCarthy's Conditional combinator -------------------------
-- See: http://www4.di.uminho.pt/~jno/ps/pdbc.pdf
-- And also: https://themattchan.com/docs/algprog.pdf

-- Guard function used in McCarthy's conditional
-- | It provides information about the outcome of testing @p@ on some input @a@,
-- encoded in terms of the coproduct injections without losing the input
-- @a@ itself.
grdS :: Applicative f => f (a -> Bool) -> f a -> f (Either a a)
grdS f a = (selector <$> (f <*> a)) <*> a
where
selector = bool Right Left

-- | McCarthy's conditional, denoted p -> f,g is a well-known functional
-- combinator, which suggests that, to reason about conditionals, one may
-- seek help in the algebra of coproducts.
--
-- This combinator is very similar to the very nature of the 'select'
-- operator and benefits from a series of properties and laws.
condS :: Selective f => f (b -> Bool) -> f (b -> c) -> f (b -> c) -> f b -> f c
condS p f g = (\r -> branch r f g) . grdS p

------------------------ Carter Schonwald's copatterns -------------------------
-- See: https://github.com/cartazio/symmetric-monoidal/blob/15b209953b7d4a47651f615b02dbb0171de8af40/src/Control/Monoidal.hs#L93
-- And also: https://twitter.com/andreymokhov/status/1102648479841701888
Expand Down

0 comments on commit 9824756

Please sign in to comment.