Skip to content

Commit d06eb34

Browse files
authored
Make it compile with MicroHs (#553)
1 parent 478bb60 commit d06eb34

File tree

6 files changed

+78
-11
lines changed

6 files changed

+78
-11
lines changed

.github/workflows/mhs-ci.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: mhs-ci
2+
3+
on:
4+
push:
5+
branches: [ "master" ]
6+
pull_request:
7+
branches: [ "master" ]
8+
9+
jobs:
10+
build-mhs-pretty:
11+
runs-on: ubuntu-latest
12+
steps:
13+
14+
- name: checkout mhs repo
15+
uses: actions/checkout@v4
16+
with:
17+
repository: augustss/MicroHs
18+
ref: v0.14.23.1
19+
path: mhs
20+
- name: make and install mhs
21+
run: |
22+
cd mhs
23+
make minstall
24+
25+
- name: checkout unordered-containers repo
26+
uses: actions/checkout@v4
27+
with:
28+
path: unordered-containers
29+
- name: compile and install unordered-containers package
30+
run: |
31+
PATH="$HOME/.mcabal/bin:$PATH"
32+
cd unordered-containers
33+
mcabal -r install
34+
35+
- name: cleanup
36+
run: |
37+
rm -rf $HOME/.mcabal

Data/HashMap/Internal.hs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ module Data.HashMap.Internal
149149
, adjust#
150150
) where
151151

152+
import Data.Traversable -- MicroHs needs this since its Prelude does not have Foldable&Traversable.
153+
-- It's harmless for GHC, and putting it first avoid a warning.
154+
152155
import Control.Applicative (Const (..))
153156
import Control.DeepSeq (NFData (..), NFData1 (..), NFData2 (..))
154157
import Control.Monad.ST (ST, runST)
@@ -192,9 +195,11 @@ data Leaf k v = L !k v
192195
instance (NFData k, NFData v) => NFData (Leaf k v) where
193196
rnf (L k v) = rnf k `seq` rnf v
194197

198+
#if defined(__GLASGOW_HASKELL__)
195199
-- | @since 0.2.17.0
196200
instance (TH.Lift k, TH.Lift v) => TH.Lift (Leaf k v) where
197201
liftTyped (L k v) = [|| L k $! v ||]
202+
#endif
198203

