Skip to content

Commit f5594a4

Browse files
committed
Squash warnings; improve HashMap construction
* Get rid of a bunch of redundant constraint warnings, record field ambiguity warnings, etc. * Use `Data.HashMap.fromListWith` instead of building from a list with `insertWith`. The built-in version does basically the same thing, but with a bunch of unsafe array freeze/thaw operations to reduce allocation and copying.
1 parent 425246d commit f5594a4

File tree

19 files changed

+56
-48
lines changed

19 files changed

+56
-48
lines changed

kore/kore.cabal

+3-3
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,9 @@ common library
156156
build-depends: gitrev >=1.3
157157
build-depends: graphviz >=2999.20
158158
-- Pin hashable to make sure cabal build uses the one Nix & Stack builds do
159-
build-depends: hashable ==1.3.0.0
159+
build-depends: hashable ==1.4.2.0
160160
build-depends: haskeline ==0.8.*
161-
build-depends: integer-gmp >=1.0
161+
build-depends: ghc-bignum >=1.2
162162
build-depends: json-rpc >=1.0
163163
build-depends: lens >=4.17
164164
build-depends: lifted-async
@@ -196,7 +196,7 @@ common library
196196
build-depends: unliftio >= 0.2
197197
build-depends: unordered-containers >=0.2
198198
build-depends: vector >=0.12
199-
build-depends: witherable ==0.3.*
199+
build-depends: witherable ==0.4.*
200200
build-depends: yaml >=0.11.5.0
201201
build-depends: zlib >=0.6
202202
build-tool-depends: happy:happy

kore/src/Kore/Attribute/PredicatePattern.hs

+1-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ instance Debug variable => Debug (PredicatePattern variable) where
6969
instance (Debug variable, Diff variable) => Diff (PredicatePattern variable)
7070

7171
instance
72-
( Functor base
73-
, Synthetic (FreeVariables variable) base
72+
( Synthetic (FreeVariables variable) base
7473
, Synthetic Simplified base
7574
) =>
7675
Synthetic (PredicatePattern variable) base

kore/src/Kore/Builtin/AssociativeCommutative.hs

+4-5
Original file line numberDiff line numberDiff line change
@@ -805,8 +805,8 @@ matchUnifyEqualsNormalizedAc
805805
matchUnifyOpaqueVariable' =
806806
matchUnifyOpaqueVariable tools
807807

808-
listToMap :: Hashable a => Ord a => [a] -> HashMap a Int
809-
listToMap = List.foldl' (\m k -> HashMap.insertWith (+) k 1 m) HashMap.empty
808+
listToMap :: Hashable a => [a] -> HashMap a Int
809+
listToMap keys = HashMap.fromListWith (+) [(k, 1) | k <- keys]
810810
mapToList :: HashMap a Int -> [a]
811811
mapToList =
812812
HashMap.foldrWithKey
@@ -872,7 +872,6 @@ matchUnifyEqualsNormalizedAc
872872

873873
withoutKeys ::
874874
Hashable k =>
875-
Eq k =>
876875
HashMap k v ->
877876
HashSet k ->
878877
HashMap k v
@@ -969,8 +968,8 @@ unifyEqualsNormalizedAc
969968
, commonVariablesCondition
970969
]
971970
where
972-
listToMap :: Hashable a => Ord a => [a] -> HashMap a Int
973-
listToMap = List.foldl' (\m k -> HashMap.insertWith (+) k 1 m) HashMap.empty
971+
listToMap :: Hashable a => [a] -> HashMap a Int
972+
listToMap keys = HashMap.fromListWith (+) [(k, 1) | k <- keys]
974973
mapToList :: HashMap a Int -> [a]
975974
mapToList =
976975
HashMap.foldrWithKey

