FOLTLTermFunctions.scala 5.23 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
53
      // Equivalence
      case Eq(t1, t2)                 if t1 == t2 => True
Christian Müller's avatar
Christian Müller committed
54

55
56
57
58
      
      // Double Temporals
      case Finally(Finally(t))                    => Finally(t)
      case Globally(Globally(t))                  => Globally(t)
Christian Müller's avatar
Christian Müller committed
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

      // Remove quantifiers if empty
      case Quantifier(qmake, xs, t) if xs.isEmpty => t
      case Quantifier(qmake, xs, True)            => True
      case Quantifier(qmake, xs, False)           => False

      // 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
87
        case UnOp(make, t1)          => make(everywhere(trans, t1))
Christian Müller's avatar
Christian Müller committed
88
89
90
91
92
        case BinOp(make, t1, t2)     => make(everywhere(trans, t1), everywhere(trans, t2))
        case x                       => x
      }
  }
  
93
  def collect[T](combine:(T*) => T, empty:T)(coll: PartialFunction[Term, T], t: Term): T = {
Christian Müller's avatar
Christian Müller committed
94
95
96
97
98
99
100
101
    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))
102
103
104
105
106
107
108
109
110
111
112
113
114
115
        case x                       => empty
      }
  }
  
   def collect[T](coll: PartialFunction[Term, List[T]], t: Term): List[T] = {
    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
116
117
118
      }
  }
  
Christian Müller's avatar
Christian Müller committed
119
120
121
122
123
124
125
126
127
128
129
  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)
    })
  }
130
131
132
133
134
135
  
  def annotate(t: Term, name: String, ignore:Set[String]) = {
    t.everywhere({
      case Fun(f, None, xs) if (!(ignore contains f)) => Fun(f, Some(name), xs)
    })
  }
Christian Müller's avatar
Christian Müller committed
136
137
138
139
140
141
142
143
144
145
146
  
  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.")
    }
    
    frees.isEmpty
  }
Christian Müller's avatar
Christian Müller committed
147
}