199204
-- | @since 0.2.14.0
200205
instance NFData k => NFData1 (Leaf k) where
@@ -701,7 +706,11 @@ lookupRecordCollision# h k m =
701706
-- this whole thing is always inlined, we don't have to worry about
702707
-- any extra CPS overhead.
703708
lookupCont ::
709+
#if defined(__GLASGOW_HASKELL__)
704710
forall rep (r :: TYPE rep) k v.
711+
#else
712+
forall r k v.
713+
#endif
705714
Eq k
706715
=> ((# #) -> r) -- Absent continuation
707716
-> (v -> Int -> r) -- Present continuation
@@ -915,10 +924,11 @@ setAtPosition i k x ary = A.update ary i (L k x)
915924

916925

917926
-- | In-place update version of insert
918-
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
919928
unsafeInsert k0 v0 m0 = runST (go h0 k0 v0 0 m0)
920929
where
921930
h0 = hash k0
931+
go :: forall s. Hash -> k -> v -> Int -> HashMap k v -> ST s (HashMap k v)
922932
go !h !k x !_ Empty = return $! Leaf h (L k x)
923933
go h k x s t@(Leaf hy l@(L ky y))
924934
| hy == h = if ky == k
@@ -2588,7 +2598,11 @@ fromListWithKey f = List.foldl' (\ m (k, v) -> unsafeInsertWithKey (\k' a b -> (
25882598
-- | \(O(n)\) Look up the value associated with the given key in an
25892599
-- array.
25902600
lookupInArrayCont ::
2601+
#if defined(__GLASGOW_HASKELL__)
25912602
forall rep (r :: TYPE rep) k v.
2603+
#else
2604+
forall r k v.
2605+
#endif
25922606
Eq k => ((# #) -> r) -> (v -> Int -> r) -> k -> A.Array (Leaf k v) -> r
25932607
lookupInArrayCont absent present k0 ary0 = go k0 ary0 0 (A.length ary0)
25942608
where
@@ -2841,9 +2855,11 @@ otherOfOneOrZero :: Int -> Int
28412855
otherOfOneOrZero i = 1 - i
28422856
{-# INLINE otherOfOneOrZero #-}
28432857

2858+
#if defined(__GLASGOW_HASKELL__)
28442859
------------------------------------------------------------------------
28452860
-- IsList instance
28462861
instance (Eq k, Hashable k) => Exts.IsList (HashMap k v) where
28472862
type Item (HashMap k v) = (k, v)
28482863
fromList = fromList
28492864
toList = toList
2865+
#endif

Data/HashMap/Internal/Array.hs

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

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

484485
-- | Strict version of 'map'.
485-
map' :: (a -> b) -> Array a -> Array b
486+
map' :: forall a b . (a -> b) -> Array a -> Array b
486487
map' f = \ ary ->
487488
let !n = length ary
488489
in run $ do
489490
mary <- new_ n
490491
go ary mary 0 n
491492
return mary
492493
where
494+
go :: forall s . Array a -> MArray s b -> Int -> Int -> ST s ()
493495
go ary mary i n
494496
| i >= n = return ()
495497
| otherwise = do
@@ -498,7 +500,7 @@ map' f = \ ary ->
498500
go ary mary (i+1) n
499501
{-# INLINE map' #-}
500502

501-
filter :: (a -> Bool) -> Array a -> Array a
503+
filter :: forall a . (a -> Bool) -> Array a -> Array a
502504
filter f = \ ary ->
503505
let !n = length ary
504506
in run $ do
@@ -509,6 +511,7 @@ filter f = \ ary ->
509511
-- Without the @!@ on @ary@ we end up reboxing the array when using
510512
-- 'differenceCollisions'. See
511513
-- https://gitlab.haskell.org/ghc/ghc/-/issues/26525.
514+
go_filter :: forall s . Array a -> MArray s a -> Int -> Int -> Int -> ST s Int
512515
go_filter !ary !mary !iAry !iMary !n
513516
| iAry >= n = return iMary
514517
| otherwise = do
@@ -520,14 +523,15 @@ filter f = \ ary ->
520523
else go_filter ary mary (iAry + 1) iMary n
521524
{-# INLINE filter #-}
522525

523-
mapMaybe :: (a -> Maybe b) -> Array a -> Array b
526+
mapMaybe :: forall a b . (a -> Maybe b) -> Array a -> Array b
524527
mapMaybe f = \ ary ->
525528
let !n = length ary
526529
in run $ do
527530
mary <- new_ n
528531
len <- go_mapMaybe ary mary 0 0 n
529532
shrink mary len
530533
where
534+
go_mapMaybe :: forall s . Array a -> MArray s b -> Int -> Int -> Int -> ST s Int
531535
go_mapMaybe !ary !mary !iAry !iMary !n
532536
| iAry >= n = return iMary
533537
| otherwise = do
@@ -539,36 +543,40 @@ mapMaybe f = \ ary ->
539543
go_mapMaybe ary mary (iAry + 1) (iMary + 1) n
540544
{-# INLINE mapMaybe #-}
541545

542-
fromList :: Int -> [a] -> Array a
546+
fromList :: forall a . Int -> [a] -> Array a
543547
fromList n xs0 =
544548
CHECK_EQ("fromList", n, Prelude.length xs0)
545549
run $ do
546550
mary <- new_ n
547551
go xs0 mary 0
548552
return mary
549553
where
554+
go :: forall s . [a] -> MArray s a -> Int -> ST s ()
550555
go [] !_ !_ = return ()
551556
go (x:xs) mary i = do write mary i x
552557
go xs mary (i+1)
553558

554-
fromList' :: Int -> [a] -> Array a
559+
fromList' :: forall a . Int -> [a] -> Array a
555560
fromList' n xs0 =
556561
CHECK_EQ("fromList'", n, Prelude.length xs0)
557562
run $ do
558563
mary <- new_ n
559564
go xs0 mary 0
560565
return mary
561566
where
567+
go :: forall s . [a] -> MArray s a -> Int -> ST s ()
562568
go [] !_ !_ = return ()
563569
go (!x:xs) mary i = do write mary i x
564570
go xs mary (i+1)
565571

572+
#if defined(__GLASGOW_HASKELL__)
566573
-- | @since 0.2.17.0
567574
instance TH.Lift a => TH.Lift (Array a) where
568575
liftTyped ar = [|| fromList' arlen arlist ||]
569576
where
570577
arlen = length ar
571578
arlist = toList ar
579+
#endif
572580

573581
toList :: Array a -> [a]
574582
toList = foldr (:) []

Data/HashMap/Internal/Strict.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{-# LANGUAGE LambdaCase #-}
44
{-# LANGUAGE MagicHash #-}
55
{-# LANGUAGE PatternGuards #-}
6+
{-# LANGUAGE ScopedTypeVariables #-}
67
{-# LANGUAGE Trustworthy #-}
78
{-# LANGUAGE UnboxedTuples #-}
89
{-# OPTIONS_HADDOCK not-home #-}
@@ -124,7 +125,7 @@ module Data.HashMap.Internal.Strict
124125
) where
125126

126127
import Control.Applicative (Const (..))
127-
import Control.Monad.ST (runST)
128+
import Control.Monad.ST (runST, ST)
128129
import Data.Bits ((.&.), (.|.))
129130
import Data.Coerce (coerce)
130131
import Data.Functor.Identity (Identity (..))
@@ -228,11 +229,12 @@ unsafeInsertWith :: (Eq k, Hashable k) => (v -> v -> v) -> k -> v -> HashMap k v
228229
unsafeInsertWith f k0 v0 m0 = unsafeInsertWithKey (const f) k0 v0 m0
229230
{-# INLINABLE unsafeInsertWith #-}
230231

231-
unsafeInsertWithKey :: (Eq k, Hashable k) => (k -> v -> v -> v) -> k -> v -> HashMap k v
232+
unsafeInsertWithKey :: forall k v. (Eq k, Hashable k) => (k -> v -> v -> v) -> k -> v -> HashMap k v
232233
-> HashMap k v
233234
unsafeInsertWithKey f k0 v0 m0 = runST (go h0 k0 v0 0 m0)
234235
where
235236
h0 = hash k0
237+
go :: forall s. Hash -> k -> v -> Int -> HashMap k v -> ST s (HashMap k v)
236238
go !h !k x !_ Empty = return $! leaf h k x
237239
go h k x s t@(Leaf hy l@(L ky y))
238240
| 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(__GLASGOW_HASKELL__)
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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ library
5757
build-depends:
5858
base >= 4.14 && < 5,
5959
deepseq >= 1.4.3,
60-
hashable >= 1.4 && < 1.6,
61-
template-haskell >= 2.16 && < 2.24
60+
hashable >= 1.4 && < 1.6
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)