1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
//-----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation. All Rights Reserved.
//
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Diagnostics.Contracts;
using Microsoft.Boogie.VCExprAST;
namespace Microsoft.Boogie
{
/// <summary>
/// The methods of this class are called in the following order:
/// DeclareType*
/// (DeclareConstant DeclareFunction)*
/// AddAxiom*
/// DeclareGlobalVariable*
/// At this time, all "attributes" are passed in as null.
/// </summary>
[ContractClass(typeof(ProverContextContracts))]
public abstract class ProverContext : ICloneable {
protected virtual void ProcessDeclaration(Declaration decl) {Contract.Requires(decl != null);}
public virtual void DeclareType(TypeCtorDecl t, string attributes) {Contract.Requires(t != null); ProcessDeclaration(t); }
public virtual void DeclareConstant(Constant c, bool uniq, string attributes) {Contract.Requires(c != null); ProcessDeclaration(c); }
public virtual void DeclareFunction(Function f, string attributes) {Contract.Requires(f != null); ProcessDeclaration(f); }
public virtual void AddAxiom(Axiom a, string attributes) {Contract.Requires(a != null); ProcessDeclaration(a); }
public virtual void DeclareGlobalVariable(GlobalVariable v, string attributes) {Contract.Requires(v != null); ProcessDeclaration(v); }
public abstract void AddAxiom(VCExpr vc);
public abstract VCExpressionGenerator ExprGen { get; }
public abstract Boogie2VCExprTranslator BoogieExprTranslator { get; }
public abstract VCGenerationOptions VCGenOptions { get; }
public abstract object Clone();
}
[ContractClassFor(typeof(ProverContext))]
public class ProverContextContracts:ProverContext{
public override void AddAxiom(VCExpr vc) {
}
public override void AddAxiom(Axiom a, string attributes)
{
}
public override VCExpressionGenerator ExprGen
{
get { Contract.Ensures(Contract.Result<VCExpressionGenerator>() != null);
throw new NotImplementedException(); }
}
public override Boogie2VCExprTranslator BoogieExprTranslator
{
get { Contract.Ensures(Contract.Result<Boogie2VCExprTranslator>() != null);
throw new NotImplementedException(); }
}
public override VCGenerationOptions VCGenOptions
{
get {Contract.Ensures(Contract.Result<VCGenerationOptions>() != null);
throw new NotImplementedException(); }
}
public override object Clone()
{
Contract.Ensures(Contract.Result<object>() != null);
throw new NotImplementedException();
}
}
// -----------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------
/// <summary>
/// This ProverContext subclass is intended for use with untyped provers that do not require names
/// to be declared before use. It constructs its context from unique constants and given axioms.
/// </summary>
public class DeclFreeProverContext : ProverContext {
protected VCExpressionGenerator gen;
protected VCGenerationOptions genOptions;
protected Boogie2VCExprTranslator translator;
protected OrderingAxiomBuilder orderingAxiomBuilder;
StringBuilder proverCommands;
StringBuilder incrementalProverCommands;
protected List<Variable> distincts;
protected List<VCExpr> axiomConjuncts;
[ContractInvariantMethod]
void ObjectInvariant()
{
Contract.Invariant(gen!=null);
Contract.Invariant(genOptions != null);
Contract.Invariant(translator != null);
Contract.Invariant(orderingAxiomBuilder != null);
Contract.Invariant(proverCommands != null);
Contract.Invariant(incrementalProverCommands != null);
Contract.Invariant(cce.NonNullElements(distincts));
Contract.Invariant(cce.NonNullElements(axiomConjuncts));
}
public VCExprTranslator/*?*/ exprTranslator;
public DeclFreeProverContext(VCExpressionGenerator gen,
VCGenerationOptions genOptions) {
Contract.Requires(gen != null);
Contract.Requires(genOptions != null);
this.gen = gen;
this.genOptions = genOptions;
Boogie2VCExprTranslator t = new Boogie2VCExprTranslator (gen, genOptions);
this.translator = t;
OrderingAxiomBuilder oab = new OrderingAxiomBuilder(gen, t);
Contract.Assert(oab != null);
oab.Setup();
this.orderingAxiomBuilder = oab;
proverCommands = new StringBuilder();
incrementalProverCommands = new StringBuilder();
distincts = new List<Variable>();
axiomConjuncts = new List<VCExpr>();
exprTranslator = null;
}
private DeclFreeProverContext(DeclFreeProverContext ctxt) {
Contract.Requires(ctxt != null);
this.gen = ctxt.gen;
this.genOptions = ctxt.genOptions;
Boogie2VCExprTranslator t = (Boogie2VCExprTranslator)ctxt.translator.Clone();
Contract.Assert(t != null);
this.translator = t;
this.orderingAxiomBuilder = new OrderingAxiomBuilder(ctxt.gen, t, ctxt.orderingAxiomBuilder);
StringBuilder cmds = new StringBuilder ();
cmds.Append(ctxt.proverCommands);
proverCommands = cmds;
StringBuilder incCmds = new StringBuilder ();
incCmds.Append(ctxt.incrementalProverCommands);
incrementalProverCommands = incCmds;
distincts = new List<Variable>(ctxt.distincts);
axiomConjuncts = new List<VCExpr>(ctxt.axiomConjuncts);
if (ctxt.exprTranslator == null)
exprTranslator = null;
else
exprTranslator = (VCExprTranslator)cce.NonNull(ctxt.exprTranslator.Clone());
}
public override object Clone() {
Contract.Ensures(Contract.Result<object>() != null);
return new DeclFreeProverContext(this);
}
internal protected void SayToProver(string msg)
{
Contract.Requires(msg != null);
msg = msg + "\r\n";
proverCommands.Append(msg);
incrementalProverCommands.Append(msg);
}
protected override void ProcessDeclaration(Declaration decl)
{
Contract.Requires(decl != null);
for (QKeyValue a = decl.Attributes; a != null; a = a.Next) {
if (a.Key == "prover" && a.Params.Count == 1) {
string cmd = a.Params[0] as string;
if (cmd != null) {
int pos = cmd.IndexOf(':');
if (pos <= 0)
throw new ProverException("Invalid syntax of :prover string: `" + cmd + "'");
string kind = cmd.Substring(0, pos);
if (genOptions.IsAnyProverCommandSupported(kind))
SayToProver(cmd.Substring(pos + 1));
}
}
}
}
public override void DeclareFunction(Function f, string attributes) {Contract.Requires(f != null);
base.ProcessDeclaration(f);
}
public override void DeclareConstant(Constant c, bool uniq, string attributes) {Contract.Requires(c != null);
base.DeclareConstant(c, uniq, attributes);
orderingAxiomBuilder.AddConstant(c);
// TODO: make separate distinct lists for names coming from different types
// e.g., one for strings, one for ints, one for program types.
if (uniq){
distincts.Add(c);
}
}
public override void AddAxiom(Axiom ax, string attributes) {Contract.Requires(ax != null);
base.AddAxiom(ax, attributes);
string ignore = ax.FindStringAttribute("ignore");
if (ignore != null && genOptions.IsAnyProverCommandSupported(ignore)) {
return;
}
axiomConjuncts.Add(translator.Translate(ax.Expr));
}
public override void AddAxiom(VCExpr vc)
{Contract.Requires(vc != null);
axiomConjuncts.Add(vc);
}
public VCExpr Axioms {
get {Contract.Ensures(Contract.Result<VCExpr>() != null);
VCExpr axioms = gen.NAry(VCExpressionGenerator.AndOp, axiomConjuncts);
List<VCExpr>/*!>!*/ distinctVars = new List<VCExpr> ();
foreach (Variable v in distincts){
Contract.Assert(v != null);
distinctVars.Add(translator.LookupVariable(v));}
axioms = gen.AndSimp(gen.Distinct(distinctVars), axioms);
if (CommandLineOptions.Clo.TypeEncodingMethod != CommandLineOptions.TypeEncoding.Monomorphic)
axioms = gen.AndSimp(orderingAxiomBuilder.Axioms, axioms);
return axioms;
}
}
public string GetProverCommands(bool full) {Contract.Ensures(Contract.Result<string>() != null);
string res = (full ? proverCommands : incrementalProverCommands).ToString();
incrementalProverCommands.Length = 0;
return res;
}
public override VCExpressionGenerator ExprGen { get {Contract.Ensures(Contract.Result<VCExpressionGenerator>() != null);
return gen;
} }
public override Boogie2VCExprTranslator BoogieExprTranslator { get {Contract.Ensures(Contract.Result<Boogie2VCExprTranslator>() != null);
return translator;
} }
public override VCGenerationOptions VCGenOptions { get {Contract.Ensures(Contract.Result<VCGenerationOptions>() != null);
return genOptions;
} }
}
// Translator from VCExpressions to strings, which are implemented
// by the various provers
[ContractClass(typeof(VCExprTranslatorContracts))]
public abstract class VCExprTranslator : ICloneable {
public abstract string translate(VCExpr expr, int polarity);
public abstract string Lookup(VCExprVar var);
public abstract Object Clone();
}
[ContractClassFor(typeof(VCExprTranslator))]
public class VCExprTranslatorContracts : VCExprTranslator {
public override object Clone() {
Contract.Ensures(Contract.Result<object>() != null);
throw new NotImplementedException();
}
public override string Lookup(VCExprVar var) {
Contract.Requires(var != null);
Contract.Ensures(Contract.Result<string>() != null);
throw new NotImplementedException();
}
public override string translate(VCExpr expr, int polarity) {
Contract.Requires(expr != null);
Contract.Ensures(Contract.Result<string>() != null);
throw new NotImplementedException();
}
}
}
|