/
UnrollingSolver.scala
954 lines (771 loc) · 35.3 KB
/
UnrollingSolver.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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
/* Copyright 2009-2018 EPFL, Lausanne */
package inox
package solvers
package unrolling
import utils._
import theories._
import evaluators._
import combinators._
import scala.collection.mutable.{Map => MutableMap, ListBuffer}
object optUnrollBound extends IntOptionDef("unroll-bound", default = -1, "<Int> | -1 (unbounded)")
object optUnrollFactor extends IntOptionDef("unroll-factor", default = 1, "<PosInt>")
object optFeelingLucky extends FlagOptionDef("feeling-lucky", false)
object optUnrollAssumptions extends FlagOptionDef("unroll-assumptions", false)
object optModelFinding extends IntOptionDef("model-finding", 0, "<PosInt> | 0 (off)") {
override val parser: OptionParsers.OptionParser[Int] = (s => s match {
case "" => Some(1)
case _ => OptionParsers.intParser(s)
})
}
trait AbstractUnrollingSolver extends Solver { self =>
import context._
import program._
import program.trees._
import program.symbols._
import SolverResponses._
protected implicit val semantics: program.Semantics
protected type Encoded
protected val encoder: transformers.ProgramTransformer { val sourceProgram: program.type }
protected val chooses: ChooseEncoder { val program: self.program.type; val sourceEncoder: self.encoder.type }
protected lazy val fullEncoder = encoder andThen chooses
protected val theories: transformers.ProgramTransformer {
val sourceProgram: fullEncoder.targetProgram.type
val targetProgram: Program { val trees: fullEncoder.targetProgram.trees.type }
}
protected lazy val programEncoder = fullEncoder andThen theories
protected lazy val s: programEncoder.sourceProgram.trees.type = programEncoder.sourceProgram.trees
protected lazy val t: programEncoder.targetProgram.trees.type = programEncoder.targetProgram.trees
protected lazy val targetProgram: programEncoder.targetProgram.type = programEncoder.targetProgram
protected implicit val targetSemantics: targetProgram.Semantics
protected final def encode(vd: ValDef): t.ValDef = programEncoder.encode(vd)
protected final def decode(vd: t.ValDef): ValDef = programEncoder.decode(vd)
protected final def encode(v: Variable): t.Variable = programEncoder.encode(v)
protected final def decode(v: t.Variable): Variable = programEncoder.decode(v)
protected final def encode(e: Expr): t.Expr = programEncoder.encode(e)
protected final def decode(e: t.Expr): Expr = programEncoder.decode(e)
protected final def encode(tpe: Type): t.Type = programEncoder.encode(tpe)
protected final def decode(tpe: t.Type): Type = programEncoder.decode(tpe)
protected val templates: Templates {
val program: targetProgram.type
type Encoded = self.Encoded
}
protected val underlying: AbstractSolver {
val program: targetProgram.type
type Trees = Encoded
}
lazy val checkModels = options.findOptionOrDefault(optCheckModels)
lazy val silentErrors = options.findOptionOrDefault(optSilentErrors)
lazy val unrollBound = options.findOptionOrDefault(optUnrollBound)
lazy val unrollFactor = options.findOptionOrDefault(optUnrollFactor)
lazy val feelingLucky = options.findOptionOrDefault(optFeelingLucky)
lazy val unrollAssumptions = options.findOptionOrDefault(optUnrollAssumptions)
lazy val modelFinding = options.findOptionOrDefault(optModelFinding) > 0
def check(config: CheckConfiguration): config.Response[Model, Assumptions] =
checkAssumptions(config)(Set.empty)
protected val freeVars = new IncrementalMap[Variable, Encoded]()
private val constraints = new IncrementalSeq[Expr]()
private val freeChooses = new IncrementalMap[Choose, Encoded]()
protected var failures: ListBuffer[Throwable] = new ListBuffer
protected var abort: Boolean = false
protected var pause: Boolean = false
def push(): Unit = {
templates.push()
constraints.push()
freeVars.push()
}
def pop(): Unit = {
templates.pop()
constraints.pop()
freeVars.pop()
}
def reset() = {
failures.clear()
abort = false
pause = false
templates.reset()
constraints.reset()
freeVars.reset()
underlying.reset()
}
def interrupt(): Unit = { abort = true }
def free(): Unit = context.interruptManager.unregisterForInterrupts(this)
protected def declareVariable(v: t.Variable): Encoded
private def removeChooses(expr: Expr): (Expr, Map[Variable, Encoded]) = {
var chooseBindings: Map[Variable, Encoded] = Map.empty
val withoutChooses = exprOps.postMap {
case c: Choose =>
val v = c.res.toVariable
chooseBindings += v -> freeChooses.cached(c)(declareVariable(encode(v)))
Some(Assume(c.pred, v))
case _ => None
} (expr)
(withoutChooses, chooseBindings)
}
def declare(vd: ValDef): Unit = context.timers.solvers.declare.run(try {
context.timers.solvers.declare.sanity.run {
assert(vd.getType.isTyped)
}
// Multiple calls to registerForInterrupts are (almost) idempotent and acceptable
context.interruptManager.registerForInterrupts(this)
val freeBindings: Map[Variable, Encoded] = (typeOps.variablesOf(vd.tpe) + vd.toVariable).map {
v => v -> freeVars.cached(v)(declareVariable(encode(v)))
}.toMap
val newClauses = context.timers.solvers.declare.clauses.run {
templates.instantiateVariable(encode(vd.toVariable), freeBindings.map(p => encode(p._1) -> p._2))
}
context.timers.solvers.declare.underlying.run {
for (cl <- newClauses) {
underlying.assertCnstr(cl)
}
}
} catch {
case e @ (_: InternalSolverError | _: Unsupported) => failures += e
})
def assertCnstr(expression: Expr): Unit = context.timers.solvers.assert.run(try {
context.timers.solvers.assert.sanity.run {
symbols.ensureWellFormed // make sure that the current program is well-formed
typeCheck(expression, BooleanType()) // make sure we've asserted a boolean-typed expression
}
// Multiple calls to registerForInterrupts are (almost) idempotent and acceptable
context.interruptManager.registerForInterrupts(this)
constraints += expression
val (withoutChooses, chooseBindings) = removeChooses(expression)
val freeBindings: Map[Variable, Encoded] = exprOps.variablesOf(withoutChooses).map {
v => v -> freeVars.cached(v)(declareVariable(encode(v)))
}.toMap
val newClauses = context.timers.solvers.assert.clauses.run {
templates.instantiateExpr(
encode(withoutChooses),
(freeBindings ++ chooseBindings).map(p => encode(p._1) -> p._2)
)
}
context.timers.solvers.assert.underlying.run {
for (cl <- newClauses) {
underlying.assertCnstr(cl)
}
}
} catch {
case e @ (_: InternalSolverError | _: Unsupported) => failures += e
})
protected def wrapModel(model: underlying.Model): ModelWrapper
trait ModelWrapper {
def modelEval(elem: Encoded, tpe: t.Type): Option[t.Expr]
def extractConstructor(elem: Encoded, tpe: t.ADTType): Option[Identifier]
def extractSet(elem: Encoded, tpe: t.SetType): Option[Seq[Encoded]]
def extractMap(elem: Encoded, tpe: t.MapType): Option[(Seq[(Encoded, Encoded)], Encoded)]
def extractBag(elem: Encoded, tpe: t.BagType): Option[Seq[(Encoded, Encoded)]]
def getChoose(id: Identifier): Option[t.Expr]
def eval(elem: Encoded, tpe: Type): Option[Expr] = modelEval(elem, encode(tpe)).flatMap {
expr => try {
Some(decode(expr))
} catch {
case u: Unsupported => None
}
}
def getModel(extract: (Encoded, Type) => Expr): program.Model = {
val vs = freeVars.toMap.map { case (v, idT) => v.toVal -> extract(idT, v.getType) }
val cs = templates.getCalls
.filter(p => modelEval(p._1, t.BooleanType()) == Some(t.BooleanLiteral(true)))
.map(_._2)
.groupBy(_.tfd)
.flatMap { case (tfd, calls) =>
chooses.getChoose(tfd.fd).map { case (id, c, vds) =>
val tpSubst = tfd.tpSubst.map(p => decode(p._1).asInstanceOf[TypeParameter] -> decode(p._2))
val from = tfd.params.map(_.getType(tfd.symbols))
val to = tfd.getType
import templates._
val inst = new typeOps.TypeInstantiator(tpSubst)
val tvds = vds map inst.transform
val tc = inst.transform(c.copy(res = c.res.freshen))
val mappings = calls.flatMap { call =>
val optArgs = (call.args zip from).map(p => modelEval(p._1.encoded, p._2))
val optRes = modelEval(mkCall(tfd, call.args.map(_.encoded)), to)
if (optArgs.forall(_.isDefined) && optRes.isDefined) {
Some(optArgs.map(_.get) -> optRes.get)
} else {
None
}
}
val body = mappings.foldRight(tc: Expr) { case ((args, img), elze) =>
IfExpr(andJoin((tvds zip args).map { case (vd, arg) =>
Equals(vd.toVariable, decode(arg))
}), decode(img), elze)
}
(id, tfd.tps.map(decode(_))) -> body
}
}
val freeCs = freeChooses.toMap.map { case (c, idT) =>
(c.res.id, Seq.empty[Type]) -> extract(idT, c.res.getType)
}
def choosesOf(e: Expr, tps: Seq[Type]): Map[(Identifier, Seq[Type]), Expr] = exprOps.collect {
case c: Choose => getChoose(c.res.id).map(e => (c.res.id, tps) -> decode(e)).toSet
case _ => Set.empty[((Identifier, Seq[Type]), Expr)]
} (e).toMap
val modelCs = vs.values.toSeq.flatMap(e => choosesOf(e, Seq.empty)) ++
(cs ++ freeCs).flatMap { case ((id, tps), e) => choosesOf(e, tps) }
inox.Model(program)(vs, cs ++ freeCs ++ modelCs)
}
}
private def emit(silenceErrors: Boolean)(msg: String) =
if (silenceErrors) reporter.debug(msg) else reporter.warning(msg)
private def validateModel(model: program.Model, assumptions: Seq[Expr], silenceErrors: Boolean): Boolean = {
val expr = andJoin(assumptions ++ constraints)
val typingCond = andJoin(model.vars.toSeq.map { case (v, value) =>
def rec(e: Expr, tpe: Type): Expr = (e, tpe) match {
case (ADT(id, _, args), adt @ ADTType(_, tps)) =>
andJoin((args zip getConstructor(id, tps).fields).map(p => rec(p._1, p._2.tpe)))
case (Tuple(es), TupleType(tps)) =>
andJoin((es zip tps).map(p => rec(p._1, p._2)))
case (FiniteSet(elems, _), SetType(base)) =>
andJoin(elems.map(e => rec(e, base)))
case (FiniteBag(elems, _), BagType(base)) =>
andJoin(elems.map(p => and(rec(p._1, base), rec(p._2, IntegerType()))))
case (FiniteMap(elems, dflt, _, _), MapType(from, to)) =>
andJoin(elems.map(p => and(rec(p._1, from), rec(p._2, to))) :+ rec(dflt, to))
case (Lambda(params, body), FunctionType(from, to)) =>
val nparams = (params zip from).map { case (vd, tpe) => vd.copy(tpe = tpe) }
forall(nparams, rec(
exprOps.replaceFromSymbols((params zip nparams.map(_.toVariable)).toMap, body), to))
case (_, RefinementType(vd, pred)) =>
and(rec(e, vd.tpe), Let(vd, e, pred).copiedFrom(e))
case (Lambda(params, body), PiType(nparams, to)) =>
forall(nparams, rec(
exprOps.replaceFromSymbols((params zip nparams.map(_.toVariable)).toMap, body), to))
case (Tuple(es), SigmaType(nparams, to)) =>
andJoin(
(es.init zip nparams).map(p => rec(p._1, p._2.tpe)) ++ (rec(es.last, to) match {
case BooleanLiteral(true) => None
case p => Some((nparams zip es.init).foldRight(p) {
case ((vd, e), p) => Let(vd, e, p).copiedFrom(p)
})
})
)
case (c: Choose, tpe) =>
BooleanLiteral(
c.res.tpe == tpe && tpe == tpe.getType && // choose is for a simple type
(model.chooses contains (c.res.id -> Seq())) // the model contains a mapping for the choose
)
case _ => BooleanLiteral(true)
}
rec(value, v.tpe)
})
// we have to check case class constructors in model for ADT invariants
val newExpr = model.vars.toSeq.foldRight(and(typingCond, expr)) {
case ((v, value), e) => Let(v, value, e)
}
val evaluator = semantics.getEvaluator(context.withOpts(
optSilentErrors(silenceErrors),
optCheckModels(checkModels || feelingLucky)
))
evaluator.eval(newExpr, inox.Model(program)(Map.empty, model.chooses)) match {
case EvaluationResults.Successful(BooleanLiteral(true)) =>
reporter.debug("- Model validated.")
true
case EvaluationResults.Successful(_) =>
reporter.debug("- Invalid model.")
false
case EvaluationResults.RuntimeError(msg) =>
emit(silenceErrors)("- Model leads to runtime error: " + msg)
false
case EvaluationResults.EvaluatorError(msg) =>
emit(silenceErrors)("- Model leads to evaluation error: " + msg)
false
}
}
private def extractSimpleModel(model: underlying.Model): program.Model = {
val wrapped = wrapModel(model)
wrapped.getModel((e, tpe) => wrapped.eval(e, tpe).getOrElse(simplestValue(tpe)))
}
private def extractTotalModel(model: underlying.Model): program.Model = {
val wrapped = wrapModel(model)
import targetProgram._
import targetProgram.trees._
import targetProgram.symbols._
// maintain extracted functions to make sure equality is well-defined
var lambdaExtractions: Seq[(Encoded, Lambda)] = Seq.empty
var chooseExtractions: Seq[(Encoded, Choose)] = Seq.empty
def modelEq(e1: Encoded, e2: Encoded): Boolean =
wrapped.modelEval(templates.mkEquals(e1, e2), BooleanType()) == Some(BooleanLiteral(true))
def extractValue(v: Encoded, tpe: Type, seen: Map[FunctionType, Set[Encoded]]): Expr = {
def functionsOf(v: Encoded, tpe: Type): (Seq[(Encoded, FunctionType)], Seq[Expr] => Expr) = {
def reconstruct(subs: Seq[(Seq[(Encoded, FunctionType)], Seq[Expr] => Expr)],
recons: Seq[Expr] => Expr): (Seq[(Encoded, FunctionType)], Seq[Expr] => Expr) =
(subs.flatMap(_._1), (exprs: Seq[Expr]) => {
var curr = exprs
recons(subs.map { case (es, recons) =>
val (used, remaining) = curr.splitAt(es.size)
curr = remaining
recons(used)
})
})
def rec(v: Encoded, tpe: Type): (Seq[(Encoded, FunctionType)], Seq[Expr] => Expr) = tpe match {
case ft: FunctionType =>
(Seq(v -> ft), es => es.head)
case TupleType(tps) =>
val id = Variable.fresh("tuple", tpe)
val encoder = templates.mkEncoder(Map(id -> v)) _
reconstruct(tps.zipWithIndex.map {
case (tpe, index) => rec(encoder(TupleSelect(id, index + 1)), tpe)
}, Tuple)
case tpe @ ADTType(sid, tps) =>
val cons = wrapped.extractConstructor(v, tpe).get
val id = Variable.fresh("adt", tpe)
val encoder = templates.mkEncoder(Map(id -> v)) _
reconstruct(getConstructor(cons, tps).fields.map {
vd => rec(encoder(ADTSelector(id, vd.id)), vd.getType)
}, ADT(cons, tps, _))
case st @ SetType(base) =>
val vs = wrapped.extractSet(v, st).get
reconstruct(vs.map(rec(_, base)), FiniteSet(_, base))
case mt @ MapType(from, to) =>
val (vs, dflt) = wrapped.extractMap(v, mt).get
reconstruct(vs.flatMap(p => Seq(rec(p._1, from), rec(p._2, to))) :+ rec(dflt, to), {
case es :+ default => FiniteMap(es.grouped(2).map(s => s(0) -> s(1)).toSeq, default, from, to)
})
case bt @ BagType(base) =>
val vs = wrapped.extractBag(v, bt).get
reconstruct(vs.map(p => rec(p._1, base)), es => FiniteBag((es zip vs).map {
case (k, (_, v)) => k -> wrapped.modelEval(v, IntegerType()).get
}, base))
case _ => (Seq.empty, (es: Seq[Expr]) => wrapped.modelEval(v, tpe).get)
}
rec(v, tpe)
}
val ev = wrapped.modelEval(v, tpe).filterNot(_.isInstanceOf[Variable])
if (ev.isDefined) {
val (functions, recons) = functionsOf(v, tpe)
recons(functions.map { case (f, tpe) =>
extractFunction(f, tpe, seen)
})
} else {
encode(program.symbols.simplestValue(decode(tpe)))
}
}
def extractFunction(f: Encoded, tpe: FunctionType, seen: Map[FunctionType, Set[Encoded]]): Expr = {
val nextSeen = seen + (tpe -> (seen(tpe) + f))
def extractLambda(f: Encoded, tpe: FunctionType): Option[Lambda] = {
val optEqTemplate = templates.getLambdaTemplates(tpe).find { tmpl =>
wrapped.modelEval(tmpl.start, BooleanType()) == Some(BooleanLiteral(true)) &&
modelEq(tmpl.ids._2, f)
}
optEqTemplate.map { tmpl =>
val localsSubst = tmpl.structure.locals.map { case (v, ev) =>
v -> extractValue(ev, v.getType, nextSeen)
}.toMap
val res = exprOps.replaceFromSymbols(localsSubst, tmpl.structure.body)
val (nl, subst) = normalizeStructure(res, onlySimple = true)
exprOps.replaceFromSymbols(subst.map { case (v, e, _) => v -> e }.toMap, nl).asInstanceOf[Lambda]
}
}
val params: Seq[ValDef] = tpe.from.map(tpe => ValDef.fresh("x", tpe, true))
val arguments = templates.getGroundInstantiations(f, tpe).flatMap { case (b, eArgs) =>
wrapped.modelEval(b, BooleanType()).filter(_ == BooleanLiteral(true)).map(_ => eArgs)
}.distinct
extractLambda(f, tpe).orElse {
if (seen(tpe).exists(e => modelEq(f, e))) {
Some(chooseExtractions.collectFirst { case (e, c) if modelEq(f, e) => c }.getOrElse {
val c = Choose(Variable.fresh("x", tpe, true).toVal, BooleanLiteral(true))
chooseExtractions :+= f -> c
c
})
} else {
None
}
}.getOrElse {
val res = if (arguments.isEmpty) {
wrapped.modelEval(f, tpe).get.asInstanceOf[Lambda]
} else if (tpe.from.isEmpty) {
Lambda(Seq.empty, extractValue(templates.mkApp(f, tpe, Seq.empty), tpe.to, nextSeen))
} else {
val projections: Map[Type, Encoded] = (arguments.head zip params)
.groupBy(p => p._2.getType)
.view.mapValues(_.head._1).toMap
val exArguments = for (args <- arguments) yield {
(params zip args).map { case (vd, arg) => extractValue(arg, vd.getType, nextSeen) }
}
val argumentsWithConds: Seq[(Seq[Encoded], Expr)] =
(for (subset <- params.toSet.subsets(); (args, exArgs) <- arguments zip exArguments) yield {
val (concreteArgs, condOpts) = params.zipWithIndex.map { case (v, i) =>
if (!subset(v)) {
(args(i), Some(Equals(v.toVariable, exArgs(i))))
} else {
(projections(v.getType), None)
}
}.unzip
(concreteArgs, andJoin(condOpts.flatten))
}).toSeq
val withConds :+ ((concreteArgs, _)) = argumentsWithConds
val default = extractValue(templates.mkApp(f, tpe, concreteArgs), tpe.to, nextSeen)
val sortedArguments = withConds
.groupBy(_._2)
.view.mapValues(_.head._1)
.toSeq
.sortBy(p => -exprOps.formulaSize(p._1))
val mappings = sortedArguments.map { case (cond, arguments) =>
(cond, extractValue(templates.mkApp(f, tpe, arguments), tpe.to, nextSeen))
}
val lambda = Lambda(params, mappings.foldRight(default) {
case ((cond, img), elze) => IfExpr(cond, img, elze)
})
// make sure `lambda` is not equal to any other distinct extracted first-class function
(lambdaExtractions.collectFirst {
case (e, img) if img.getType == lambda.getType && modelEq(e, f) => Left(img)
case (encoded, `lambda`) => Right(encoded)
}) match {
case Some(Right(enc)) => wrapped.modelEval(enc, tpe).get match {
case Lambda(_, Let(_, Tuple(es), _)) =>
uniquateClosure(if (es.size % 2 == 0) -es.size / 2 else es.size / 2, lambda)
case l => throw new InternalSolverError(name, "Unexpected extracted lambda format: " + l.asString)
}
case Some(Left(img)) => img
case None => lambda
}
}
lambdaExtractions :+= f -> res
res
}
}
/* The solver may return values that cannot be decoded by the theory encoder if they
* weren't constrained by the generated clauses (eg. default values for functions).
* We replace these by `simplestValue` instead of crashing as they won't have any influence
* on model evaluation. */
def decodeOrSimplest(e: t.Expr): s.Expr = try {
decode(e)
} catch {
case t: TheoryException => program.symbols.simplestValue(decode(e.getType))
}
val initSeen: Map[FunctionType, Set[Encoded]] = Map.empty.withDefaultValue(Set.empty)
val exModel = wrapped.getModel((e, tpe) => decodeOrSimplest(extractValue(e, encode(tpe), initSeen)))
val exChooses = chooseExtractions.toMap.map { case (e, c) =>
c -> lambdaExtractions.collectFirst {
case (f, lambda) if lambda.getType == c.res.getType && modelEq(f, e) => lambda
}.get
}
val chooses = exChooses.map(p => (p._1.res.id, Seq.empty[s.Type]) -> decodeOrSimplest(p._2))
inox.Model(program)(exModel.vars, exModel.chooses ++ chooses)
}
def checkAssumptions(config: Configuration)(assumptions: Set[Expr]): config.Response[Model, Assumptions] =
context.timers.solvers.unrolling.run(scala.util.Try({
// throw error immediately if a previous call has already failed
if (failures.nonEmpty) throw failures.head
// Multiple calls to registerForInterrupts are (almost) idempotent and acceptable
context.interruptManager.registerForInterrupts(this)
val assumptionsSeq : Seq[Expr] = assumptions.toSeq
val encodedAssumptions : Seq[Encoded] = assumptionsSeq.map { expr =>
val vars = exprOps.variablesOf(expr)
templates.mkEncoder(vars.map(v => encode(v) -> freeVars(v)).toMap)(encode(expr))
}
val encodedToAssumptions : Map[Encoded, Expr] = (encodedAssumptions zip assumptionsSeq).toMap
def decodeAssumptions(core: Set[Encoded]): Set[Expr] = {
core.flatMap(ast => encodedToAssumptions.get(ast) match {
case Some(n @ Not(_: Variable)) => Some(n)
case Some(v: Variable) => Some(v)
case _ => None
})
}
import SolverResponses._
sealed abstract class CheckState
class CheckResult(val response: config.Response[Model, Assumptions]) extends CheckState
case class Validate(model: underlying.Model) extends CheckState
case object ModelCheck extends CheckState
case object FiniteRangeCheck extends CheckState
case object InstantiateQuantifiers extends CheckState
case object ProofCheck extends CheckState
case object Unroll extends CheckState
object CheckResult {
def cast(resp: SolverResponse[underlying.Model, Set[underlying.Trees]]): CheckResult =
CheckResult(config.convert(config.cast(resp), extractSimpleModel, decodeAssumptions))
def apply[M <: Model, A <: Assumptions](resp: config.Response[M, A]): CheckResult = resp match {
case SatWithModel(_) if !checkModels && abort => new CheckResult(config cast Unknown)
case _ => new CheckResult(resp)
}
def unapply(res: CheckResult): Option[config.Response[Model, Assumptions]] = Some(res.response)
}
object Abort {
def unapply[A,B](resp: SolverResponse[A,B]): Boolean = {
if (failures.nonEmpty || abort || pause) {
true
} else if (resp == Unknown) {
if (!silentErrors) {
reporter.error("Something went wrong. Underlying solver returned Unknown.")
}
true
} else {
false
}
}
}
var unrollCount: Int = 0
def canUnroll: Boolean = unrollBound < 0 || unrollCount < unrollBound
var currentState: CheckState = ModelCheck
while (!currentState.isInstanceOf[CheckResult]) {
currentState = currentState match {
case Abort() =>
CheckResult.cast(Unknown)
case ModelCheck =>
reporter.debug(" - Running search...")
val getModel = !templates.requiresFiniteRangeCheck || checkModels || templates.hasAxioms
val checkConfig = config
.max(Configuration(model = getModel, unsatAssumptions = unrollAssumptions && templates.canUnroll))
val res: SolverResponse[underlying.Model, Set[underlying.Trees]] = context.timers.solvers.unrolling.check.run {
underlying.checkAssumptions(checkConfig)(
encodedAssumptions.toSet ++ templates.satisfactionAssumptions
)
}
reporter.debug(" - Finished search with blocked literals")
res match {
case Abort() =>
CheckResult.cast(Unknown)
case _: Satisfiable if templates.requiresFiniteRangeCheck =>
FiniteRangeCheck
case Sat =>
CheckResult.cast(Sat)
case SatWithModel(model) =>
Validate(model)
case _: Unsatisfiable if !templates.canUnroll =>
CheckResult.cast(res)
case UnsatWithAssumptions(assumptions) if unrollAssumptions =>
for (b <- assumptions) templates.promoteBlocker(b)
ProofCheck
case _ =>
ProofCheck
}
case FiniteRangeCheck =>
reporter.debug(" - Verifying finite ranges")
val clauses = templates.getFiniteRangeClauses
val res: SolverResponse[underlying.Model, Set[underlying.Trees]] = context.timers.solvers.unrolling.check.run {
underlying.push()
for (cl <- encodedAssumptions.toSeq ++ templates.satisfactionAssumptions ++ clauses) {
underlying.assertCnstr(cl)
}
val res = underlying.check(Model)
underlying.pop()
res
}
reporter.debug(" - Finished checking finite ranges")
res match {
case Abort() =>
CheckResult.cast(Unknown)
case SatWithModel(model) =>
Validate(model)
case _ =>
InstantiateQuantifiers
}
case Validate(umodel) =>
(try { Some(extractTotalModel(umodel)) } catch {
case NoSimpleValue(tpe) =>
if (!silentErrors) {
reporter.error("No simple value found for type " + tpe.asString)
}
None
}).map { model =>
lazy val sat = CheckResult(config cast (if (config.withModel) SatWithModel(model) else Sat))
lazy val unknown = CheckResult cast Unknown
val valid = !checkModels || validateModel(model, assumptionsSeq, silenceErrors = silentErrors)
if (checkModels && valid) {
sat
} else if (abort || pause) {
unknown
} else if (checkModels && !valid) {
if (!silentErrors) {
reporter.error("Something went wrong. The model should have been valid, yet we got this:")
reporter.error(" " + model.asString.replaceAll("\n", "\n "))
reporter.error("for formula " + andJoin(assumptionsSeq ++ constraints).asString)
}
unknown
} else if (templates.hasAxioms) {
val wrapped = wrapModel(umodel)
val optError = templates.getAxioms.view.flatMap { q =>
if (wrapped.modelEval(q.guard, t.BooleanType()) != Some(t.BooleanLiteral(false))) {
q.checkForall { (e1, e2) =>
wrapped.modelEval(templates.mkEquals(e1, e2), t.BooleanType()) == Some(t.BooleanLiteral(true))
}.map(err => q.body -> err)
} else {
None
}
}.headOption
optError match {
case Some((expr, err)) =>
if (!silentErrors) {
reporter.error("Quantification " + expr.asString(templates.program.printerOpts) +
" does not fit in supported fragment.\n Reason: " + err)
reporter.error("Model obtained was:")
reporter.error(" " + model.asString.replaceAll("\n", "\n "))
}
unknown
case None =>
sat
}
} else {
sat
}
}.getOrElse(CheckResult cast Unknown)
case InstantiateQuantifiers =>
if (templates.quantificationsManager.unrollGeneration.isEmpty) {
if (!silentErrors) {
reporter.error("Something went wrong. The model is not transitive yet we can't instantiate!?")
}
CheckResult.cast(Unknown)
} else {
templates.promoteQuantifications
Unroll
}
case ProofCheck =>
if (feelingLucky) {
reporter.debug(" - Running search without blocked literals (w/ lucky test)")
} else {
reporter.debug(" - Running search without blocked literals (w/o lucky test)")
}
// we always ask for a model here in order to give priority to blockers that
// are keeping quantified clause instantiations from being considered
val res: SolverResponse[underlying.Model, Set[underlying.Trees]] =
context.timers.solvers.unrolling.check.run {
underlying.checkAssumptions(config max Configuration(model = true))(
encodedAssumptions.toSet ++ templates.refutationAssumptions
)
}
reporter.debug(" - Finished search without blocked literals")
res match {
case Abort() =>
CheckResult.cast(Unknown)
case _: Unsatisfiable =>
CheckResult.cast(res)
case SatWithModel(model) =>
lazy val luckyModel = if (!feelingLucky) None else {
(try { Some(extractSimpleModel(model)) } catch { case _: NoSimpleValue => None })
.filter(exModel => validateModel(exModel, assumptionsSeq, silenceErrors = true))
}
if (luckyModel.isDefined) {
CheckResult(config cast (if (config.withModel) SatWithModel(luckyModel.get) else Sat))
} else {
val wrapped = wrapModel(model)
if (modelFinding) {
for {
b <- templates.satisfactionAssumptions
v = templates.extractNot(b).getOrElse(b)
if wrapped.eval(v, BooleanType()) == Some(BooleanLiteral(true))
} templates.promoteBlocker(v)
}
for {
(inst, bs) <- templates.getInstantiationsWithBlockers
if wrapped.eval(inst, BooleanType()) == Some(BooleanLiteral(false))
b <- bs
} templates.promoteBlocker(b, force = true)
Unroll
}
}
case Unroll if !canUnroll =>
reporter.debug(s"- We need to keep going, but reached unroll bound ($unrollBound)")
CheckResult.cast(Unknown)
case Unroll => context.timers.solvers.unrolling.unroll.run {
reporter.debug("- We need to keep going")
// Unrolling `unrollFactor` times
for (i <- 1 to unrollFactor.toInt if templates.canUnroll && !abort && !pause) {
val newClauses = templates.unroll
for (ncl <- newClauses) {
underlying.assertCnstr(ncl)
}
}
unrollCount += 1
reporter.debug(" - Finished unrolling")
ModelCheck
}
}
}
val CheckResult(res) = currentState
res
}).recover {
case e @ (_: InternalSolverError | _: Unsupported) =>
if (reporter.isDebugEnabled) reporter.debug(e)
else if (!silentErrors && !abort) reporter.error(e.getMessage)
config.cast(Unknown)
}.get)
}
trait UnrollingSolver extends AbstractUnrollingSolver { self =>
import context._
import program._
import program.trees._
import program.symbols._
type Encoded = t.Expr
protected val underlying: AbstractSolver {
val program: targetProgram.type
type Trees = t.Expr
type Model = targetProgram.Model
}
override lazy val name = "U:"+underlying.name
object templates extends {
val program: targetProgram.type = targetProgram
val context = self.context
val semantics: targetProgram.Semantics = self.targetSemantics
} with Templates {
import program._
import program.trees._
import program.symbols._
type Encoded = Expr
def asString(expr: Expr): String = expr.asString
def abort: Boolean = self.abort
def pause: Boolean = self.pause
def encodeSymbol(v: Variable): Expr = v.freshen
def mkEncoder(bindings: Map[Variable, Expr])(e: Expr): Expr = exprOps.replaceFromSymbols(bindings, e)
def mkSubstituter(substMap: Map[Expr, Expr]): Expr => Expr = (e: Expr) => exprOps.replace(substMap, e)
def mkNot(e: Expr) = not(e)
def mkOr(es: Expr*) = orJoin(es)
def mkAnd(es: Expr*) = andJoin(es)
def mkEquals(l: Expr, r: Expr) = Equals(l, r)
def mkImplies(l: Expr, r: Expr) = implies(l, r)
def extractNot(e: Expr) = e match {
case Not(e2) => Some(e2)
case _ => None
}
def decodePartial(e: Expr, tpe: Type): Option[Expr] = Some(e)
}
protected lazy val modelEvaluator: DeterministicEvaluator { val program: self.targetProgram.type } =
targetSemantics.getEvaluator(context.withOpts(optIgnoreContracts(true)))
protected def declareVariable(v: t.Variable): t.Variable = v
protected def wrapModel(model: targetProgram.Model): super.ModelWrapper = ModelWrapperImpl(model)
private case class ModelWrapperImpl(model: targetProgram.Model) extends super.ModelWrapper {
private def e(expr: t.Expr): Option[t.Expr] = modelEvaluator.eval(expr, model).result
def extractConstructor(elem: t.Expr, tpe: t.ADTType): Option[Identifier] = e(elem) match {
case Some(t.ADT(id, _, _)) => Some(id)
case _ => None
}
def extractSet(elem: t.Expr, tpe: t.SetType): Option[Seq[t.Expr]] = e(elem) match {
case Some(t.FiniteSet(elems, _)) => Some(elems)
case _ => None
}
def extractBag(elem: t.Expr, tpe: t.BagType): Option[Seq[(t.Expr, t.Expr)]] = e(elem) match {
case Some(t.FiniteBag(elems, _)) => Some(elems)
case _ => None
}
def extractMap(elem: t.Expr, tpe: t.MapType): Option[(Seq[(t.Expr, t.Expr)], t.Expr)] = e(elem) match {
case Some(t.FiniteMap(elems, default, _, _)) => Some((elems, default))
case _ => None
}
def modelEval(elem: t.Expr, tpe: t.Type): Option[t.Expr] = e(elem)
def getChoose(id: Identifier): Option[t.Expr] = model.chooses.collectFirst {
case ((cid, tps), e) if cid == id && tps.isEmpty => e
}
override def toString = model.asString(targetProgram.printerOpts)
}
override def dbg(msg: => Any) = underlying.dbg(msg)
override def push(): Unit = {
super.push()
underlying.push()
}
override def pop(): Unit = {
super.pop()
underlying.pop()
}
override def reset(): Unit = {
underlying.reset()
super.reset()
}
override def interrupt(): Unit = {
super.interrupt()
underlying.interrupt()
}
override def free(): Unit = {
super.free()
underlying.free()
}
}