Skip to content

Commit

Permalink
Revise definitions of stimes. (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanknowles authored Jun 21, 2023
2 parents ca70ad6 + bc8db62 commit c595234
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/examples/Examples/RecoveredMap.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ import Prelude hiding

import Control.DeepSeq
( NFData )
import Data.Coerce
( coerce )
import Data.Maybe
( mapMaybe )
import Data.Monoid
( First (..) )
import Data.MonoidMap
( MonoidMap )
import Data.Semigroup
( Semigroup (stimes), stimesIdempotentMonoid )
import Data.Set
( Set )

Expand All @@ -29,7 +33,11 @@ import qualified Data.MonoidMap as MonoidMap
newtype Map k v = Map
-- 'First' is used to mimic the left-biased nature of 'Data.Map':
{unMap :: MonoidMap k (First v)}
deriving newtype (Eq, NFData, Semigroup, Monoid)
deriving newtype (Eq, NFData, Monoid)

instance Ord k => Semigroup (Map k v) where
(<>) = coerce @(MonoidMap k (First v) -> _ -> _) (<>)
stimes = stimesIdempotentMonoid

instance (Show k, Show v) => Show (Map k v) where
show = ("fromList " <>) . show . toList
Expand Down
2 changes: 2 additions & 0 deletions src/internal/Data/MonoidMap/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ instance (Ord k, MonoidNull v) =>
Semigroup (MonoidMap k v)
where
(<>) = append
stimes 0 = const mempty
stimes 1 = id
stimes n = map (stimes n)

instance (Ord k, MonoidNull v, Commutative v) =>
Expand Down
31 changes: 31 additions & 0 deletions src/test/Examples/RecoveredMapSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import Data.Monoid
( Sum (..) )
import Data.Proxy
( Proxy (..) )
import Data.Semigroup
( Semigroup (stimes) )
import Data.Set
( Set )
import Data.Text
Expand All @@ -36,6 +38,7 @@ import Test.QuickCheck
, CoArbitrary
, Fun
, Function
, NonNegative (..)
, Property
, Testable
, applyFun
Expand Down Expand Up @@ -148,6 +151,11 @@ specFor keyType valueType = do
prop_append_toList
@k @v & property

describe "Times" $ do
it "prop_stimes_toList" $
prop_stimes_toList
@k @v & property

describe "Delete" $ do
it "prop_delete_lookup" $
prop_delete_lookup
Expand Down Expand Up @@ -301,6 +309,29 @@ prop_append_toList kvs1 kvs2 =
ks1 = Set.fromList (fst <$> kvs1)
ks2 = Set.fromList (fst <$> kvs2)

--------------------------------------------------------------------------------
-- Times
--------------------------------------------------------------------------------

prop_stimes_toList
:: forall k v. (Ord k, Show k, Eq v, Show v)
=> [(k, v)]
-> NonNegative Int
-> Property
prop_stimes_toList kvs (NonNegative n) =
(===)
(RMap.toList (stimes n (RMap.fromList kvs)))
(OMap.toList (stimes n (OMap.fromList kvs)))
& cover 1
(n == 0)
"n == 0"
& cover 1
(n == 1)
"n == 1"
& cover 10
(n >= 2)
"n >= 2"

--------------------------------------------------------------------------------
-- Delete
--------------------------------------------------------------------------------
Expand Down

0 comments on commit c595234

Please sign in to comment.