Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various changes to make it compile with MicroHs. #1043

Merged
merged 12 commits into from
Oct 4, 2024
31 changes: 31 additions & 0 deletions .github/workflows/mhs-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: MicroHs CI for containers

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
build-mhs-containers:
runs-on: ubuntu-latest
steps:
- name: checkout containers repo
uses: actions/checkout@v4
with:
path: cont
- name: checkout mhs repo
uses: actions/checkout@v4
with:
repository: augustss/MicroHs
augustss marked this conversation as resolved.
Show resolved Hide resolved
path: mhs
- name: make mhs
run: |
cd mhs
make
# It's pretty ugly with the list of modules here, but I don't know a nice way of getting it from the cabal file.
# I'll make it nicer with mcabal later.
- name: compile containers package
run: |
cd mhs
MHSCPPHS=./bin/cpphs ./bin/mhs -Pcontainers-test -ocontainers-test.pkg -i../cont/containers/src -XCPP -I../cont/containers/include Data.Containers.ListUtils Data.IntMap Data.IntMap.Lazy Data.IntMap.Strict Data.IntMap.Strict.Internal Data.IntMap.Internal Data.IntMap.Internal.Debug Data.IntMap.Merge.Lazy Data.IntMap.Merge.Strict Data.IntSet.Internal Data.IntSet.Internal.IntTreeCommons Data.IntSet Data.Map Data.Map.Lazy Data.Map.Merge.Lazy Data.Map.Strict.Internal Data.Map.Strict Data.Map.Merge.Strict Data.Map.Internal Data.Map.Internal.Debug Data.Set.Internal Data.Set Data.Graph Data.Sequence Data.Sequence.Internal Data.Sequence.Internal.Sorting Data.Tree Utils.Containers.Internal.BitUtil Utils.Containers.Internal.BitQueue Utils.Containers.Internal.StrictPair
2 changes: 2 additions & 0 deletions containers/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

### Bug fixes

* Make the package compile with MicroHs. (Lennart Augustsson)

* `Data.Map.Strict.mergeWithKey` now forces the result of the combining function
to WHNF. (Soumik Sarkar)

Expand Down
4 changes: 3 additions & 1 deletion containers/containers.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ source-repository head

Library
default-language: Haskell2010
build-depends: base >= 4.10 && < 5, array >= 0.4.0.0, deepseq >= 1.2 && < 1.6, template-haskell
build-depends: base >= 4.10 && < 5, array >= 0.4.0.0, deepseq >= 1.2 && < 1.6
if impl(ghc)
build-depends: template-haskell
hs-source-dirs: src
ghc-options: -O2 -Wall -fwarn-incomplete-uni-patterns -fwarn-incomplete-record-updates

Expand Down
4 changes: 2 additions & 2 deletions containers/include/containers.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
#define HASKELL_CONTAINERS_H

/*
* On GHC, include MachDeps.h to get WORD_SIZE_IN_BITS macro.
* On GHC and MicroHs, include MachDeps.h to get WORD_SIZE_IN_BITS macro.
*/
#ifdef __GLASGOW_HASKELL__
#if defined(__GLASGOW_HASKELL__) || defined(__MHS__)
#include "MachDeps.h"
#endif

Expand Down
8 changes: 6 additions & 2 deletions containers/src/Data/Graph.hs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ import Data.Tree (Tree(Node), Forest)

-- std interfaces
import Data.Foldable as F
#if MIN_VERSION_base(4,18,0)
#if MIN_VERSION_base(4,18,0) && defined(__GLASGOW_HASKELL__)
import qualified Data.Foldable1 as F1
augustss marked this conversation as resolved.
Show resolved Hide resolved
#endif
import Control.DeepSeq (NFData(rnf))
Expand All @@ -130,7 +130,9 @@ import qualified Data.Array as UA
import qualified Data.List as L
import Data.List.NonEmpty (NonEmpty(..))
import qualified Data.List.NonEmpty as NE
#ifdef __GLASGOW_HASKELL__
import Data.Functor.Classes
#endif
#if !MIN_VERSION_base(4,11,0)
import Data.Semigroup (Semigroup (..))
#endif
Expand Down Expand Up @@ -194,6 +196,7 @@ instance Lift vertex => Lift (SCC vertex) where

