@@ -79,8 +79,6 @@ data FailReason
7979 SubsortingError SortError
8080 | -- | The two terms have differing argument lengths
8181 ArgLengthsDiffer Term Term
82- | -- | Not a matching substitution
83- SubjectVariableMatch Term Variable
8482 deriving stock (Eq , Show )
8583
8684instance FromModifiersT mods => Pretty (PrettyWithModifiers mods FailReason ) where
@@ -115,8 +113,6 @@ instance FromModifiersT mods => Pretty (PrettyWithModifiers mods FailReason) whe
115113 pretty $ show err
116114 ArgLengthsDiffer t1 t2 ->
117115 hsep [" Argument length differ" , pretty' @ mods t1, pretty' @ mods t2]
118- SubjectVariableMatch t v ->
119- hsep [" Cannot match variable in subject:" , pretty' @ mods v, pretty' @ mods t]
120116
121117{- | Attempts to find a simple unifying substitution for the given
122118 terms.
@@ -216,9 +212,11 @@ match1 _ t1@DomainValue{} t2@KSet{}
216212match1 _ t1@ DomainValue {} t2@ ConsApplication {} = failWith $ DifferentSymbols t1 t2
217213match1 _ t1@ DomainValue {} t2@ FunctionApplication {} = addIndeterminate t1 t2
218214-- match with var on the RHS must be indeterminate when evaluating functions. see: https://github.com/runtimeverification/hs-backend-booster/issues/231
219- match1 Rewrite t1@ DomainValue {} (Var t2) = failWith $ SubjectVariableMatch t1 t2
220- match1 Implies t1@ DomainValue {} (Var t2) = failWith $ SubjectVariableMatch t1 t2
221215match1 Eval t1@ DomainValue {} t2@ Var {} = addIndeterminate t1 t2
216+ -- match with var on RHS may lead to branching during rewriting, see https://github.com/runtimeverification/haskell-backend/issues/4100
217+ -- Related cases are currently marked with a special function so they can be identified and changed together later (extending branching functionality)
218+ match1 Rewrite t1@ DomainValue {} (Var v2) = subjectVariableMatch t1 v2
219+ match1 Implies t1@ DomainValue {} (Var v2) = subjectVariableMatch t1 v2
222220match1 Eval t1@ Injection {} t2@ AndTerm {} = addIndeterminate t1 t2
223221match1 _ t1@ Injection {} (AndTerm t2a t2b) = enqueueRegularProblem t1 t2a >> enqueueRegularProblem t1 t2b
224222match1 _ t1@ Injection {} t2@ DomainValue {} = failWith $ DifferentSymbols t1 t2
@@ -228,6 +226,7 @@ match1 _ t1@Injection{} t2@KList{}
228226match1 _ t1@ Injection {} t2@ KSet {} = failWith $ DifferentSymbols t1 t2
229227match1 _ t1@ Injection {} t2@ ConsApplication {} = failWith $ DifferentSymbols t1 t2
230228match1 _ t1@ Injection {} t2@ FunctionApplication {} = addIndeterminate t1 t2
229+ match1 Rewrite t1@ Injection {} (Var v2) = subjectVariableMatch t1 v2
231230match1 _ t1@ Injection {} t2@ Var {} = addIndeterminate t1 t2
232231match1 Eval t1@ KMap {} t2@ AndTerm {} = addIndeterminate t1 t2
233232match1 _ t1@ KMap {} (AndTerm t2a t2b) = enqueueRegularProblem t1 t2a >> enqueueRegularProblem t1 t2b
@@ -239,7 +238,7 @@ match1 _ t1@KMap{} t2@KList{}
239238match1 _ t1@ KMap {} t2@ KSet {} = failWith $ DifferentSymbols t1 t2
240239match1 _ t1@ KMap {} t2@ ConsApplication {} = failWith $ DifferentSymbols t1 t2
241240match1 _ t1@ KMap {} t2@ FunctionApplication {} = addIndeterminate t1 t2
242- match1 Rewrite t1@ KMap {} (Var t2 ) = failWith $ SubjectVariableMatch t1 t2
241+ match1 Rewrite t1@ KMap {} (Var v2 ) = subjectVariableMatch t1 v2
243242match1 _ t1@ KMap {} t2@ Var {} = addIndeterminate t1 t2
244243match1 Eval t1@ KList {} t2@ AndTerm {} = addIndeterminate t1 t2
245244match1 _ t1@ KList {} (AndTerm t2a t2b) = enqueueRegularProblem t1 t2a >> enqueueRegularProblem t1 t2b
@@ -252,7 +251,7 @@ match1 _ t1@(KList def1 heads1 rest1) t2@(KList def2 heads2
252251match1 _ t1@ KList {} t2@ KSet {} = failWith $ DifferentSymbols t1 t2
253252match1 _ t1@ KList {} t2@ ConsApplication {} = failWith $ DifferentSymbols t1 t2
254253match1 _ t1@ KList {} t2@ FunctionApplication {} = addIndeterminate t1 t2
255- match1 Rewrite t1@ KList {} (Var t2) = failWith $ SubjectVariableMatch t1 t2
254+ match1 Rewrite t1@ KList {} (Var t2) = subjectVariableMatch t1 t2
256255match1 _ t1@ KList {} t2@ Var {} = addIndeterminate t1 t2
257256match1 Eval t1@ KSet {} t2@ AndTerm {} = addIndeterminate t1 t2
258257match1 _ t1@ KSet {} (AndTerm t2a t2b) = enqueueRegularProblem t1 t2a >> enqueueRegularProblem t1 t2b
@@ -264,7 +263,7 @@ match1 _ t1@KSet{} t2@KList{}
264263match1 _ t1@ (KSet def1 patElements patRest) t2@ (KSet def2 subjElements subjRest) = if def1 == def2 then matchSets def1 patElements patRest subjElements subjRest else failWith $ DifferentSorts t1 t2
265264match1 _ t1@ KSet {} t2@ ConsApplication {} = failWith $ DifferentSymbols t1 t2
266265match1 _ t1@ KSet {} t2@ FunctionApplication {} = addIndeterminate t1 t2
267- match1 Rewrite t1@ KSet {} (Var t2) = failWith $ SubjectVariableMatch t1 t2
266+ match1 Rewrite t1@ KSet {} (Var t2) = subjectVariableMatch t1 t2
268267match1 _ t1@ KSet {} t2@ Var {} = addIndeterminate t1 t2
269268match1 Eval t1@ ConsApplication {} t2@ AndTerm {} = addIndeterminate t1 t2
270269match1 _ t1@ ConsApplication {} (AndTerm t2a t2b) = enqueueRegularProblem t1 t2a >> enqueueRegularProblem t1 t2b
@@ -276,7 +275,7 @@ match1 _ t1@ConsApplication{} t2@KSet{}
276275match1 matchTy (ConsApplication symbol1 sorts1 args1) (ConsApplication symbol2 sorts2 args2) = matchSymbolAplications matchTy symbol1 sorts1 args1 symbol2 sorts2 args2
277276match1 Eval (ConsApplication symbol1 sorts1 args1) (FunctionApplication symbol2 sorts2 args2) = matchSymbolAplications Eval symbol1 sorts1 args1 symbol2 sorts2 args2
278277match1 _ t1@ ConsApplication {} t2@ FunctionApplication {} = addIndeterminate t1 t2
279- match1 Rewrite t1@ ConsApplication {} (Var t2) = failWith $ SubjectVariableMatch t1 t2
278+ match1 Rewrite t1@ ConsApplication {} (Var t2) = subjectVariableMatch t1 t2
280279match1 _ t1@ ConsApplication {} t2@ Var {} = addIndeterminate t1 t2
281280match1 Eval t1@ FunctionApplication {} t2@ AndTerm {} = addIndeterminate t1 t2
282281match1 _ t1@ FunctionApplication {} (AndTerm t2a t2b) = enqueueRegularProblem t1 t2a >> enqueueRegularProblem t1 t2b
@@ -294,7 +293,7 @@ match1 Eval (FunctionApplication symbol1 sorts1 args1) (ConsApplication symbo
294293match1 _ t1@ FunctionApplication {} t2@ ConsApplication {} = addIndeterminate t1 t2
295294match1 Eval (FunctionApplication symbol1 sorts1 args1) (FunctionApplication symbol2 sorts2 args2) = matchSymbolAplications Eval symbol1 sorts1 args1 symbol2 sorts2 args2
296295match1 _ t1@ FunctionApplication {} t2@ FunctionApplication {} = addIndeterminate t1 t2
297- match1 Rewrite t1@ FunctionApplication {} (Var t2) = failWith $ SubjectVariableMatch t1 t2
296+ match1 Rewrite t1@ FunctionApplication {} (Var t2) = subjectVariableMatch t1 t2
298297match1 _ t1@ FunctionApplication {} t2@ Var {} = addIndeterminate t1 t2
299298match1 Eval t1@ Var {} t2@ AndTerm {} = addIndeterminate t1 t2
300299match1 _ t1@ Var {} (AndTerm t2a t2b) = enqueueRegularProblem t1 t2a >> enqueueRegularProblem t1 t2b
@@ -448,6 +447,12 @@ matchVar
448447 else Injection termSort variableSort term2
449448 else failWith $ DifferentSorts (Var var) term2
450449
450+ -- Subject variable matches are currently marked as indeterminate.
451+ -- The code may be extended to collect these as separate conditional
452+ -- results (for branching).
453+ subjectVariableMatch :: Term -> Variable -> StateT MatchState (Except MatchResult ) ()
454+ subjectVariableMatch t v = addIndeterminate t (Var v)
455+
451456{- | pair up the argument lists and return the pairs in the first argument. If the lists
452457are of equal length, return Nothing in second, else return the remaining
453458terms in the longer list, tagged with their origin).
0 commit comments