| author | Christian Urban <christian dot urban at kcl dot ac dot uk> | 
| Tue, 25 Nov 2014 02:04:48 +0000 | |
| changeset 331 | 54a1fbe96b14 | 
| parent 198 | 2ce98ee39990 | 
| permissions | -rw-r--r-- | 
| 131 | 1 | import scala.language.implicitConversions | 
| 2 | import scala.language.reflectiveCalls | |
| 3 | import scala.util._ | |
| 4 | ||
| 5 | abstract class Term | |
| 6 | case class Var(s: String) extends Term | |
| 7 | case class Const(s: String) extends Term | |
| 8 | case class Fun(s: String, ts: List[Term]) extends Term | |
| 9 | ||
| 10 | abstract class Form | |
| 11 | case object True extends Form | |
| 12 | case object False extends Form | |
| 13 | case class Pred(s: String, ts: List[Term]) extends Form | |
| 14 | case class Imp(f1: Form, f2: Form) extends Form | |
| 15 | case class Says(p: String, f: Form) extends Form | |
| 16 | case class And(f1: Form, f2: Form) extends Form | |
| 17 | case class Or(f1: Form, f2: Form) extends Form | |
| 135 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 18 | case class Sends(p: String, q: String, f: Form) extends Form | 
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 19 | case class Enc(f1: Form, f2: Form) extends Form | 
| 131 | 20 | |
| 21 | case class Judgement(gamma: Set[Form], f: Form) {
 | |
| 22 | def lhs = gamma | |
| 23 | def rhs = f | |
| 24 | } | |
| 25 | ||
| 26 | // some syntactic sugar | |
| 27 | implicit def FormOps(f1: Form) = new {
 | |
| 28 | def -> (f2: Form) = Imp(f1, f2) | |
| 29 | } | |
| 30 | implicit def StringOps(p: String) = new {
 | |
| 31 | def says (f: Form) = Says(p, f) | |
| 32 | } | |
| 33 | implicit def SetFormOps(gamma: Set[Form]) = new {
 | |
| 34 | def |- (f: Form) : Judgement = Judgement(gamma, f) | |
| 35 | } | |
| 36 | ||
| 37 | val Admin = "Admin" | |
| 38 | val Bob = "Bob" | |
| 39 | val Del = Pred("del_file", Nil)
 | |
| 40 | ||
| 41 | val Gamma: Set[Form] = | |
| 42 | Set( (Admin says Del) -> Del, | |
| 43 | Admin says ((Bob says Del) -> Del), | |
| 44 | Bob says Del ) | |
| 45 | ||
| 46 | val goal = Gamma |- Del // request: provable or not? | |
| 47 | ||
| 48 | def partitions[A](s: Set[A]): Set[(A, Set[A])] = | |
| 49 | s.map (e => (e, s - e)) | |
| 50 | ||
| 51 | ||
| 52 | def prove(j: Judgement, sc: () => Unit) : Unit = {
 | |
| 53 | if (j.lhs.contains(j.rhs)) sc () // Axiom rule | |
| 54 |   else { 
 | |
| 55 | prove1(j, sc); | |
| 56 | for ((f, lhs_rest) <- partitions(j.lhs)) prove2(f, lhs_rest, j.rhs, sc) | |
| 57 | } | |
| 58 | } | |
| 59 | ||
| 60 | def prove1(j: Judgement, sc: () => Unit) : Unit = | |
| 61 |   j.rhs match {
 | |
| 62 | case True => sc () | |
| 63 | case False => () | |
| 64 | case Imp(f1, f2) => prove(j.lhs + f1 |- f2, sc) | |
| 136 
058504a45c34
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
135diff
changeset | 65 | case Says(p, Enc(f1, f2)) => | 
| 
058504a45c34
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
135diff
changeset | 66 | prove(j.lhs |- Says(p, f1), | 
| 
058504a45c34
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
135diff
changeset | 67 | () => prove(j.lhs |- Says(p, f2), sc)) | 
| 131 | 68 | case Says(p, f1) => prove(j.lhs |- f1, sc) | 
| 69 | case Or(f1, f2) => | |
| 70 |       { prove(j.lhs |- f1, sc);
 | |
| 71 | prove(j.lhs |- f2, sc) } | |
| 72 | case And(f1, f2) => | |
| 73 | prove(j.lhs |- f1, | |
| 74 | () => prove(j.lhs |- f2, sc)) | |
| 135 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 75 | case Sends(p, q, f) => prove(j.lhs + (p says f) |- (q says f), sc) | 
| 131 | 76 | case _ => () | 
| 77 | } | |
| 78 | ||
| 79 | def prove2(f: Form, lhs_rest: Set[Form], rhs: Form, sc: () => Unit) : Unit = | |
| 80 |   f match {
 | |
| 81 | case True => prove(lhs_rest |- rhs, sc) | |
| 82 | case False => sc () | |
| 83 | case And(f1, f2) => | |
| 84 | prove(lhs_rest + f1 + f2 |- rhs, sc) | |
| 85 | case Imp(f1, f2) => | |
| 86 | prove(lhs_rest |- f1, | |
| 87 | () => prove(lhs_rest + f2 |- rhs, sc)) | |
| 135 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 88 | case Sends(p, q, f) => | 
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 89 | prove(lhs_rest |- (p says f), | 
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 90 | () => prove(lhs_rest + (q says f) |- rhs, sc)) | 
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 91 | case Enc(f1, f2) => | 
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 92 | prove(lhs_rest |- f1, | 
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 93 | () => prove(lhs_rest + f2 |- rhs, sc)) | 
| 131 | 94 | case Or(f1, f2) => | 
| 95 | prove(lhs_rest + f1 |- rhs, | |
| 96 | () => prove(lhs_rest + f2 |- rhs, sc)) | |
| 136 
058504a45c34
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
135diff
changeset | 97 | case Says(p, Enc(f1, f2)) => | 
| 
058504a45c34
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
135diff
changeset | 98 | prove(lhs_rest |- Says(p, f2), | 
| 
058504a45c34
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
135diff
changeset | 99 | () => prove(lhs_rest + Says(p, f1) |- rhs, sc)) | 
| 131 | 100 | case Says(p, Imp(f1, f2)) => | 
| 101 | prove(lhs_rest |- Says(p, f1), | |
| 135 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 102 | () => prove(lhs_rest + Says(p, f2) |- rhs, sc)) | 
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 103 | |
| 131 | 104 | case _ => () | 
| 105 | } | |
| 106 | ||
| 107 | // function that calls prove and returns immediately once a proof is found | |
| 108 | def run (j : Judgement) : Unit = {
 | |
| 109 |   def sc () = { println ("Yes!"); throw new Exception }
 | |
| 110 | Try(prove(j, sc)) getOrElse () | |
| 111 | } | |
| 112 | ||
| 113 | run (goal) | |
| 114 | ||
| 115 | run (Set[Form]() |- False -> Del) | |
| 116 | run (Set[Form]() |- True -> Del) | |
| 117 | run (Set[Form]() |- Del -> True) | |
| 118 | run (Set[Form]() |- Del -> Del) | |
| 119 | run (Set[Form]() |- Del -> Or(False, Del)) | |
| 120 | ||
| 121 | ||
| 122 | val Gamma1 : Set[Form] = | |
| 123 | Set( Admin says ((Bob says Del) -> Del), | |
| 124 | Bob says Del ) | |
| 125 | ||
| 126 | val goal1 = Gamma1 |- Del // not provable | |
| 127 | run (goal1) | |
| 128 | ||
| 129 | ||
| 130 | val f1 = "P" says Pred("F1", Nil)
 | |