#endif

#ifdef __GLASGOW_HASKELL__
-- | @since 0.5.9
instance Eq1 SCC where
liftEq eq (AcyclicSCC v1) (AcyclicSCC v2) = eq v1 v2
Expand All @@ -209,13 +212,14 @@ instance Read1 SCC where
readsUnaryWith rp "AcyclicSCC" AcyclicSCC <>
readsUnaryWith (liftReadsPrec rp rl) "NECyclicSCC" NECyclicSCC <>
readsUnaryWith (const rl) "CyclicSCC" CyclicSCC
#endif

-- | @since 0.5.9
instance F.Foldable SCC where
foldr c n (AcyclicSCC v) = c v n
foldr c n (NECyclicSCC vs) = foldr c n vs

#if MIN_VERSION_base(4,18,0)
#if MIN_VERSION_base(4,18,0) && defined(__GLASGOW_HASKELL__)
-- | @since 0.7.0
instance F1.Foldable1 SCC where
foldMap1 f (AcyclicSCC v) = f v
Expand Down
31 changes: 22 additions & 9 deletions containers/src/Data/IntMap/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,9 @@ import Data.Semigroup (Semigroup(stimes))
import Data.Semigroup (Semigroup((<>)))
#endif
import Data.Semigroup (stimesIdempotentMonoid)
#ifdef __GLASGOW_HASKELL__
import Data.Functor.Classes
#endif

