Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Semigroup constraint for the class Action #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions src/Data/Monoid/Action.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import Data.Group
-- instance of the form @Action m SomeType@ since it will overlap
-- with instances of the form @Action SomeMonoid t@. Newtype
-- wrappers can be used to (awkwardly) get around this.
class Action m s where
class Semigroup m => Action m s where

-- | Convert a value of type @m@ to an action on @s@ values.
act :: m -> s -> s
Expand Down Expand Up @@ -85,17 +85,11 @@ instance Action m s => Action (Maybe m) s where
instance Action (Endo a) a where
act = appEndo

instance Num a => Action Integer (Sum a) where
n `act` a = fromInteger n <> a
instance Num a => Action (Sum a) a where
a `act` n = getSum (a <> Sum n)

instance Num a => Action Integer (Product a) where
n `act` a = fromInteger n <> a

instance Fractional a => Action Rational (Sum a) where
n `act` a = Sum (fromRational n) <> a

instance Fractional a => Action Rational (Product a) where
n `act` a = Product (fromRational n) <> a
instance Num a => Action (Product a) a where
a `act` n = getProduct (a <> Product n)

-- | An action of a group is "free transitive", "regular", or a "torsor"
-- iff it is invertible.
Expand Down
10 changes: 8 additions & 2 deletions src/Data/Monoid/MList.hs
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,16 @@ instance (t :>: a) => (:>:) (b ::: t) a where
newtype SM m = SM m
deriving Show

instance (Action (SM a) l2, Action l1 l2) => Action (a, l1) l2 where
instance Semigroup a => Semigroup (SM a) where
SM x <> SM y = SM (x <> y)

instance Monoid a => Monoid (SM a) where
mempty = SM mempty

instance (Semigroup a, Action (SM a) l2, Action l1 l2) => Action (a, l1) l2 where
act (a,l) = act (SM a) . act l

instance Action (SM a) () where
instance Semigroup a => Action (SM a) () where
act _ _ = ()

instance (Action a a', Action (SM a) l) => Action (SM a) (Maybe a', l) where
Expand Down
Loading