kore/src/Kore/Builtin/Int.hs

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE MagicHash #-}
2+
{-# LANGUAGE UnboxedSums #-}
23

34
{- |
45
Module : Kore.Builtin.Int
@@ -89,9 +90,11 @@ import Data.Text (
8990
import GHC.Integer (
9091
smallInteger,
9192
)
92-
import GHC.Integer.GMP.Internals (
93-
powModInteger,
94-
recipModInteger,
93+
import GHC.Num.Integer (
94+
integerFromNatural,
95+
integerPowMod#,
96+
integerRecipMod#,
97+
integerToNatural,
9598
)
9699
import GHC.Integer.Logarithms (
97100
integerLog2#,
@@ -364,6 +367,18 @@ powmod b e m
364367
| m == 0 = Nothing
365368
| e < 0 && recipModInteger b m == 0 = Nothing
366369
| otherwise = Just (powModInteger b e m)
370+
where
371+
-- These functions are deprecated in integer-gmp. I'm not sure how to
372+
-- adapt best to the new ghc-bignum regime.
373+
recipModInteger :: Integer -> Integer -> Integer
374+
recipModInteger x m' = case integerRecipMod# x (integerToNatural m') of
375+
(# y | #) -> integerFromNatural y
376+
(# | () #) -> 0
377+
378+
powModInteger :: Integer -> Integer -> Integer -> Integer
379+
powModInteger b' e' m' = case integerPowMod# b' e' (integerToNatural m') of
380+
(# r | #) -> integerFromNatural r
381+
(# | () #) -> 0
367382

368383
evalEq :: Builtin.Function
369384
evalEq sideCondition resultSort arguments@[_intLeft, _intRight] =

kore/src/Kore/Internal/Key.hs

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Copyright : (c) Runtime Verification, 2018-2021
33
License : BSD-3-Clause
44
-}
5+
-- The Synthetic KeyAttributes base instance needs this.
6+
{-# OPTIONS_GHC -Wno-redundant-constraints #-}
57
module Kore.Internal.Key (
68
Key (..),
79
KeyF (..),
@@ -63,6 +65,8 @@ newtype KeyAttributes = KeyAttributes
6365
deriving anyclass (SOP.Generic, SOP.HasDatatypeInfo)
6466
deriving anyclass (Debug, Diff)
6567

68+
-- GHC claims the Functor constraint is redundant, but it gives an error
69+
-- without it. See https://gitlab.haskell.org/ghc/ghc/-/issues/22838
6670
instance (Synthetic Sort base, Functor base) => Synthetic KeyAttributes base where
6771
synthetic base =
6872
KeyAttributes

kore/src/Kore/Internal/SideCondition.hs

-1
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,6 @@ mapVariables adj condition@(SideCondition _ _ _ _ _) =
443443

444444
-- | Utility function for mapping on the keys and values of a 'HashMap'.
445445
mapKeysAndValues ::
446-
Eq b =>
447446
Hashable b =>
448447
(a -> b) ->
449448
HashMap a a ->

kore/src/Kore/Internal/TermLike/TermLike.hs

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
{-# LANGUAGE CPP #-}
21
{-# LANGUAGE UndecidableInstances #-}
32

43
{- |
@@ -55,7 +54,6 @@ import Data.Functor.Identity (
5554
import Data.Generics.Product
5655
import Data.Generics.Product qualified as Lens.Product
5756
import Data.HashMap.Strict qualified as HashMap
58-
import Data.Kind (Constraint)
5957
import Data.Map.Strict (
6058
Map,
6159
)
@@ -694,13 +692,7 @@ instance Ord variable => Ord (TermLike variable) where
694692
(Recursive.project -> _ :< pat2) =
695693
compare pat1 pat2
696694

697-
#if MIN_VERSION_hashable(1,4,0)
698-
type HashableConstraint variable = Eq variable
699-
#else
700-
type HashableConstraint variable = () :: Constraint
701-
#endif
702-
703-
instance HashableConstraint variable => Hashable (TermLike variable) where
695+
instance Eq variable => Hashable (TermLike variable) where
704696
hashWithSalt salt (TermLike__ _ hsh _) =
705697
salt `hashWithSalt` hsh -- HACK
706698
{-# INLINE hashWithSalt #-}

kore/src/Kore/Log/ErrorOutOfDate.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Control.Monad.Catch (
1515
MonadThrow,
1616
throwM,
1717
)
18-
import Log
18+
import Log hiding (message)
1919
import Prelude.Kore
2020
import Pretty
2121

kore/src/Kore/Log/ErrorParse.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Control.Monad.Catch (
1515
MonadThrow,
1616
throwM,
1717
)
18-
import Log
18+
import Log hiding (message)
1919
import Prelude.Kore
2020
import Pretty
2121

kore/src/Kore/Log/WarnNotAPredicate.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import Kore.Internal.Conditional qualified as Conditional
1616
import Kore.Internal.Pattern (Pattern)
1717
import Kore.Internal.Predicate (Predicate)
1818
import Kore.Rewrite.RewritingVariable
19-
import Log
19+
import Log hiding (severity)
2020
import Prelude.Kore
2121
import Pretty (Pretty)
2222
import Pretty qualified

kore/src/Kore/Simplify/Simplify.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ instance MonadSimplify Simplifier where
311311
{-# INLINE askMemo #-}
312312

313313
instance
314-
(WithLog LogMessage m, MonadSimplify m, Monoid w) =>
314+
(MonadSimplify m, Monoid w) =>
315315
MonadSimplify (AccumT w m)
316316
where
317317
localAxiomEquations locally =

kore/src/Log.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ runLoggerT LoggerT{getLoggerT} logAction =
264264
runReaderT getLoggerT LoggerEnv{logAction}
265265
{-# INLINE runLoggerT #-}
266266

267-
instance (MonadCatch m, MonadThrow m) => MonadLog (LoggerT m) where
267+
instance MonadCatch m => MonadLog (LoggerT m) where
268268
logEntry entry = do
269269
someLogAction <- askLogAction
270270
lift $ someLogAction <& toEntry entry

kore/test/Test/Kore/Builtin/Map.hs

-1
Original file line numberDiff line numberDiff line change
@@ -1582,7 +1582,6 @@ distinctVariables variables =
15821582
-- | Utility function for mapping over the keys of a 'HashMap'.
15831583
mapKeys ::
15841584
Hashable k2 =>
1585-
Eq k2 =>
15861585
(k1 -> k2) ->
15871586
HashMap k1 v ->
15881587
HashMap k2 v

kore/test/Test/Kore/Builtin/Set.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1917,7 +1917,7 @@ normalizedSet elements opaque =
19171917
mkIntVar :: Id -> TermLike VariableName
19181918
mkIntVar variableName = mkElemVar $ mkElementVariable variableName intSort
19191919

1920-
setIntersectionsAreEmpty :: Hashable a => Eq a => [HashSet a] -> Bool
1920+
setIntersectionsAreEmpty :: Hashable a => [HashSet a] -> Bool
19211921
setIntersectionsAreEmpty [] = True
19221922
setIntersectionsAreEmpty (set : sets) =
19231923
setIntersectionsAreEmpty sets

kore/test/Test/Kore/Equation/Common.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import Data.Sup (
2020
Sup (..),
2121
)
2222
import GHC.Natural (
23-
intToNatural,
23+
wordToNatural,
2424
)
2525
import Kore.Attribute.Axiom (Simplification (..))
2626
import Kore.Attribute.Axiom.Concrete (
@@ -84,7 +84,7 @@ functionAxiomUnification symbol args right requires =
8484
where
8585
~left = mkApplySymbol symbol variables
8686
sorts = fmap termLikeSort args
87-
~variables = generateVariables (intToNatural (length args)) sorts
87+
~variables = generateVariables (wordToNatural (fromIntegral (length args))) sorts
8888
generateVariables ~n sorts' =
8989
-- lazy argument to prevent arithmetic underflow
9090
fmap makeElementVariable (zip [0 .. n - 1] sorts')

kore/test/Test/Kore/Rewrite/MockSymbols.hs

+8-7
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import Kore.Attribute.Pattern.ConstructorLike (
4646
isConstructorLike,
4747
)
4848
import Kore.Attribute.Sort qualified as Attribute
49+
import Kore.Attribute.Sort qualified as AttributeSort
4950
import Kore.Attribute.Sort.Concat qualified as Attribute
5051
import Kore.Attribute.Sort.Constructors qualified as Attribute (
5152
Constructors,
@@ -1793,7 +1794,7 @@ sortAttributesMapping =
17931794
,
17941795
( mapSort
17951796
, Default.def
1796-
{ Attribute.hook = Hook (Just "MAP.Map")
1797+
{ AttributeSort.hook = Hook (Just "MAP.Map")
17971798
, Attribute.unit =
17981799
Attribute.Unit (Just $ toSymbolOrAlias unitMapSymbol)
17991800
, Attribute.element =
@@ -1805,7 +1806,7 @@ sortAttributesMapping =
18051806
,
18061807
( listSort
18071808
, Default.def
1808-
{ Attribute.hook = Hook (Just "LIST.List")
1809+
{ AttributeSort.hook = Hook (Just "LIST.List")
18091810
, Attribute.unit =
18101811
Attribute.Unit (Just $ toSymbolOrAlias unitListSymbol)
18111812
, Attribute.element =
@@ -1817,7 +1818,7 @@ sortAttributesMapping =
18171818
,
18181819
( setSort
18191820
, Default.def
1820-
{ Attribute.hook = Hook (Just "SET.Set")
1821+
{ AttributeSort.hook = Hook (Just "SET.Set")
18211822
, Attribute.unit =
18221823
Attribute.Unit (Just $ toSymbolOrAlias unitSetSymbol)
18231824
, Attribute.element =
@@ -1828,20 +1829,20 @@ sortAttributesMapping =
18281829
)
18291830
,
18301831
( intSort
1831-
, Default.def{Attribute.hook = Hook (Just "INT.Int")}
1832+
, Default.def{AttributeSort.hook = Hook (Just "INT.Int")}
18321833
)
18331834
,
18341835
( boolSort
1835-
, Default.def{Attribute.hook = Hook (Just "BOOL.Bool")}
1836+
, Default.def{AttributeSort.hook = Hook (Just "BOOL.Bool")}
18361837
)
18371838
,
18381839
( stringSort
1839-
, Default.def{Attribute.hook = Hook (Just "STRING.String")}
1840+
, Default.def{AttributeSort.hook = Hook (Just "STRING.String")}
18401841
)
18411842
, -- Also add attributes for the implicitly defined sorts.
18421843

18431844
( stringMetaSort
1844-
, Default.def{Attribute.hook = Hook (Just "STRING.String")}
1845+
, Default.def{AttributeSort.hook = Hook (Just "STRING.String")}
18451846
)
18461847
]
18471848

kore/test/Test/Kore/Variables/V.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import Data.Sup
1313
import Debug
1414
import GHC.Generics qualified as GHC
1515
import Generics.SOP qualified as SOP
16-
import Kore.Internal.TermLike
16+
import Kore.Internal.TermLike hiding (counter)
1717
import Kore.Unparser
18-
import Kore.Variables.Fresh
18+
import Kore.Variables.Fresh hiding (counter)
1919
import Numeric.Natural
2020
import Prelude.Kore
2121
import Pretty

kore/test/Test/Kore/Variables/W.hs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ import Data.Sup
1414
import Debug
1515
import GHC.Generics qualified as GHC
1616
import Generics.SOP qualified as SOP
17-
import Kore.Internal.TermLike
17+
import Kore.Internal.TermLike hiding (counter)
1818
import Kore.Unparser
19-
import Kore.Variables.Fresh
19+
import Kore.Variables.Fresh hiding (counter)
2020
import Numeric.Natural
2121
import Prelude.Kore
2222
import Pretty
23-
import Test.Kore.Variables.V
23+
import Test.Kore.Variables.V hiding (counter)
2424

2525
data W = W {value :: String, counter :: Maybe (Sup Natural)}
2626
deriving stock (Show, Eq, Ord)

kore/test/Test/Kore/With.hs

+4-4
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ instance
354354
newtype VariableElement child = VariableElement {getVariableElement :: child}
355355

356356
instance
357-
(Hashable child, Ord child) =>
357+
Hashable child =>
358358
With
359359
(NormalizedAc NormalizedSet Key child)
360360
(VariableElement child)
@@ -379,15 +379,15 @@ instance
379379
internalSet
380380

381381
instance
382-
(Hashable child, Ord child) =>
382+
Hashable child =>
383383
With
384384
(NormalizedAc NormalizedSet Key child)
385385
[VariableElement child]
386386
where
387387
with = foldl' with
388388

389389
instance
390-
(Hashable child, Ord child) =>
390+
Hashable child =>
391391
With
392392
(NormalizedSet Key child)
393393
(VariableElement child)
@@ -398,7 +398,7 @@ instance
398398
NormalizedSet (ac `with` value)
399399

400400
instance
401-
(Hashable child, Ord child) =>
401+
Hashable child =>
402402
With
403403
(NormalizedSet Key child)
404404
[VariableElement child]

0 commit comments

Comments
 (0)