Add Monoid instance for StateStore

Summary: Add a monoid instance for merging two StateStores.

Reviewed By: watashi

Differential Revision: D8961041

fbshipit-source-id: f6539ec0f0915b572a42ab34c098a9be6c8664f1
This commit is contained in:
Andrew Farmer 2018-07-23 18:22:50 -07:00 committed by Facebook Github Bot
parent aba36943e5
commit 2f8204cc2a

View File

@ -24,6 +24,7 @@ module Haxl.Core.StateStore
import Data.Map (Map)
import qualified Data.Map.Strict as Map
import Data.Monoid
import Data.Typeable
import Unsafe.Coerce
@ -47,6 +48,11 @@ class Typeable1 f => StateKey (f :: * -> *) where
-- | The 'StateStore' maps a 'StateKey' to the 'State' for that type.
newtype StateStore = StateStore (Map TypeRep StateStoreData)
instance Monoid StateStore where
mempty = stateEmpty
-- Left-biased union
mappend (StateStore m1) (StateStore m2) = StateStore $ m1 <> m2
-- | Encapsulates the type of 'StateStore' data so we can have a
-- heterogeneous collection.
data StateStoreData = forall f. StateKey f => StateStoreData (State f)