FOLTLTermFunctions.scala 5.47 KB
Newer Older
Christian Müller's avatar
Christian Müller committed
1
2
3
package de.tum.workflows.foltl

import FOLTL._
Christian Müller's avatar
Christian Müller committed
4
import com.typesafe.scalalogging.LazyLogging
Christian Müller's avatar
Christian Müller committed
5

Christian Müller's avatar
Christian Müller committed
6
object TermFunctions extends LazyLogging {
Christian Müller's avatar
Christian Müller committed
7
8
9
10
11
12
13

  def freeVars(t: Term) = {
    def free(t: Term, bound: Set[Var]): Set[Var] = {
      t match {
        // Extractors
        // Exists, Forall
        case Quantifier(_, ps, t)  => free(t, bound ++ ps)
Christian Müller's avatar
Christian Müller committed
14
        case UnOp(make, t)         => free(t, bound)
Christian Müller's avatar
Christian Müller committed
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
        // And, Or, Eq, Implies
        case BinOp(_, t1, t2)      => free(t1, bound) ++ free(t2, bound)

        case Fun(_, Some(ind), ps) => (ps.toSet -- bound) + Var(ind)
        case Fun(_, _, ps)         => (ps.toSet -- bound)
        case v: Var if !bound(v)   => Set(v)
        case x                     => Set()
      }
    }
    free(t, Set())
  }

  def simplify(t: Term): Term = {
    val simp: PartialFunction[Term, Term] = {
      // Push negation inward
      case Neg(Or(t1, t2))                        => And(Neg(t1), Neg(t2))
      case Neg(And(t1, t2))                       => Or(Neg(t1), Neg(t2))
      case Neg(Globally(t))                       => Finally(Neg(t))
      case Neg(Finally(t))                        => Globally(Neg(t))
      case Neg(Next(t))                           => Next(Neg(t))
      case Neg(Forall(vars, t))                   => Exists(vars, Neg(t))
      case Neg(Exists(vars, t))                   => Forall(vars, Neg(t))
      case Neg(Implies(t1, t2))                   => And(t1, Neg(t2))

      // Simple laws
      case Neg(True)                              => False
      case Neg(False)                             => True
42
      case Neg(Neg(t))                            => t
Christian Müller's avatar
Christian Müller committed
43
44
45
46
47
48
49
50
51
      case Or(True, t)                            => True
      case Or(t, True)                            => True
      case Or(False, t)                           => t
      case Or(t, False)                           => t
      case And(False, t)                          => False
      case And(t, False)                          => False
      case And(True, t)                           => t
      case And(t, True)                           => t

52
      // Equivalence
53
      case Eq(t1, t2) if t1 == t2                 => True
Christian Müller's avatar
Christian Müller committed
54

55
56
57
      // Double Temporals
      case Finally(Finally(t))                    => Finally(t)
      case Globally(Globally(t))                  => Globally(t)
Christian Müller's avatar
Christian Müller committed
58
59

      // Remove quantifiers if empty
60
61
62
      case Quantifier(_, xs, t) if xs.isEmpty => t
      case Quantifier(_, _, True)            => True
      case Quantifier(_, _, False)           => False
Christian Müller's avatar
Christian Müller committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

      // Remove variables from quantifiers if not used in the body
      //      case Quantifier(qmake, xs, t) if !(xs.toSet -- t.freeVars()).isEmpty =>
      //        qmake((xs.toSet.intersect(t.freeVars())).toList, t)
      //
      //      // Split off binops from quantifiers
      //      case Quantifier(qmake, xs, BinOp(make, t1, t2)) if (t1.freeVars().intersect(xs.toSet)).isEmpty =>
      //        make(t1, qmake(xs, t2))
      //      case Quantifier(qmake, xs, BinOp(make, t1, t2)) if (t2.freeVars().intersect(xs.toSet)).isEmpty =>
      //        make(qmake(xs, t1), t2)
    }

    val t1 = everywhere(simp, t)
    if (t == t1) t1 else simplify(t1)
  }

  def everywhere(trans: PartialFunction[Term, Term], t: Term): Term = {
    if (trans.isDefinedAt(t))
      trans(t)
    else
      t match {
        // Extractors
        case Quantifier(make, ps, t) => make(ps, everywhere(trans, t))
Christian Müller's avatar
Christian Müller committed
86
        case UnOp(make, t1)          => make(everywhere(trans, t1))
Christian Müller's avatar
Christian Müller committed
87
88
89
90
        case BinOp(make, t1, t2)     => make(everywhere(trans, t1), everywhere(trans, t2))
        case x                       => x
      }
  }
91
92
93
94
95
96
97
98
99
100

  def opsize(t: Term): Int = {
    t match {
      // Extractors
      case Quantifier(_, ps, t) => 1 + t.opsize()
      case UnOp(_, t)           => 1 + t.opsize()
      case BinOp(_, t1, t2)     => 1 + t1.opsize() + t2.opsize()
      case x                    => 1
    }
  }
Christian Müller's avatar
Christian Müller committed
101
  
102
  def collect[T](combine: (T*) => T, empty: T)(coll: PartialFunction[Term, T], t: Term): T = {
Christian Müller's avatar
Christian Müller committed
103
104
105
106
107
108
109
110
    if (coll.isDefinedAt(t))
      coll(t)
    else
      t match {
        // Extractors
        case Quantifier(make, ps, t) => collect(combine, empty)(coll, t)
        case UnOp(make, t)           => collect(combine, empty)(coll, t)
        case BinOp(make, t1, t2)     => combine(collect(combine, empty)(coll, t1), collect(combine, empty)(coll, t2))
111
112
113
        case x                       => empty
      }
  }
114
115

  def collect[T](coll: PartialFunction[Term, List[T]], t: Term): List[T] = {
116
117
118
119
120
121
122
123
124
    if (coll.isDefinedAt(t))
      coll(t)
    else
      t match {
        // Extractors
        case Quantifier(make, ps, t) => collect(coll, t)
        case UnOp(make, t)           => collect(coll, t)
        case BinOp(make, t1, t2)     => List.concat(collect(coll, t1), collect(coll, t2))
        case x                       => List.empty
Christian Müller's avatar
Christian Müller committed
125
126
      }
  }
127

Christian Müller's avatar
Christian Müller committed
128
129
130
131
132
133
134
135
136
137
138
  def assumeEmpty(t: Term, name: String) = {
    t.everywhere({
      case Fun(f, _, _) if f == name => False
    })
  }

  def annotate(t: Term, name: String) = {
    t.everywhere({
      case Fun(f, None, xs) => Fun(f, Some(name), xs)
    })
  }
139
140

  def annotate(t: Term, name: String, ignore: Set[String]) = {
141
142
143
144
    t.everywhere({
      case Fun(f, None, xs) if (!(ignore contains f)) => Fun(f, Some(name), xs)
    })
  }
145

Christian Müller's avatar
Christian Müller committed
146
147
148
149
150
151
152
  def checkSanity(t: Term) = {
    // TODO add more things, f.e. existentials
    // T1, T2 can appear freely
    val frees = t.freeVars() -- Set(Var(Properties.T1), Var(Properties.T2))
    if (!frees.isEmpty) {
      logger.warn(s"Sanity check failed: $frees appear free in the property.")
    }
153

Christian Müller's avatar
Christian Müller committed
154
155
    frees.isEmpty
  }
Christian Müller's avatar
Christian Müller committed
156
}