Skip to content

Commit 352ab8f

Browse files
committed
Add Lift instances
Closes #342
1 parent c481228 commit 352ab8f

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed

Data/HashMap/Internal.hs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
{-# LANGUAGE BangPatterns, CPP, DeriveDataTypeable, MagicHash #-}
2-
{-# LANGUAGE ScopedTypeVariables #-}
1+
{-# LANGUAGE CPP #-}
2+
{-# LANGUAGE BangPatterns #-}
3+
{-# LANGUAGE DeriveDataTypeable #-}
4+
{-# LANGUAGE DeriveLift #-}
5+
{-# LANGUAGE LambdaCase #-}
6+
{-# LANGUAGE MagicHash #-}
37
{-# LANGUAGE PatternGuards #-}
48
{-# LANGUAGE RoleAnnotations #-}
9+
{-# LANGUAGE ScopedTypeVariables #-}
510
{-# LANGUAGE TypeFamilies #-}
611
{-# LANGUAGE UnboxedTuples #-}
7-
{-# LANGUAGE LambdaCase #-}
812
#if __GLASGOW_HASKELL__ >= 802
913
{-# LANGUAGE TypeInType #-}
1014
{-# LANGUAGE UnboxedSums #-}
@@ -180,6 +184,7 @@ import GHC.Exts (TYPE, Int (..), Int#)
180184
import Data.Functor.Identity (Identity (..))
181185
import Control.Applicative (Const (..))
182186
import Data.Coerce (coerce)
187+
import qualified Language.Haskell.TH.Syntax as TH
183188

184189
-- | A set of values. A set cannot contain duplicate values.
185190
------------------------------------------------------------------------
@@ -189,7 +194,7 @@ hash :: H.Hashable a => a -> Hash
189194
hash = fromIntegral . H.hash
190195

191196
data Leaf k v = L !k v
192-
deriving (Eq)
197+
deriving (Eq, TH.Lift)
193198

194199
instance (NFData k, NFData v) => NFData (Leaf k v) where
195200
rnf (L k v) = rnf k `seq` rnf v
@@ -215,7 +220,7 @@ data HashMap k v
215220
| Leaf !Hash !(Leaf k v)
216221
| Full !(A.Array (HashMap k v))
217222
| Collision !Hash !(A.Array (Leaf k v))
218-
deriving (Typeable)
223+
deriving (Typeable, TH.Lift)
219224

220225
type role HashMap nominal representational
221226

Data/HashMap/Internal/Array.hs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE BangPatterns, CPP, MagicHash, Rank2Types, UnboxedTuples, ScopedTypeVariables #-}
2+
{-# LANGUAGE TemplateHaskellQuotes #-}
23
{-# OPTIONS_GHC -fno-full-laziness -funbox-strict-fields #-}
34
{-# OPTIONS_HADDOCK not-home #-}
45

@@ -69,6 +70,7 @@ module Data.HashMap.Internal.Array
6970
, traverse'
7071
, toList
7172
, fromList
73+
, fromList'
7274
) where
7375

7476
import Control.Applicative (liftA2)
@@ -84,6 +86,8 @@ import GHC.Exts (SmallArray#, newSmallArray#, readSmallArray#, writeSmallArray#,
8486
SmallMutableArray#, sizeofSmallArray#, copySmallArray#, thawSmallArray#,
8587
sizeofSmallMutableArray#, copySmallMutableArray#, cloneSmallMutableArray#)
8688

89+
import qualified Language.Haskell.TH.Syntax as TH
90+
8791
#if defined(ASSERTS)
8892
import qualified Prelude
8993
#endif
@@ -474,6 +478,27 @@ fromList n xs0 =
474478
go (x:xs) mary i = do write mary i x
475479
go xs mary (i+1)
476480

481+
fromList' :: Int -> [a] -> Array a
482+
fromList' n xs0 =
483+
CHECK_EQ("fromList'", n, Prelude.length xs0)
484+
run $ do
485+
mary <- new_ n
486+
go xs0 mary 0
487+
where
488+
go [] !mary !_ = return mary
489+
go (!x:xs) mary i = do write mary i x
490+
go xs mary (i+1)
491+
492+
instance TH.Lift a => TH.Lift (Array a) where
493+
#if MIN_VERSION_template_haskell(2,16,0)
494+
liftTyped ar = [|| fromList' arlen arlist ||]
495+
#else
496+
lift ar = [| fromList' arlen arlist |]
497+
#endif
498+
where
499+
arlen = length ar
500+
arlist = toList ar
501+
477502
toList :: Array a -> [a]
478503
toList = foldr (:) []
479504

Data/HashSet/Internal.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
{-# LANGUAGE CPP, DeriveDataTypeable #-}
1+
{-# LANGUAGE CPP #-}
2+
{-# LANGUAGE DeriveDataTypeable #-}
3+
{-# LANGUAGE DeriveLift #-}
24
{-# LANGUAGE RoleAnnotations #-}
35
{-# LANGUAGE TypeFamilies #-}
46
{-# LANGUAGE Trustworthy #-}
@@ -114,11 +116,12 @@ import qualified Data.Hashable.Lifted as H
114116
#if MIN_VERSION_deepseq(1,4,3)
115117
import qualified Control.DeepSeq as NF
116118
#endif
119+
import qualified Language.Haskell.TH.Syntax as TH
117120

118121
-- | A set of values. A set cannot contain duplicate values.
119122
newtype HashSet a = HashSet {
120123
asMap :: HashMap a ()
121-
} deriving (Typeable)
124+
} deriving (Typeable, TH.Lift)
122125

123126
type role HashSet nominal
124127

unordered-containers.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ library
5656
build-depends:
5757
base >= 4.9 && < 5,
5858
deepseq >= 1.1,
59-
hashable >= 1.0.1.1 && < 1.5
59+
hashable >= 1.0.1.1 && < 1.5,
60+
template-haskell
6061

6162
default-language: Haskell2010
6263

0 commit comments

Comments
 (0)