packages feed

observable-sharing 0.2.4 → 0.2.5

raw patch · 2 files changed

+24/−15 lines, 2 files

Files

observable-sharing.cabal view
@@ -2,7 +2,7 @@ -- documentation, see http://haskell.org/cabal/users-guide/  name:                observable-sharing-version:             0.2.4+version:             0.2.5 synopsis:            Simple observable sharing -- description:          license:             BSD3
src/Data/Ref/Map.hs view
@@ -20,9 +20,9 @@   , difference   , intersection     -    -- eehh...   , Entry(..)-  , elems+  , toList     -- :: Map f -> [(Name a, f a)]+  , fromList   -- :: [(Name a, f a)] -> Map f   ) where  import Control.Applicative ((<$>))@@ -42,15 +42,14 @@ -- * Reference indexed maps -------------------------------------------------------------------------------- --- | Shorthand for stable names-type Name  = StableName+-- | Shorthand for stable names.+type Name = StableName --- | HideType, for hiding types!+-- | For hiding types. data HideType f where   Hide :: f a -> HideType f --- | A reference indexed map.---   Useful for associating info with a reference.+-- | A reference indexed map. Useful for associating info with a reference. -- --   Note: this is generally unsound when `f` is a GADT! data Map f = Map (IntMap [(HideType Name, HideType f)])@@ -90,9 +89,9 @@ lookup :: Name a -> Map f -> Maybe (f a) lookup n (Map m) =  case M.lookup (hashStableName n) m of   Nothing -> Nothing-  Just xs -> case find (\(Hide x,_) -> eqStableName x n) xs of-    Nothing         -> Nothing-    Just (_,Hide f) -> Just $ unsafeCoerce f+  Just xs -> case find (\(Hide x, _) -> eqStableName x n) xs of+    Nothing          -> Nothing+    Just (_, Hide f) -> Just $ unsafeCoerce f  -- | Associates a reference with the specified value. If the map already contains -- a mapping for the reference, the old value is replaced.@@ -152,16 +151,26 @@ intersection (Map m) (Map n) = Map $ M.intersection m n  ----------------------------------------------------------------------------------- Still not sure about these.+-- ** Lists --- | ...+-- | Entry in map. data Entry f = forall a. Entry (Name a) (f a)  -- | Fetches all the elements of a map.-elems :: Map f -> [Entry f]-elems (Map m) = fmap pack . concat $ M.elems m+toList :: Map f -> [Entry f]+toList (Map m) = fmap pack . concat $ M.elems m   where     pack :: (HideType Name, HideType f) -> Entry f     pack (Hide n, Hide f) = Entry n (unsafeCoerce f)++-- | Constructs a map from a list of entries.+fromList :: [Entry f] -> Map f+fromList = Map . M.fromList . fmap keys . fmap unpack+  where+    unpack :: Entry f -> (HideType Name, HideType f)+    unpack (Entry n f) = (Hide n, Hide f)++    keys :: (HideType Name, HideType f) -> (M.Key, [(HideType Name, HideType f)])+    keys e@(Hide n, _) = (hashStableName n, [e])  --------------------------------------------------------------------------------