-
Notifications
You must be signed in to change notification settings - Fork 20
/
TemplateGenerator.scala
647 lines (527 loc) · 25 KB
/
TemplateGenerator.scala
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/* Copyright 2009-2018 EPFL, Lausanne */
package inox
package solvers
package unrolling
import utils._
import scala.collection.mutable.{Map => MutableMap}
trait TemplateGenerator { self: Templates =>
import context._
import program._
import program.trees._
import program.symbols._
protected type TemplateClauses = (
Map[Variable, Encoded],
Map[Variable, Encoded],
Map[Variable, Set[Variable]],
Map[Variable, Seq[Expr]],
Seq[Expr],
Types,
Equalities,
Seq[LambdaTemplate],
Seq[QuantificationTemplate]
)
protected def emptyClauses: TemplateClauses =
(Map.empty, Map.empty, Map.empty, Map.empty, Seq.empty, Map.empty, Map.empty, Seq.empty, Seq.empty)
protected implicit class ClausesWrapper(clauses: TemplateClauses) {
def ++(that: TemplateClauses): TemplateClauses = {
val (thisConds, thisExprs, thisTree, thisGuarded, thisEqs, thisTps, thisEqualities, thisLambdas, thisQuants) = clauses
val (thatConds, thatExprs, thatTree, thatGuarded, thatEqs, thatTps, thatEqualities, thatLambdas, thatQuants) = that
(thisConds ++ thatConds, thisExprs ++ thatExprs, thisTree merge thatTree,
thisGuarded merge thatGuarded, thisEqs ++ thatEqs, thisTps merge thatTps,
thisEqualities merge thatEqualities, thisLambdas ++ thatLambdas, thisQuants ++ thatQuants)
}
def +(pair: (Variable, Expr)): TemplateClauses = {
val (thisConds, thisExprs, thisTree, thisGuarded, thisEqs, thisTps, thisEqualities, thisLambdas, thisQuants) = clauses
(thisConds, thisExprs, thisTree, thisGuarded merge pair, thisEqs, thisTps, thisEqualities, thisLambdas, thisQuants)
}
def proj: (
Map[Variable, Encoded],
Map[Variable, Encoded],
Map[Variable, Set[Variable]],
Types,
Equalities,
Seq[LambdaTemplate],
Seq[QuantificationTemplate]
) = {
val (thisConds, thisExprs, thisTree, _, _, thisTypes, thisEqualities, thisLambdas, thisQuants) = clauses
(thisConds, thisExprs, thisTree, thisTypes, thisEqualities, thisLambdas, thisQuants)
}
}
private def isSimple(expr: Expr): Boolean = {
exprOps.isSimple(expr) && !exprOps.exists {
case Equals(e1, e2) => unrollEquality(e1.getType)
case _ => false
} (expr)
}
def mkClauses(pathVar: Variable, expr: Expr, substMap: Map[Variable, Encoded], polarity: Option[Boolean] = None): TemplateClauses = {
val (p, tmplClauses) = mkExprClauses(pathVar, expr, substMap, polarity)
tmplClauses + (pathVar -> p)
}
def mkClauses(pathVar: Variable, tpe: Type, expr: Expr, substMap: Map[Variable, Encoded])
(implicit generator: TypingGenerator): TemplateClauses = {
val (p, tmplClauses) = mkTypeClauses(pathVar, tpe, expr, substMap)
tmplClauses + (pathVar -> p)
}
protected def mkExprStructure(
pathVar: Variable,
expr: Expr,
substMap: Map[Variable, Encoded],
onlySimple: Boolean = false
): (Expr, TemplateStructure, Map[Variable, Encoded]) = {
val (struct, depsByScope) = normalizeStructure(expr)
val deps = depsByScope.map { case (v, e, _) => v -> e }.toMap
val (depSubst, depContents) =
depsByScope.foldLeft(substMap, TemplateContents.empty(pathVar -> substMap(pathVar), Seq())) {
case ((depSubst, contents), (v, expr, conditions)) =>
if (isSimple(expr)) {
// Note that we can ignore conditions in this case as the underlying
// solver is able to find satisfying assignments for all simple terms
val encoder = mkEncoder(depSubst) _
val ePointers = Template.lambdaPointers(encoder)(expr)
(depSubst + (v -> encoder(expr)), contents.copy(pointers = contents.pointers ++ ePointers))
} else if (!isSimple(expr) && conditions.isEmpty) {
// We optimize for the case where conditions is empty as the quantifier
// instantiation procedure relies on path condition variables staying the
// same whenever possible.
val (e, cls) = mkExprClauses(pathVar, expr, depSubst)
// setup the full encoding substMap
val (conds, exprs, tree, types, equals, lmbds, quants) = cls.proj
val clauseSubst: Map[Variable, Encoded] = depSubst ++ conds ++ exprs ++
lmbds.map(_.ids) ++ quants.flatMap(_.mapping) ++ equals.flatMap(_._2.map(_.symbols))
val (eCalls, eApps, eMatchers, ePointers) = Template.extractCalls(e, clauseSubst)
val (clsClauses, clsCalls, clsApps, clsMatchers, clsPointers, _) =
Template.encode(pathVar -> substMap(pathVar), Seq.empty, cls, clauseSubst)
(depSubst + (v -> mkEncoder(clauseSubst)(e)), contents merge (
conds, exprs, tree, clsClauses, types,
clsCalls merge Map(substMap(pathVar) -> eCalls),
clsApps merge Map(substMap(pathVar) -> eApps),
clsMatchers merge Map(substMap(pathVar) -> eMatchers),
equals, lmbds, quants, clsPointers ++ ePointers
))
} else {
val condVar = Variable.fresh("p", BooleanType())
val exprVar = Variable.fresh("r", v.getType)
val localSubst = depSubst + (condVar -> encodeSymbol(condVar)) + (exprVar -> encodeSymbol(exprVar))
val cls = mkClauses(pathVar, Equals(condVar, andJoin(conditions)), localSubst) ++
mkClauses(condVar, Equals(exprVar, expr), localSubst)
// setup the full encoding substMap
val (conds, exprs, tree, types, equals, lmbds, quants) = cls.proj
val clauseSubst: Map[Variable, Encoded] = localSubst ++ conds ++ exprs ++
lmbds.map(_.ids) ++ quants.flatMap(_.mapping) ++ equals.flatMap(_._2.map(_.symbols))
val (clsClauses, clsCalls, clsApps, clsMatchers, clsPointers, _) =
Template.encode(pathVar -> substMap(pathVar), Seq.empty, cls, clauseSubst)
(depSubst + (v -> localSubst(exprVar)), contents merge (
conds + (condVar -> localSubst(condVar)),
exprs + (exprVar -> localSubst(exprVar)),
tree merge Map(pathVar -> Set(condVar)),
clsClauses, types, clsCalls, clsApps, clsMatchers, equals, lmbds, quants, clsPointers
))
}
}
val sortedDeps = exprOps.variablesOf(struct).map(v => v -> deps(v)).toSeq.sortBy(_._1.id)
val dependencies = sortedDeps.map(p => depSubst(p._1))
val structure = new TemplateStructure(struct, dependencies, depContents)
val freshSubst = exprOps.variablesOf(struct).map(v => v -> v.freshen).toMap
val freshDeps = depSubst.map { case (v, e) => freshSubst.getOrElse(v, v) -> e }
(exprOps.replaceFromSymbols(freshSubst, struct), structure, freshDeps)
}
final private class Builder(pathVar: Variable, substMap: Map[Variable, Encoded]) {
var condVars = Map[Variable, Encoded]()
var condTree = Map[Variable, Set[Variable]](pathVar -> Set.empty)
def storeCond(pathVar: Variable, id: Variable): Unit = {
condVars += id -> encodeSymbol(id)
condTree += pathVar -> (condTree.getOrElse(pathVar, Set.empty) + id)
}
@inline def encodedCond(id: Variable): Encoded = substMap.getOrElse(id, condVars(id))
var exprVars = Map[Variable, Encoded]()
@inline def storeExpr(id: Variable): Unit = exprVars += id -> encodeSymbol(id)
// Represents clauses of the form:
// id => expr && ... && expr
var guardedExprs = Map[Variable, Seq[Expr]]()
def storeGuarded(guardVar: Variable, expr: Expr): Unit = {
assert(expr.getType == BooleanType(), expr.asString + " is not of type Boolean. " + explainTyping(expr))
val prev = guardedExprs.getOrElse(guardVar, Nil)
guardedExprs += guardVar -> (expr +: prev)
}
var types = Map[Encoded, Set[Typing]]()
def storeType(pathVar: Variable, tpe: Type, arg: Expr)(implicit generator: TypingGenerator): Expr = {
val b = encodedCond(pathVar)
val encoder = mkEncoder(localSubst) _
val closures = typeOps.variablesOf(tpe).toSeq.sortBy(_.id).map(encoder).map(Left(_))
val (result, typing) = generator match {
case FreeGenerator | ContractGenerator=>
val typeCall: Variable = Variable.fresh("tp", BooleanType(), true)
storeExpr(typeCall)
(typeCall, Typing(tpe, encoder(arg), Constraint(exprVars(typeCall), closures, generator == FreeGenerator)))
case CaptureGenerator(container, containerType) =>
// @nv: note that we only store the non-dependent type here as we don't need
// to consider dependent types when looking at captures
(BooleanLiteral(true), Typing(tpe.getType, encoder(arg), Capture(container, containerType)))
}
types += b -> (types.getOrElse(b, Set.empty) + typing)
result
}
// Represents equations (simple formulas)
var equations = Seq[Expr]()
@inline def iff(e1: Expr, e2: Expr): Unit = equations :+= Equals(e1, e2)
var lambdas = Seq[LambdaTemplate]()
@inline def registerLambda(lambda: LambdaTemplate): Unit = lambdas :+= lambda
var quantifications = Seq[QuantificationTemplate]()
@inline def registerQuantification(quantification: QuantificationTemplate): Unit =
quantifications :+= quantification
var equalities = Map[Encoded, Set[Equality]]()
@inline def storeEquality(guardVar: Variable, e1: Expr, e2: Expr): Unit = {
val b = encodedCond(guardVar)
val prev: Set[Equality] = equalities.getOrElse(b, Set.empty)
val encoder: Expr => Encoded = mkEncoder(localSubst)
equalities += b -> (prev + Equality(e1.getType, encoder(e1), encoder(e2)))
}
@inline def localSubst: Map[Variable, Encoded] =
substMap ++ condVars ++ exprVars ++ lambdas.map(_.ids)
def result: TemplateClauses =
(condVars, exprVars, condTree, guardedExprs, equations, types, equalities, lambdas, quantifications)
def ++=(that: TemplateClauses): this.type = {
val (conds, exprs, tree, guarded, eqs, tpes, equls, lmbds, quants) = that
condVars ++= conds
exprVars ++= exprs
condTree = condTree merge tree
guardedExprs = guardedExprs merge guarded
equations ++= eqs
types = types merge tpes
equalities ++= equls
lambdas ++= lmbds
quantifications ++= quants
this
}
}
protected def mkExprClauses(
pathVar: Variable,
expr: Expr,
substMap: Map[Variable, Encoded],
polarity: Option[Boolean] = None
): (Expr, TemplateClauses) = {
val builder = new Builder(pathVar, substMap)
import builder._
def rec(pathVar: Variable, expr: Expr, pol: Option[Boolean]): Expr = expr match {
case a @ Assume(cond, body) =>
val e = rec(pathVar, cond, Some(true))
storeGuarded(pathVar, e)
rec(pathVar, body, pol)
case c @ Choose(res, pred) =>
val newExpr = res.toVariable.freshen
storeExpr(newExpr)
val (tpeExpr, tmplClauses) = mkTypeClauses(pathVar, res.tpe, newExpr, localSubst)(FreeGenerator)
storeGuarded(pathVar, tpeExpr)
builder ++= tmplClauses
val p = rec(pathVar, exprOps.replace(Map(res.toVariable -> newExpr), pred), Some(true))
storeGuarded(pathVar, p)
newExpr
case l @ Let(i, e: Lambda, b) =>
val re = rec(pathVar, e, None) // guaranteed variable!
val rb = rec(pathVar, exprOps.replace(Map(i.toVariable -> re), b), pol)
rb
case l @ Let(i, e, b) =>
val newExpr: Variable = Variable.fresh("lt", i.getType, true)
storeExpr(newExpr)
val re = rec(pathVar, e, None)
storeGuarded(pathVar, Equals(newExpr, re))
val rb = rec(pathVar, exprOps.replace(Map(i.toVariable -> newExpr), b), pol)
rb
case n @ Not(e) =>
Not(rec(pathVar, e, pol.map(!_)))
case i @ Implies(lhs, rhs) =>
if (!isSimple(i)) {
rec(pathVar, Or(Not(lhs), rhs), pol)
} else {
implies(rec(pathVar, lhs, None), rec(pathVar, rhs, None))
}
case a @ And(parts) =>
val partitions = SeqUtils.groupWhile(parts)(isSimple)
partitions.map(andJoin) match {
case Seq(e) => e
case seq =>
val newExpr: Variable = Variable.fresh("e", BooleanType(), true)
storeExpr(newExpr)
def recAnd(pathVar: Variable, partitions: Seq[Expr]): Unit = partitions match {
case x :: Nil =>
storeGuarded(pathVar, Equals(newExpr, rec(pathVar, x, pol)))
case x :: xs =>
val newRes: Variable = Variable.fresh("res", BooleanType(), true)
storeExpr(newRes)
val xrec = rec(pathVar, x, pol)
storeGuarded(pathVar, Equals(newRes, xrec))
val newBool: Variable = Variable.fresh("b", BooleanType(), true)
storeCond(pathVar, newBool)
storeGuarded(pathVar, implies(not(newRes), not(newExpr)))
iff(and(pathVar, newRes), newBool)
recAnd(newBool, xs)
case Nil => scala.sys.error("Should never happen!")
}
recAnd(pathVar, seq)
newExpr
}
case o @ Or(parts) =>
val partitions = SeqUtils.groupWhile(parts)(isSimple)
partitions.map(orJoin) match {
case Seq(e) => e
case seq =>
val newExpr: Variable = Variable.fresh("e", BooleanType(), true)
storeExpr(newExpr)
def recOr(pathVar: Variable, partitions: Seq[Expr]): Unit = partitions match {
case x :: Nil =>
storeGuarded(pathVar, Equals(newExpr, rec(pathVar, x, pol)))
case x :: xs =>
val newRes: Variable = Variable.fresh("res", BooleanType(), true)
storeExpr(newRes)
val xrec = rec(pathVar, x, None)
storeGuarded(pathVar, Equals(newRes, xrec))
val newBool: Variable = Variable.fresh("b", BooleanType(), true)
storeCond(pathVar, newBool)
storeGuarded(pathVar, implies(newRes, newExpr))
iff(and(pathVar, not(newRes)), newBool)
recOr(newBool, xs)
case Nil => scala.sys.error("Should never happen!")
}
recOr(pathVar, seq)
newExpr
}
case i @ IfExpr(cond, thenn, elze) => {
if (isSimple(i)) {
i
} else {
val newBool1 : Variable = Variable.fresh("b", BooleanType(), true)
val newBool2 : Variable = Variable.fresh("b", BooleanType(), true)
val newExpr : Variable = Variable.fresh("e", i.getType, true)
val condVar : Variable = Variable.fresh("c", BooleanType(), true)
storeCond(pathVar, newBool1)
storeCond(pathVar, newBool2)
storeExpr(newExpr)
storeExpr(condVar)
val crec = rec(pathVar, cond, None)
val trec = rec(newBool1, thenn, pol)
val erec = rec(newBool2, elze, pol)
storeGuarded(pathVar, Equals(condVar, crec))
iff(and(pathVar, condVar), newBool1)
iff(and(pathVar, not(condVar)), newBool2)
storeGuarded(newBool1, Equals(newExpr, trec))
storeGuarded(newBool2, Equals(newExpr, erec))
newExpr
}
}
case l: Lambda =>
val template = LambdaTemplate(pathVar -> encodedCond(pathVar), l, localSubst)
builder.registerLambda(template)
template.ids._1
case f: Forall =>
val (assumptions, without: Forall) = liftAssumptions(f)
for (a <- assumptions) {
rec(pathVar, a, Some(true))
}
val TopLevelAnds(conjuncts) = without.body
val conjunctQs = conjuncts.map { conj =>
val vars = exprOps.variablesOf(conj)
val conjArgs = without.params.filter(vd => vars(vd.toVariable) || hasInstance(vd.tpe) != Some(true))
if (conjArgs.isEmpty) {
rec(pathVar, conj, pol)
} else {
val forall = Forall(conjArgs, conj)
pol match {
case Some(p) =>
val (res, template) = QuantificationTemplate(pathVar -> encodedCond(pathVar), p, forall, localSubst)
registerQuantification(template)
res
case None =>
val (res, negTemplate) = QuantificationTemplate(pathVar -> encodedCond(pathVar), false, forall, localSubst)
val inst = Variable.fresh("neg-inst", BooleanType(), true)
storeExpr(inst)
iff(inst, res)
val (_, posTemplate) = QuantificationTemplate(inst -> exprVars(inst), true, forall, localSubst, defer = true)
registerQuantification(negTemplate)
registerQuantification(posTemplate)
res
}
}
}
andJoin(conjunctQs)
case Equals(e1, e2) if unrollEquality(e1.getType) =>
val (v, _) = equalitySymbol(e1.getType)
val re1 = rec(pathVar, e1, pol)
val re2 = rec(pathVar, e2, pol)
storeEquality(pathVar, re1, re2)
Application(v, Seq(re1, re2))
case Operator(as, r) => r(as.map(a => rec(pathVar, a, None)))
}
val p = rec(pathVar, expr, polarity)
(p, builder.result)
}
/** Generates the clauses and other bookkeeping relevant to a type unfolding template. */
protected def mkTypeClauses(
pathVar: Variable,
tpe: Type,
expr: Expr,
substMap: Map[Variable, Encoded]
)(implicit generator: TypingGenerator): (Expr, TemplateClauses) = {
val builder = new Builder(pathVar, substMap)
import builder._
case class RecursionState(
recurseAdt: Boolean, // visit adt children/fields
recurseMap: Boolean, // unroll map definition
recurseSet: Boolean, // unroll set definition
recurseBag: Boolean // unroll bag definition
)
def rec(pathVar: Variable, tpe: Type, expr: Expr, state: RecursionState): Expr = tpe match {
case tpe if !(generator unroll tpe) => BooleanLiteral(true) // nothing to do here!
case (_: FunctionType | _: PiType) => storeType(pathVar, tpe, expr)
case tp: TypeParameter if generator == FreeGenerator => typesManager.storeTypeParameter(tp)
case RefinementType(vd, pred) =>
val newExpr: Variable = Variable.fresh("lt", vd.getType, true)
storeExpr(newExpr)
storeGuarded(pathVar, Equals(newExpr, expr))
val (p, predClauses) = mkExprClauses(pathVar,
exprOps.replaceFromSymbols(Map(vd -> newExpr), pred), localSubst)
builder ++= predClauses
and(rec(pathVar, vd.tpe, expr, state), p)
case SigmaType(params, to) =>
val (newExprs, recParams) = params.zipWithIndex.map { case (vd, i) =>
val newExpr: Variable = Variable.fresh("lt", vd.getType, true)
storeExpr(newExpr)
storeGuarded(pathVar, Equals(newExpr, TupleSelect(expr, i + 1)))
(newExpr, rec(pathVar, vd.tpe, newExpr, state))
}.unzip
val recTo = rec(pathVar,
typeOps.replaceFromSymbols((params zip newExprs).toMap, to),
TupleSelect(expr, params.size + 1), state)
andJoin(recParams :+ recTo)
case adt: ADTType =>
val sort = adt.getSort
and(
sort.invariant
.filter(_ => generator == FreeGenerator)
.map(_.applied(Seq(expr)))
.getOrElse(BooleanLiteral(true)),
if (sort.definition.isInductive && !state.recurseAdt) {
storeType(pathVar, tpe, expr)
} else {
val newExpr = Variable.fresh("e", BooleanType(), true)
storeExpr(newExpr)
val stored = for (tcons <- sort.constructors) yield {
if (tcons.fields.exists(vd => generator unroll vd.tpe)) {
val newBool: Variable = Variable.fresh("b", BooleanType(), true)
storeCond(pathVar, newBool)
val recProp = andJoin(for (vd <- tcons.fields) yield {
rec(newBool, vd.tpe, ADTSelector(expr, vd.id), state.copy(recurseAdt = false))
})
iff(and(pathVar, isCons(expr, tcons.id)), newBool)
storeGuarded(newBool, Equals(newExpr, recProp))
true
} else {
false
}
}
if (stored.foldLeft(false)(_ || _)) {
newExpr
} else {
BooleanLiteral(true)
}
}
)
case TupleType(tpes) =>
andJoin(for ((tpe, idx) <- tpes.zipWithIndex) yield {
rec(pathVar, tpe, TupleSelect(expr, idx + 1), state)
})
case MapType(from, to) =>
if (!state.recurseMap) {
storeType(pathVar, tpe, expr)
} else {
val newBool1 : Variable = Variable.fresh("mapIsEmpty", BooleanType(), true)
val newBool2 : Variable = Variable.fresh("mapNonEmpty", BooleanType(), true)
storeCond(pathVar, newBool1)
storeCond(pathVar, newBool2)
val condVar : Variable = Variable.fresh("condMapEmpty", BooleanType(), true)
val newExpr = Variable.fresh("mapRes", BooleanType(), true)
val keyExpr: Variable = Variable.fresh("mapKey", from, true)
val valueExpr: Variable = Variable.fresh("mapValue", to, true)
val defaultExpr: Variable = Variable.fresh("mapDefault", to, true)
val restExpr: Variable = Variable.fresh("mapRest", tpe, true)
storeExpr(condVar)
storeExpr(newExpr)
storeExpr(keyExpr)
storeExpr(valueExpr)
storeExpr(defaultExpr)
storeExpr(restExpr)
storeGuarded(pathVar, Equals(condVar, Equals(expr, FiniteMap(Seq(), defaultExpr, from, to))))
iff(and(pathVar, condVar), newBool1)
iff(and(pathVar, not(condVar)), newBool2)
storeGuarded(newBool1, newExpr)
storeGuarded(newBool2, Equals(expr, MapUpdated(restExpr, keyExpr, valueExpr)))
storeGuarded(newBool2, Equals(newExpr, and(
rec(newBool2, tpe, restExpr, state.copy(recurseMap = false)),
rec(newBool2, from, keyExpr, state),
rec(newBool2, to, valueExpr, state)
)))
and(rec(pathVar, to, defaultExpr, state), newExpr)
}
case SetType(base) =>
if (!state.recurseSet) {
storeType(pathVar, tpe, expr)
} else {
val newBool1 : Variable = Variable.fresh("setIsEmpty", BooleanType(), true)
val newBool2 : Variable = Variable.fresh("setNonEmpty", BooleanType(), true)
storeCond(pathVar, newBool1)
storeCond(pathVar, newBool2)
val condVar : Variable = Variable.fresh("condSetEmpty", BooleanType(), true)
val newExpr = Variable.fresh("setRes", BooleanType(), true)
val elemExpr: Variable = Variable.fresh("setElem", base, true)
val restExpr: Variable = Variable.fresh("setRest", tpe, true)
storeExpr(condVar)
storeExpr(newExpr)
storeExpr(elemExpr)
storeExpr(restExpr)
storeGuarded(pathVar, Equals(condVar, Equals(expr, FiniteSet(Seq(), base))))
iff(and(pathVar, condVar), newBool1)
iff(and(pathVar, not(condVar)), newBool2)
storeGuarded(newBool1, newExpr)
storeGuarded(newBool2, Equals(expr, SetUnion(FiniteSet(Seq(elemExpr), base), restExpr)))
storeGuarded(newBool2, Equals(newExpr, and(
rec(newBool2, tpe, restExpr, state.copy(recurseSet = false)),
rec(newBool2, base, elemExpr, state)
)))
newExpr
}
case BagType(base) =>
if (!state.recurseBag) {
storeType(pathVar, tpe, expr)
} else {
val newBool1 : Variable = Variable.fresh("bagIsEmpty", BooleanType(), true)
val newBool2 : Variable = Variable.fresh("bagNonEmpty", BooleanType(), true)
storeCond(pathVar, newBool1)
storeCond(pathVar, newBool2)
val condVar : Variable = Variable.fresh("condBagEmpty", BooleanType(), true)
val newExpr = Variable.fresh("bagRes", BooleanType(), true)
val elemExpr: Variable = Variable.fresh("bagElem", base, true)
val multExpr: Variable = Variable.fresh("bagMult", IntegerType(), true)
val restExpr: Variable = Variable.fresh("bagRest", tpe, true)
storeExpr(condVar)
storeExpr(newExpr)
storeExpr(elemExpr)
storeExpr(multExpr)
storeExpr(restExpr)
storeGuarded(pathVar, Equals(condVar, Equals(expr, FiniteBag(Seq(), base))))
iff(and(pathVar, condVar), newBool1)
iff(and(pathVar, not(condVar)), newBool2)
storeGuarded(newBool1, newExpr)
storeGuarded(newBool2, Equals(expr, BagUnion(FiniteBag(Seq((elemExpr, multExpr)), base), restExpr)))
storeGuarded(newBool2, GreaterThan(multExpr, IntegerLiteral(0)))
storeGuarded(newBool2, Equals(newExpr, and(
rec(newBool2, tpe, restExpr, state.copy(recurseBag = false)),
rec(newBool2, base, elemExpr, state),
rec(newBool2, IntegerType(), multExpr, state)
)))
newExpr
}
case _ => throw new InternalSolverError(s"Unexpected unrollable: ${tpe.asString}")
}
val p = rec(pathVar, tpe, expr, RecursionState(true, true, true, true))
(p, builder.result)
}
}