From ebfbdcd09b92a55b6c9f5a1f7feaa4f98c2236ba Mon Sep 17 00:00:00 2001 From: joe Date: Tue, 12 Aug 2014 16:04:01 -0400 Subject: IntMapClass experiment --- IntMapClass.hs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 IntMapClass.hs (limited to 'IntMapClass.hs') diff --git a/IntMapClass.hs b/IntMapClass.hs new file mode 100644 index 0000000..49e1125 --- /dev/null +++ b/IntMapClass.hs @@ -0,0 +1,41 @@ +{-# LANGUAGE CPP, + FlexibleContexts, + MultiParamTypeClasses, + GeneralizedNewtypeDeriving, + DeriveDataTypeable #-} +module IntMapClass where + +import qualified Data.IntMap.Strict as IntMap +import Data.IntMap.Strict ( IntMap ) +import Data.Typeable ( Typeable ) +import Data.Data ( Data ) +import Data.Foldable ( Foldable ) +import Data.Traversable ( Traversable ) +import Data.Monoid ( Monoid ) +import Control.DeepSeq ( NFData ) +#if MIN_VERSION_base(4,7,0) +import Data.Coerce +#else +class Coercible a b where coerce :: a -> b +#endif + +newtype IMap k a = IMap (IntMap a) + deriving + ( Functor + , Typeable + , Foldable + , Traversable + , Eq + , Data + , Ord + , Read + , Show + , Monoid + , NFData + ) + +adapt_m_k :: Coercible k Int => (IntMap a -> Int -> x) -> IMap k a -> k -> x +adapt_m_k f (IMap m) k = f m (coerce k) + +(!) :: Coercible k Int => IMap k a -> k -> a +(!) = adapt_m_k (IntMap.!) -- cgit v1.2.3