Skip to content

Commit 19f7475

Browse files
Remove the Target type from Kore.Equation (#2344)
Co-authored-by: Thomas Tuegel <[email protected]> Co-authored-by: Thomas Tuegel <[email protected]>
1 parent b996531 commit 19f7475

File tree

7 files changed

+238
-71
lines changed

7 files changed

+238
-71
lines changed

kore/src/Kore/Equation/Application.hs

Lines changed: 41 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ License : NCSA
44
55
-}
66

7+
{-# LANGUAGE Strict #-}
78
module Kore.Equation.Application
89
( attemptEquation
910
, AttemptEquationResult
@@ -113,10 +114,6 @@ import Kore.TopBottom
113114
import Kore.Unparser
114115
( Unparse (..)
115116
)
116-
import Kore.Variables.Target
117-
( Target
118-
)
119-
import qualified Kore.Variables.Target as Target
120117
import Log
121118
( Entry (..)
122119
, MonadLog
@@ -155,7 +152,7 @@ attemptEquation
155152
=> MonadSimplify simplifier
156153
=> InternalVariable variable
157154
=> SideCondition variable
158-
-> TermLike (Target variable)
155+
-> TermLike variable
159156
-> Equation variable
160157
-> simplifier (AttemptEquationResult variable)
161158
attemptEquation sideCondition termLike equation =
@@ -179,20 +176,16 @@ attemptEquation sideCondition termLike equation =
179176
& whileMatch
180177
(equation', predicate) <-
181178
applyAndSelectMatchResult matchResults
182-
let matchPredicate' =
183-
Predicate.mapVariables
184-
(pure Target.unTarget)
185-
matchPredicate
186179
return
187180
( equation'
188-
, makeAndPredicate predicate matchPredicate'
181+
, makeAndPredicate predicate matchPredicate
189182
)
190183
let Equation { requires } = equation'
191184
checkRequires sideCondition predicate requires & whileCheckRequires
192185
let Equation { right, ensures } = equation'
193186
return $ Pattern.withCondition right $ from @(Predicate _) ensures
194187
where
195-
equationRenamed = targetEquationVariables sideCondition termLike equation
188+
equationRenamed = refreshVariables sideCondition termLike equation
196189
matchError =
197190
MatchError
198191
{ matchTerm = termLike
@@ -203,7 +196,7 @@ attemptEquation sideCondition termLike equation =
203196
& MaybeT & noteT matchError
204197

205198
applyAndSelectMatchResult
206-
:: [MatchResult (Target variable)]
199+
:: [MatchResult variable]
207200
-> ExceptT
208201
(AttemptEquationError variable)
209202
simplifier
@@ -234,13 +227,13 @@ applySubstitutionAndSimplify
234227
:: HasCallStack
235228
=> MonadSimplify simplifier
236229
=> InternalVariable variable
237-
=> Predicate (Target variable)
238-
-> Maybe (Predicate (Target variable))
239-
-> Map (SomeVariableName (Target variable)) (TermLike (Target variable))
230+
=> Predicate variable
231+
-> Maybe (Predicate variable)
232+
-> Map (SomeVariableName variable) (TermLike variable)
240233
-> ExceptT
241-
(MatchError (Target variable))
234+
(MatchError variable)
242235
simplifier
243-
[MatchResult (Target variable)]
236+
[MatchResult variable]
244237
applySubstitutionAndSimplify
245238
argument
246239
antiLeft
@@ -284,9 +277,9 @@ applyMatchResult
284277
:: forall monad variable
285278
. Monad monad
286279
=> InternalVariable variable
287-
=> Equation (Target variable)
288-
-> MatchResult (Target variable)
289-
-> ExceptT (ApplyMatchResultErrors (Target variable)) monad
280+
=> Equation variable
281+
-> MatchResult variable
282+
-> ExceptT (ApplyMatchResultErrors variable) monad
290283
(Equation variable, Predicate variable)
291284
applyMatchResult equation matchResult@(predicate, substitution) = do
292285
case errors of
@@ -297,21 +290,28 @@ applyMatchResult equation matchResult@(predicate, substitution) = do
297290
}
298291
_ -> return ()
299292
let predicate' =
300-
Predicate.substitute substitution predicate
301-
& Predicate.mapVariables (pure Target.unTarget)
293+
Predicate.substitute orientedSubstitution predicate
302294
equation' =
303-
Equation.substitute substitution equation
304-
& Equation.mapVariables (pure Target.unTarget)
295+
Equation.substitute orientedSubstitution equation
305296
return (equation', predicate')
306297
where
307-
equationVariables = freeVariables equation & FreeVariables.toList
298+
orientedSubstitution = Substitution.orientSubstitution occursInEquation substitution
299+
300+
equationVariables = freeVariables equation
301+
302+
occursInEquation :: (SomeVariableName variable -> Bool)
303+
occursInEquation = \someVariableName ->
304+
Set.member someVariableName equationVariableNames
305+
306+
equationVariableNames =
307+
Set.map variableName (FreeVariables.toSet equationVariables)
308308

309309
errors =
310-
concatMap checkVariable equationVariables
311-
<> checkNonTargetVariables
310+
concatMap checkVariable (FreeVariables.toList equationVariables)
311+
<> checkNotInEquation
312312

313313
checkVariable Variable { variableName } =
314-
case Map.lookup variableName substitution of
314+
case Map.lookup variableName orientedSubstitution of
315315
Nothing -> [NotMatched variableName]
316316
Just termLike ->
317317
checkConcreteVariable variableName termLike
@@ -331,9 +331,9 @@ applyMatchResult equation matchResult@(predicate, substitution) = do
331331
| otherwise
332332
= empty
333333

334-
checkNonTargetVariables =
334+
checkNotInEquation =
335335
NonMatchingSubstitution
336-
<$> filter Target.isSomeNonTargetName (Map.keys substitution)
336+
<$> filter (not . occursInEquation) (Map.keys orientedSubstitution)
337337

338338
Equation { attributes } = equation
339339
concretes =
@@ -400,37 +400,27 @@ checkRequires sideCondition predicate requires =
400400
. Simplifier.localSimplifierAxioms (const mempty)
401401
withAxioms = id
402402

403-
{- | Make the 'Equation' variables distinct from the initial pattern.
404-
405-
The variables are marked 'Target' and renamed to avoid any variables in the
406-
'SideCondition' or the 'TermLike'.
407-
408-
-}
409-
targetEquationVariables
403+
refreshVariables
410404
:: forall variable
411405
. InternalVariable variable
412406
=> SideCondition variable
413-
-> TermLike (Target variable)
407+
-> TermLike variable
414408
-> Equation variable
415-
-> Equation (Target variable)
416-
targetEquationVariables sideCondition initial =
409+
-> Equation variable
410+
refreshVariables sideCondition initial =
417411
snd
418412
. Equation.refreshVariables avoiding
419-
. Equation.mapVariables Target.mkUnifiedTarget
420413
where
421414
avoiding = sideConditionVariables <> freeVariables initial
422-
sideConditionVariables =
423-
FreeVariables.mapFreeVariables
424-
Target.mkUnifiedNonTarget
425-
$ freeVariables sideCondition
415+
sideConditionVariables = freeVariables sideCondition
426416

427417
-- * Errors
428418

429419
{- | Errors that can occur during 'attemptEquation'.
430420
-}
431421
data AttemptEquationError variable
432-
= WhileMatch !(MatchError (Target variable))
433-
| WhileApplyMatchResult !(ApplyMatchResultErrors (Target variable))
422+
= WhileMatch !(MatchError variable)
423+
| WhileApplyMatchResult !(ApplyMatchResultErrors variable)
434424
| WhileCheckRequires !(CheckRequiresError variable)
435425
deriving (Eq, Ord, Show)
436426
deriving (GHC.Generic)
@@ -445,27 +435,25 @@ mapAttemptEquationErrorVariables
445435
mapAttemptEquationErrorVariables adj =
446436
\case
447437
WhileMatch matchError ->
448-
WhileMatch $ mapMatchErrorVariables adjTarget matchError
438+
WhileMatch $ mapMatchErrorVariables adj matchError
449439
WhileApplyMatchResult applyMatchResultErrors ->
450440
WhileApplyMatchResult
451441
$ mapApplyMatchResultErrorsVariables
452-
adjTarget
442+
adj
453443
applyMatchResultErrors
454444
WhileCheckRequires checkRequiresError ->
455445
WhileCheckRequires
456446
$ mapCheckRequiresErrorVariables adj checkRequiresError
457-
where
458-
adjTarget = fmap <$> adj
459447

460448
whileMatch
461449
:: Functor monad
462-
=> ExceptT (MatchError (Target variable)) monad a
450+
=> ExceptT (MatchError variable) monad a
463451
-> ExceptT (AttemptEquationError variable) monad a
464452
whileMatch = withExceptT WhileMatch
465453

466454
whileApplyMatchResult
467455
:: Functor monad
468-
=> ExceptT (ApplyMatchResultErrors (Target variable)) monad a
456+
=> ExceptT (ApplyMatchResultErrors variable) monad a
469457
-> ExceptT (AttemptEquationError variable) monad a
470458
whileApplyMatchResult = withExceptT WhileApplyMatchResult
471459

kore/src/Kore/Internal/Substitution.hs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ module Kore.Internal.Substitution
4747
, mkNormalization
4848
, applyNormalized
4949
, substitute
50+
, orientSubstitution
5051
) where
5152

5253
import Prelude.Kore hiding
@@ -648,6 +649,106 @@ variables (NormalizedSubstitution subst) = Map.keysSet subst
648649
variables (Substitution subst) =
649650
foldMap (Set.singleton . assignedVariable) subst
650651

652+
{- | Apply an orientation to all variable-renaming substitutions.
653+
654+
A variable-renaming substitution is a pair of the form
655+
656+
@
657+
X:S = Y:S
658+
@
659+
660+
In a normalized substitution, the variable on the left-hand side of each
661+
substitution pair may not appear in any other substitution pair. The order of a
662+
variable-renaming pair is logically irrelevant, but often we have a preference
663+
for which of @X@ and @Y@ should appear on the left-hand side (that is, we have a
664+
preferred /orientation/).
665+
666+
@orientSubstitution@ applies an orientation to a normalized substitution and
667+
yields a normalized substitution. The orientation is expressed as a function
668+
669+
@
670+
SomeVariableName variable -> Bool
671+
@
672+
673+
returning `True` when the named variable is preferred on the left-hand side of a
674+
variable-renaming substitution pair. Each variable-renaming pair is oriented so
675+
that the variable on the left-hand side is a preferred variable, if possible.
676+
@orientSubstitution@ does not alter substitution pairs where both or neither
677+
variable is preferred for the left-hand side.
678+
-}
679+
orientSubstitution
680+
:: forall variable
681+
. InternalVariable variable
682+
=> (SomeVariableName variable -> Bool)
683+
-- ^ Orientation: Is the named variable preferred on the left-hand side of
684+
-- variable-renaming substitution pairs?
685+
-> Map (SomeVariableName variable) (TermLike variable)
686+
-- ^ Normalized substitution
687+
-> Map (SomeVariableName variable) (TermLike variable)
688+
orientSubstitution toLeft substitution =
689+
foldl' go substitution $ Map.toList substitution
690+
where
691+
go substitutionInProgress initialPair@(initialKey, _)
692+
| Just (newKey, newValue) <- retractReorderedPair initialPair =
693+
-- Re-orienting X = Y as Y = X.
694+
let newPair = Map.singleton newKey newValue
695+
in case Map.lookup newKey substitutionInProgress of
696+
Nothing ->
697+
-- There is no other Y = X substitution in the map.
698+
substitutionInProgress
699+
-- Remove X = Y pair.
700+
& Map.delete initialKey
701+
-- Apply Y = X to the right-hand side of all pairs.
702+
& Map.map (TermLike.substitute newPair)
703+
-- Insert Y = X pair.
704+
& Map.insert newKey newValue
705+
Just already ->
706+
-- There is a substitution Y = T in the map.
707+
substitutionInProgress
708+
-- Remove Y = T.
709+
& Map.delete newKey
710+
-- Apply Y = X to the right-hand side of all pairs.
711+
& Map.map (TermLike.substitute newPair)
712+
-- Insert Y = X pair.
713+
& Map.insert newKey newValue
714+
-- Apply X = T to the right-hand side of all pairs. This
715+
-- substitution never needs to be reoriented, but the reason why
716+
-- is subtle:
717+
-- 1. The Y = T substitution came from another swapped pair. It
718+
-- was not present in the original substitution: the original
719+
-- substitution was normalized, and the substitution we are
720+
-- reorienting now had Y on the right-hand side, so Y was not
721+
-- on the left-hand side of any pair in the original
722+
-- subsitution.
723+
-- 2. If Y = T came from another swapped pair, then T is not a
724+
-- preferred variable.
725+
-- 3. We just checked that X is not a preferred variable.
726+
-- 4. Therefore, X = T is a valid orientation.
727+
& Map.map
728+
(TermLike.substitute (Map.singleton initialKey already))
729+
-- Insert X = T pair.
730+
& Map.insert initialKey already
731+
| otherwise = substitutionInProgress
732+
733+
retractReorderedPair
734+
:: (SomeVariableName variable, TermLike variable)
735+
-> Maybe (SomeVariableName variable, TermLike variable)
736+
retractReorderedPair (xName, TermLike.Var_ (Variable yName ySort))
737+
| isSameMultiplicity xName yName
738+
, toLeft yName
739+
, not (toLeft xName)
740+
= Just (yName, TermLike.mkVar (Variable xName ySort))
741+
retractReorderedPair _ = Nothing
742+
743+
isSameMultiplicity
744+
:: SomeVariableName variable
745+
-> SomeVariableName variable
746+
-> Bool
747+
isSameMultiplicity x y
748+
| SomeVariableNameElement _ <- x, SomeVariableNameElement _ <- y = True
749+
| SomeVariableNameSet _ <- x, SomeVariableNameSet _ <- y = True
750+
| otherwise = False
751+
651752
{- | The result of /normalizing/ a substitution.
652753
653754
'normalized' holds the part of the substitution was normalized successfully.

kore/src/Kore/Step/Axiom/EvaluationStrategy.hs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Maintainer : [email protected]
77
Stability : experimental
88
Portability : portable
99
-}
10+
11+
{-# LANGUAGE Strict #-}
1012
module Kore.Step.Axiom.EvaluationStrategy
1113
( builtinEvaluation
1214
, definitionEvaluation
@@ -119,7 +121,10 @@ attemptEquationAndAccumulateErrors condition term equation =
119121
where
120122
attemptEquation =
121123
ExceptRT . ExceptT
122-
$ Equation.attemptEquation condition term equation
124+
$ Equation.attemptEquation
125+
condition
126+
(TermLike.mapVariables (pure Target.unTarget) term)
127+
equation
123128
>>= either (return . Left . Option . Just . Min) (fmap Right . apply)
124129
apply = Equation.applyEquation condition equation
125130

@@ -143,8 +148,11 @@ simplificationEvaluation
143148
simplificationEvaluation equation =
144149
BuiltinAndAxiomSimplifier $ \term condition -> do
145150
let equation' = Equation.mapVariables (pure fromVariableName) equation
146-
term' = TermLike.mapVariables Target.mkUnifiedNonTarget term
147-
result <- Equation.attemptEquation condition term' equation'
151+
result <-
152+
Equation.attemptEquation
153+
condition
154+
term
155+
equation'
148156
let apply = Equation.applyEquation condition equation'
149157
case result of
150158
Right applied -> do

kore/src/Kore/Step/Simplification/SubstitutionSimplifier.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ License : NCSA
44
55
-}
66

7+
{-# LANGUAGE Strict #-}
78
module Kore.Step.Simplification.SubstitutionSimplifier
89
( SubstitutionSimplifier (..)
910
, substitutionSimplifier
@@ -376,7 +377,6 @@ simplifySubstitutionWorker sideCondition makeAnd' = \substitution -> do
376377
return substitution'
377378

378379
sideConditionRepresentation = SideCondition.toRepresentation sideCondition
379-
380380
data Private variable =
381381
Private
382382
{ accum :: !(Condition variable)

0 commit comments

Comments
 (0)