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