-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The Action typeclass really represents left actions. This moves Action under an Action namespace with the name LeftAction. It also updates files where Action instances are defined and make them use LeftActions. It also creates a type alias Action in the old Action namespace pointing to LeftAction and act pointing to leftAct for compatibility.
- Loading branch information
Mistral Contrastin
committed
Dec 26, 2018
1 parent
d7c1f7b
commit 2cead61
Showing
7 changed files
with
120 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,13 @@ | ||
{-# LANGUAGE FlexibleInstances #-} | ||
{-# LANGUAGE MultiParamTypeClasses #-} | ||
|
||
----------------------------------------------------------------------------- | ||
-- | | ||
-- Module : Data.Monoid.Action | ||
-- Copyright : (c) 2011 diagrams-core team (see LICENSE) | ||
-- License : BSD-style (see LICENSE) | ||
-- Maintainer : diagrams-discuss@googlegroups.com | ||
-- | ||
-- Monoid and semigroup actions. | ||
-- | ||
----------------------------------------------------------------------------- | ||
{-# LANGUAGE ConstraintKinds #-} | ||
|
||
module Data.Monoid.Action | ||
( Action(..) | ||
) where | ||
|
||
import Data.Semigroup | ||
|
||
------------------------------------------------------------ | ||
-- Monoid and semigroup actions | ||
------------------------------------------------------------ | ||
|
||
-- | Type class for monoid (and semigroup) actions, where monoidal | ||
-- values of type @m@ \"act\" on values of another type @s@. | ||
-- Instances are required to satisfy the laws | ||
-- | ||
-- * @act mempty = id@ | ||
-- | ||
-- * @act (m1 \`mappend\` m2) = act m1 . act m2@ | ||
-- | ||
-- Semigroup instances are required to satisfy the second law but with | ||
-- ('<>') instead of 'mappend'. Additionally, if the type @s@ has | ||
-- any algebraic structure, @act m@ should be a homomorphism. For | ||
-- example, if @s@ is also a monoid we should have @act m mempty = | ||
-- mempty@ and @act m (s1 \`mappend\` s2) = (act m s1) \`mappend\` | ||
-- (act m s2)@. | ||
-- | ||
-- By default, @act = const id@, so for a type @M@ which should have | ||
-- no action on anything, it suffices to write | ||
-- | ||
-- > instance Action M s | ||
-- | ||
-- with no method implementations. | ||
-- | ||
-- It is a bit awkward dealing with instances of @Action@, since it | ||
-- is a multi-parameter type class but we can't add any functional | ||
-- dependencies---the relationship between monoids and the types on | ||
-- which they act is truly many-to-many. In practice, this library | ||
-- has chosen to have instance selection for @Action@ driven by the | ||
-- /first/ type parameter. That is, you should never write an | ||
-- 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 | ||
|
||
-- | Convert a value of type @m@ to an action on @s@ values. | ||
act :: m -> s -> s | ||
act = const id | ||
|
||
-- | @()@ acts as the identity. | ||
instance Action () l where | ||
act () = id | ||
( Action | ||
, act | ||
) where | ||
|
||
-- | @Nothing@ acts as the identity; @Just m@ acts as @m@. | ||
instance Action m s => Action (Option m) s where | ||
act (Option Nothing) s = s | ||
act (Option (Just m)) s = act m s | ||
import Data.Monoid.Action.LeftAction | ||
|
||
-- | @Endo@ acts by application. | ||
-- | ||
-- Note that in order for this instance to satisfy the @Action@ | ||
-- laws, whenever the type @a@ has some sort of algebraic structure, | ||
-- the type @Endo a@ must be considered to represent /homomorphisms/ | ||
-- (structure-preserving maps) on @a@, even though there is no way | ||
-- to enforce this in the type system. For example, if @a@ is an | ||
-- instance of @Monoid@, then one should only use @Endo a@ values | ||
-- @f@ with the property that @f mempty = mempty@ and @f (a <> b) = | ||
-- f a <> f b@. | ||
instance Action (Endo a) a where | ||
act = appEndo | ||
type Action = LeftAction | ||
|
||
act :: Action m s => m -> s -> s | ||
act = leftAct |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
{-# LANGUAGE FlexibleInstances #-} | ||
{-# LANGUAGE MultiParamTypeClasses #-} | ||
|
||
----------------------------------------------------------------------------- | ||
-- | | ||
-- Module : Data.Monoid.Action.LeftAction | ||
-- Copyright : (c) 2011 diagrams-core team (see LICENSE) | ||
-- License : BSD-style (see LICENSE) | ||
-- Maintainer : diagrams-discuss@googlegroups.com | ||
-- | ||
-- Monoid and semigroup left actions. | ||
-- | ||
----------------------------------------------------------------------------- | ||
|
||
module Data.Monoid.Action.LeftAction | ||
( LeftAction(..) | ||
) where | ||
|
||
import Data.Semigroup | ||
|
||
------------------------------------------------------------ | ||
-- Monoid and semigroup left actions | ||
------------------------------------------------------------ | ||
|
||
-- | Type class for left monoid (and semigroup) actions, where monoidal | ||
-- values of type @m@ act (@leftAct@) on values of another type @s@. | ||
-- Instances are required to satisfy the laws | ||
-- | ||
-- * @leftAct mempty = id@ | ||
-- | ||
-- * @leftAct (m1 \`mappend\` m2) = leftAct m1 . leftAct m2@ | ||
-- | ||
-- Semigroup instances are required to satisfy the second law but with | ||
-- ('<>') instead of 'mappend'. Additionally, if the type @s@ has | ||
-- any algebraic structure, @leftAct m@ should be a homomorphism. For | ||
-- example, if @s@ is also a monoid we should have @leftAct m mempty = | ||
-- mempty@ and @leftAct m (s1 \`mappend\` s2) = (leftAct m s1) \`mappend\` | ||
-- (leftAct m s2)@. | ||
-- | ||
-- By default, @leftAct = const id@, so for a type @M@ which should have | ||
-- no action on anything, it suffices to write | ||
-- | ||
-- > instance LeftAction M s | ||
-- | ||
-- with no method implementations. | ||
-- | ||
-- It is a bit awkward dealing with instances of @LeftAction@, since it | ||
-- is a multi-parameter type class but we can't add any functional | ||
-- dependencies---the relationship between monoids and the types on | ||
-- which they act is truly many-to-many. In practice, this library | ||
-- has chosen to have instance selection for @LeftAction@ driven by the | ||
-- /first/ type parameter. That is, you should never write an | ||
-- instance of the form @LeftAction 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 LeftAction m s where | ||
|
||
-- | Convert a value of type @m@ to an left action on @s@ values. | ||
leftAct :: m -> s -> s | ||
leftAct = const id | ||
|
||
-- | @()@ acts as the identity. | ||
instance LeftAction () l where | ||
leftAct () = id | ||
|
||
-- | @Nothing@ acts as the identity; @Just m@ acts as @m@. | ||
instance LeftAction m s => LeftAction (Option m) s where | ||
leftAct (Option Nothing) s = s | ||
leftAct (Option (Just m)) s = leftAct m s | ||
|
||
-- | @Endo@ acts by application. | ||
-- | ||
-- Note that in order for this instance to satisfy the @LeftAction@ | ||
-- laws, whenever the type @a@ has some sort of algebraic structure, | ||
-- the type @Endo a@ must be considered to represent /homomorphisms/ | ||
-- (structure-preserving maps) on @a@, even though there is no way | ||
-- to enforce this in the type system. For example, if @a@ is an | ||
-- instance of @Monoid@, then one should only use @Endo a@ values | ||
-- @f@ with the property that @f mempty = mempty@ and @f (a <> b) = | ||
-- f a <> f b@. | ||
instance LeftAction (Endo a) a where | ||
leftAct = appEndo | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters