Skip to content

Simplify side conditions #2393

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

Merged
merged 25 commits into from
Mar 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
6279d8b
Add side condition simplifier
ana-pantilie Feb 8, 2021
e2712ad
Do not simplify conditions with themselves as the side condition
ana-pantilie Feb 8, 2021
019ece4
Move SideCondition simplification to Pattern simplifier
ana-pantilie Feb 10, 2021
10a3da1
Remove unused import
ana-pantilie Feb 10, 2021
827bc7e
Remove unused instance
ana-pantilie Feb 11, 2021
3772fc7
Use condition simplifier in side condition simplifier
ana-pantilie Feb 11, 2021
e8e9b03
Add integration test from frontend suite
ana-pantilie Feb 11, 2021
39391a3
Move side condition simplifier to pattern simplifier
ana-pantilie Feb 11, 2021
d270511
Add simple tests for simplifySideCondition
ana-pantilie Feb 11, 2021
4f6745b
Add export
ana-pantilie Feb 11, 2021
13b511a
New pattern condition is simplified side condition; add unit test
ana-pantilie Feb 12, 2021
0aa413a
Side condition simplifier simplifies Conditions
ana-pantilie Feb 16, 2021
4b59413
Move to Condition simplifier, mark final conjunction as simplified
ana-pantilie Feb 19, 2021
2baa22a
Fix unit tests
ana-pantilie Feb 22, 2021
d7a6535
Temp: some clean-up with debugging traces
ana-pantilie Feb 22, 2021
610243c
TermLike.simplify: assertion only checks for Fully simplified, ignore…
ana-pantilie Feb 23, 2021
a24c331
Integration test changes
ana-pantilie Feb 23, 2021
c577433
Some clean-up
ana-pantilie Feb 23, 2021
c662a65
Merge remote-tracking branch 'origin/master' into side-condition-simp…
ana-pantilie Feb 24, 2021
d07845f
General clean-up
ana-pantilie Feb 24, 2021
68062ca
Add simple unit tests
ana-pantilie Feb 24, 2021
70d955b
Add documentation
ana-pantilie Feb 24, 2021
ed69643
Update kore/src/Kore/Attribute/Pattern/Simplified.hs
ana-pantilie Mar 2, 2021
fe45338
Review: rename isSimplified... functions
ana-pantilie Mar 2, 2021
494bb21
Merge branch 'master' into side-condition-simplification
ana-pantilie Mar 2, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions kore/src/Kore/Attribute/Pattern.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ module Kore.Attribute.Pattern
, mapVariables
, traverseVariables
, deleteFreeVariable
, isFullySimplified
, isSimplified
, isSimplifiedAnyCondition
, isSimplifiedSomeCondition
, setSimplified
, simplifiedAttribute
, constructorLikeAttribute
Expand Down Expand Up @@ -63,12 +64,14 @@ import qualified Kore.Attribute.Pattern.FreeVariables as FreeVariables
import Kore.Attribute.Pattern.Function
import Kore.Attribute.Pattern.Functional
import Kore.Attribute.Pattern.Simplified hiding
( isFullySimplified
, isSimplified
( isSimplified
, isSimplifiedAnyCondition
, isSimplifiedSomeCondition
)
import qualified Kore.Attribute.Pattern.Simplified as Simplified
( isFullySimplified
, isSimplified
( isSimplified
, isSimplifiedAnyCondition
, isSimplifiedSomeCondition
)
import Kore.Attribute.Synthetic
import Kore.Debug
Expand Down Expand Up @@ -156,17 +159,26 @@ isSimplified sideCondition patt@Pattern {simplified} =
assertSimplifiedConsistency patt
$ Simplified.isSimplified sideCondition simplified

{- Checks whether the pattern is simplified relative to some side condition.
-}
isSimplifiedSomeCondition
:: HasCallStack
=> Pattern variable -> Bool
isSimplifiedSomeCondition patt@Pattern {simplified} =
assertSimplifiedConsistency patt
$ Simplified.isSimplifiedSomeCondition simplified

{- Checks whether the pattern is simplified relative to any side condition.
-}
isFullySimplified :: HasCallStack => Pattern variable -> Bool
isFullySimplified patt@Pattern {simplified} =
isSimplifiedAnyCondition :: HasCallStack => Pattern variable -> Bool
isSimplifiedAnyCondition patt@Pattern {simplified} =
assertSimplifiedConsistency patt
$ Simplified.isFullySimplified simplified
$ Simplified.isSimplifiedAnyCondition simplified

assertSimplifiedConsistency :: HasCallStack => Pattern variable -> a -> a
assertSimplifiedConsistency Pattern {constructorLike, simplified}
| isConstructorLike constructorLike
, not (Simplified.isFullySimplified simplified) =
, not (Simplified.isSimplifiedAnyCondition simplified) =
error "Inconsistent attributes, constructorLike implies fully simplified."
| otherwise = id

Expand Down
34 changes: 27 additions & 7 deletions kore/src/Kore/Attribute/Pattern/Simplified.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ module Kore.Attribute.Pattern.Simplified
, pattern Simplified_
, Type (..)
, isSimplified
, isFullySimplified
, isSimplifiedAnyCondition
, isSimplifiedSomeCondition
, simplifiedTo
, notSimplified
, fullySimplified
Expand Down Expand Up @@ -225,6 +226,11 @@ Simplified_ _ Any `simplifiedTo` s@(Simplified_ Fully Any) = s

s1@(Simplified_ _ _) `simplifiedTo` s2@(Simplified_ Partly _) = s1 <> s2

{- | Is the pattern fully simplified under the given side condition?

See also: 'isSimplifiedAnyCondition', 'isSimplifiedSomeCondition'.

-}
isSimplified :: SideCondition.Representation -> Simplified -> Bool
isSimplified _ (Simplified_ Fully Any) = True
isSimplified currentCondition (Simplified_ Fully (Condition condition)) =
Expand All @@ -233,12 +239,26 @@ isSimplified _ (Simplified_ Fully Unknown) = False
isSimplified _ (Simplified_ Partly _) = False
isSimplified _ NotSimplified = False

isFullySimplified :: Simplified -> Bool
isFullySimplified (Simplified_ Fully Any) = True
isFullySimplified (Simplified_ Fully (Condition _)) = False
isFullySimplified (Simplified_ Fully Unknown) = False
isFullySimplified (Simplified_ Partly _) = False
isFullySimplified NotSimplified = False
{- | Is the pattern fully simplified under any side condition?

See also: 'isSimplified', 'isSimplifiedSomeCondition'.

-}
isSimplifiedAnyCondition :: Simplified -> Bool
isSimplifiedAnyCondition (Simplified_ Fully Any) = True
isSimplifiedAnyCondition (Simplified_ Fully (Condition _)) = False
isSimplifiedAnyCondition (Simplified_ Fully Unknown) = False
isSimplifiedAnyCondition (Simplified_ Partly _) = False
isSimplifiedAnyCondition NotSimplified = False

{- | Is the pattern fully simplified under some side condition?

See also: 'isSimplified', 'isSimplifiedAnyCondition'.

-}
isSimplifiedSomeCondition :: Simplified -> Bool
isSimplifiedSomeCondition (Simplified_ Fully _) = True
isSimplifiedSomeCondition _ = False

fullySimplified :: Simplified
fullySimplified = Simplified_ Fully Any
Expand Down
30 changes: 22 additions & 8 deletions kore/src/Kore/Attribute/PredicatePattern.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ module Kore.Attribute.PredicatePattern
-- simplified is excluded on purpose
, simplifiedAttribute
, isSimplified
, isFullySimplified
, isSimplifiedAnyCondition
, isSimplifiedSomeCondition
, setSimplified
, mapVariables
, traverseVariables
Expand Down Expand Up @@ -39,12 +40,14 @@ import qualified Kore.Attribute.Pattern.FreeVariables as FreeVariables
( freeVariables
)
import Kore.Attribute.Pattern.Simplified hiding
( isFullySimplified
, isSimplified
( isSimplified
, isSimplifiedAnyCondition
, isSimplifiedSomeCondition
)
import qualified Kore.Attribute.Pattern.Simplified as Simplified
( isFullySimplified
, isSimplified
( isSimplified
, isSimplifiedAnyCondition
, isSimplifiedSomeCondition
)
import Kore.Attribute.Synthetic
import Kore.Debug
Expand Down Expand Up @@ -97,12 +100,23 @@ isSimplified
:: SideCondition.Representation -> PredicatePattern variable -> Bool
isSimplified sideCondition = Simplified.isSimplified sideCondition . simplifiedAttribute

{- Checks whether the pattern is simplified relative to some side condition.
-}
isSimplifiedSomeCondition
:: PredicatePattern variable -> Bool
isSimplifiedSomeCondition =
Simplified.isSimplifiedSomeCondition . simplifiedAttribute

{- Checks whether the pattern is simplified relative to any side condition.
-}
isFullySimplified :: PredicatePattern variable -> Bool
isFullySimplified PredicatePattern {simplified} = Simplified.isFullySimplified simplified
isSimplifiedAnyCondition :: PredicatePattern variable -> Bool
isSimplifiedAnyCondition PredicatePattern {simplified} =
Simplified.isSimplifiedAnyCondition simplified

setSimplified :: Simplified -> PredicatePattern variable -> PredicatePattern variable
setSimplified
:: Simplified
-> PredicatePattern variable
-> PredicatePattern variable
setSimplified simplified patt = patt { simplified }

{- | Use the provided mapping to replace all variables in a 'Pattern'.
Expand Down
5 changes: 1 addition & 4 deletions kore/src/Kore/Equation/Simplification.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ import Kore.Internal.Pattern
)
import qualified Kore.Internal.Pattern as Pattern
import qualified Kore.Internal.Predicate as Predicate
import qualified Kore.Internal.SideCondition as SideCondition
( top
)
import qualified Kore.Internal.Substitution as Substitution
import qualified Kore.Internal.TermLike as TermLike
import qualified Kore.Step.Simplification.Pattern as Pattern
Expand Down Expand Up @@ -134,4 +131,4 @@ simplifyPattern
-> simplifier (OrPattern variable)
simplifyPattern patt =
Simplifier.localSimplifierAxioms (const mempty)
$ Pattern.simplify SideCondition.top patt
$ Pattern.simplify patt
11 changes: 3 additions & 8 deletions kore/src/Kore/Exec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ import Kore.Internal.Predicate
, makeMultipleOrPredicate
)
import qualified Kore.Internal.SideCondition as SideCondition
( top
, topTODO
)
import Kore.Internal.TermLike
import Kore.Log.ErrorRewriteLoop
( errorRewriteLoop
Expand Down Expand Up @@ -224,8 +221,7 @@ exec depthLimit breadthLimit verifiedModule strategy initialTerm =
finals <-
getFinalConfigsOf $ do
initialConfig <-
Pattern.simplify SideCondition.top
(Pattern.fromTermLike initialTerm)
Pattern.simplify (Pattern.fromTermLike initialTerm)
>>= Logic.scatter
let
updateQueue = \as ->
Expand Down Expand Up @@ -372,8 +368,7 @@ search depthLimit breadthLimit verifiedModule termLike searchPattern searchConfi
initialized <- initializeAndSimplify verifiedModule
let Initialized { rewriteRules } = initialized
simplifiedPatterns <-
Pattern.simplify SideCondition.top
$ Pattern.fromTermLike termLike
Pattern.simplify $ Pattern.fromTermLike termLike
let
initialPattern =
case toList simplifiedPatterns of
Expand Down Expand Up @@ -401,7 +396,7 @@ search depthLimit breadthLimit verifiedModule termLike searchPattern searchConfi
searchGraph
searchConfig
(match
SideCondition.topTODO
SideCondition.top
(mkRewritingPattern searchPattern)
)
executionGraph
Expand Down
13 changes: 13 additions & 0 deletions kore/src/Kore/Internal/OrPattern.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Kore.Internal.OrPattern
, coerceSort
, isSimplified
, hasSimplifiedChildren
, hasSimplifiedChildrenIgnoreConditions
, forgetSimplified
, fromPatterns
, toPatterns
Expand Down Expand Up @@ -95,6 +96,18 @@ hasSimplifiedChildren
hasSimplifiedChildren sideCondition =
all (Pattern.hasSimplifiedChildren sideCondition)

{- | Checks whether all patterns in the disjunction have simplified children,
ignoring the conditions used to simplify them.

See also: 'Pattern.hasSimplifiedChildrenIgnoreConditions'

-}
hasSimplifiedChildrenIgnoreConditions
:: InternalVariable variable
=> OrPattern variable -> Bool
hasSimplifiedChildrenIgnoreConditions =
all Pattern.hasSimplifiedChildrenIgnoreConditions

forgetSimplified
:: InternalVariable variable => OrPattern variable -> OrPattern variable
forgetSimplified = fromPatterns . map Pattern.forgetSimplified . toPatterns
Expand Down
15 changes: 15 additions & 0 deletions kore/src/Kore/Internal/Pattern.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module Kore.Internal.Pattern
, Kore.Internal.Pattern.freeElementVariables
, isSimplified
, hasSimplifiedChildren
, hasSimplifiedChildrenIgnoreConditions
, forgetSimplified
, markSimplified
, simplifiedAttribute
Expand Down Expand Up @@ -162,6 +163,20 @@ hasSimplifiedChildren sideCondition patt =
Conditional { term, predicate, substitution } = patt
clauses = MultiAnd.fromPredicate predicate

{- | Similar to 'hasSimplifiedChildren', only that it ignores the conditions
used to simplify the children.
-}
hasSimplifiedChildrenIgnoreConditions
:: Ord variable
=> Pattern variable -> Bool
hasSimplifiedChildrenIgnoreConditions patt =
TermLike.isSimplifiedSomeCondition term
&& all Predicate.isSimplifiedSomeCondition clauses
&& Substitution.isSimplifiedSomeCondition substitution
where
Conditional { term, predicate, substitution } = patt
clauses = MultiAnd.fromPredicate predicate

forgetSimplified
:: InternalVariable variable => Pattern variable -> Pattern variable
forgetSimplified patt =
Expand Down
16 changes: 16 additions & 0 deletions kore/src/Kore/Internal/Predicate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module Kore.Internal.Predicate
, isPredicate
, simplifiedAttribute
, isSimplified
, isSimplifiedSomeCondition
, isFreeOf
, freeElementVariables
, hasFreeVariable
Expand Down Expand Up @@ -142,6 +143,7 @@ import Kore.Internal.TermLike hiding
, forgetSimplified
, hasFreeVariable
, isSimplified
, isSimplifiedSomeCondition
, mapVariables
, markSimplified
, markSimplifiedConditional
Expand Down Expand Up @@ -911,9 +913,23 @@ instance HasFreeVariables (Predicate variable) variable where
simplifiedAttribute :: Predicate variable -> Simplified
simplifiedAttribute = Attribute.simplifiedAttribute . extractAttributes

{- | Is the 'Predicate' fully simplified under the given side condition?

See also: 'isSimplifiedSomeCondition'.

-}
isSimplified :: SideCondition.Representation -> Predicate variable -> Bool
isSimplified condition = Attribute.isSimplified condition . extractAttributes

{- | Is the 'Predicate' fully simplified under some side condition?

See also: 'isSimplified'.

-}
isSimplifiedSomeCondition :: Predicate variable -> Bool
isSimplifiedSomeCondition =
Attribute.isSimplifiedSomeCondition . extractAttributes

cannotSimplifyNotSimplifiedError
:: (HasCallStack, InternalVariable variable)
=> PredicateF variable (Predicate variable) -> a
Expand Down
11 changes: 9 additions & 2 deletions kore/src/Kore/Internal/SideCondition/SideCondition.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ module Kore.Internal.SideCondition.SideCondition
) where

import Prelude.Kore
import Pretty
( Pretty (..)
)

import Data.Hashable
( Hashed
, hashed
, unhashed
)

import Data.Type.Equality
Expand All @@ -30,7 +34,7 @@ import Type.Reflection
)

data Representation where
Representation :: Ord a => !(TypeRep a) -> !(Hashed a) -> Representation
Representation :: (Ord a, Pretty a) => !(TypeRep a) -> !(Hashed a) -> Representation

instance Eq Representation where
(==) (Representation typeRep1 hashed1) (Representation typeRep2 hashed2) =
Expand Down Expand Up @@ -64,9 +68,12 @@ instance NFData Representation where
rnf (Representation typeRep1 hashed1) = typeRep1 `seq` hashed1 `seq` ()
{-# INLINE rnf #-}

instance Pretty Representation where
pretty (Representation _ h) = pretty (unhashed h)

-- | Creates a 'Representation'. Should not be used directly.
-- See 'Kore.Internal.SideCondition.toRepresentation'.
mkRepresentation :: (Ord a, Hashable a, Typeable a) => a -> Representation
mkRepresentation :: (Ord a, Hashable a, Typeable a, Pretty a) => a -> Representation
mkRepresentation = Representation typeRep . hashed

instance Debug Representation where
Expand Down
Loading