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

Christian Müller's avatar
Christian Müller committed
3
import de.tum.workflows.Utils
Christian Müller's avatar
Christian Müller committed
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
//import de.tum.workflows.foltl.TermFunctions._

object FOLTL {
  abstract class Term {
    def simplify() = TermFunctions.simplify(this)
    def freeVars() = TermFunctions.freeVars(this)
    def everywhere(trans: PartialFunction[Term, Term]) = TermFunctions.everywhere(trans, this)
    def assumeEmpty(name: String) = TermFunctions.assumeEmpty(this, name)
    def annotate(name:String) = TermFunctions.annotate(this, name)
    
    def bracketed():String = this match {
      case _:BinOp => "(" + this + ")"
      case _ => this.toString()
    }
  }

  case object True extends Term
  case object False extends Term

  case class Var(name: String) extends Term {
    override def toString() = name
  }
  
  object Fun {
    def apply(name: String, params: List[Var]):Fun = Fun(name, None, params)
  }
  case class Fun(name: String, ind:Option[String], params: List[Var]) extends Term {
Christian Müller's avatar
Christian Müller committed
31
    override def toString() = name + Utils.mkString(ind, "(", "", ")") + params.mkString("(", ",", ")")
Christian Müller's avatar
Christian Müller committed
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
  }

  trait Quantifier extends Term {
    def qmake: (List[Var], Term) => Quantifier
    def qname: String
    def vars: List[Var]
    def t: Term
    
    override def toString() = {
      qname + " " + vars.mkString(",") + ". " + t.bracketed()
    }
  }
  
  object Quantifier {
    def unapply(q: Quantifier) = Some((q.qmake, q.vars, q.t))
  }

  case class Exists(vars: List[Var], t: Term) extends Quantifier {
    val qname = "∃"
    val qmake = Exists.apply _
  }
  case class Forall(vars: List[Var], t: Term) extends Quantifier {
    val qname = "∀"
    val qmake = Forall.apply _
  }

  trait TemporalTerm extends Term

  case class Next(t:Term) extends TemporalTerm {
    override def toString() = {
      "X " + t.bracketed()
    }
  }
  case class Globally(t: Term) extends TemporalTerm {
    override def toString() = {
      "G " + t.bracketed()
    }
  }
  case class Until(t1: Term, t2: Term) extends TemporalTerm {
    override def toString() = {
      t1.bracketed() + " U " + t2.bracketed()
    }
  }
  case class Finally(t: Term) extends TemporalTerm {
    override def toString() = {
      "F " + t.bracketed()
    }
  }

  trait BinOp extends Term {
    def make:(Term, Term) => BinOp
    def opname: String
    def t1: Term
    def t2: Term 
    
    override def toString() = {
      (t1, t2) match {
        case (t1: BinOp, t2:BinOp) if (opname == t1.opname == t2.opname) => t1 + " " + opname + " " + t2
        case (t1: BinOp, _) if (opname == t1.opname) => t1 + " " + opname + " " + t2.bracketed()
        case (_, t2:BinOp) if (opname == t2.opname) => t1.bracketed() + " " + opname + " " + t2
        case _ => t1.bracketed() + " " + opname + " " + t2.bracketed()
      }
    }
  }
  
  object BinOp {
    def unapply(binop: BinOp) = {
      Some((binop.make, binop.t1, binop.t2))
    }
       
    def makeL(make:(Term, Term) => BinOp, l: List[Term]):Term = {
      l match {
        case List(x) => x
        case x :: xs => make(x, makeL(make, xs))
      }
    }
  }
  
  case class Or(t1: Term, t2: Term) extends BinOp {
    val opname = "∨"
    val make = Or.apply _
  }
  
  case class And(t1: Term, t2: Term) extends BinOp {
    val opname = "∧"
    val make = And.apply _
  }
  case class Eq(t1: Term, t2:Term) extends BinOp {
    val opname = "↔"
    val make = Eq.apply _
  }
  case class Implies(t1: Term, t2: Term) extends BinOp {
    val opname = "→"
    val make = Implies.apply _
  }

  trait UnOp extends Term {
    def opname: String
    def t: Term
    
    override def toString() = opname + t.bracketed
  }

  case class Neg(t: Term) extends UnOp {
    val opname = "¬"
  }

}