Attic/programs/prove.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Sat, 27 Dec 2014 10:54:16 +0000
changeset 362 bed460c26522
parent 198 2ce98ee39990
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
198
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
abstract class Term
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
case class Var(s: String) extends Term
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
case class Fun(s: String, ts: List[Term]) extends Term
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
abstract class Form
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
object True extends Form
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
object False extends Form
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
case class Pred(s: String, ts: List[Term]) extends Form
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
case class Imp(f1: Form, f2: Form) extends Form
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
case class Says(p: String, f: Form) extends Form
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
case class And(f1: Form, f2: Form) extends Form
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
case class Or(f1: Form, f2: Form) extends Form
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
case class Controls(p: String, f: Form) extends Form
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
case class Judgement(Gamma: List[Form], F: Form) {
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
  def lhs = Gamma
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
  def rhs = F
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
}
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
val Alice = "Alice"
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
val Bob = "Bob"
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
val Send = Pred("Send", Nil)
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
val Gamma = 
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
  List( Imp(Says(Bob, Send), Send),
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
        Says(Bob, Imp(Says(Alice, Send), Send)),
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
        Says(Alice, Send) )
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
val goal = Judgement(Gamma, Send)
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
def sc () = { println ("Yes!") }
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
class Main {
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
def prove(j: Judgement, sc: () => Unit) : Unit = j match {
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
  case Judgement(lhs, rhs) => 
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
    { if (lhs.exists(f => f == rhs))  sc () 
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
      else prove1(lhs, rhs, sc) 
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
    }
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
}
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
def partitions [A] (l: List[A]): List[(A, List[A])]  = 
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
  l.map (s => (s, l - s))
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
def prove1(lhs: List[Form], rhs: Form, sc: () => Unit) : Unit = 
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
  rhs match {
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
    case Imp(f1, f2) => prove(Judgement(f1::lhs, f2), sc)
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
    /*case Says(p, f) => prove(Judgement(lhs, f), sc)*/
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
    /*case Controls(p, f) => prove(Judgement(lhs, f), sc)*/
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
    case Or(f1, f2) => { prove(Judgement(lhs, f1), sc);
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
                         prove(Judgement(lhs, f2), sc) }
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
    case And(f1, f2) => prove(Judgement(lhs, f1), 
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
                            () => prove(Judgement(lhs, f2), sc))
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
    case _ => { for ((f, lhs_rest) <- partitions(lhs))
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
                  prove2(f, lhs_rest, rhs, sc) }
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
  }
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
def prove2(f: Form, lhs_rest: List[Form], rhs: Form, sc: () => Unit) : Unit = 
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
  f match {
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
    case Imp(f1, f2) => 
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
      prove(Judgement(lhs_rest, f1), 
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
            () => prove(Judgement(f2::lhs_rest, rhs), sc))
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
    case Says(p, Imp(f1, f2)) => 
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
      prove(Judgement(lhs_rest, Says(p, f1)), 
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
            () => prove(Judgement(Says(p, f2)::lhs_rest, rhs), sc)) 
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
    case Controls(p, f) =>
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
      prove(Judgement(lhs_rest, Says(p, f)),
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
            () => prove(Judgement(f::lhs_rest, rhs), sc))
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
    case _ => ()
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
  }
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
}
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
  
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
val main = new Main
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
val Foo = Pred("Foo", Nil)
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
main.prove (Judgement (Gamma, And(Foo, Send)), sc)
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
main.prove (Judgement (Nil, Foo), sc)
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
main.prove (Judgement (Nil, Imp(Send, Send)), sc)
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
main.prove (Judgement (Gamma, Send), sc)
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
main.prove (Judgement (Gamma, Foo), sc)
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
val F1 = Imp(Says(Bob, Send), Send)
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
val F2 = Says(Bob, Imp(Says(Alice, Send), Send))
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
val F3 = Says(Alice, Send)
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
main.prove (Judgement (Nil, Imp(F1, Imp(F2, Imp(F3, Send)))), sc)
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
val Server = "Server"
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
def Sends(p: String, q: String, f: Form) : Form =
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
  Imp(Says(p, f), Says(q, f))
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
def Enc(f: Form, k: Form) : Form = Imp(k, f)
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
def Connect(p: String, q: String) : Form =
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
  Pred("Connect", List(Var(p), Var(q)))
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
val Msg = Pred("Msg", Nil)
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
val Kas = Pred("Kas", Nil)
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
val Kbs = Pred("Kbs", Nil)
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
val Kab = Pred("Kab", Nil)
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   105
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
val Gamma_big = 
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107
  List( Says(Alice, Kas),
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
        Says(Bob, Kbs),
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
        Says(Alice, Msg),
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   110
        Says(Alice, Connect(Alice, Bob)),
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   111
        Sends(Alice, Server, Connect(Alice, Bob)),
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   112
        Says(Server, Imp(Connect(Alice, Bob), Enc(Kab, Kas))),
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   113
        Says(Server, Imp(Connect(Alice, Bob), Enc(Enc(Kab, Kbs), Kas))),
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   114
        Sends(Server, Alice, Enc(Kab, Kas)),
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   115
        Sends(Server, Alice, Enc(Enc(Kab, Kbs), Kas)),
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   116
        Sends(Alice, Bob, Enc(Kab, Kbs)),
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   117
        Sends(Alice, Bob, Enc(Msg, Kab))
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   118
      )
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   119
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   120
2ce98ee39990 reorganised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   121
main.prove (Judgement(Gamma_big, Says(Bob, Msg)), sc)