Skip to content

Commit f41cba2

Browse files
authored
Revert "Revert #2315 (#2385)" (#2389)
This reverts commit 266597e.
1 parent 4fb8352 commit f41cba2

File tree

18 files changed

+1875
-943
lines changed

18 files changed

+1875
-943
lines changed

kore/src/Debug.hs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
{-# LANGUAGE UndecidableInstances #-}
2-
31
{- |
42
Copyright : (c) Runtime Verification, 2019
53
License : NCSA
64
75
-}
6+
{-# LANGUAGE Strict #-}
7+
{-# LANGUAGE UndecidableInstances #-}
88

99
module Debug
1010
(
@@ -37,6 +37,14 @@ import Data.Hashable
3737
( Hashed
3838
, unhashed
3939
)
40+
import Data.HashMap.Strict
41+
( HashMap
42+
)
43+
import qualified Data.HashMap.Strict as HashMap
44+
import Data.HashSet
45+
( HashSet
46+
)
47+
import qualified Data.HashSet as HashSet
4048
import Data.Int
4149
import Data.Map.Strict
4250
( Map
@@ -332,11 +340,21 @@ instance (Debug k, Debug a) => Debug (Map.Map k a) where
332340
(parens (precOut >= 10) . Pretty.sep)
333341
["Data.Map.Strict.fromList", debug (Map.toList as)]
334342

343+
instance (Debug k, Debug a) => Debug (HashMap k a) where
344+
debugPrec as precOut =
345+
(parens (precOut >= 10) . Pretty.sep)
346+
["Data.HashMap.Strict.fromList", debug (HashMap.toList as)]
347+
335348
instance Debug a => Debug (Set a) where
336349
debugPrec as precOut =
337350
(parens (precOut >= 10) . Pretty.sep)
338351
["Data.Set.fromList", debug (Set.toList as)]
339352

353+
instance Debug a => Debug (HashSet a) where
354+
debugPrec as precOut =
355+
(parens (precOut >= 10) . Pretty.sep)
356+
["Data.HashSet.fromList", debug (HashSet.toList as)]
357+
340358
instance Debug a => Debug (Seq a) where
341359
debugPrec as precOut =
342360
(parens (precOut >= 10) . Pretty.sep)
@@ -592,6 +610,16 @@ instance
592610
wrapFromList diff' precOut =
593611
parens (precOut >= 10) $ "Data.Map.Strict.fromList" <+> diff' 10
594612

613+
instance
614+
( Debug key, Debug value, Diff key, Diff value )
615+
=> Diff (HashMap key value)
616+
where
617+
diffPrec as bs =
618+
fmap wrapFromList $ diffPrec (HashMap.toList as) (HashMap.toList bs)
619+
where
620+
wrapFromList diff' precOut =
621+
parens (precOut >= 10) $ "Data.HashMap.Strict.fromList" <+> diff' 10
622+
595623
instance (Debug a, Debug b, Diff a, Diff b) => Diff (a, b)
596624

597625
instance (Debug a, Diff a) => Diff (Set a) where
@@ -601,6 +629,13 @@ instance (Debug a, Diff a) => Diff (Set a) where
601629
wrapFromList diff' precOut =
602630
parens (precOut >= 10) $ "Data.Set.fromList" <+> diff' 10
603631

632+
instance (Debug a, Diff a) => Diff (HashSet a) where
633+
diffPrec as bs =
634+
fmap wrapFromList $ diffPrec (HashSet.toList as) (HashSet.toList bs)
635+
where
636+
wrapFromList diff' precOut =
637+
parens (precOut >= 10) $ "Data.HashSet.fromList" <+> diff' 10
638+
604639
instance Diff ExitCode
605640

606641
instance (Debug a, Debug b, Diff a, Diff b) => Diff (Either a b)

kore/src/Kore/Builtin/AssocComm/CeilSimplifier.hs

Lines changed: 114 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,11 @@ import Prelude.Kore
1717
import Control.Error
1818
( MaybeT
1919
)
20-
import qualified Control.Lens as Lens
21-
import Control.Monad
22-
( zipWithM
23-
)
2420
import Control.Monad.Reader
2521
( MonadReader
2622
)
2723
import qualified Control.Monad.Reader as Reader
28-
import qualified Data.List as List
24+
import qualified Data.Bifunctor as Bifunctor
2925
import qualified Data.Map.Strict as Map
3026

3127
import Kore.Attribute.Pattern.FreeVariables
@@ -176,6 +172,8 @@ generalizeMapElement freeVariables' element =
176172
newBuiltinAssocCommCeilSimplifier
177173
:: forall normalized simplifier
178174
. MonadReader (SideCondition RewritingVariableName) simplifier
175+
=> Ord (Element normalized (TermLike RewritingVariableName))
176+
=> Ord (Value normalized (TermLike RewritingVariableName ))
179177
=> MonadSimplify simplifier
180178
=> Traversable (Value normalized)
181179
=> AcWrapper normalized
@@ -188,116 +186,98 @@ newBuiltinAssocCommCeilSimplifier mkBuiltin mkNotMember =
188186
CeilSimplifier $ \Ceil { ceilChild } -> do
189187
let internalAc@InternalAc { builtinAcChild } = ceilChild
190188
sideCondition <- Reader.ask
191-
let normalizedAc
192-
:: NormalizedAc normalized Key (TermLike RewritingVariableName)
193-
normalizedAc = unwrapAc builtinAcChild
194-
NormalizedAc
195-
{ elementsWithVariables = abstractElements
196-
, concreteElements
197-
, opaque
198-
}
199-
= normalizedAc
200-
201-
let defineOpaquePair
202-
:: TermLike RewritingVariableName
203-
-> TermLike RewritingVariableName
204-
-> MultiAnd (OrCondition RewritingVariableName)
205-
defineOpaquePair opaque1 opaque2 =
206-
internalAc
207-
{ builtinAcChild =
208-
wrapAc
209-
emptyNormalizedAc { opaque = [opaque1, opaque2] }
210-
}
211-
& mkBuiltin
212-
& makeCeilPredicate
213-
-- TODO (thomas.tuegel): Do not mark this simplified.
214-
-- Marking here may prevent user-defined axioms from applying.
215-
-- At present, we wouldn't apply such an axiom, anyway.
216-
& Predicate.markSimplifiedMaybeConditional Nothing
217-
& OrCondition.fromPredicate
218-
& MultiAnd.singleton
219-
220-
defineOpaquePairs
221-
:: TermLike RewritingVariableName
222-
-> [TermLike RewritingVariableName]
223-
-> MultiAnd (OrCondition RewritingVariableName)
224-
defineOpaquePairs this others =
225-
foldMap (defineOpaquePair this) others
226-
227-
definedOpaquePairs :: MultiAnd (OrCondition RewritingVariableName)
228-
definedOpaquePairs =
229-
mconcat
230-
$ zipWith defineOpaquePairs opaque
231-
$ tail $ List.tails opaque
232-
233-
let abstractKeys, concreteKeys
234-
:: [TermLike RewritingVariableName]
235-
abstractValues, concreteValues, allValues
236-
:: [Value normalized (TermLike RewritingVariableName)]
237-
(abstractKeys, abstractValues) =
238-
unzip (unwrapElement <$> abstractElements)
239-
concreteKeys = from @Key <$> Map.keys concreteElements
240-
concreteValues = Map.elems concreteElements
241-
allValues = concreteValues <> abstractValues
242-
243-
let makeEvaluateTerm, defineAbstractKey, defineOpaque
244-
:: TermLike RewritingVariableName
245-
-> MaybeT simplifier (OrCondition RewritingVariableName)
246-
makeEvaluateTerm = makeEvaluateTermCeil sideCondition
247-
defineAbstractKey = makeEvaluateTerm
248-
defineOpaque = makeEvaluateTerm
249-
250-
defineValue
251-
:: Value normalized (TermLike RewritingVariableName)
252-
-> MaybeT
253-
simplifier
254-
(MultiAnd (OrCondition RewritingVariableName))
255-
defineValue = foldlM worker mempty
256-
where
257-
worker multiAnd termLike = do
258-
evaluated <- makeEvaluateTerm termLike
259-
return (multiAnd <> MultiAnd.singleton evaluated)
260-
261-
TermLike.assertConstructorLikeKeys concreteKeys $ return ()
262-
263-
-- concreteKeys are defined by assumption
264-
definedKeys <- traverse defineAbstractKey abstractKeys
265-
definedOpaque <- traverse defineOpaque opaque
266-
definedValues <- traverse defineValue allValues
267-
-- concreteKeys are distinct by assumption
268-
distinctConcreteKeys <-
269-
traverse (flip distinctKey concreteKeys) abstractKeys
270-
distinctAbstractKeys <-
271-
zipWithM distinctKey
272-
abstractKeys
273-
(tail $ List.tails abstractKeys)
274-
let conditions :: MultiAnd (OrCondition RewritingVariableName)
275-
conditions =
276-
mconcat
277-
[ MultiAnd.make definedKeys
278-
, MultiAnd.make definedOpaque
279-
, mconcat definedValues
280-
, mconcat distinctConcreteKeys
281-
, mconcat distinctAbstractKeys
282-
, foldMap (notMembers normalizedAc) opaque
283-
, definedOpaquePairs
284-
]
285-
189+
let symbolicKeys = getSymbolicKeysOfAc builtinAcChild
190+
symbolicValues = getSymbolicValuesOfAc builtinAcChild
191+
concreteValues = getConcreteValuesOfAc builtinAcChild
192+
opaqueElements = opaque . unwrapAc $ builtinAcChild
193+
definedKeysAndOpaque <-
194+
traverse
195+
(makeEvaluateTermCeil sideCondition)
196+
(symbolicKeys <> opaqueElements)
197+
& fmap MultiAnd.make
198+
definedValues <-
199+
traverse
200+
(defineValue sideCondition)
201+
(symbolicValues <> concreteValues)
202+
& fmap mconcat
203+
definedSubCollections <-
204+
definePairWiseElements mkBuiltin mkNotMember internalAc
205+
. generatePairWiseElements
206+
$ builtinAcChild
207+
let conditions =
208+
definedKeysAndOpaque
209+
<> definedValues
210+
<> definedSubCollections
286211
And.simplifyEvaluatedMultiPredicate sideCondition conditions
287212
where
213+
defineValue
214+
:: SideCondition RewritingVariableName
215+
-> Value normalized (TermLike RewritingVariableName)
216+
-> MaybeT
217+
simplifier
218+
(MultiAnd (OrCondition RewritingVariableName))
219+
defineValue sideCondition = foldlM worker mempty
220+
where
221+
worker multiAnd termLike = do
222+
evaluated <- makeEvaluateTermCeil sideCondition termLike
223+
return (multiAnd <> MultiAnd.singleton evaluated)
288224

289-
distinctKey
290-
:: TermLike RewritingVariableName
291-
-> [TermLike RewritingVariableName]
292-
-> MaybeT simplifier (MultiAnd (OrCondition RewritingVariableName))
293-
distinctKey thisKey otherKeys =
294-
MultiAnd.make <$> traverse (notEquals thisKey) otherKeys
225+
definePairWiseElements
226+
:: forall normalized simplifier
227+
. MonadSimplify simplifier
228+
=> MonadReader (SideCondition RewritingVariableName) simplifier
229+
=> AcWrapper normalized
230+
=> MkBuiltinAssocComm normalized RewritingVariableName
231+
-> MkNotMember normalized RewritingVariableName
232+
-> InternalAc Key normalized (TermLike RewritingVariableName)
233+
-> PairWiseElements normalized Key (TermLike RewritingVariableName)
234+
-> MaybeT simplifier (MultiAnd (OrCondition RewritingVariableName))
235+
definePairWiseElements mkBuiltin mkNotMember internalAc pairWiseElements = do
236+
definedKeyPairs <-
237+
traverse
238+
distinctKey
239+
(symbolicKeyPairs <> symbolicConcreteKeyPairs)
240+
& fmap MultiAnd.make
241+
let definedElementOpaquePairs =
242+
foldMap
243+
notMember
244+
(symbolicOpaquePairs <> concreteOpaquePairs')
245+
definedOpaquePairs =
246+
foldMap defineOpaquePair opaquePairs
247+
return . fold $
248+
[ definedKeyPairs
249+
, definedElementOpaquePairs
250+
, definedOpaquePairs
251+
]
252+
where
253+
PairWiseElements
254+
{ symbolicPairs
255+
, opaquePairs
256+
, symbolicConcretePairs
257+
, symbolicOpaquePairs
258+
, concreteOpaquePairs
259+
} = pairWiseElements
260+
symbolicKeyPairs =
261+
Bifunctor.bimap
262+
(fst . unwrapElement)
263+
(fst . unwrapElement)
264+
<$> symbolicPairs
265+
symbolicConcreteKeyPairs =
266+
Bifunctor.bimap
267+
(fst . unwrapElement)
268+
(from @Key @(TermLike _) . fst)
269+
<$> symbolicConcretePairs
270+
concreteOpaquePairs' =
271+
Bifunctor.first
272+
wrapConcreteElement
273+
<$> concreteOpaquePairs
295274

296-
notEquals
297-
:: TermLike RewritingVariableName
298-
-> TermLike RewritingVariableName
275+
distinctKey
276+
:: ( TermLike RewritingVariableName
277+
, TermLike RewritingVariableName
278+
)
299279
-> MaybeT simplifier (OrCondition RewritingVariableName)
300-
notEquals t1 t2 = do
280+
distinctKey (t1, t2) = do
301281
sideCondition <- Reader.ask
302282
Equals.makeEvaluateTermsToPredicate tMin tMax sideCondition
303283
>>= Not.simplifyEvaluatedPredicate
@@ -306,42 +286,39 @@ newBuiltinAssocCommCeilSimplifier mkBuiltin mkNotMember =
306286
(tMin, tMax) = minMax t1 t2
307287

308288
notMember
309-
:: TermLike RewritingVariableName
310-
-> Element normalized (TermLike RewritingVariableName)
289+
:: ( Element normalized (TermLike RewritingVariableName)
290+
, TermLike RewritingVariableName
291+
)
311292
-> MultiAnd (OrCondition RewritingVariableName)
312-
notMember termLike element =
293+
notMember (element, termLike) =
313294
mkNotMember element termLike
314295
& OrCondition.fromPredicate
315296
& MultiAnd.singleton
316297

317-
notMembers
318-
:: NormalizedAc normalized Key (TermLike RewritingVariableName)
319-
-> TermLike RewritingVariableName
298+
defineOpaquePair
299+
:: ( TermLike RewritingVariableName
300+
, TermLike RewritingVariableName
301+
)
320302
-> MultiAnd (OrCondition RewritingVariableName)
321-
notMembers normalizedAc termLike =
322-
Lens.foldMapOf foldElements (notMember termLike) normalizedAc
323-
324-
foldElements
325-
:: AcWrapper collection
326-
=> InternalVariable variable
327-
=> Lens.Fold
328-
(NormalizedAc collection Key (TermLike variable))
329-
(Element collection (TermLike variable))
330-
foldElements =
331-
Lens.folding $ \normalizedAc ->
332-
let
333-
concreteElements' =
334-
concreteElements normalizedAc
335-
& Map.toList
336-
& map wrapConcreteElement
337-
symbolicElements' = elementsWithVariables normalizedAc
338-
in
339-
concreteElements' <> symbolicElements'
303+
defineOpaquePair (opaque1, opaque2) =
304+
internalAc
305+
{ builtinAcChild =
306+
wrapAc
307+
emptyNormalizedAc { opaque = [opaque1, opaque2] }
308+
}
309+
& mkBuiltin
310+
& makeCeilPredicate
311+
-- TODO (thomas.tuegel): Do not mark this simplified.
312+
-- Marking here may prevent user-defined axioms from applying.
313+
-- At present, we wouldn't apply such an axiom, anyway.
314+
& Predicate.markSimplifiedMaybeConditional Nothing
315+
& OrCondition.fromPredicate
316+
& MultiAnd.singleton
340317

341318
fromElement
342319
:: AcWrapper normalized
343-
=> Element normalized (TermLike variable)
344-
-> NormalizedAc normalized Key (TermLike variable)
320+
=> Element normalized (TermLike RewritingVariableName)
321+
-> NormalizedAc normalized Key (TermLike RewritingVariableName)
345322
fromElement element
346323
| Just concreteKey <- retractKey symbolicKey
347324
= emptyNormalizedAc { concreteElements = Map.singleton concreteKey value }

kore/src/Kore/Debug.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
{-# OPTIONS_GHC -Wno-unused-top-binds #-}
2-
31
{- |
42
Copyright : (c) Runtime Verification, 2018
53
License : NCSA
@@ -41,6 +39,8 @@ It also works for test error messages:
4139
4240
Enjoy.
4341
-}
42+
{-# OPTIONS_GHC -Wno-unused-top-binds #-}
43+
{-# LANGUAGE Strict #-}
4444

4545
module Kore.Debug
4646
(

0 commit comments

Comments
 (0)