59
|
1 |
|
60
|
2 |
abstract class Term
|
59
|
3 |
case class Var(s: String) extends Term
|
|
4 |
case class Const(s: String) extends Term
|
|
5 |
case class Fun(s: String, ts: List[Term]) extends Term
|
|
6 |
|
60
|
7 |
abstract class Form {
|
|
8 |
def -> (that: Form) = Imp(this, that)
|
|
9 |
}
|
59
|
10 |
case object True extends Form
|
|
11 |
case object False extends Form
|
|
12 |
case class Pred(s: String, ts: List[Term]) extends Form
|
|
13 |
case class Imp(f1: Form, f2: Form) extends Form
|
|
14 |
case class Says(p: String, f: Form) extends Form
|
|
15 |
case class And(f1: Form, f2: Form) extends Form
|
|
16 |
case class Or(f1: Form, f2: Form) extends Form
|
|
17 |
|
|
18 |
case class Judgement(Gamma: List[Form], F: Form) {
|
|
19 |
def lhs = Gamma
|
|
20 |
def rhs = F
|
|
21 |
}
|
|
22 |
|
|
23 |
val Admin = "Admin"
|
|
24 |
val Bob = "Bob"
|
|
25 |
val Del = Pred("del_file", Nil)
|
|
26 |
|
|
27 |
|
|
28 |
val Gamma =
|
60
|
29 |
List( Says(Admin, Del) -> Del,
|
|
30 |
Says(Admin, Says(Bob, Del) -> Del),
|
59
|
31 |
Says(Bob, Del) )
|
|
32 |
|
|
33 |
val goal = Judgement(Gamma, Del) // request: provable or not?
|
|
34 |
|
|
35 |
def prove(j: Judgement, sc: () => Unit) : Unit = {
|
|
36 |
if (j.lhs.contains(j.rhs)) sc() // Axiom rule
|
|
37 |
else prove1(j.lhs, j.rhs, sc)
|
|
38 |
}
|
|
39 |
|
|
40 |
def partitions(ls: List[Form]): List[(Form, List[Form])] =
|
|
41 |
ls.map (s => (s, ls - s))
|
|
42 |
|
|
43 |
def prove1(lhs: List[Form], rhs: Form, sc: () => Unit) : Unit =
|
|
44 |
rhs match {
|
|
45 |
case Imp(f1, f2) => prove(Judgement(f1::lhs, f2), sc)
|
|
46 |
case Says(p, f1) => prove(Judgement(lhs, f1), sc)
|
|
47 |
case Or(f1, f2) =>
|
|
48 |
{ prove(Judgement(lhs, f1), sc);
|
|
49 |
prove(Judgement(lhs, f2), sc) }
|
|
50 |
case And(f1, f2) =>
|
|
51 |
prove(Judgement(lhs, f1),
|
|
52 |
() => prove(Judgement(lhs, f2), sc))
|
|
53 |
case _ => { for ((f, lhs_rest) <- partitions(lhs))
|
|
54 |
prove2(f, lhs_rest, rhs, sc) }
|
|
55 |
}
|
|
56 |
|
|
57 |
def prove2(f: Form, lhs_rest: List[Form], rhs: Form, sc: () => Unit) : Unit =
|
|
58 |
f match {
|
|
59 |
case And(f1, f2) =>
|
|
60 |
prove(Judgement(f1::f2::lhs_rest, rhs), sc)
|
|
61 |
case Imp(f1, f2) =>
|
|
62 |
prove(Judgement(lhs_rest, f1),
|
|
63 |
() => prove(Judgement(f2::lhs_rest, rhs), sc))
|
|
64 |
case Or(f1, f2) =>
|
|
65 |
prove(Judgement(f1::lhs_rest, rhs),
|
|
66 |
() => prove(Judgement(f2::lhs_rest, rhs), sc))
|
|
67 |
case Says(p, Imp(f1, f2)) =>
|
|
68 |
prove(Judgement(lhs_rest, Says(p, f1)),
|
|
69 |
() => prove(Judgement(Says(p, f2)::lhs_rest, rhs), sc))
|
|
70 |
case _ => ()
|
|
71 |
}
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
// function that calls prove and returns immediately once a proof is found
|
|
76 |
def run (j : Judgement) : Unit = {
|
|
77 |
try {
|
|
78 |
def sc () = { println ("Yes!"); throw new Exception }
|
|
79 |
prove(j, sc)
|
|
80 |
}
|
|
81 |
catch { case e: Exception => () }
|
|
82 |
}
|
|
83 |
|
|
84 |
run (goal)
|
|
85 |
|
|
86 |
|
60
|
87 |
run (Judgement(Nil, Del -> Del))
|
59
|
88 |
|
|
89 |
val Chr = "Christian"
|
|
90 |
val HoD = "Michael Luck"
|
|
91 |
val Email = Pred("may_btain_email", List(Const(Chr)))
|
|
92 |
val AtLib = Pred("is_at_library", List(Const(Chr)))
|
|
93 |
val Chr_Staff = Pred("is_staff", List(Const(Chr)))
|
|
94 |
|
60
|
95 |
val Policy_HoD = Says(HoD, Chr_Staff) -> Chr_Staff
|
|
96 |
val Policy_Lib = And(Chr_Staff, AtLib) -> Email
|
59
|
97 |
val HoD_says = Says(HoD, Chr_Staff)
|
|
98 |
|
|
99 |
run (Judgement (List(AtLib, Policy_HoD, Policy_Lib, HoD_says), Email))
|
|
100 |
|
|
101 |
// consider the cases for true and false
|