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
|
/* NSC -- new Scala compiler
*
* Copyright 2011-2013 LAMP/EPFL
* @author Adriaan Moors
*/
package scala.tools.nsc.transform.patmat
import scala.tools.nsc.symtab._
import scala.language.postfixOps
import scala.collection.mutable
import scala.reflect.internal.util.Statistics
import scala.reflect.internal.util.Position
import scala.reflect.internal.util.HashSet
trait Logic extends Debugging {
import PatternMatchingStats._
private def max(xs: Seq[Int]) = if (xs isEmpty) 0 else xs max
private def alignedColumns(cols: Seq[AnyRef]): Seq[String] = {
def toString(x: AnyRef) = if (x eq null) "" else x.toString
if (cols.isEmpty || cols.tails.isEmpty) cols map toString
else {
val colLens = cols map (c => toString(c).length)
val maxLen = max(colLens)
val avgLen = colLens.sum/colLens.length
val goalLen = maxLen min avgLen*2
def pad(s: String) = {
val toAdd = ((goalLen - s.length) max 0) + 2
(" " * (toAdd/2)) + s + (" " * (toAdd/2 + (toAdd%2)))
}
cols map (x => pad(toString(x)))
}
}
def alignAcrossRows(xss: List[List[AnyRef]], sep: String, lineSep: String = "\n"): String = {
val maxLen = max(xss map (_.length))
val padded = xss map (xs => xs ++ List.fill(maxLen - xs.length)(null))
padded.transpose.map(alignedColumns).transpose map (_.mkString(sep)) mkString(lineSep)
}
// http://www.cis.upenn.edu/~cis510/tcl/chap3.pdf
// http://users.encs.concordia.ca/~ta_ahmed/ms_thesis.pdf
// propositional logic with constants and equality
trait PropositionalLogic {
type Type
type Tree
class Prop
case class Eq(p: Var, q: Const) extends Prop
type Const
type TypeConst <: Const
def TypeConst: TypeConstExtractor
trait TypeConstExtractor {
def apply(tp: Type): Const
}
type ValueConst <: Const
def ValueConst: ValueConstExtractor
trait ValueConstExtractor {
def apply(p: Tree): Const
}
val NullConst: Const
type Var <: AbsVar
val Var: VarExtractor
trait VarExtractor {
def apply(x: Tree): Var
def unapply(v: Var): Some[Tree]
}
// resets hash consing -- only supposed to be called by TreeMakersToProps
def prepareNewAnalysis(): Unit
trait AbsVar {
// indicate we may later require a prop for V = C
def registerEquality(c: Const): Unit
// call this to indicate null is part of the domain
def registerNull(): Unit
// can this variable be null?
def mayBeNull: Boolean
// compute the domain and return it (call registerNull first!)
def domainSyms: Option[Set[Sym]]
// the symbol for this variable being equal to its statically known type
// (only available if registerEquality has been called for that type before)
def symForStaticTp: Option[Sym]
// for this var, call it V, turn V = C into the equivalent proposition in boolean logic
// registerEquality(c) must have been called prior to this call
// in fact, all equalities relevant to this variable must have been registered
def propForEqualsTo(c: Const): Prop
// populated by registerEquality
// once implications has been called, must not call registerEquality anymore
def implications: List[(Sym, List[Sym], List[Sym])]
}
// would be nice to statically check whether a prop is equational or pure,
// but that requires typing relations like And(x: Tx, y: Ty) : (if(Tx == PureProp && Ty == PureProp) PureProp else Prop)
case class And(a: Prop, b: Prop) extends Prop
case class Or(a: Prop, b: Prop) extends Prop
case class Not(a: Prop) extends Prop
case object True extends Prop
case object False extends Prop
// symbols are propositions
abstract case class Sym(variable: Var, const: Const) extends Prop {
private[this] val id = Sym.nextSymId
override def toString = variable +"="+ const +"#"+ id
}
class UniqueSym(variable: Var, const: Const) extends Sym(variable, const)
object Sym {
private val uniques: HashSet[Sym] = new HashSet("uniques", 512)
def apply(variable: Var, const: Const): Sym = {
val newSym = new UniqueSym(variable, const)
(uniques findEntryOrUpdate newSym)
}
private def nextSymId = {_symId += 1; _symId}; private var _symId = 0
}
def /\(props: Iterable[Prop]) = if (props.isEmpty) True else props.reduceLeft(And(_, _))
def \/(props: Iterable[Prop]) = if (props.isEmpty) False else props.reduceLeft(Or(_, _))
trait PropTraverser {
def apply(x: Prop): Unit = x match {
case And(a, b) => apply(a); apply(b)
case Or(a, b) => apply(a); apply(b)
case Not(a) => apply(a)
case Eq(a, b) => applyVar(a); applyConst(b)
case _ =>
}
def applyVar(x: Var): Unit = {}
def applyConst(x: Const): Unit = {}
}
def gatherVariables(p: Prop): Set[Var] = {
val vars = new mutable.HashSet[Var]()
(new PropTraverser {
override def applyVar(v: Var) = vars += v
})(p)
vars.toSet
}
trait PropMap {
def apply(x: Prop): Prop = x match { // TODO: mapConserve
case And(a, b) => And(apply(a), apply(b))
case Or(a, b) => Or(apply(a), apply(b))
case Not(a) => Not(apply(a))
case p => p
}
}
// to govern how much time we spend analyzing matches for unreachability/exhaustivity
object AnalysisBudget {
import scala.tools.cmd.FromString.IntFromString
val max = sys.props.get("scalac.patmat.analysisBudget").collect(IntFromString.orElse{case "off" => Integer.MAX_VALUE}).getOrElse(256)
abstract class Exception(val advice: String) extends RuntimeException("CNF budget exceeded")
object exceeded extends Exception(
s"(The analysis required more space than allowed. Please try with scalac -Dscalac.patmat.analysisBudget=${AnalysisBudget.max*2} or -Dscalac.patmat.analysisBudget=off.)")
}
// convert finite domain propositional logic with subtyping to pure boolean propositional logic
// a type test or a value equality test are modelled as a variable being equal to some constant
// a variable V may be assigned multiple constants, as long as they do not contradict each other
// according to subtyping, e.g., V = ConstantType(1) and V = Int are valid assignments
// we rewrite V = C to a fresh boolean symbol, and model what we know about the variable's domain
// in a prelude (the equality axioms)
// 1. a variable with a closed domain (of a sealed type) must be assigned one of the instantiatable types in its domain
// 2. for each variable V in props, and each constant C it is compared to,
// compute which assignments imply each other (as in the example above: V = 1 implies V = Int)
// and which assignments are mutually exclusive (V = String implies -(V = Int))
//
// note that this is a conservative approximation: V = Constant(A) and V = Constant(B)
// are considered mutually exclusive (and thus both cases are considered reachable in {case A => case B =>}),
// even though A may be equal to B (and thus the second case is not "dynamically reachable")
//
// TODO: for V1 representing x1 and V2 standing for x1.head, encode that
// V1 = Nil implies -(V2 = Ci) for all Ci in V2's domain (i.e., it is unassignable)
// may throw an AnalysisBudget.Exception
def removeVarEq(props: List[Prop], modelNull: Boolean = false): (Formula, List[Formula]) = {
val start = if (Statistics.canEnable) Statistics.startTimer(patmatAnaVarEq) else null
val vars = new scala.collection.mutable.HashSet[Var]
object gatherEqualities extends PropTraverser {
override def apply(p: Prop) = p match {
case Eq(v, c) =>
vars += v
v.registerEquality(c)
case _ => super.apply(p)
}
}
object rewriteEqualsToProp extends PropMap {
override def apply(p: Prop) = p match {
case Eq(v, c) => v.propForEqualsTo(c)
case _ => super.apply(p)
}
}
props foreach gatherEqualities.apply
if (modelNull) vars foreach (_.registerNull())
val pure = props map (p => eqFreePropToSolvable(rewriteEqualsToProp(p)))
val eqAxioms = formulaBuilder
@inline def addAxiom(p: Prop) = addFormula(eqAxioms, eqFreePropToSolvable(p))
debug.patmat("removeVarEq vars: "+ vars)
vars.foreach { v =>
// if v.domainSyms.isEmpty, we must consider the domain to be infinite
// otherwise, since the domain fully partitions the type of the value,
// exactly one of the types (and whatever it implies, imposed separately) must be chosen
// consider X ::= A | B | C, and A => B
// coverage is formulated as: A \/ B \/ C and the implications are
v.domainSyms foreach { dsyms => addAxiom(\/(dsyms)) }
// when this variable cannot be null the equality corresponding to the type test `(x: T)`, where T is x's static type,
// is always true; when the variable may be null we use the implication `(x != null) => (x: T)` for the axiom
v.symForStaticTp foreach { symForStaticTp =>
if (v.mayBeNull) addAxiom(Or(v.propForEqualsTo(NullConst), symForStaticTp))
else addAxiom(symForStaticTp)
}
v.implications foreach { case (sym, implied, excluded) =>
// when sym is true, what must hold...
implied foreach (impliedSym => addAxiom(Or(Not(sym), impliedSym)))
// ... and what must not?
excluded foreach (excludedSym => addAxiom(Or(Not(sym), Not(excludedSym))))
}
}
debug.patmat("eqAxioms:\n"+ cnfString(toFormula(eqAxioms)))
debug.patmat("pure:"+ pure.map(p => cnfString(p)).mkString("\n"))
if (Statistics.canEnable) Statistics.stopTimer(patmatAnaVarEq, start)
(toFormula(eqAxioms), pure)
}
// an interface that should be suitable for feeding a SAT solver when the time comes
type Formula
type FormulaBuilder
// creates an empty formula builder to which more formulae can be added
def formulaBuilder: FormulaBuilder
// val f = formulaBuilder; addFormula(f, f1); ... addFormula(f, fN)
// toFormula(f) == andFormula(f1, andFormula(..., fN))
def addFormula(buff: FormulaBuilder, f: Formula): Unit
def toFormula(buff: FormulaBuilder): Formula
// the conjunction of formulae `a` and `b`
def andFormula(a: Formula, b: Formula): Formula
// equivalent formula to `a`, but simplified in a lightweight way (drop duplicate clauses)
def simplifyFormula(a: Formula): Formula
// may throw an AnalysisBudget.Exception
def propToSolvable(p: Prop): Formula = {
val (eqAxioms, pure :: Nil) = removeVarEq(List(p), modelNull = false)
andFormula(eqAxioms, pure)
}
// may throw an AnalysisBudget.Exception
def eqFreePropToSolvable(p: Prop): Formula
def cnfString(f: Formula): String
type Model = Map[Sym, Boolean]
val EmptyModel: Model
val NoModel: Model
def findModelFor(f: Formula): Model
def findAllModelsFor(f: Formula): List[Model]
}
trait CNF extends PropositionalLogic {
/** Override Array creation for efficiency (to not go through reflection). */
private implicit val clauseTag: scala.reflect.ClassTag[Clause] = new scala.reflect.ClassTag[Clause] {
def runtimeClass: java.lang.Class[Clause] = classOf[Clause]
final override def newArray(len: Int): Array[Clause] = new Array[Clause](len)
}
import scala.collection.mutable.ArrayBuffer
type FormulaBuilder = ArrayBuffer[Clause]
def formulaBuilder = ArrayBuffer[Clause]()
def formulaBuilderSized(init: Int) = new ArrayBuffer[Clause](init)
def addFormula(buff: FormulaBuilder, f: Formula): Unit = buff ++= f
def toFormula(buff: FormulaBuilder): Formula = buff
// CNF: a formula is a conjunction of clauses
type Formula = FormulaBuilder
def formula(c: Clause*): Formula = ArrayBuffer(c: _*)
type Clause = Set[Lit]
// a clause is a disjunction of distinct literals
def clause(l: Lit*): Clause = l.toSet
type Lit
def Lit(sym: Sym, pos: Boolean = true): Lit
def andFormula(a: Formula, b: Formula): Formula = a ++ b
def simplifyFormula(a: Formula): Formula = a.distinct
private def merge(a: Clause, b: Clause) = a ++ b
// throws an AnalysisBudget.Exception when the prop results in a CNF that's too big
// TODO: be smarter/more efficient about this (http://lara.epfl.ch/w/sav09:tseitin_s_encoding)
def eqFreePropToSolvable(p: Prop): Formula = {
def negationNormalFormNot(p: Prop, budget: Int): Prop =
if (budget <= 0) throw AnalysisBudget.exceeded
else p match {
case And(a, b) => Or(negationNormalFormNot(a, budget - 1), negationNormalFormNot(b, budget - 1))
case Or(a, b) => And(negationNormalFormNot(a, budget - 1), negationNormalFormNot(b, budget - 1))
case Not(p) => negationNormalForm(p, budget - 1)
case True => False
case False => True
case s: Sym => Not(s)
}
def negationNormalForm(p: Prop, budget: Int = AnalysisBudget.max): Prop =
if (budget <= 0) throw AnalysisBudget.exceeded
else p match {
case And(a, b) => And(negationNormalForm(a, budget - 1), negationNormalForm(b, budget - 1))
case Or(a, b) => Or(negationNormalForm(a, budget - 1), negationNormalForm(b, budget - 1))
case Not(negated) => negationNormalFormNot(negated, budget - 1)
case True
| False
| (_ : Sym) => p
}
val TrueF = formula()
val FalseF = formula(clause())
def lit(s: Sym) = formula(clause(Lit(s)))
def negLit(s: Sym) = formula(clause(Lit(s, false)))
def conjunctiveNormalForm(p: Prop, budget: Int = AnalysisBudget.max): Formula = {
def distribute(a: Formula, b: Formula, budget: Int): Formula =
if (budget <= 0) throw AnalysisBudget.exceeded
else
(a, b) match {
// true \/ _ = true
// _ \/ true = true
case (trueA, trueB) if trueA.size == 0 || trueB.size == 0 => TrueF
// lit \/ lit
case (a, b) if a.size == 1 && b.size == 1 => formula(merge(a(0), b(0)))
// (c1 /\ ... /\ cn) \/ d = ((c1 \/ d) /\ ... /\ (cn \/ d))
// d \/ (c1 /\ ... /\ cn) = ((d \/ c1) /\ ... /\ (d \/ cn))
case (cs, ds) =>
val (big, small) = if (cs.size > ds.size) (cs, ds) else (ds, cs)
big flatMap (c => distribute(formula(c), small, budget - (big.size*small.size)))
}
if (budget <= 0) throw AnalysisBudget.exceeded
p match {
case True => TrueF
case False => FalseF
case s: Sym => lit(s)
case Not(s: Sym) => negLit(s)
case And(a, b) =>
val cnfA = conjunctiveNormalForm(a, budget - 1)
val cnfB = conjunctiveNormalForm(b, budget - cnfA.size)
cnfA ++ cnfB
case Or(a, b) =>
val cnfA = conjunctiveNormalForm(a)
val cnfB = conjunctiveNormalForm(b)
distribute(cnfA, cnfB, budget - (cnfA.size + cnfB.size))
}
}
val start = if (Statistics.canEnable) Statistics.startTimer(patmatCNF) else null
val res = conjunctiveNormalForm(negationNormalForm(p))
if (Statistics.canEnable) Statistics.stopTimer(patmatCNF, start)
//
if (Statistics.canEnable) patmatCNFSizes(res.size).value += 1
// debug.patmat("cnf for\n"+ p +"\nis:\n"+cnfString(res))
res
}
}
trait DPLLSolver extends CNF {
// a literal is a (possibly negated) variable
def Lit(sym: Sym, pos: Boolean = true) = new Lit(sym, pos)
class Lit(val sym: Sym, val pos: Boolean) {
override def toString = if (!pos) "-"+ sym.toString else sym.toString
override def equals(o: Any) = o match {
case o: Lit => (o.sym eq sym) && (o.pos == pos)
case _ => false
}
override def hashCode = sym.hashCode + pos.hashCode
def unary_- = Lit(sym, !pos)
}
def cnfString(f: Formula) = alignAcrossRows(f map (_.toList) toList, "\\/", " /\\\n")
// adapted from http://lara.epfl.ch/w/sav10:simple_sat_solver (original by Hossein Hojjat)
val EmptyModel = Map.empty[Sym, Boolean]
val NoModel: Model = null
// returns all solutions, if any (TODO: better infinite recursion backstop -- detect fixpoint??)
def findAllModelsFor(f: Formula): List[Model] = {
val vars: Set[Sym] = f.flatMap(_ collect {case l: Lit => l.sym}).toSet
// debug.patmat("vars "+ vars)
// the negation of a model -(S1=True/False /\ ... /\ SN=True/False) = clause(S1=False/True, ...., SN=False/True)
def negateModel(m: Model) = clause(m.toSeq.map{ case (sym, pos) => Lit(sym, !pos) } : _*)
def findAllModels(f: Formula, models: List[Model], recursionDepthAllowed: Int = 10): List[Model]=
if (recursionDepthAllowed == 0) models
else {
debug.patmat("find all models for\n"+ cnfString(f))
val model = findModelFor(f)
// if we found a solution, conjunct the formula with the model's negation and recurse
if (model ne NoModel) {
val unassigned = (vars -- model.keySet).toList
debug.patmat("unassigned "+ unassigned +" in "+ model)
def force(lit: Lit) = {
val model = withLit(findModelFor(dropUnit(f, lit)), lit)
if (model ne NoModel) List(model)
else Nil
}
val forced = unassigned flatMap { s =>
force(Lit(s, true)) ++ force(Lit(s, false))
}
debug.patmat("forced "+ forced)
val negated = negateModel(model)
findAllModels(f :+ negated, model :: (forced ++ models), recursionDepthAllowed - 1)
}
else models
}
findAllModels(f, Nil)
}
private def withLit(res: Model, l: Lit): Model = if (res eq NoModel) NoModel else res + (l.sym -> l.pos)
private def dropUnit(f: Formula, unitLit: Lit): Formula = {
val negated = -unitLit
// drop entire clauses that are trivially true
// (i.e., disjunctions that contain the literal we're making true in the returned model),
// and simplify clauses by dropping the negation of the literal we're making true
// (since False \/ X == X)
val dropped = formulaBuilderSized(f.size)
for {
clause <- f
if !(clause contains unitLit)
} dropped += (clause - negated)
dropped
}
def findModelFor(f: Formula): Model = {
@inline def orElse(a: Model, b: => Model) = if (a ne NoModel) a else b
debug.patmat("DPLL\n"+ cnfString(f))
val start = if (Statistics.canEnable) Statistics.startTimer(patmatAnaDPLL) else null
val satisfiableWithModel: Model =
if (f isEmpty) EmptyModel
else if(f exists (_.isEmpty)) NoModel
else f.find(_.size == 1) match {
case Some(unitClause) =>
val unitLit = unitClause.head
// debug.patmat("unit: "+ unitLit)
withLit(findModelFor(dropUnit(f, unitLit)), unitLit)
case _ =>
// partition symbols according to whether they appear in positive and/or negative literals
val pos = new mutable.HashSet[Sym]()
val neg = new mutable.HashSet[Sym]()
f.foreach{_.foreach{ lit =>
if (lit.pos) pos += lit.sym else neg += lit.sym
}}
// appearing in both positive and negative
val impures = pos intersect neg
// appearing only in either positive/negative positions
val pures = (pos ++ neg) -- impures
if (pures nonEmpty) {
val pureSym = pures.head
// turn it back into a literal
// (since equality on literals is in terms of equality
// of the underlying symbol and its positivity, simply construct a new Lit)
val pureLit = Lit(pureSym, pos(pureSym))
// debug.patmat("pure: "+ pureLit +" pures: "+ pures +" impures: "+ impures)
val simplified = f.filterNot(_.contains(pureLit))
withLit(findModelFor(simplified), pureLit)
} else {
val split = f.head.head
// debug.patmat("split: "+ split)
orElse(findModelFor(f :+ clause(split)), findModelFor(f :+ clause(-split)))
}
}
if (Statistics.canEnable) Statistics.stopTimer(patmatAnaDPLL, start)
satisfiableWithModel
}
}
}
trait ScalaLogic extends Logic { self: PatternMatching =>
trait TreesAndTypesDomain extends PropositionalLogic with CheckableTreeAndTypeAnalysis {
type Type = global.Type
type Tree = global.Tree
// resets hash consing -- only supposed to be called by TreeMakersToProps
def prepareNewAnalysis(): Unit = { Var.resetUniques(); Const.resetUniques() }
object Var extends VarExtractor {
private var _nextId = 0
def nextId = {_nextId += 1; _nextId}
def resetUniques() = {_nextId = 0; uniques.clear()}
private val uniques = new mutable.HashMap[Tree, Var]
def apply(x: Tree): Var = uniques getOrElseUpdate(x, new Var(x, x.tpe))
def unapply(v: Var) = Some(v.path)
}
class Var(val path: Tree, staticTp: Type) extends AbsVar {
private[this] val id: Int = Var.nextId
// private[this] var canModify: Option[Array[StackTraceElement]] = None
private[this] def ensureCanModify() = {} //if (canModify.nonEmpty) debug.patmat("BUG!"+ this +" modified after having been observed: "+ canModify.get.mkString("\n"))
private[this] def observed() = {} //canModify = Some(Thread.currentThread.getStackTrace)
// don't access until all potential equalities have been registered using registerEquality
private[this] val symForEqualsTo = new mutable.HashMap[Const, Sym]
// when looking at the domain, we only care about types we can check at run time
val staticTpCheckable: Type = checkableType(staticTp)
private[this] var _mayBeNull = false
def registerNull(): Unit = { ensureCanModify(); if (NullTp <:< staticTpCheckable) _mayBeNull = true }
def mayBeNull: Boolean = _mayBeNull
// case None => domain is unknown,
// case Some(List(tps: _*)) => domain is exactly tps
// we enumerate the subtypes of the full type, as that allows us to filter out more types statically,
// once we go to run-time checks (on Const's), convert them to checkable types
// TODO: there seems to be bug for singleton domains (variable does not show up in model)
lazy val domain: Option[Set[Const]] = {
val subConsts = enumerateSubtypes(staticTp).map{ tps =>
tps.toSet[Type].map{ tp =>
val domainC = TypeConst(tp)
registerEquality(domainC)
domainC
}
}
val allConsts =
if (mayBeNull) {
registerEquality(NullConst)
subConsts map (_ + NullConst)
} else
subConsts
observed(); allConsts
}
// populate equalitySyms
// don't care about the result, but want only one fresh symbol per distinct constant c
def registerEquality(c: Const): Unit = {ensureCanModify(); symForEqualsTo getOrElseUpdate(c, Sym(this, c))}
// return the symbol that represents this variable being equal to the constant `c`, if it exists, otherwise False (for robustness)
// (registerEquality(c) must have been called prior, either when constructing the domain or from outside)
def propForEqualsTo(c: Const): Prop = {observed(); symForEqualsTo.getOrElse(c, False)}
// [implementation NOTE: don't access until all potential equalities have been registered using registerEquality]p
/** the information needed to construct the boolean proposition that encods the equality proposition (V = C)
*
* that models a type test pattern `_: C` or constant pattern `C`, where the type test gives rise to a TypeConst C,
* and the constant pattern yields a ValueConst C
*
* for exhaustivity, we really only need implication (e.g., V = 1 implies that V = 1 /\ V = Int, if both tests occur in the match,
* and thus in this variable's equality symbols), but reachability also requires us to model things like V = 1 precluding V = "1"
*/
lazy val implications = {
/** when we know V = C, which other equalities must hold
*
* in general, equality to some type implies equality to its supertypes
* (this multi-valued kind of equality is necessary for unreachability)
* note that we use subtyping as a model for implication between instanceof tests
* i.e., when S <:< T we assume x.isInstanceOf[S] implies x.isInstanceOf[T]
* unfortunately this is not true in general (see e.g. SI-6022)
*/
def implies(lower: Const, upper: Const): Boolean =
// values and null
lower == upper ||
// type implication
(lower != NullConst && !upper.isValue &&
instanceOfTpImplies(if (lower.isValue) lower.wideTp else lower.tp, upper.tp))
// if(r) debug.patmat("implies : "+(lower, lower.tp, upper, upper.tp))
// else debug.patmat("NOT implies: "+(lower, upper))
/** does V = C preclude V having value `other`?
(1) V = null is an exclusive assignment,
(2) V = A and V = B, for A and B value constants, are mutually exclusive unless A == B
we err on the safe side, for example:
- assume `val X = 1; val Y = 1`, then
(2: Int) match { case X => case Y => <falsely considered reachable> }
- V = 1 does not preclude V = Int, or V = Any, it could be said to preclude V = String, but we don't model that
(3) for types we could try to do something fancy, but be conservative and just say no
*/
def excludes(a: Const, b: Const): Boolean =
a != b && ((a == NullConst || b == NullConst) || (a.isValue && b.isValue))
// if(r) debug.patmat("excludes : "+(a, a.tp, b, b.tp))
// else debug.patmat("NOT excludes: "+(a, b))
/*
[ HALF BAKED FANCINESS: //!equalitySyms.exists(common => implies(common.const, a) && implies(common.const, b)))
when type tests are involved, we reason (conservatively) under a closed world assumption,
since we are really only trying to counter the effects of the symbols that we introduce to model type tests
we don't aim to model the whole subtyping hierarchy, simply to encode enough about subtyping to do unreachability properly
consider the following hierarchy:
trait A
trait B
trait C
trait AB extends B with A
// two types are mutually exclusive if there is no equality symbol whose constant implies both
object Test extends App {
def foo(x: Any) = x match {
case _ : C => println("C")
case _ : AB => println("AB")
case _ : (A with B) => println("AB'")
case _ : B => println("B")
case _ : A => println("A")
}
of course this kind of reasoning is not true in general,
but we can safely pretend types are mutually exclusive as long as there are no counter-examples in the match we're analyzing}
*/
val excludedPair = new mutable.HashSet[ExcludedPair]
case class ExcludedPair(a: Const, b: Const) {
override def equals(o: Any) = o match {
case ExcludedPair(aa, bb) => (a == aa && b == bb) || (a == bb && b == aa)
case _ => false
}
// make ExcludedPair(a, b).hashCode == ExcludedPair(b, a).hashCode
override def hashCode = a.hashCode ^ b.hashCode
}
equalitySyms map { sym =>
// if we've already excluded the pair at some point (-A \/ -B), then don't exclude the symmetric one (-B \/ -A)
// (nor the positive implications -B \/ A, or -A \/ B, which would entail the equality axioms falsifying the whole formula)
val todo = equalitySyms filterNot (b => (b.const == sym.const) || excludedPair(ExcludedPair(b.const, sym.const)))
val (excluded, notExcluded) = todo partition (b => excludes(sym.const, b.const))
val implied = notExcluded filter (b => implies(sym.const, b.const))
debug.patmat("eq axioms for: "+ sym.const)
debug.patmat("excluded: "+ excluded)
debug.patmat("implied: "+ implied)
excluded foreach { excludedSym => excludedPair += ExcludedPair(sym.const, excludedSym.const)}
(sym, implied, excluded)
}
}
// accessing after calling registerNull will result in inconsistencies
lazy val domainSyms: Option[Set[Sym]] = domain map { _ map symForEqualsTo }
lazy val symForStaticTp: Option[Sym] = symForEqualsTo.get(TypeConst(staticTpCheckable))
// don't access until all potential equalities have been registered using registerEquality
private lazy val equalitySyms = {observed(); symForEqualsTo.values.toList}
// don't call until all equalities have been registered and registerNull has been called (if needed)
def describe = {
def domain_s = domain match {
case Some(d) => d mkString (" ::= ", " | ", "// "+ symForEqualsTo.keys)
case _ => symForEqualsTo.keys mkString (" ::= ", " | ", " | ...")
}
s"$this: ${staticTp}${domain_s} // = $path"
}
override def toString = "V"+ id
}
import global.{ConstantType, Constant, SingletonType, Literal, Ident, singleType}
import global.definitions.{AnyClass, UnitClass}
// all our variables range over types
// a literal constant becomes ConstantType(Constant(v)) when the type allows it (roughly, anyval + string + null)
// equality between variables: SingleType(x) (note that pattern variables cannot relate to each other -- it's always patternVar == nonPatternVar)
object Const {
def resetUniques() = {_nextTypeId = 0; _nextValueId = 0; uniques.clear() ; trees.clear()}
private var _nextTypeId = 0
def nextTypeId = {_nextTypeId += 1; _nextTypeId}
private var _nextValueId = 0
def nextValueId = {_nextValueId += 1; _nextValueId}
private val uniques = new mutable.HashMap[Type, Const]
private[TreesAndTypesDomain] def unique(tp: Type, mkFresh: => Const): Const =
uniques.get(tp).getOrElse(
uniques.find {case (oldTp, oldC) => oldTp =:= tp} match {
case Some((_, c)) =>
debug.patmat("unique const: "+ (tp, c))
c
case _ =>
val fresh = mkFresh
debug.patmat("uniqued const: "+ (tp, fresh))
uniques(tp) = fresh
fresh
})
private val trees = mutable.HashSet.empty[Tree]
// hashconsing trees (modulo value-equality)
private[TreesAndTypesDomain] def uniqueTpForTree(t: Tree): Type =
// a new type for every unstable symbol -- only stable value are uniqued
// technically, an unreachable value may change between cases
// thus, the failure of a case that matches on a mutable value does not exclude the next case succeeding
// (and thuuuuus, the latter case must be considered reachable)
if (!t.symbol.isStable) t.tpe.narrow
else trees find (a => a.correspondsStructure(t)(sameValue)) match {
case Some(orig) =>
debug.patmat("unique tp for tree: "+ (orig, orig.tpe))
orig.tpe
case _ =>
// duplicate, don't mutate old tree (TODO: use a map tree -> type instead?)
val treeWithNarrowedType = t.duplicate setType t.tpe.narrow
debug.patmat("uniqued: "+ (t, t.tpe, treeWithNarrowedType.tpe))
trees += treeWithNarrowedType
treeWithNarrowedType.tpe
}
}
sealed abstract class Const {
def tp: Type
def wideTp: Type
def isAny = wideTp.typeSymbol == AnyClass
def isValue: Boolean //= tp.isStable
// note: use reference equality on Const since they're hash-consed (doing type equality all the time is too expensive)
// the equals inherited from AnyRef does just this
}
// find most precise super-type of tp that is a class
// we skip non-class types (singleton types, abstract types) so that we can
// correctly compute how types relate in terms of the values they rule out
// e.g., when we know some value must be of type T, can it still be of type S? (this is the positive formulation of what `excludes` on Const computes)
// since we're talking values, there must have been a class involved in creating it, so rephrase our types in terms of classes
// (At least conceptually: `true` is an instance of class `Boolean`)
private def widenToClass(tp: Type): Type =
if (tp.typeSymbol.isClass) tp
else tp.baseType(tp.baseClasses.head)
object TypeConst extends TypeConstExtractor {
def apply(tp: Type) = {
if (tp =:= NullTp) NullConst
else if (tp.isInstanceOf[SingletonType]) ValueConst.fromType(tp)
else Const.unique(tp, new TypeConst(tp))
}
def unapply(c: TypeConst): Some[Type] = Some(c.tp)
}
// corresponds to a type test that does not imply any value-equality (well, except for outer checks, which we don't model yet)
sealed class TypeConst(val tp: Type) extends Const {
assert(!(tp =:= NullTp))
/*private[this] val id: Int = */ Const.nextTypeId
val wideTp = widenToClass(tp)
def isValue = false
override def toString = tp.toString //+"#"+ id
}
// p is a unique type or a constant value
object ValueConst extends ValueConstExtractor {
def fromType(tp: Type) = {
assert(tp.isInstanceOf[SingletonType])
val toString = tp match {
case ConstantType(c) => c.escapedStringValue
case _ => tp.toString
}
Const.unique(tp, new ValueConst(tp, tp.widen, toString))
}
def apply(p: Tree) = {
val tp = p.tpe.normalize
if (tp =:= NullTp) NullConst
else {
val wideTp = widenToClass(tp)
val narrowTp =
if (tp.isInstanceOf[SingletonType]) tp
else p match {
case Literal(c) =>
if (c.tpe.typeSymbol == UnitClass) c.tpe
else ConstantType(c)
case Ident(_) if p.symbol.isStable =>
// for Idents, can encode uniqueness of symbol as uniqueness of the corresponding singleton type
// for Selects, which are handled by the next case, the prefix of the select varies independently of the symbol (see pos/virtpatmat_unreach_select.scala)
singleType(tp.prefix, p.symbol)
case _ =>
Const.uniqueTpForTree(p)
}
val toString =
if (hasStableSymbol(p)) p.symbol.name.toString // tp.toString
else p.toString //+"#"+ id
Const.unique(narrowTp, new ValueConst(narrowTp, checkableType(wideTp), toString)) // must make wide type checkable so that it is comparable to types from TypeConst
}
}
}
sealed class ValueConst(val tp: Type, val wideTp: Type, override val toString: String) extends Const {
// debug.patmat("VC"+(tp, wideTp, toString))
assert(!(tp =:= NullTp)) // TODO: assert(!tp.isStable)
/*private[this] val id: Int = */Const.nextValueId
def isValue = true
}
lazy val NullTp = ConstantType(Constant(null))
case object NullConst extends Const {
def tp = NullTp
def wideTp = NullTp
def isValue = true
override def toString = "null"
}
}
}
|