Skip to content

Commit

Permalink
Fixes gathering information from AccountedPred & Scales
Browse files Browse the repository at this point in the history
  • Loading branch information
sakehl committed Jun 28, 2024
1 parent 0e97812 commit e3ed0a5
Showing 1 changed file with 26 additions and 36 deletions.
62 changes: 26 additions & 36 deletions src/rewrite/vct/rewrite/SimplifyNestedQuantifiers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -149,19 +149,14 @@ case class SimplifyNestedQuantifiers[Pre <: Generation]()
FramedProof[Post](pre, body, post)(proof.blame)(proof.o)
}

// def getConditions(preds: AccountedPredicate[Pre]): Seq[Expr[Pre]] =
// preds match {
// case UnitAccountedPredicate(pred) => getConditions(pred)
// case SplitAccountedPredicate(left, right) =>
// getConditions(left) ++ getConditions(right)
// }
//
// def getConditions(e: Expr[Pre]): Seq[Expr[Pre]] =
// e match {
// case And(left, right) => getConditions(left) ++ getConditions(right)
// case Star(left, right) => getConditions(left) ++ getConditions(right)
// case other => Seq[Expr[Pre]](other)
// }
override def dispatch(p: AccountedPredicate[Pre]) : AccountedPredicate[Post] = {
p match {
case u@UnitAccountedPredicate(pred) =>
topLevel = true
u.rewriteDefault()
case s@SplitAccountedPredicate(left, right) => s.rewriteDefault()
}
}

override def dispatch(loopContract: LoopContract[Pre]): LoopContract[Post] = {
val loopInvariant: LoopInvariant[Pre] =
Expand All @@ -186,15 +181,18 @@ case class SimplifyNestedQuantifiers[Pre <: Generation]()

topLevel = true
infoGetter.setupInfo()
val contextEverywhere = dispatch(contract.contextEverywhere)
val oldInfo = infoGetter

// Reuse information from context everywhere
val requires = dispatch(contract.requires)
equalityChecker = ExpressionEqualityCheck()
infoGetter.setupInfo()

// Again reuse information from context everywhere
infoGetter = oldInfo
val ensures = dispatch(contract.ensures)
topLevel = false
equalityChecker = ExpressionEqualityCheck()

// TODO: Is context everywhere al distributed here? If not, we need to do more.
val contextEverywhere = dispatch(contract.contextEverywhere)
topLevel = false

val signals = contract.signals.map(element => dispatch(element))
val givenArgs =
Expand Down Expand Up @@ -286,23 +284,29 @@ case class SimplifyNestedQuantifiers[Pre <: Generation]()
var newBinder = false

def setData(): Unit = {
val allConditions = unfoldBody(Seq())
val allConditions = unfoldBody(Seq(), Seq())
// Split bounds that are independent of any binding variables
val (newIndependentConditions, potentialBounds) = allConditions
.partition(indepOf(bindings, _))
independentConditions.addAll(newIndependentConditions)
getBounds(potentialBounds)
}

def unfoldBody(prevConditions: Seq[Expr[Pre]]): Seq[Expr[Pre]] = {
def unfoldBody(prevConditions: Seq[Expr[Pre]], scales: Seq[Expr[Pre] => Expr[Pre]]): Seq[Expr[Pre]] = {
val (allConditions, mainBody) = unfoldImplies[Pre](body)
val newConditions = prevConditions ++ allConditions
val (newVars, secondBody) =
mainBody match {
case Forall(newVars, _, secondBody) => (newVars, secondBody)
case Starall(newVars, _, secondBody) => (newVars, secondBody)
// Strip Scales
case s@Scale(scale, res) =>
val newScales = scales :+ ((r: Expr[Pre]) => Scale(scale, r)(s.o))
body = res
return unfoldBody(newConditions, newScales)
case _ =>
body = mainBody
// Re-aply scales from right to left
body = scales.foldRight(mainBody)((s, b) => s(b))
return newConditions
}

Expand All @@ -316,7 +320,7 @@ case class SimplifyNestedQuantifiers[Pre <: Generation]()

body = secondBody

unfoldBody(newConditions)
unfoldBody(newConditions, scales)
}

def containsOtherBinders(e: Expr[Pre]): Boolean = {
Expand Down Expand Up @@ -421,18 +425,6 @@ case class SimplifyNestedQuantifiers[Pre <: Generation]()
}
}

def testPairs[A](
xs: Iterable[A],
ys: Iterable[A],
f: (A, A) => Boolean,
): Boolean = {
for (x <- xs)
for (y <- ys)
if (f(x, y))
return true
false
}

/** We check if there now any binding variables which resolve to just a
* single value, which happens if it has equal lower and upper bounds. E.g.
* forall(int i,j; i == 0 && i <= j && j < 5; xs[j+i]) ==> forall(int j; 0
Expand Down Expand Up @@ -870,7 +862,6 @@ case class SimplifyNestedQuantifiers[Pre <: Generation]()
* additionally add base_{i-1} / a_{i-1} < n_{i-1} (derived from (x_{i-1}
* < xmin_i + n_{i-1})
*/
// TODO ABOVE
def check_vars_list(
vars: List[Variable[Pre]]
): Option[SubstituteForall] = {
Expand Down Expand Up @@ -989,7 +980,6 @@ case class SimplifyNestedQuantifiers[Pre <: Generation]()
Seq(PointerSubscript(newGen(arrayIndex.array), xNewVar)(
triggerBlame
))
// Seq(PointerAdd(newGen(arrayIndex.array), xNewVar)(triggerBlame))
)
}

Expand Down

0 comments on commit e3ed0a5

Please sign in to comment.