Skip to content

Commit 12e3e15

Browse files
committed
Make it compile with MicroHs
1 parent 5ce9758 commit 12e3e15

File tree

5 files changed

+38
-9
lines changed

5 files changed

+38
-9
lines changed

Data/HashMap/Internal.hs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ import qualified Data.List as List
181181
import qualified GHC.Exts as Exts
182182
import qualified Language.Haskell.TH.Syntax as TH
183183

184+
#if defined(__MHS__)
185+
import Data.Traversable
186+
#endif
187+
184188
-- | Convenience function. Compute a hash value for the given value.
185189
hash :: H.Hashable a => a -> Hash
186190
hash = fromIntegral . H.hash
@@ -191,9 +195,11 @@ data Leaf k v = L !k v
191195
instance (NFData k, NFData v) => NFData (Leaf k v) where
192196
rnf (L k v) = rnf k `seq` rnf v
193197

198+
#if !defined(__MHS__)
194199
-- | @since 0.2.17.0
195200
instance (TH.Lift k, TH.Lift v) => TH.Lift (Leaf k v) where
196201
liftTyped (L k v) = [|| L k $! v ||]
202+
#endif
197203

198204
-- | @since 0.2.14.0
199205
instance NFData k => NFData1 (Leaf k) where
@@ -700,7 +706,11 @@ lookupRecordCollision# h k m =
700706
-- this whole thing is always inlined, we don't have to worry about
701707
-- any extra CPS overhead.
702708
lookupCont ::
709+
#if !defined(__MHS__)
703710
forall rep (r :: TYPE rep) k v.
711+
#else
712+
forall r k v.
713+
#endif
704714
Eq k
705715
=> ((# #) -> r) -- Absent continuation
706716
-> (v -> Int -> r) -- Present continuation
@@ -914,10 +924,11 @@ setAtPosition i k x ary = A.update ary i (L k x)
914924

915925

916926
-- | In-place update version of insert
917-
unsafeInsert :: (Eq k, Hashable k) => k -> v -> HashMap k v -> HashMap k v
927+
unsafeInsert :: forall k v. (Eq k, Hashable k) => k -> v -> HashMap k v -> HashMap k v
918928
unsafeInsert k0 v0 m0 = runST (go h0 k0 v0 0 m0)
919929
where
920930
h0 = hash k0
931+
go :: forall s. Hash -> k -> v -> Int -> HashMap k v -> ST s (HashMap k v)
921932
go !h !k x !_ Empty = return $! Leaf h (L k x)
922933
go h k x s t@(Leaf hy l@(L ky y))
923934
| hy == h = if ky == k
@@ -2413,7 +2424,11 @@ fromListWithKey f = List.foldl' (\ m (k, v) -> unsafeInsertWithKey (\k' a b -> (
24132424
-- | \(O(n)\) Look up the value associated with the given key in an
24142425
-- array.
24152426
lookupInArrayCont ::
2427+
#if !defined(__MHS__)
24162428
forall rep (r :: TYPE rep) k v.
2429+
#else
2430+
forall r k v.
2431+
#endif
24172432
Eq k => ((# #) -> r) -> (v -> Int -> r) -> k -> A.Array (Leaf k v) -> r
24182433
lookupInArrayCont absent present k0 ary0 = go k0 ary0 0 (A.length ary0)
24192434
where
@@ -2666,9 +2681,11 @@ otherOfOneOrZero :: Int -> Int
26662681
otherOfOneOrZero i = 1 - i
26672682
{-# INLINE otherOfOneOrZero #-}
26682683

2684+
#if !defined(__MHS__)
26692685
------------------------------------------------------------------------
26702686
-- IsList instance
26712687
instance (Eq k, Hashable k) => Exts.IsList (HashMap k v) where
26722688
type Item (HashMap k v) = (k, v)
26732689
fromList = fromList
26742690
toList = toList
2691+
#endif

Data/HashMap/Internal/Array.hs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -464,14 +464,15 @@ deleteM ary idx = do
464464
where !count = length ary
465465
{-# INLINE deleteM #-}
466466

467-
map :: (a -> b) -> Array a -> Array b
467+
map :: forall a b . (a -> b) -> Array a -> Array b
468468
map f = \ ary ->
469469
let !n = length ary
470470
in run $ do
471471
mary <- new_ n
472472
go ary mary 0 n
473473
return mary
474474
where
475+
go :: forall s. Array a -> MArray s b -> Int -> Int -> ST s ()
475476
go ary mary i n
476477
| i >= n = return ()
477478
| otherwise = do
@@ -481,14 +482,15 @@ map f = \ ary ->
481482
{-# INLINE map #-}
482483

483484
-- | Strict version of 'map'.
484-
map' :: (a -> b) -> Array a -> Array b
485+
map' :: forall a b . (a -> b) -> Array a -> Array b
485486
map' f = \ ary ->
486487
let !n = length ary
487488
in run $ do
488489
mary <- new_ n
489490
go ary mary 0 n
490491
return mary
491492
where
493+
go :: forall s . Array a -> MArray s b -> Int -> Int -> ST s ()
492494
go ary mary i n
493495
| i >= n = return ()
494496
| otherwise = do
@@ -497,7 +499,7 @@ map' f = \ ary ->
497499
go ary mary (i+1) n
498500
{-# INLINE map' #-}
499501

500-
filter :: (a -> Bool) -> Array a -> Array a
502+
filter :: forall a . (a -> Bool) -> Array a -> Array a
501503
filter f = \ ary ->
502504
let !n = length ary
503505
in run $ do
@@ -508,6 +510,7 @@ filter f = \ ary ->
508510
-- Without the @!@ on @ary@ we end up reboxing the array when using
509511
-- 'differenceCollisions'. See
510512
-- https://gitlab.haskell.org/ghc/ghc/-/issues/26525.
513+
go_filter :: forall s . Array a -> MArray s a -> Int -> Int -> Int -> ST s Int
511514
go_filter !ary !mary !iAry !iMary !n
512515
| iAry >= n = return iMary
513516
| otherwise = do
@@ -519,36 +522,40 @@ filter f = \ ary ->
519522
else go_filter ary mary (iAry + 1) iMary n
520523
{-# INLINE filter #-}
521524

522-
fromList :: Int -> [a] -> Array a
525+
fromList :: forall a . Int -> [a] -> Array a
523526
fromList n xs0 =
524527
CHECK_EQ("fromList", n, Prelude.length xs0)
525528
run $ do
526529
mary <- new_ n
527530
go xs0 mary 0
528531
return mary
529532
where
533+
go :: forall s . [a] -> MArray s a -> Int -> ST s ()
530534
go [] !_ !_ = return ()
531535
go (x:xs) mary i = do write mary i x
532536
go xs mary (i+1)
533537

534-
fromList' :: Int -> [a] -> Array a
538+
fromList' :: forall a . Int -> [a] -> Array a
535539
fromList' n xs0 =
536540
CHECK_EQ("fromList'", n, Prelude.length xs0)
537541
run $ do
538542
mary <- new_ n
539543
go xs0 mary 0
540544
return mary
541545
where
546+
go :: forall s . [a] -> MArray s a -> Int -> ST s ()
542547
go [] !_ !_ = return ()
543548
go (!x:xs) mary i = do write mary i x
544549
go xs mary (i+1)
545550

551+
#if !defined(__MHS__)
546552
-- | @since 0.2.17.0
547553
instance TH.Lift a => TH.Lift (Array a) where
548554
liftTyped ar = [|| fromList' arlen arlist ||]
549555
where
550556
arlen = length ar
551557
arlist = toList ar
558+
#endif
552559

553560
toList :: Array a -> [a]
554561
toList = foldr (:) []

Data/HashMap/Internal/Strict.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ module Data.HashMap.Internal.Strict
123123
) where
124124

125125
import Control.Applicative (Const (..))
126-
import Control.Monad.ST (runST)
126+
import Control.Monad.ST (runST, ST)
127127
import Data.Bits ((.&.), (.|.))
128128
import Data.Coerce (coerce)
129129
import Data.Functor.Identity (Identity (..))
@@ -227,11 +227,12 @@ unsafeInsertWith :: (Eq k, Hashable k) => (v -> v -> v) -> k -> v -> HashMap k v
227227
unsafeInsertWith f k0 v0 m0 = unsafeInsertWithKey (const f) k0 v0 m0
228228
{-# INLINABLE unsafeInsertWith #-}
229229

230-
unsafeInsertWithKey :: (Eq k, Hashable k) => (k -> v -> v -> v) -> k -> v -> HashMap k v
230+
unsafeInsertWithKey :: forall k v. (Eq k, Hashable k) => (k -> v -> v -> v) -> k -> v -> HashMap k v
231231
-> HashMap k v
232232
unsafeInsertWithKey f k0 v0 m0 = runST (go h0 k0 v0 0 m0)
233233
where
234234
h0 = hash k0
235+
go :: forall s. Hash -> k -> v -> Int -> HashMap k v -> ST s (HashMap k v)
235236
go !h !k x !_ Empty = return $! leaf h k x
236237
go h k x s t@(Leaf hy l@(L ky y))
237238
| hy == h = if ky == k

Data/HashSet/Internal.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,9 @@ fromList :: (Eq a, Hashable a) => [a] -> HashSet a
450450
fromList = HashSet . List.foldl' (\ m k -> H.unsafeInsert k () m) H.empty
451451
{-# INLINE fromList #-}
452452

453+
#if !defined(__MHS__)
453454
instance (Eq a, Hashable a) => Exts.IsList (HashSet a) where
454455
type Item (HashSet a) = a
455456
fromList = fromList
456457
toList = toList
458+
#endif

unordered-containers.cabal

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ library
5858
base >= 4.14 && < 5,
5959
deepseq >= 1.4.3,
6060
hashable >= 1.4 && < 1.6,
61-
template-haskell >= 2.16 && < 2.24
61+
if impl(ghc)
62+
build-depends:
63+
template-haskell >= 2.16 && < 2.24
6264

6365
default-language: Haskell2010
6466

0 commit comments

Comments
 (0)