programs/prove1.scala
author Christian Urban <urbanc@in.tum.de>
Tue, 30 Oct 2012 07:11:42 +0000
changeset 59 8b44bd114292
child 60 93578c484ab1
permissions -rw-r--r--
added slides
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
59
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
abstract class Term
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
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
abstract class Form
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
case object True extends Form
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
case object False extends Form
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
case class Pred(s: String, ts: List[Term]) extends Form
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
case class Imp(f1: Form, f2: Form) extends Form
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
case class Says(p: String, f: Form) extends Form 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
case class And(f1: Form, f2: Form) extends Form 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
case class Or(f1: Form, f2: Form) extends Form 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
case class Judgement(Gamma: List[Form], F: Form) {
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
  def lhs = Gamma
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
  def rhs = F
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
}
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
val Admin = "Admin"
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
val Bob = "Bob"
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
val Del = Pred("del_file", Nil)
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
val Gamma = 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
  List( Imp(Says(Admin, Del), Del),
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
        Says(Admin, Imp(Says(Bob, Del), Del)),
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
        Says(Bob, Del) )
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
val goal = Judgement(Gamma, Del) // request: provable or not?
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
def prove(j: Judgement, sc: () => Unit) : Unit = {
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
  if (j.lhs.contains(j.rhs))  sc()   // Axiom rule 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
  else prove1(j.lhs, j.rhs, sc) 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
}
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
def partitions(ls: List[Form]): List[(Form, List[Form])]  = 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
  ls.map (s => (s, ls - s))
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
def prove1(lhs: List[Form], rhs: Form, sc: () => Unit) : Unit = 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
  rhs match {
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
    case Imp(f1, f2) => prove(Judgement(f1::lhs, f2), sc) 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
    case Says(p, f1) => prove(Judgement(lhs, f1), sc) 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
    case Or(f1, f2) => 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
      { prove(Judgement(lhs, f1), sc);
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
        prove(Judgement(lhs, f2), sc) }
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
    case And(f1, f2) => 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
      prove(Judgement(lhs, f1), 
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 _ => { for ((f, lhs_rest) <- partitions(lhs))
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
                  prove2(f, lhs_rest, rhs, sc) }
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
  }
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
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
    56
  f match {
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
    case And(f1, f2) =>
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
      prove(Judgement(f1::f2::lhs_rest, rhs), sc)
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
    case Imp(f1, f2) => 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
      prove(Judgement(lhs_rest, f1), 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
            () => prove(Judgement(f2::lhs_rest, rhs), sc))
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
    case Or(f1, f2) => 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
      prove(Judgement(f1::lhs_rest, rhs), 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
            () => prove(Judgement(f2::lhs_rest, rhs), sc))
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
    case Says(p, Imp(f1, f2)) => 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
      prove(Judgement(lhs_rest, Says(p, f1)), 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
            () => prove(Judgement(Says(p, f2)::lhs_rest, rhs), sc)) 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
    case _ => ()
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
  }
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
  
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
// function that calls prove and returns immediately once a proof is found
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
def run (j : Judgement) : Unit = {
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
  try { 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
    def sc () = { println ("Yes!"); throw new Exception }
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
    prove(j, sc) 
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
  catch { case e: Exception => () }
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
} 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
run (goal)
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
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
run (Judgement(Nil, Imp(Del, Del)))
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
val Chr = "Christian"
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
val HoD = "Michael Luck"
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
val Email = Pred("may_btain_email", List(Const(Chr)))
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
val AtLib = Pred("is_at_library", List(Const(Chr)))
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
val Chr_Staff = Pred("is_staff", List(Const(Chr)))
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
val Policy_HoD = Imp(Says(HoD, Chr_Staff), Chr_Staff)
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
val Policy_Lib = Imp(And(Chr_Staff, AtLib), Email)
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
val HoD_says = Says(HoD, Chr_Staff)
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
run (Judgement (List(AtLib, Policy_HoD, Policy_Lib, HoD_says), Email))
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
// consider the cases for true and false