Skip to content

Commit

Permalink
Merge pull request #40 from solomon-b/solomon/semigroupal-infix-opera…
Browse files Browse the repository at this point in the history
…tors

Adds common Semigroupal infix operators
  • Loading branch information
solomon-b authored Jan 10, 2024
2 parents 87fd721 + bfdb1d8 commit 17fb4ee
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Revision history for monoidal-functors

## Upcoming
* Adds common infix operators for Semigroupal.

## 0.2.3.0 -- 2023-08-03
* Add support for GHC 9.6
Expand Down
32 changes: 31 additions & 1 deletion src/Data/Bifunctor/Monoidal.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
module Data.Bifunctor.Monoidal
( -- * Semigroupal
Semigroupal (..),
(|??|),
(|**|),
(|++|),
(|&&|),
(|+*|),

-- * Unital
Unital (..),
Expand Down Expand Up @@ -28,7 +33,7 @@ import Data.Semigroupoid (Semigroupoid (..))
import Data.These (These (..), these)
import Data.Tuple (fst, snd, uncurry)
import Data.Void (Void, absurd)
import Prelude (Either (..))
import Prelude (Either (..), curry)

--------------------------------------------------------------------------------

Expand Down Expand Up @@ -212,6 +217,31 @@ instance Alternative f => Semigroupal (->) (,) Either (,) (Forget (f r)) where
combine :: (Forget (f r) x y, Forget (f r) x' y') -> Forget (f r) (x, x') (Either y y')
combine (Forget f, Forget g) = Forget $ \(x, x') -> f x <|> g x'

infixr 9 |??|

(|??|) :: Semigroupal (->) t1 t2 (,) p => p a b -> p a' b' -> p (a `t1` a') (b `t2` b')
(|??|) = curry combine

infixr 9 |**|

(|**|) :: (Semigroupal (->) (,) (,) (,) p) => p a b -> p a' b' -> p (a, a') (b, b')
(|**|) = curry combine

infixr 9 |++|

(|++|) :: (Semigroupal (->) Either Either (,) p) => p a b -> p a' b' -> p (Either a a') (Either b b')
(|++|) = curry combine

infixr 9 |&&|

(|&&|) :: (Semigroupal (->) These These (,) p) => p a b -> p a' b' -> p (These a a') (These b b')
(|&&|) = curry combine

infixr 9 |+*|

(|+*|) :: (Semigroupal (->) Either (,) (,) p) => p a b -> p a' b' -> p (Either a a') (b, b')
(|+*|) = curry combine

--------------------------------------------------------------------------------

-- | Given monoidal categories \((\mathcal{C}, \otimes, I_{\mathcal{C}})\) and \((\mathcal{D}, \bullet, I_{\mathcal{D}})\).
Expand Down
9 changes: 0 additions & 9 deletions src/Data/Functor/Monoidal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ module Data.Functor.Monoidal
Semigroupal (..),
(|?|),
(|*|),
type (|*|),
(|+|),
type (|+|),
(|&|),
type (|&|),

-- * Unital
Unital (..),
Expand Down Expand Up @@ -341,12 +338,6 @@ infixr 3 |&|
(|&|) :: Semigroupal (->) These (,) f => f a -> f b -> f (These a b)
(|&|) = curry combine

type (|*|) = (,)

type (|+|) = Either

type (|&|) = These

--------------------------------------------------------------------------------

-- | Given monoidal categories \((\mathcal{C}, \otimes, I_{\mathcal{C}})\) and \((\mathcal{D}, \bullet, I_{\mathcal{D}})\).
Expand Down
26 changes: 26 additions & 0 deletions src/Data/Trifunctor/Monoidal.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module Data.Trifunctor.Monoidal
( -- * Semigroupal
Semigroupal (..),
(|***|),
(|*+*|),
(|+++|),
(|&&&|),

-- * Unital
Unital (..),
Expand All @@ -13,6 +17,8 @@ where
--------------------------------------------------------------------------------

import Control.Category.Tensor (Associative, Tensor)
import Prelude (curry, Either)
import Data.These (These)

--------------------------------------------------------------------------------

Expand Down Expand Up @@ -49,6 +55,26 @@ class
-- | A natural transformation \(\phi_{ABC,XYZ} : F\ A\ B\ C \bullet F\ X\ Y\ Z \to F\ (A \otimes X)\ (B \otimes Y) (C \otimes Z)\).
combine :: to (f x y z) (f x' y' z') `cat` f (t1 x x') (t2 y y') (t3 z z')

infixr 9 |*+*|

(|*+*|) :: (Semigroupal (->) (,) Either (,) (,) p) => p a b c -> p a' b c' -> p (a, a') (Either b b) (c, c')
(|*+*|) = curry combine

infixr 9 |***|

(|***|) :: (Semigroupal (->) (,) (,) (,) (,) p) => p a b c -> p a' b' c' -> p (a, a') (b, b') (c, c')
(|***|) = curry combine

infixr 9 |+++|

(|+++|) :: (Semigroupal (->) Either Either Either (,) p) => p a b c -> p a' b' c' -> p (Either a a') (Either b b') (Either c c')
(|+++|) = curry combine

infixr 9 |&&&|

(|&&&|) :: (Semigroupal (->) These These These (,) p) => p a b c -> p a' b' c' -> p (These a a') (These b b') (These c c')
(|&&&|) = curry combine

--------------------------------------------------------------------------------

-- | Given monoidal categories \((\mathcal{C}, \otimes, I_{\mathcal{C}})\) and \((\mathcal{D}, \bullet, I_{\mathcal{D}})\).
Expand Down

0 comments on commit 17fb4ee

Please sign in to comment.