~fgaz/bimap-many

9ae94b07cff2d836a80f4ffa543abf31ee27614e — Francesco Gazzetta 6 months ago b1257a5
Lazy/strict Functor and Traversable instances
2 files changed, 14 insertions(+), 5 deletions(-)

M src/Data/BimapMany.hs
M src/Data/Map/Signature.hsig
M src/Data/BimapMany.hs => src/Data/BimapMany.hs +12 -5
@@ 1,4 1,4 @@
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveGeneric #-}
module Data.BimapMany
(


@@ 94,10 94,9 @@ data BimapMany a b c = BimapMany
  --   (a, bs) ∈ l, b ∈ bs,   => (a, b) ∈ m, (b, as) ∈ r, a ∈ as
  --   (b, as) ∈ r, a ∈ as,   => (a, b) ∈ m, (a, bs) ∈ l, b ∈ bs
  --   ((a, b), _) ∈ m        => (a, bs) ∈ l, (b, as) ∈ r, a ∈ as, b ∈ bs
  -- TODO use proper lazy/strict functions for these instances
  -- MAYBE move the data definition to a separate module and share it, like Map
  -- Or better, provide coercion functions
  deriving (Functor, Foldable, Traversable, Generic)
  deriving (Foldable, Generic)

-- TODO provide coercion functions

instance (Show a, Show b, Show c) => Show (BimapMany a b c) where
  show x = "fromList " ++ show (toList x)


@@ 118,6 117,14 @@ instance (Ord a, Ord b) => Monoid (BimapMany a b c) where
  {-# INLINABLE mempty #-}
  mempty = empty

instance Functor (BimapMany a b) where
  {-# INLINABLE fmap #-}
  fmap f (BimapMany l r m) = BimapMany l r $ M.map f m

instance Traversable (BimapMany a b) where
  {-# INLINABLE traverse #-}
  traverse f (BimapMany l r m) = BimapMany l r <$> M.traverseWithKey (const f) m

instance (NFData a, NFData b, NFData c) => NFData (BimapMany a b c)

-- * Construction

M src/Data/Map/Signature.hsig => src/Data/Map/Signature.hsig +2 -0
@@ 28,5 28,7 @@ null :: Map k a -> Bool
size :: Map k a -> Int
union :: Ord k => Map k a -> Map k a -> Map k a
unionWith :: Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
map :: (a -> b) -> Map k a -> Map k b
traverseWithKey :: Applicative t => (k -> a -> t b) -> Map k a -> t (Map k b)
keys :: Map k a -> [k]
toList :: Map k a -> [(k, a)]