programs/prove1.scala
author Christian Urban <urbanc@in.tum.de>
Tue, 13 Nov 2012 04:50:22 +0000
changeset 65 8d3c4efb91b3
parent 62 e8071a3f13b2
child 127 56cf3a9a2693
permissions -rw-r--r--
added
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
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
def prove(j: Judgement, sc: () => Unit) : Unit = {
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
  if (j.lhs.contains(j.rhs))  sc()   // Axiom rule 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
  else prove1(j.lhs, j.rhs, sc) 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
}
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
62
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
    39
def partitions[A](ls: List[A]): List[(A, List[A])]  = 
59
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
  ls.map (s => (s, ls - s))
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
def prove1(lhs: List[Form], rhs: Form, sc: () => Unit) : Unit = 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
  rhs match {
62
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
    44
    case True => sc()
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
    45
    case False => ()
59
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
    case Imp(f1, f2) => prove(Judgement(f1::lhs, f2), sc) 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
    case Says(p, f1) => prove(Judgement(lhs, f1), sc) 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
    case Or(f1, f2) => 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
      { prove(Judgement(lhs, f1), sc);
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
        prove(Judgement(lhs, f2), sc) }
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
    case And(f1, f2) => 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
      prove(Judgement(lhs, f1), 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
            () => prove(Judgement(lhs, f2), sc))
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
    case _ => { for ((f, lhs_rest) <- partitions(lhs))
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
                  prove2(f, lhs_rest, rhs, sc) }
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
  }
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
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
    59
  f match {
62
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
    60
    case True => prove(Judgement(lhs_rest, rhs), sc)
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
    61
    case False => sc()
59
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
    case And(f1, f2) =>
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
      prove(Judgement(f1::f2::lhs_rest, rhs), sc)
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
    case Imp(f1, f2) => 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
      prove(Judgement(lhs_rest, f1), 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
            () => prove(Judgement(f2::lhs_rest, rhs), sc))
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
    case Or(f1, f2) => 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
      prove(Judgement(f1::lhs_rest, rhs), 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
            () => prove(Judgement(f2::lhs_rest, rhs), sc))
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
    case Says(p, Imp(f1, f2)) => 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
      prove(Judgement(lhs_rest, Says(p, f1)), 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
            () => prove(Judgement(Says(p, f2)::lhs_rest, rhs), sc)) 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
    case _ => ()
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
  }
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
// function that calls prove and returns immediately once a proof is found
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
def run (j : Judgement) : Unit = {
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
  try { 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
    def sc () = { println ("Yes!"); throw new Exception }
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
    prove(j, sc) 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
  }
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
  catch { case e: Exception => () }
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
} 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
62
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
    87
run (Judgement (Nil, False -> Del))
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
    88
run (Judgement (Nil, True -> Del))
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
    89
run (Judgement (Nil, Del -> True))
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
    90
59
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
run (goal)
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
62
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
    93
val Gamma1 = 
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
    94
  List( Says(Admin, Says(Bob, Del) -> Del),
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
    95
        Says(Bob, Del) )
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
    96
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
    97
val goal1 = Judgement(Gamma1, Del) // not provable
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
    98
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
    99
run (goal1)
59
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
60
Christian Urban <urbanc@in.tum.de>
parents: 59
diff changeset
   101
run (Judgement(Nil, Del -> Del))
59
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
62
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
   103
run (Judgement(Nil, Del -> Or(False, Del)))
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
   104
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
   105
59
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
val Chr = "Christian"
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
val HoD = "Michael Luck"
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
val Email = Pred("may_btain_email", List(Const(Chr)))
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
val AtLib = Pred("is_at_library", List(Const(Chr)))
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
val Chr_Staff = Pred("is_staff", List(Const(Chr)))
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
60
Christian Urban <urbanc@in.tum.de>
parents: 59
diff changeset
   112
val Policy_HoD = Says(HoD, Chr_Staff) -> Chr_Staff
Christian Urban <urbanc@in.tum.de>
parents: 59
diff changeset
   113
val Policy_Lib = And(Chr_Staff, AtLib) -> Email
59
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
val HoD_says = Says(HoD, Chr_Staff)
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
run (Judgement (List(AtLib, Policy_HoD, Policy_Lib, HoD_says), Email))
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
65
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   118