Skip to content

Commit

Permalink
Reuse substitution internalisation code
Browse files Browse the repository at this point in the history
Handle with care
  • Loading branch information
geo2a committed Oct 18, 2024
1 parent b1793b8 commit 4e34924
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
3 changes: 2 additions & 1 deletion booster/library/Booster/Pattern/ApplyEquations.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import Booster.Pattern.Util
import Booster.Prettyprinter (renderOneLineText)
import Booster.SMT.Interface qualified as SMT
import Booster.Syntax.Json.Externalise (externaliseTerm)
import Booster.Syntax.Json.Internalise (extractSubsitution)
import Booster.Util (Bound (..))
import Kore.JsonRpc.Types.ContextLog (CLContext (CLWithId), IdContext (CtxCached))
import Kore.Util (showHashHex)
Expand Down Expand Up @@ -483,7 +484,7 @@ evaluatePattern' pat@Pattern{term, ceilConditions} = withPatternContext pat $ do
-- evaluatedConstraints. To avoid duplicating constraints (i.e. having equivalent entities
-- in pat.predicate and pat.substitution), we discard the old substitution here
-- and extract a possible simplified one from evaluatedConstraints.
let (simplifiedSubsitution, simplifiedConstraints) = partitionPredicates (Set.toList evaluatedConstraints)
let (simplifiedSubsitution, simplifiedConstraints) = extractSubsitution (Set.toList evaluatedConstraints)

pure
Pattern
Expand Down
5 changes: 3 additions & 2 deletions booster/library/Booster/Pattern/Rewrite.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import Booster.Pattern.Util
import Booster.Prettyprinter
import Booster.SMT.Interface qualified as SMT
import Booster.Syntax.Json.Externalise (externaliseTerm)
import Booster.Syntax.Json.Internalise (extractSubsitution)
import Booster.Util (Flag (..))

newtype RewriteT io a = RewriteT
Expand Down Expand Up @@ -367,7 +368,7 @@ applyRule pat@Pattern{ceilConditions} rule =
let (newSubsitution, newConstraints) = partitionPredicates ensuredConditions

-- compose the existing substitution pattern and the newly acquired one
let modifiedPatternSubst = newSubsitution `compose` pat.substitution
let (modifiedPatternSubst, leftoverConstraints) = extractSubsitution . asEquations $ newSubsitution `compose` pat.substitution

let rewrittenTerm = substituteInTerm (modifiedPatternSubst `compose` ruleSubstitution) rule.rhs
substitutedNewConstraints =
Expand All @@ -379,7 +380,7 @@ applyRule pat@Pattern{ceilConditions} rule =
Pattern
rewrittenTerm
-- adding new constraints that have not been trivially `Top`, substituting the Ex# variables
(pat.constraints <> substitutedNewConstraints)
(pat.constraints <> substitutedNewConstraints <> Set.fromList leftoverConstraints)
modifiedPatternSubst -- ruleSubstitution is not needed, do not attach it to the result
ceilConditions
withContext CtxSuccess $
Expand Down
3 changes: 2 additions & 1 deletion booster/library/Booster/Syntax/Json/Externalise.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Data.Text.Encoding qualified as Text
import Booster.Pattern.Base (externaliseKmapUnsafe)
import Booster.Pattern.Base qualified as Internal
import Booster.Pattern.Bool qualified as Internal
import Booster.Pattern.Substitution qualified as Substitution
import Booster.Pattern.Util (sortOfTerm)
import Data.Map (Map)
import Data.Map qualified as Map
Expand All @@ -35,7 +36,7 @@ externalisePattern ::
externalisePattern Internal.Pattern{term = term, constraints, ceilConditions, substitution = ensuredSubstitution} inputSubstitution =
-- need a sort for the predicates in external format
let sort = externaliseSort $ sortOfTerm term
substitutions = ensuredSubstitution <> inputSubstitution -- TODO ensuredSubstitution takes priority. Do we even need inputSubstitution?
substitutions = inputSubstitution <> (ensuredSubstitution `Substitution.compose` inputSubstitution)
externalisedSubstitution =
if null substitutions
then Nothing
Expand Down
6 changes: 6 additions & 0 deletions booster/library/Booster/Syntax/Json/Internalise.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ module Booster.Syntax.Json.Internalise (
pattern CheckSubsorts,
pattern IgnoreSubsorts,
logPatternError,
-- substitution mining
extractSubsitution,
-- for test only
InternalisedPredicate (..),
) where
Expand Down Expand Up @@ -508,6 +510,10 @@ mbSubstitution = \case
where
boolPred = BoolPred . Internal.Predicate

extractSubsitution ::
[Internal.Predicate] -> (Map Internal.Variable Internal.Term, [Internal.Predicate])
extractSubsitution = mkSubstitution . map mbSubstitution . coerce

internalisePred ::
Flag "alias" ->
Flag "subsorts" ->
Expand Down

0 comments on commit 4e34924

Please sign in to comment.