import Control.DeepSeq (NFData(rnf))
import Data.Bits
Expand Down Expand Up @@ -395,8 +397,10 @@ data IntMap a = Bin {-# UNPACK #-} !Prefix
type IntSetPrefix = Int
type IntSetBitMap = Word

#ifdef __GLASGOW_HASKELL__
-- | @since 0.6.6
deriving instance Lift a => Lift (IntMap a)
#endif

bitmapOf :: Int -> IntSetBitMap
bitmapOf x = shiftLL 1 (x .&. IntSet.suffixBitMask)
Expand Down Expand Up @@ -2067,7 +2071,7 @@ merge g1 g2 f m1 m2 =
--
-- @since 0.5.9
mergeA
:: (Applicative f)
:: forall f a b c . (Applicative f)
augustss marked this conversation as resolved.
Show resolved Hide resolved
=> WhenMissing f a c -- ^ What to do with keys in @m1@ but not @m2@
-> WhenMissing f b c -- ^ What to do with keys in @m2@ but not @m1@
-> WhenMatched f a b c -- ^ What to do with keys in both @m1@ and @m2@
Expand Down Expand Up @@ -2112,6 +2116,7 @@ mergeA
EQL -> binA p1 (go l1 l2) (go r1 r2)
NOM -> linkA (unPrefix p1) (g1t t1) (unPrefix p2) (g2t t2)

subsingletonBy :: forall a' . (Key -> a' -> f (Maybe c)) -> Key -> a' -> f (IntMap c)
subsingletonBy gk k x = maybe Nil (Tip k) <$> gk k x
{-# INLINE subsingletonBy #-}

Expand All @@ -2133,10 +2138,10 @@ mergeA
-- | A variant of 'link_' which makes sure to execute side-effects
-- in the right order.
linkA
:: Applicative f
=> Int -> f (IntMap a)
-> Int -> f (IntMap a)
-> f (IntMap a)
:: forall a' . Applicative f
=> Int -> f (IntMap a')
-> Int -> f (IntMap a')
-> f (IntMap a')
linkA k1 t1 k2 t2
| natFromInt k1 < natFromInt k2 = binA p t1 t2
| otherwise = binA p t2 t1
Expand All @@ -2148,11 +2153,11 @@ mergeA
-- A variant of 'bin' that ensures that effects for negative keys are executed
-- first.
binA
:: Applicative f
:: forall a' . Applicative f
=> Prefix
-> f (IntMap a)
-> f (IntMap a)
-> f (IntMap a)
-> f (IntMap a')
-> f (IntMap a')
-> f (IntMap a')
binA p a b
| signBranch p = liftA2 (flip (bin p)) b a
| otherwise = liftA2 (bin p) a b
Expand Down Expand Up @@ -3444,6 +3449,7 @@ equal Nil Nil = True
equal _ _ = False
{-# INLINABLE equal #-}

#ifdef __GLASGOW_HASKELL__
-- | @since 0.5.9
instance Eq1 IntMap where
liftEq eq (Bin p1 l1 r1) (Bin p2 l2 r2)
Expand All @@ -3452,6 +3458,7 @@ instance Eq1 IntMap where
= (kx == ky) && (eq x y)
liftEq _eq Nil Nil = True
liftEq _eq _ _ = False
#endif

{--------------------------------------------------------------------
Ord
Expand All @@ -3460,10 +3467,12 @@ instance Eq1 IntMap where
instance Ord a => Ord (IntMap a) where
compare m1 m2 = compare (toList m1) (toList m2)

#ifdef __GLASGOW_HASKELL__
-- | @since 0.5.9
instance Ord1 IntMap where
liftCompare cmp m n =
liftCompare (liftCompare cmp) (toList m) (toList n)
#endif

{--------------------------------------------------------------------
Functor
Expand All @@ -3486,13 +3495,15 @@ instance Show a => Show (IntMap a) where
showsPrec d m = showParen (d > 10) $
showString "fromList " . shows (toList m)

#ifdef __GLASGOW_HASKELL__
-- | @since 0.5.9
instance Show1 IntMap where
liftShowsPrec sp sl d m =
showsUnaryWith (liftShowsPrec sp' sl') "fromList" d (toList m)
where
sp' = liftShowsPrec sp sl
sl' = liftShowList sp sl
#endif

{--------------------------------------------------------------------
Read
Expand All @@ -3512,13 +3523,15 @@ instance (Read e) => Read (IntMap e) where
return (fromList xs,t)
#endif

#ifdef __GLASGOW_HASKELL__
-- | @since 0.5.9
instance Read1 IntMap where
liftReadsPrec rp rl = readsData $
readsUnaryWith (liftReadsPrec rp' rl') "fromList" fromList
where
rp' = liftReadsPrec rp rl
rl' = liftReadList rp rl
#endif

{--------------------------------------------------------------------
Helpers
Expand Down
8 changes: 4 additions & 4 deletions containers/src/Data/IntSet/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1641,16 +1641,16 @@ takeWhileAntitoneBits :: Int -> (Int -> Bool) -> Nat -> Nat
{-# INLINE foldr'Bits #-}
{-# INLINE takeWhileAntitoneBits #-}

lowestBitMask :: Nat -> Nat
lowestBitMask x = x .&. negate x
{-# INLINE lowestBitMask #-}

#if defined(__GLASGOW_HASKELL__)

lowestBitSet x = countTrailingZeros x

highestBitSet x = WORD_SIZE_IN_BITS - 1 - countLeadingZeros x

lowestBitMask :: Nat -> Nat
lowestBitMask x = x .&. negate x
{-# INLINE lowestBitMask #-}

-- Reverse the order of bits in the Nat.
revNat :: Nat -> Nat
#if WORD_SIZE_IN_BITS==32
Expand Down
25 changes: 22 additions & 3 deletions containers/src/Data/Map/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE TypeFamilies #-}
#endif
#define USE_MAGIC_PROXY 1
#endif

#ifdef USE_MAGIC_PROXY
{-# LANGUAGE MagicHash #-}
Expand Down Expand Up @@ -379,7 +379,9 @@ module Data.Map.Internal (

import Data.Functor.Identity (Identity (..))
import Control.Applicative (liftA3)
#ifdef __GLASGOW_HASKELL__
import Data.Functor.Classes
#endif
import Data.Semigroup (stimesIdempotentMonoid)
import Data.Semigroup (Arg(..), Semigroup(stimes))
#if !(MIN_VERSION_base(4,11,0))
Expand All @@ -389,7 +391,9 @@ import Control.Applicative (Const (..))
import Control.DeepSeq (NFData(rnf))
import Data.Bits (shiftL, shiftR)
import qualified Data.Foldable as Foldable
#ifdef __GLASGOW_HASKELL__
import Data.Bifoldable
#endif
import Utils.Containers.Internal.Prelude hiding
(lookup, map, filter, foldr, foldl, foldl', null, splitAt, take, drop)
import Prelude ()
Expand All @@ -416,10 +420,9 @@ import GHC.Exts (Proxy#, proxy# )
import qualified GHC.Exts as GHCExts
import Text.Read hiding (lift)
import Data.Data
import qualified Control.Category as Category
import Data.Coerce
#endif

import qualified Control.Category as Category

{--------------------------------------------------------------------
Operators
Expand Down Expand Up @@ -2340,6 +2343,7 @@ instance Functor f => Functor (WhenMatched f k x y) where
fmap = mapWhenMatched
{-# INLINE fmap #-}

#ifdef __GLASGOW_HASKELL__
-- | @since 0.5.9
instance (Monad f, Applicative f) => Category.Category (WhenMatched f k x) where
id = zipWithMatched (\_ _ y -> y)
Expand All @@ -2351,6 +2355,7 @@ instance (Monad f, Applicative f) => Category.Category (WhenMatched f k x) where
Just r -> runWhenMatched f k x r
{-# INLINE id #-}
{-# INLINE (.) #-}
#endif

-- | Equivalent to @ ReaderT k (ReaderT x (ReaderT y (MaybeT f))) @
--
Expand Down Expand Up @@ -4303,6 +4308,7 @@ bin k x l r
Eq
--------------------------------------------------------------------}

#ifdef __GLASGOW_HASKELL__
instance (Eq k,Eq a) => Eq (Map k a) where
m1 == m2 = liftEq2 (==) (==) m1 m2
{-# INLINABLE (==) #-}
Expand All @@ -4327,11 +4333,16 @@ sameSizeLiftEq2 keq eq m1 m2 =
Nothing -> False :*: it
Just (KeyValue ky y :*: it') -> (keq kx ky && eq x y) :*: it'
{-# INLINE sameSizeLiftEq2 #-}
#else
instance (Eq k,Eq a) => Eq (Map k a) where
t1 == t2 = (size t1 == size t2) && (toAscList t1 == toAscList t2)
#endif
augustss marked this conversation as resolved.
Show resolved Hide resolved

{--------------------------------------------------------------------
Ord
--------------------------------------------------------------------}

#ifdef __GLASGOW_HASKELL__
instance (Ord k, Ord v) => Ord (Map k v) where
compare m1 m2 = liftCmp2 compare compare m1 m2
{-# INLINABLE compare #-}
Expand Down Expand Up @@ -4359,11 +4370,16 @@ liftCmp2 kcmp cmp m1 m2 = case runOrdM (foldMapWithKey f m1) (iterator m2) of
Nothing -> GT :*: it
Just (KeyValue ky y :*: it') -> (kcmp kx ky <> cmp x y) :*: it'
{-# INLINE liftCmp2 #-}
#else
instance (Ord k, Ord v) => Ord (Map k v) where
compare m1 m2 = compare (toAscList m1) (toAscList m2)
#endif

{--------------------------------------------------------------------
Lifted instances
--------------------------------------------------------------------}

#ifdef __GLASGOW_HASKELL__
-- | @since 0.5.9
instance Show2 Map where
liftShowsPrec2 spk slk spv slv d m =
Expand All @@ -4383,6 +4399,7 @@ instance (Ord k, Read k) => Read1 (Map k) where
where
rp' = liftReadsPrec rp rl
rl' = liftReadList rp rl
#endif

{--------------------------------------------------------------------
Functor
Expand Down Expand Up @@ -4448,6 +4465,7 @@ instance Foldable.Foldable (Map k) where
product = foldl' (*) 1
{-# INLINABLE product #-}

#ifdef __GLASGOW_HASKELL__
-- | @since 0.6.3.1
instance Bifoldable Map where
bifold = go
Expand All @@ -4468,6 +4486,7 @@ instance Bifoldable Map where
go (Bin 1 k v _ _) = f k `mappend` g v
go (Bin _ k v l r) = go l `mappend` (f k `mappend` (g v `mappend` go r))
{-# INLINE bifoldMap #-}
#endif

instance (NFData k, NFData a) => NFData (Map k a) where
rnf Tip = ()
Expand Down
2 changes: 2 additions & 0 deletions containers/src/Data/Map/Strict/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,9 @@ import Data.Map.Internal
, argSet
, assocs
, atKeyImpl
#ifdef __GLASGOW_HASKELL__
, atKeyPlain
#endif
, balance
, balanceL
, balanceR
Expand Down
Loading