Skip to content
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

Refactored the define attribute on functions #236

Merged
merged 31 commits into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ab5df04
fixup! Add support for function definitions
Feb 22, 2020
f29eedd
Add support for function definitions
Feb 15, 2020
f321806
fixup! Add support for function definitions
Feb 15, 2020
d746ece
fixup! Add support for function definitions
Feb 21, 2020
8b416f9
fixup! Add support for function definitions
Feb 21, 2020
18e18ab
fixup! Add support for function definitions
Feb 22, 2020
a4aed19
fixup! Add support for function definitions
Feb 22, 2020
778d897
Refactored support for function definitions
zvonimir May 28, 2020
e61b35a
Minor fix, no need to pass false as immutable to forall constructor
zvonimir May 28, 2020
d9be498
Fixed indentation in parser
zvonimir May 28, 2020
2cbc782
Moved data type processing into a separate function
zvonimir May 28, 2020
32d5c3d
Moved processing of function definitions into a separate file
zvonimir May 28, 2020
5fabe6f
Prevent function arguments from being declared in SMT queries
zvonimir May 28, 2020
d6820fe
Cleaned up the code and added some comments
zvonimir May 28, 2020
c45d561
Fixed a problem with missing function definition dependencies
zvonimir May 29, 2020
770b21b
Renamed DefBody into DefinitionBody
zvonimir May 29, 2020
6fae2eb
Function cannot have both inline and define enabled
zvonimir May 29, 2020
2978973
Added assertions and more comments
zvonimir May 29, 2020
2dbc8c3
Added more complex regressions that use function definitions
zvonimir May 29, 2020
a3f14e4
Added more regressions
zvonimir May 29, 2020
11d834f
Minor fix since I messed up expected output for a regression
zvonimir May 29, 2020
c3c56bf
Merge branch 'master' into define-func
zvonimir May 29, 2020
440921e
Fixed expected regression results based on updates to CIVL
zvonimir May 29, 2020
c3a1f48
Merge branch 'master' into define-func
zvonimir May 29, 2020
b90bb21
Added useArrayTheory to SMACK regression that uses function definitions
zvonimir May 29, 2020
7795d0f
Function definitions work only with monomorphic functions
zvonimir May 29, 2020
719c84f
Added checking and error reporting for polymorphic functions
zvonimir May 29, 2020
723f921
Minor fix to wording of error messages
zvonimir Jun 2, 2020
83bbd83
Added regression that checks for recursion in function definitions
zvonimir Jun 2, 2020
6728ce2
Minor fix
zvonimir Jun 3, 2020
5260c33
Check for function monomorphism before creating definition body
zvonimir Jun 3, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 51 additions & 3 deletions Source/Core/Absy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2671,8 +2671,10 @@ public override void Emit(TokenTextWriter stream, int level) { }
public class Function : DeclWithFormals {
public string Comment;

// the body is only set if the function is declared with {:inline}
// Body is only set if the function is declared with {:inline}
public Expr Body;
// DefBody is only set if the function is declared with {:define}
public NAryExpr DefBody;
zvonimir marked this conversation as resolved.
Show resolved Hide resolved
public Axiom DefinitionAxiom;

public IList<Axiom> otherDefinitionAxioms;
Expand Down Expand Up @@ -2774,6 +2776,13 @@ public override void Emit(TokenTextWriter stream, int level) {
Body.Emit(stream);
stream.WriteLine();
stream.WriteLine("}");
} else if (DefBody != null) {
stream.WriteLine();
stream.WriteLine("{");
stream.Write(level + 1, "");
DefBody.Args[1].Emit(stream);
stream.WriteLine();
stream.WriteLine("}");
} else {
stream.WriteLine(";");
}
Expand All @@ -2791,11 +2800,14 @@ public override void Resolve(ResolutionContext rc) {
RegisterFormals(InParams, rc);
RegisterFormals(OutParams, rc);
ResolveAttributes(rc);
if (Body != null)
{
if (Body != null) {
rc.StateMode = ResolutionContext.State.StateLess;
Body.Resolve(rc);
rc.StateMode = ResolutionContext.State.Single;
} else if (DefBody != null) {
rc.StateMode = ResolutionContext.State.StateLess;
DefBody.Resolve(rc);
rc.StateMode = ResolutionContext.State.Single;
}
rc.PopVarContext();
Type.CheckBoundVariableOccurrences(TypeParameters,
Expand All @@ -2819,6 +2831,15 @@ public override void Typecheck(TypecheckingContext tc) {
tc.Error(Body,
"function body with invalid type: {0} (expected: {1})",
Body.Type, cce.NonNull(OutParams[0]).TypedIdent.Type);
} else if (DefBody != null) {
DefBody.Typecheck(tc);

// We are matching the type of the function body with output param, and not the type
// of DefBody, which is always going to be bool (since it is of the form func_call == func_body)
if (!cce.NonNull(DefBody.Args[1].Type).Unify(cce.NonNull(OutParams[0]).TypedIdent.Type))
tc.Error(DefBody.Args[1],
"function body with invalid type: {0} (expected: {1})",
DefBody.Args[1].Type, cce.NonNull(OutParams[0]).TypedIdent.Type);
}
}

Expand Down Expand Up @@ -2871,6 +2892,33 @@ public Axiom CreateDefinitionAxiom(Expr definition, QKeyValue kv = null) {
DefinitionAxiom = new Axiom(tok, def);
return DefinitionAxiom;
}

// Generates function definition of the form func_call == func_body
// For example, for
// function {:define} foo(x:int) returns(int) { x + 1 }
// this will generate
// foo(x):int == x + 1
// We need the left hand call part later on to be able to generate
// the appropriate SMTlib style function definition.
public NAryExpr CreateFunctionDefinition(Expr body) {
zvonimir marked this conversation as resolved.
Show resolved Hide resolved
Contract.Requires(body != null);

List<Expr> callArgs = new List<Expr>();
int i = 0;
foreach (Formal/*!*/ f in InParams) {
Contract.Assert(f != null);
string nm = f.TypedIdent.HasName ? f.TypedIdent.Name : "_" + i;
callArgs.Add(new IdentifierExpr(f.tok, nm));
i++;
}

Expr call = new NAryExpr(tok, new FunctionCall(new IdentifierExpr(tok, Name)), callArgs);
// specify the type of the function, because it might be that
// type parameters only occur in the output type
call = Expr.CoerceType(tok, call, (Type)OutParams[0].TypedIdent.Type.Clone());
NAryExpr def = Expr.Binary(tok, BinaryOperator.Opcode.Eq, call, body);
return def;
}
}

public class Macro : Function {
Expand Down
16 changes: 8 additions & 8 deletions Source/Core/AbsyQuant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -593,29 +593,29 @@ public override int GetHashCode() {

public class ForallExpr : QuantifierExpr {
public ForallExpr(IToken/*!*/ tok, List<TypeVariable>/*!*/ typeParams,
List<Variable>/*!*/ dummies, QKeyValue kv, Trigger triggers, Expr/*!*/ body, bool immutable=false)
List<Variable>/*!*/ dummies, QKeyValue kv, Trigger triggers, Expr/*!*/ body, bool immutable = false)
: base(tok, typeParams, dummies, kv, triggers, body, immutable) {
Contract.Requires(tok != null);
Contract.Requires(typeParams != null);
Contract.Requires(dummies != null);
Contract.Requires(body != null);
Contract.Requires(dummies.Count + typeParams.Count > 0);
}
public ForallExpr(IToken tok, List<Variable> dummies, Trigger triggers, Expr body, bool immutable=false)
public ForallExpr(IToken tok, List<Variable> dummies, Trigger triggers, Expr body, bool immutable = false)
: base(tok, new List<TypeVariable>(), dummies, null, triggers, body, immutable) {
Contract.Requires(body != null);
Contract.Requires(dummies != null);
Contract.Requires(tok != null);
Contract.Requires(dummies.Count > 0);
}
public ForallExpr(IToken tok, List<Variable> dummies, Expr body, bool immutable=false)
public ForallExpr(IToken tok, List<Variable> dummies, Expr body, bool immutable = false)
: base(tok, new List<TypeVariable>(), dummies, null, null, body, immutable) {
Contract.Requires(body != null);
Contract.Requires(dummies != null);
Contract.Requires(tok != null);
Contract.Requires(dummies.Count > 0);
}
public ForallExpr(IToken tok, List<TypeVariable> typeParams, List<Variable> dummies, Expr body, bool immutable=false)
public ForallExpr(IToken tok, List<TypeVariable> typeParams, List<Variable> dummies, Expr body, bool immutable = false)
: base(tok, typeParams, dummies, null, null, body, immutable) {
Contract.Requires(body != null);
Contract.Requires(dummies != null);
Expand All @@ -639,22 +639,22 @@ public override BinderKind Kind {

public class ExistsExpr : QuantifierExpr {
public ExistsExpr(IToken/*!*/ tok, List<TypeVariable>/*!*/ typeParams, List<Variable>/*!*/ dummies,
QKeyValue kv, Trigger triggers, Expr/*!*/ body, bool immutable=false)
QKeyValue kv, Trigger triggers, Expr/*!*/ body, bool immutable = false)
: base(tok, typeParams, dummies, kv, triggers, body, immutable) {
Contract.Requires(tok != null);
Contract.Requires(typeParams != null);
Contract.Requires(dummies != null);
Contract.Requires(body != null);
Contract.Requires(dummies.Count + typeParams.Count > 0);
}
public ExistsExpr(IToken tok, List<Variable> dummies, Trigger triggers, Expr body, bool immutable=false)
public ExistsExpr(IToken tok, List<Variable> dummies, Trigger triggers, Expr body, bool immutable = false)
: base(tok, new List<TypeVariable>(), dummies, null, triggers, body, immutable) {
Contract.Requires(body != null);
Contract.Requires(dummies != null);
Contract.Requires(tok != null);
Contract.Requires(dummies.Count > 0);
}
public ExistsExpr(IToken tok, List<Variable> dummies, Expr body, bool immutable=false)
public ExistsExpr(IToken tok, List<Variable> dummies, Expr body, bool immutable = false)
: base(tok, new List<TypeVariable>(), dummies, null, null, body, immutable) {
Contract.Requires(body != null);
Contract.Requires(dummies != null);
Expand Down Expand Up @@ -898,7 +898,7 @@ public override int GetHashCode() {

public class LambdaExpr : BinderExpr {
public LambdaExpr(IToken/*!*/ tok, List<TypeVariable>/*!*/ typeParameters,
List<Variable>/*!*/ dummies, QKeyValue kv, Expr/*!*/ body, bool immutable=false)
List<Variable>/*!*/ dummies, QKeyValue kv, Expr/*!*/ body, bool immutable = false)
: base(tok, typeParameters, dummies, kv, body, immutable) {
Contract.Requires(tok != null);
Contract.Requires(typeParameters != null);
Expand Down
2 changes: 2 additions & 0 deletions Source/Core/BoogiePL.atg
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,8 @@ Function<.out List<Declaration>/*!*/ ds.>
// generate either an axiom or a function body
if (QKeyValue.FindBoolAttribute(kv, "inline")) {
func.Body = definition;
} else if (QKeyValue.FindBoolAttribute(kv, "define")) {
func.DefBody = func.CreateFunctionDefinition(definition);
} else {
ds.Add(func.CreateDefinitionAxiom(definition, kv));
}
Expand Down
5 changes: 5 additions & 0 deletions Source/Core/CommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1621,6 +1621,11 @@ result caching (default: ""<impl. name>:0"").
{:bvbuiltin ""spec""}
Rewrite the function to built-in prover function symbol 'fn'.

{:define}
Turn this function into a definition (using the define-fun construct)
when using the SMT-LIB backend. Can only be used with non-recursive
functions.

{:inline}
{:inline true}
Expand function according to its definition before going to the prover.
Expand Down
4 changes: 3 additions & 1 deletion Source/Core/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,8 @@ void Function(out List<Declaration>/*!*/ ds) {
// generate either an axiom or a function body
if (QKeyValue.FindBoolAttribute(kv, "inline")) {
func.Body = definition;
} else if (QKeyValue.FindBoolAttribute(kv, "define")) {
func.DefBody = func.CreateFunctionDefinition(definition);
} else {
ds.Add(func.CreateDefinitionAxiom(definition, kv));
}
Expand Down Expand Up @@ -2430,4 +2432,4 @@ public FatalError(string m): base(m) {}
}


}
}
4 changes: 4 additions & 0 deletions Source/Core/StandardVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ public virtual Function VisitFunction(Function node) {
node = (Function)this.VisitDeclWithFormals(node);
if (node.Body != null)
node.Body = this.VisitExpr(node.Body);
if (node.DefBody != null)
node.DefBody = (NAryExpr)this.VisitExpr(node.DefBody);
return node;
}
public virtual GlobalVariable VisitGlobalVariable(GlobalVariable node) {
Expand Down Expand Up @@ -894,6 +896,8 @@ public override Function VisitFunction(Function node)
node = (Function)this.VisitDeclWithFormals(node);
if (node.Body != null)
this.VisitExpr(node.Body);
if (node.DefBody != null)
this.VisitExpr(node.DefBody);
return node;
}
public override GlobalVariable VisitGlobalVariable(GlobalVariable node)
Expand Down
Loading