Attic/programs/prove1.scala
author Christian Urban <urbanc@in.tum.de>
Tue, 26 Sep 2017 10:36:19 +0100
changeset 531 35ffb7a7eafa
parent 198 2ce98ee39990
permissions -rw-r--r--
fixed bug
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
59
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
60
Christian Urban <urbanc@in.tum.de>
parents: 59
diff changeset
     2
abstract class Term 
59
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
case class Var(s: String) extends Term 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
case class Const(s: String) extends Term 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
case class Fun(s: String, ts: List[Term]) extends Term
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
60
Christian Urban <urbanc@in.tum.de>
parents: 59
diff changeset
     7
abstract class Form {
Christian Urban <urbanc@in.tum.de>
parents: 59
diff changeset
     8
  def -> (that: Form) = Imp(this, that)
Christian Urban <urbanc@in.tum.de>
parents: 59
diff changeset
     9
}
59
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
case object True extends Form
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
case object False extends Form
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
case class Pred(s: String, ts: List[Term]) extends Form
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
case class Imp(f1: Form, f2: Form) extends Form
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
case class Says(p: String, f: Form) extends Form 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
case class And(f1: Form, f2: Form) extends Form 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
case class Or(f1: Form, f2: Form) extends Form 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
case class Judgement(Gamma: List[Form], F: Form) {
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
  def lhs = Gamma
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
  def rhs = F
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
}
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
val Admin = "Admin"
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
val Bob = "Bob"
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
val Del = Pred("del_file", Nil)
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
val Gamma = 
60
Christian Urban <urbanc@in.tum.de>
parents: 59
diff changeset
    28
  List( Says(Admin, Del) -> Del,
Christian Urban <urbanc@in.tum.de>
parents: 59
diff changeset
    29
        Says(Admin, Says(Bob, Del) -> Del),
59
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
        Says(Bob, Del) )
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
val goal = Judgement(Gamma, Del) // request: provable or not?
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
129
10526c967679 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 127
diff changeset
    34
def partitions[A](ls: List[A]): List[(A, List[A])]  = 
10526c967679 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 127
diff changeset
    35
  ls.map (s => (s, ls diff List(s)))
10526c967679 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 127
diff changeset
    36
10526c967679 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 127
diff changeset
    37
59
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
def prove(j: Judgement, sc: () => Unit) : Unit = {
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
  if (j.lhs.contains(j.rhs))  sc()   // Axiom rule 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
  else prove1(j.lhs, j.rhs, sc) 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
}
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
def prove1(lhs: List[Form], rhs: Form, sc: () => Unit) : Unit = 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
  rhs match {
129
10526c967679 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 127
diff changeset
    45
    case True => sc ()
62
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
    46
    case False => ()
59
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
    case Imp(f1, f2) => prove(Judgement(f1::lhs, f2), sc) 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
    case Says(p, f1) => prove(Judgement(lhs, f1), sc) 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
    case Or(f1, f2) => 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
      { prove(Judgement(lhs, f1), sc);
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
        prove(Judgement(lhs, f2), sc) }
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
    case And(f1, f2) => 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
      prove(Judgement(lhs, f1), 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
            () => prove(Judgement(lhs, f2), sc))
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
    case _ => { for ((f, lhs_rest) <- partitions(lhs))
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
                  prove2(f, lhs_rest, rhs, sc) }
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
  }
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
def prove2(f: Form, lhs_rest: List[Form], rhs: Form, sc: () => Unit) : Unit = 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
  f match {
62
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
    61
    case True => prove(Judgement(lhs_rest, rhs), sc)
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
    62
    case False => sc()
59
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
    case And(f1, f2) =>
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
      prove(Judgement(f1::f2::lhs_rest, rhs), sc)
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
    case Imp(f1, f2) => 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
      prove(Judgement(lhs_rest, f1), 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
            () => prove(Judgement(f2::lhs_rest, rhs), sc))
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
    case Or(f1, f2) => 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
      prove(Judgement(f1::lhs_rest, rhs), 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
            () => prove(Judgement(f2::lhs_rest, rhs), sc))
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
    case Says(p, Imp(f1, f2)) => 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
      prove(Judgement(lhs_rest, Says(p, f1)), 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
            () => prove(Judgement(Says(p, f2)::lhs_rest, rhs), sc)) 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
    case _ => ()
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
  }
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
  
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
// function that calls prove and returns immediately once a proof is found
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
def run (j : Judgement) : Unit = {
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
  try { 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
    def sc () = { println ("Yes!"); throw new Exception }
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
    prove(j, sc) 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
  }
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
  catch { case e: Exception => () }
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
} 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
62
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
    88
run (Judgement (Nil, False -> Del))
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
    89
run (Judgement (Nil, True -> Del))
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
    90
run (Judgement (Nil, Del -> True))
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
    91
59
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
run (goal)
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
62
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
    94
val Gamma1 = 
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
    95
  List( Says(Admin, Says(Bob, Del) -> Del),
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
    96
        Says(Bob, Del) )
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
    97
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
    98
val goal1 = Judgement(Gamma1, Del) // not provable
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
    99
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
   100
run (goal1)
59
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
60
Christian Urban <urbanc@in.tum.de>
parents: 59
diff changeset
   102
run (Judgement(Nil, Del -> Del))
59
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
62
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
   104
run (Judgement(Nil, Del -> Or(False, Del)))
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
   105
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
   106
59
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
val Chr = "Christian"
127
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 65
diff changeset
   108
val HoD = "Peter"
59
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
val Email = Pred("may_btain_email", List(Const(Chr)))
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
val AtLib = Pred("is_at_library", List(Const(Chr)))
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
val Chr_Staff = Pred("is_staff", List(Const(Chr)))
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
60
Christian Urban <urbanc@in.tum.de>
parents: 59
diff changeset
   113
val Policy_HoD = Says(HoD, Chr_Staff) -> Chr_Staff
Christian Urban <urbanc@in.tum.de>
parents: 59
diff changeset
   114
val Policy_Lib = And(Chr_Staff, AtLib) -> Email
59
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
val HoD_says = Says(HoD, Chr_Staff)
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
run (Judgement (List(AtLib, Policy_HoD, Policy_Lib, HoD_says), Email))
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
65
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   119