| 131 | val f2 = "Q" says Pred("F2", Nil)
 | |
| 132 | run (Set[Form](And(f1, f2)) |- And(f2, f1)) | |
| 133 | ||
| 134 | ||
| 135 | val Chr = "Christian" | |
| 136 | val HoD = "Peter" | |
| 132 
53e24ca037ce
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
131diff
changeset | 137 | val Email = Pred("may_obtain_email", List(Const(Chr)))
 | 
| 131 | 138 | val AtLib = Pred("is_at_library", List(Const(Chr)))
 | 
| 139 | val Chr_Staff = Pred("is_staff", List(Const(Chr)))
 | |
| 140 | ||
| 141 | val Policy_HoD = (HoD says Chr_Staff) -> Chr_Staff | |
| 142 | val Policy_Lib = And(Chr_Staff, AtLib) -> Email | |
| 143 | val HoD_says = HoD says Chr_Staff | |
| 144 | ||
| 132 
53e24ca037ce
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
131diff
changeset | 145 | run (Set[Form](AtLib, Policy_HoD, Policy_Lib) |- Email) | 
| 131 | 146 | |
| 135 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 147 | println("Server Example")
 | 
| 131 | 148 | |
| 135 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 149 | def Connect(p: String, q: String) : Form = | 
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 150 |   Pred("Connect", List(Var(p), Var(q)))
 | 
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 151 | |
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 152 | |
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 153 | val A = "A" | 
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 154 | val B = "B" | 
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 155 | val S = "S" | 
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 156 | val CAB = Connect(A, B) | 
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 157 | val Msg = Pred("Msg", Nil)
 | 
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 158 | val KAS = Pred("Kas", Nil)
 | 
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 159 | val KBS = Pred("Kbs", Nil)
 | 
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 160 | val KAB = Pred("Kab", Nil)
 | 
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 161 | |
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 162 | val Gamma_big : Set[Form] = | 
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 163 | Set( A says CAB, | 
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 164 | Sends(A, S, CAB), | 
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 165 | S says (CAB -> Enc(KAB, KAS)), | 
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 166 | S says (CAB -> Enc(Enc(KAB, KBS), KAS)), | 
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 167 | Sends(S, A, Enc(KAB, KAS)), | 
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 168 | Sends(S, A, Enc(Enc(KAB, KBS), KAS)), | 
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 169 | Sends(A, B, Enc(KAB, KBS)), | 
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 170 | Sends(A, B, Enc(Msg, KAB)), | 
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 171 | A says KAS, | 
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 172 | B says KBS, | 
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 173 | S says KAS, | 
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 174 | A says (Enc(Msg, KAB)) | 
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 175 | ) | 
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 176 | |
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 177 | run (Gamma_big |- (B says Msg)) | 
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 178 | |
| 
e78af5feb655
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
132diff
changeset | 179 |