author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Thu, 26 Nov 2015 14:54:45 +0000 | |
changeset 438 | 85a25932811f |
parent 198 | 2ce98ee39990 |
permissions | -rw-r--r-- |
129
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1 |
import scala.language.implicitConversions |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
2 |
import scala.language.reflectiveCalls |
131
d35b2ee2e788
add
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
130
diff
changeset
|
3 |
import scala.util._ |
129
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
4 |
|
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
abstract class Term |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
case class Var(s: String) extends Term |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
case class Const(s: String) extends Term |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
case class Fun(s: String, ts: List[Term]) extends Term |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
|
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
abstract class Form |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
11 |
case object True extends Form |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
12 |
case object False extends Form |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
13 |
case class Pred(s: String, ts: List[Term]) extends Form |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
14 |
case class Imp(f1: Form, f2: Form) extends Form |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
15 |
case class Says(p: String, f: Form) extends Form |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
16 |
case class And(f1: Form, f2: Form) extends Form |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
17 |
case class Or(f1: Form, f2: Form) extends Form |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
18 |
|
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
19 |
case class Judgement(gamma: Set[Form], f: Form) { |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
20 |
def lhs = gamma |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
21 |
def rhs = f |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
22 |
} |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
23 |
|
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
24 |
// some syntactic sugar |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
25 |
implicit def FormOps(f1: Form) = new { |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
26 |
def -> (f2: Form) = Imp(f1, f2) |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
27 |
} |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
28 |
implicit def StringOps(p: String) = new { |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
29 |
def says (f: Form) = Says(p, f) |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
30 |
} |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
31 |
implicit def SetFormOps(gamma: Set[Form]) = new { |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
32 |
def |- (f: Form) : Judgement = Judgement(gamma, f) |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
33 |
} |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
34 |
|
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
35 |
val Admin = "Admin" |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
36 |
val Bob = "Bob" |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
37 |
val Del = Pred("del_file", Nil) |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
38 |
|
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
39 |
val Gamma: Set[Form] = |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
40 |
Set( (Admin says Del) -> Del, |
130
4e8482e50590
more slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
129
diff
changeset
|
41 |
Admin says ((Bob says Del) -> Del), |
4e8482e50590
more slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
129
diff
changeset
|
42 |
Bob says Del ) |
129
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
43 |
|
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
44 |
val goal = Gamma |- Del // request: provable or not? |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
45 |
|
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
46 |
def partitions[A](s: Set[A]): Set[(A, Set[A])] = |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
47 |
s.map (e => (e, s - e)) |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
48 |
|
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
49 |
|
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
50 |
def prove(j: Judgement, sc: () => Unit) : Unit = { |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
51 |
if (j.lhs.contains(j.rhs)) sc () // Axiom rule |
130
4e8482e50590
more slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
129
diff
changeset
|
52 |
else prove1(j, sc) |
129
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
53 |
} |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
54 |
|
130
4e8482e50590
more slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
129
diff
changeset
|
55 |
def prove1(j: Judgement, sc: () => Unit) : Unit = |
4e8482e50590
more slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
129
diff
changeset
|
56 |
j.rhs match { |
129
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
57 |
case True => sc () |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
58 |
case False => () |
130
4e8482e50590
more slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
129
diff
changeset
|
59 |
case Imp(f1, f2) => prove(j.lhs + f1 |- f2, sc) |
4e8482e50590
more slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
129
diff
changeset
|
60 |
case Says(p, f1) => prove(j.lhs |- f1, sc) |
129
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
61 |
case Or(f1, f2) => |
130
4e8482e50590
more slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
129
diff
changeset
|
62 |
{ prove(j.lhs |- f1, sc); |
4e8482e50590
more slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
129
diff
changeset
|
63 |
prove(j.lhs |- f2, sc) } |
129
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
64 |
case And(f1, f2) => |
130
4e8482e50590
more slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
129
diff
changeset
|
65 |
prove(j.lhs |- f1, |
4e8482e50590
more slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
129
diff
changeset
|
66 |
() => prove(j.lhs |- f2, sc)) |
4e8482e50590
more slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
129
diff
changeset
|
67 |
case _ => { for ((f, lhs_rest) <- partitions(j.lhs)) |
4e8482e50590
more slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
129
diff
changeset
|
68 |
prove2(f, lhs_rest, j.rhs, sc) } |
129
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
69 |
} |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
70 |
|
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
71 |
def prove2(f: Form, lhs_rest: Set[Form], rhs: Form, sc: () => Unit) : Unit = |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
72 |
f match { |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
73 |
case True => prove(lhs_rest |- rhs, sc) |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
74 |
case False => sc () |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
75 |
case And(f1, f2) => |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
76 |
prove(lhs_rest + f1 + f2 |- rhs, sc) |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
77 |
case Imp(f1, f2) => |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
78 |
prove(lhs_rest |- f1, |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
79 |
() => prove(lhs_rest + f2 |- rhs, sc)) |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
80 |
case Or(f1, f2) => |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
81 |
prove(lhs_rest + f1 |- rhs, |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
82 |
() => prove(lhs_rest + f2 |- rhs, sc)) |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
83 |
case Says(p, Imp(f1, f2)) => |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
84 |
prove(lhs_rest |- Says(p, f1), |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
85 |
() => prove(lhs_rest + Says(p, f2) |- rhs, sc)) |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
86 |
case _ => () |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
87 |
} |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
88 |
|
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
89 |
// function that calls prove and returns immediately once a proof is found |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
90 |
def run (j : Judgement) : Unit = { |
131
d35b2ee2e788
add
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
130
diff
changeset
|
91 |
def sc () = { println ("Yes!"); throw new Exception } |
d35b2ee2e788
add
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
130
diff
changeset
|
92 |
Try(prove(j, sc)) getOrElse () |
129
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
93 |
} |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
94 |
|
131
d35b2ee2e788
add
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
130
diff
changeset
|
95 |
run (goal) |
d35b2ee2e788
add
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
130
diff
changeset
|
96 |
|
129
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
97 |
run (Set[Form]() |- False -> Del) |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
98 |
run (Set[Form]() |- True -> Del) |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
99 |
run (Set[Form]() |- Del -> True) |
131
d35b2ee2e788
add
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
130
diff
changeset
|
100 |
run (Set[Form]() |- Del -> Del) |
d35b2ee2e788
add
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
130
diff
changeset
|
101 |
run (Set[Form]() |- Del -> Or(False, Del)) |
129
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
102 |
|
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
103 |
|
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
104 |
val Gamma1 : Set[Form] = |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
105 |
Set( Admin says ((Bob says Del) -> Del), |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
106 |
Bob says Del ) |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
107 |
|
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
108 |
val goal1 = Gamma1 |- Del // not provable |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
109 |
run (goal1) |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
110 |
|
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
111 |
|
131
d35b2ee2e788
add
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
130
diff
changeset
|
112 |
val f1 = Pred("F1", Nil) |
d35b2ee2e788
add
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
130
diff
changeset
|
113 |
val f2 = Pred("F2", Nil) |
d35b2ee2e788
add
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
130
diff
changeset
|
114 |
run (Set[Form](And(f1, f2)) |- And(f2, f1)) |
129
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
115 |
|
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
116 |
|
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
117 |
val Chr = "Christian" |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
118 |
val HoD = "Peter" |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
119 |
val Email = Pred("may_btain_email", List(Const(Chr))) |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
120 |
val AtLib = Pred("is_at_library", List(Const(Chr))) |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
121 |
val Chr_Staff = Pred("is_staff", List(Const(Chr))) |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
122 |
|
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
123 |
val Policy_HoD = (HoD says Chr_Staff) -> Chr_Staff |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
124 |
val Policy_Lib = And(Chr_Staff, AtLib) -> Email |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
125 |
val HoD_says = HoD says Chr_Staff |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
126 |
|
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
127 |
run (Set[Form](AtLib, Policy_HoD, Policy_Lib, HoD_says) |- Email) |
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
128 |
|
10526c967679
added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
129 |