Skip to content

Commit 6910660

Browse files
authored
Add Lift instances (#343)
Add Lift instances
1 parent bd165b0 commit 6910660

File tree

5 files changed

+55
-4
lines changed

5 files changed

+55
-4
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
* Define `dataCast1` for `HashMap`.
44

5+
* [Add `Lift` instances for Template Haskell](https://github.com/haskell-unordered-containers/unordered-containers/pull/343)
6+
57
## [0.2.16.0]
68

79
* [Increase maximum branching factor from 16 to 32](https://github.com/haskell-unordered-containers/unordered-containers/pull/317)

Data/HashMap/Internal.hs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
{-# LANGUAGE BangPatterns, CPP, MagicHash #-}
2-
{-# LANGUAGE ScopedTypeVariables #-}
1+
{-# LANGUAGE CPP #-}
2+
{-# LANGUAGE BangPatterns #-}
3+
{-# LANGUAGE DeriveLift #-}
4+
{-# LANGUAGE LambdaCase #-}
5+
{-# LANGUAGE MagicHash #-}
36
{-# LANGUAGE PatternGuards #-}
47
{-# LANGUAGE RoleAnnotations #-}
8+
{-# LANGUAGE ScopedTypeVariables #-}
9+
{-# LANGUAGE StandaloneDeriving #-}
10+
{-# LANGUAGE TemplateHaskellQuotes #-}
511
{-# LANGUAGE TypeFamilies #-}
612
{-# LANGUAGE UnboxedTuples #-}
7-
{-# LANGUAGE LambdaCase #-}
813
#if __GLASGOW_HASKELL__ >= 802
914
{-# LANGUAGE TypeInType #-}
1015
{-# LANGUAGE UnboxedSums #-}
@@ -179,6 +184,7 @@ import GHC.Exts (TYPE, Int (..), Int#)
179184
import Data.Functor.Identity (Identity (..))
180185
import Control.Applicative (Const (..))
181186
import Data.Coerce (coerce)
187+
import qualified Language.Haskell.TH.Syntax as TH
182188

183189
-- | A set of values. A set cannot contain duplicate values.
184190
------------------------------------------------------------------------
@@ -193,6 +199,14 @@ data Leaf k v = L !k v
193199
instance (NFData k, NFData v) => NFData (Leaf k v) where
194200
rnf (L k v) = rnf k `seq` rnf v
195201

202+
-- | @since 0.2.17.0
203+
instance (TH.Lift k, TH.Lift v) => TH.Lift (Leaf k v) where
204+
#if MIN_VERSION_template_haskell(2,16,0)
205+
liftTyped (L k v) = [|| L k $! v ||]
206+
#else
207+
lift (L k v) = [| L k $! v |]
208+
#endif
209+
196210
#if MIN_VERSION_deepseq(1,4,3)
197211
-- | @since 0.2.14.0
198212
instance NFData k => NF.NFData1 (Leaf k) where
@@ -217,6 +231,9 @@ data HashMap k v
217231

218232
type role HashMap nominal representational
219233

234+
-- | @since 0.2.17.0
235+
deriving instance (TH.Lift k, TH.Lift v) => TH.Lift (HashMap k v)
236+
220237
instance (NFData k, NFData v) => NFData (HashMap k v) where
221238
rnf Empty = ()
222239
rnf (BitmapIndexed _ ary) = rnf ary

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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{-# LANGUAGE CPP #-}
2+
{-# LANGUAGE DeriveLift #-}
23
{-# LANGUAGE RoleAnnotations #-}
4+
{-# LANGUAGE StandaloneDeriving #-}
35
{-# LANGUAGE TypeFamilies #-}
46
{-# LANGUAGE Trustworthy #-}
57
{-# OPTIONS_HADDOCK not-home #-}
@@ -113,6 +115,7 @@ import qualified Data.Hashable.Lifted as H
113115
#if MIN_VERSION_deepseq(1,4,3)
114116
import qualified Control.DeepSeq as NF
115117
#endif
118+
import qualified Language.Haskell.TH.Syntax as TH
116119

117120
-- | A set of values. A set cannot contain duplicate values.
118121
newtype HashSet a = HashSet {
@@ -121,6 +124,9 @@ newtype HashSet a = HashSet {
121124

122125
type role HashSet nominal
123126

127+
-- | @since 0.2.17.0
128+
deriving instance TH.Lift a => TH.Lift (HashSet a)
129+
124130
instance (NFData a) => NFData (HashSet a) where
125131
rnf = rnf . asMap
126132
{-# INLINE rnf #-}

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 < 2.19
6061

6162
default-language: Haskell2010
6263

0 commit comments

Comments
 (0)