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