-
Notifications
You must be signed in to change notification settings - Fork 54
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
Clustering (--comparefuns for equivalence checking) #931
Open
drganam
wants to merge
7
commits into
epfl-lara:scala-2
Choose a base branch
from
drganam:clustering
base: scala-2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4c32479
@traceInduct clustering implementation
drganam 1952ed9
Refactor @traceInduct clustering implementation
drganam 11a0071
Fix extraction of Array.updated
jad-hamza 1fdf28f
Revert
drganam a56bb51
Refactoring
drganam 6dc5121
Add documentation
drganam 7ed129f
Update trace report printing
drganam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ package stainless | |
package extraction | ||
package trace | ||
|
||
import stainless.utils.CheckFilter | ||
|
||
trait Trace extends CachingPhase with SimpleFunctions with IdentitySorts { self => | ||
val s: Trees | ||
val t: termination.Trees | ||
|
@@ -24,6 +26,60 @@ trait Trace extends CachingPhase with SimpleFunctions with IdentitySorts { self | |
override val t: self.t.type = self.t | ||
} | ||
|
||
override protected def extractSymbols(context: TransformerContext, symbols: s.Symbols): t.Symbols = { | ||
import symbols._ | ||
import trees._ | ||
|
||
if (Trace.getModels.isEmpty) { | ||
val models = symbols.functions.values.toList.filter(elem => isModel(elem.id)).map(elem => elem.id) | ||
Trace.setModels(models) | ||
Trace.nextModel | ||
} | ||
|
||
if (Trace.getFunctions.isEmpty) { | ||
val functions = symbols.functions.values.toList.filter(elem => shouldBeChecked(elem.id)).map(elem => elem.id) | ||
Trace.setFunctions(functions) | ||
Trace.nextFunction | ||
} | ||
|
||
def checkPair(fd1: s.FunDef, fd2: s.FunDef): s.FunDef = { | ||
val name = CheckFilter.fixedFullName(fd1.id)+"$"+CheckFilter.fixedFullName(fd2.id) | ||
|
||
val newParams = fd1.params.map{param => param.freshen} | ||
val newParamVars = newParams.map{param => param.toVariable} | ||
val newParamTypes = fd1.tparams.map{tparam => tparam.freshen} | ||
val newParamTps = newParamTypes.map{tparam => tparam.tp} | ||
|
||
val vd = s.ValDef.fresh("holds", s.BooleanType()) | ||
val post = s.Lambda(Seq(vd), vd.toVariable) | ||
|
||
val body = s.Ensuring(s.Equals(s.FunctionInvocation(fd1.id, newParamTps, newParamVars), s.FunctionInvocation(fd2.id, newParamTps, newParamVars)), post) | ||
val flags: Seq[s.Flag] = Seq(s.Derived(fd1.id), s.Annotation("traceInduct",List(StringLiteral(fd1.id.name)))) | ||
|
||
new s.FunDef(FreshIdentifier(name), newParamTypes, newParams, s.BooleanType(), body, flags) | ||
} | ||
|
||
def newFuns: List[s.FunDef] = (Trace.getModel, Trace.getFunction) match { | ||
case (Some(model), Some(function)) => { | ||
val m = symbols.functions(model) | ||
val f = symbols.functions(function) | ||
if (m != f && m.params.size == f.params.size) { | ||
val newFun = checkPair(m, f) | ||
Trace.setTrace(newFun.id) | ||
List(newFun) | ||
} | ||
else { | ||
Trace.reportWrong | ||
Nil | ||
} | ||
} | ||
case _ => Nil | ||
} | ||
|
||
val extracted = super.extractSymbols(context, symbols) | ||
registerFunctions(extracted, newFuns.map(f => extractFunction(symbols, f))) | ||
} | ||
|
||
override protected def extractFunction(symbols: Symbols, fd: FunDef): t.FunDef = { | ||
import symbols._ | ||
var funInv: Option[FunctionInvocation] = None | ||
|
@@ -33,13 +89,12 @@ trait Trace extends CachingPhase with SimpleFunctions with IdentitySorts { self | |
case Annotation("traceInduct", fun) => { | ||
exprOps.preTraversal { | ||
case _ if funInv.isDefined => // do nothing | ||
case fi @ FunctionInvocation(tfd, _, args) if symbols.isRecursive(tfd) && (fun.contains(StringLiteral(tfd.name)) || fun.contains(StringLiteral(""))) | ||
=> { | ||
case fi @ FunctionInvocation(tfd, _, args) if symbols.isRecursive(tfd) && (fun.contains(StringLiteral(tfd.name)) || fun.contains(StringLiteral(""))) => { | ||
val paramVars = fd.params.map(_.toVariable) | ||
val argCheck = args.forall(paramVars.contains) && args.toSet.size == args.size | ||
if (argCheck) | ||
funInv = Some(fi) | ||
} | ||
} | ||
case _ => | ||
}(fd.fullBody) | ||
} | ||
|
@@ -105,8 +160,8 @@ trait Trace extends CachingPhase with SimpleFunctions with IdentitySorts { self | |
val argsMap = callee.params.map(_.toVariable).zip(finv.args).toMap | ||
val tparamMap = callee.typeArgs.zip(finv.tfd.tps).toMap | ||
val inlinedBody = typeOps.instantiateType(exprOps.replaceFromSymbols(argsMap, callee.body.get), tparamMap) | ||
val inductScheme = inductPattern(inlinedBody) | ||
|
||
val inductScheme = inductPattern(inlinedBody) | ||
val prevBody = function.fullBody match { | ||
case Ensuring(body, pred) => body | ||
case _ => function.fullBody | ||
|
@@ -115,19 +170,86 @@ trait Trace extends CachingPhase with SimpleFunctions with IdentitySorts { self | |
// body, pre and post for the tactFun | ||
|
||
val body = andJoin(Seq(inductScheme, prevBody)) | ||
val precondition = function.precondition | ||
val postcondition = function.postcondition | ||
|
||
val precondition = exprOps.preconditionOf(function.fullBody) //function.precondition | ||
val postcondition = exprOps.postconditionOf(function.fullBody) //function.postcondition | ||
val bodyPre = exprOps.withPrecondition(body, precondition) | ||
val bodyPost = exprOps.withPostcondition(bodyPre,postcondition) | ||
function.copy(function.id, function.tparams, function.params, function.returnType, bodyPost, function.flags) | ||
} | ||
|
||
function.copy(function.id, function.tparams, function.params, function.returnType, bodyPost, function.flags) | ||
type Path = Seq[String] | ||
|
||
private lazy val pathsOpt: Option[Seq[Path]] = context.options.findOption(optCompareFuns) map { functions => | ||
functions map CheckFilter.fullNameToPath | ||
} | ||
|
||
} | ||
private def shouldBeChecked(fid: Identifier): Boolean = pathsOpt match { | ||
case None => false | ||
|
||
case Some(paths) => | ||
// Support wildcard `_` as specified in the documentation. | ||
// A leading wildcard is always assumes. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small typo: |
||
val path: Path = CheckFilter.fullNameToPath(CheckFilter.fixedFullName(fid)) | ||
paths exists { p => | ||
if (p endsWith Seq("_")) path containsSlice p.init | ||
else path endsWith p | ||
} | ||
} | ||
|
||
private lazy val pathsOptModels: Option[Seq[Path]] = context.options.findOption(optModels) map { functions => | ||
functions map CheckFilter.fullNameToPath | ||
} | ||
|
||
private def isModel(fid: Identifier): Boolean = pathsOptModels match { | ||
case None => false | ||
|
||
case Some(paths) => | ||
// Support wildcard `_` as specified in the documentation. | ||
// A leading wildcard is always assumes. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here for "assumes", perhaps you could factor this code into a common function |
||
val path: Path = CheckFilter.fullNameToPath(CheckFilter.fixedFullName(fid)) | ||
paths exists { p => | ||
if (p endsWith Seq("_")) path containsSlice p.init | ||
else path endsWith p | ||
} | ||
} | ||
|
||
} | ||
|
||
object Trace { | ||
var clusters: Map[Identifier, List[Identifier]] = Map() | ||
var errors: List[Identifier] = List() | ||
var unknowns: List[Identifier] = List() | ||
var wrong: List[Identifier] = List() | ||
|
||
def optionsError(implicit ctx: inox.Context): Boolean = | ||
!ctx.options.findOptionOrDefault(frontend.optBatchedProgram) && | ||
(!ctx.options.findOptionOrDefault(optModels).isEmpty || !ctx.options.findOptionOrDefault(optCompareFuns).isEmpty) | ||
|
||
def printEverything(implicit ctx: inox.Context) = { | ||
import ctx.{ reporter, timers } | ||
if(!clusters.isEmpty || !errors.isEmpty || !unknowns.isEmpty) { | ||
reporter.info(s"Printing equivalence checking results:") | ||
allModels.foreach(model => { | ||
val l = clusters(model).mkString(", ") | ||
reporter.info(s"List of functions that are equivalent to model $model: $l") | ||
}) | ||
val errorneous = errors.mkString(", ") | ||
reporter.info(s"List of erroneous functions: $errorneous") | ||
val timeouts = unknowns.mkString(", ") | ||
reporter.info(s"List of timed-out functions: $timeouts") | ||
} | ||
} | ||
|
||
var allModels: List[Identifier] = List() | ||
var tmpModels: List[Identifier] = List() | ||
|
||
var allFunctions: List[Identifier] = List() | ||
var tmpFunctions: List[Identifier] = List() | ||
|
||
var model: Option[Identifier] = None | ||
var function: Option[Identifier] = None | ||
var trace: Option[Identifier] = None | ||
|
||
def apply(ts: Trees, tt: termination.Trees)(implicit ctx: inox.Context): ExtractionPipeline { | ||
val s: ts.type | ||
val t: tt.type | ||
|
@@ -136,4 +258,107 @@ object Trace { | |
override val t: tt.type = tt | ||
override val context = ctx | ||
} | ||
|
||
def setModels(m: List[Identifier]) = { | ||
allModels = m | ||
tmpModels = m | ||
clusters = (m zip m.map(_ => Nil)).toMap | ||
} | ||
|
||
def setFunctions(f: List[Identifier]) = { | ||
allFunctions = f | ||
tmpFunctions = f | ||
} | ||
|
||
def getModels = allModels | ||
|
||
def getFunctions = allFunctions | ||
|
||
//model for the current iteration | ||
def getModel = model | ||
|
||
//function to check in the current iteration | ||
def getFunction = function | ||
|
||
def setTrace(t: Identifier) = trace = Some(t) | ||
def getTrace = trace | ||
|
||
//iterate model for the current function | ||
def nextModel = (tmpModels, allModels) match { | ||
case (x::xs, _) => { // check the next model for the current function | ||
tmpModels = xs | ||
model = Some(x) | ||
} | ||
case (Nil, x::xs) => { | ||
tmpModels = allModels | ||
model = Some(x) | ||
tmpModels = xs | ||
function = tmpFunctions match { | ||
case x::xs => { | ||
tmpFunctions = xs | ||
Some(x) | ||
} | ||
case Nil => None | ||
} | ||
} | ||
case _ => model = None | ||
} | ||
|
||
//iterate function to check; reset model | ||
def nextFunction = tmpFunctions match { | ||
case x::xs => { | ||
tmpFunctions = xs | ||
function = Some(x) | ||
tmpModels = allModels | ||
tmpModels match { | ||
case Nil => model = None | ||
case x::xs => { | ||
model = Some(x) | ||
tmpModels = xs | ||
} | ||
} | ||
function | ||
} | ||
case Nil => { | ||
function = None | ||
} | ||
} | ||
|
||
def nextIteration[T <: AbstractReport[T]](report: AbstractReport[T])(implicit context: inox.Context): Boolean = trace match { | ||
case Some(t) => { | ||
if (report.hasError(t)) reportError | ||
else if (report.hasUnknown(t)) reportUnknown | ||
else reportValid | ||
!isDone | ||
} | ||
case None => { | ||
nextFunction | ||
!isDone | ||
} | ||
} | ||
|
||
private def isDone = function == None | ||
|
||
private def reportError = { | ||
errors = function.get::errors | ||
nextFunction | ||
} | ||
|
||
private def reportUnknown = { | ||
nextModel | ||
if (model == None) { | ||
unknowns = function.get::unknowns | ||
nextFunction | ||
} | ||
} | ||
|
||
private def reportValid = { | ||
clusters = clusters + (model.get -> (function.get::clusters(model.get))) | ||
nextFunction | ||
} | ||
|
||
private def reportWrong = { | ||
trace = None | ||
wrong = function.get::wrong | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you try to manipulate specifications (getting pre/postconditions, and changing them), using the new
BodyWithSpecs
API?