|
| 1 | +{-# LANGUAGE DerivingVia #-} |
| 2 | +{-# LANGUAGE GeneralizedNewtypeDeriving #-} |
| 3 | +{-# LANGUAGE LinearTypes #-} |
| 4 | +{-# LANGUAGE NoImplicitPrelude #-} |
| 5 | +{-# LANGUAGE StandaloneDeriving #-} |
| 6 | + |
| 7 | +-- | = The linear semigroup hierarchy |
| 8 | +-- |
| 9 | +-- TODO: documentation |
| 10 | + |
| 11 | +module Data.Semigroup.Linear |
| 12 | + ( Semigroup(..) |
| 13 | + , Monoid(..) |
| 14 | + , LEndo(..), appLEndo |
| 15 | + , module Data.Semigroup |
| 16 | + ) |
| 17 | + where |
| 18 | + |
| 19 | +import Prelude.Linear.Internal.Simple |
| 20 | +import Data.Semigroup hiding (Semigroup(..)) |
| 21 | +import qualified Data.Semigroup as Prelude |
| 22 | +import qualified Prelude |
| 23 | +import qualified Unsafe.Linear as Unsafe |
| 24 | + |
| 25 | +class Prelude.Semigroup a => Semigroup a where |
| 26 | + (<>) :: a ->. a ->. a |
| 27 | + |
| 28 | +class (Semigroup a, Prelude.Monoid a) => Monoid a where |
| 29 | + {-# MINIMAL #-} |
| 30 | + mempty :: a |
| 31 | + mempty = mempty |
| 32 | + -- convenience redefine |
| 33 | + |
| 34 | +--------------- |
| 35 | +-- Instances -- |
| 36 | +--------------- |
| 37 | + |
| 38 | +instance Semigroup () where |
| 39 | + () <> () = () |
| 40 | + |
| 41 | +newtype LEndo a = LEndo (a ->. a) |
| 42 | + |
| 43 | +-- TODO: have this as a newtype deconstructor once the right type can be |
| 44 | +-- correctly inferred |
| 45 | +appLEndo :: LEndo a ->. a ->. a |
| 46 | +appLEndo (LEndo f) = f |
| 47 | + |
| 48 | +instance Prelude.Semigroup (LEndo a) where |
| 49 | + LEndo f <> LEndo g = LEndo (f . g) |
| 50 | +instance Prelude.Monoid (LEndo a) where |
| 51 | + mempty = LEndo id |
| 52 | +instance Semigroup (LEndo a) where |
| 53 | + LEndo f <> LEndo g = LEndo (f . g) |
| 54 | +instance Monoid (LEndo a) where |
| 55 | + |
| 56 | +instance (Semigroup a, Semigroup b) => Semigroup (a,b) where |
| 57 | + (a,x) <> (b,y) = (a <> b, x <> y) |
| 58 | +instance (Monoid a, Monoid b) => Monoid (a,b) |
| 59 | + |
| 60 | +instance Semigroup a => Semigroup (Dual a) where |
| 61 | + Dual x <> Dual y = Dual (y <> x) |
| 62 | +instance Monoid a => Monoid (Dual a) |
| 63 | + |
| 64 | +newtype LWrap a = LWrap a |
| 65 | + deriving (Prelude.Semigroup, Prelude.Monoid) |
| 66 | + |
| 67 | +-- This instance is unsafe: do not export LWrap so it cannot be used. |
| 68 | +instance Prelude.Semigroup a => Semigroup (LWrap a) where |
| 69 | + LWrap a <> LWrap b = LWrap (Unsafe.toLinear2 (Prelude.<>) a b) |
| 70 | +instance Prelude.Monoid a => Monoid (LWrap a) |
| 71 | + |
| 72 | +-- XXX: I think these are safe but I'm not fully confident |
| 73 | +deriving via (LWrap (Sum a)) instance Prelude.Num a => Semigroup (Sum a) |
| 74 | +deriving via (LWrap (Sum a)) instance Prelude.Num a => Monoid (Sum a) |
| 75 | +deriving via (LWrap (Product a)) instance Prelude.Num a => Semigroup (Product a) |
| 76 | +deriving via (LWrap (Product a)) instance Prelude.Num a => Monoid (Product a) |
| 77 | + |
| 78 | +-- Bools are movable so this is fine |
| 79 | +deriving via LWrap All instance Semigroup All |
| 80 | +deriving via LWrap All instance Monoid All |
| 81 | +deriving via LWrap Any instance Semigroup Any |
| 82 | +deriving via LWrap Any instance Monoid Any |
0 commit comments