programs/prove1.scala
author Christian Urban <urbanc@in.tum.de>
Tue, 30 Oct 2012 14:27:59 +0000
changeset 60 93578c484ab1
parent 59 8b44bd114292
child 62 e8071a3f13b2
permissions -rw-r--r--
tuned
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
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
val Gamma = 
60
Christian Urban <urbanc@in.tum.de>
parents: 59
diff changeset
    29
  List( Says(Admin, Del) -> Del,
Christian Urban <urbanc@in.tum.de>
parents: 59
diff changeset
    30
        Says(Admin, Says(Bob, Del) -> Del),
59
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
        Says(Bob, Del) )
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
val goal = Judgement(Gamma, Del) // request: provable or not?
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
def prove(j: Judgement, sc: () => Unit) : Unit = {
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
  if (j.lhs.contains(j.rhs))  sc()   // Axiom rule 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
  else prove1(j.lhs, j.rhs, sc) 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
}
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
def partitions(ls: List[Form]): List[(Form, List[Form])]  = 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
  ls.map (s => (s, ls - s))
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 {
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
    case Imp(f1, f2) => prove(Judgement(f1::lhs, f2), sc) 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
    case Says(p, f1) => prove(Judgement(lhs, f1), sc) 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
    case Or(f1, f2) => 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
      { prove(Judgement(lhs, f1), sc);
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
        prove(Judgement(lhs, f2), sc) }
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
    case And(f1, f2) => 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
      prove(Judgement(lhs, f1), 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
            () => prove(Judgement(lhs, f2), sc))
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
    case _ => { for ((f, lhs_rest) <- partitions(lhs))
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
                  prove2(f, lhs_rest, rhs, sc) }
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
  }
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
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
    58
  f match {
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
    case And(f1, f2) =>
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
      prove(Judgement(f1::f2::lhs_rest, rhs), sc)
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
    case Imp(f1, f2) => 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
      prove(Judgement(lhs_rest, f1), 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
            () => prove(Judgement(f2::lhs_rest, rhs), sc))
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
    case Or(f1, f2) => 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
      prove(Judgement(f1::lhs_rest, rhs), 
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 Says(p, Imp(f1, f2)) => 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
      prove(Judgement(lhs_rest, Says(p, f1)), 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
            () => prove(Judgement(Says(p, f2)::lhs_rest, rhs), sc)) 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
    case _ => ()
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
  
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
// function that calls prove and returns immediately once a proof is found
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
def run (j : Judgement) : Unit = {
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
  try { 
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
    def sc () = { println ("Yes!"); throw new Exception }
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
    prove(j, sc) 
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
  catch { case e: Exception => () }
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
} 
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
run (goal)
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
60
Christian Urban <urbanc@in.tum.de>
parents: 59
diff changeset
    87
run (Judgement(Nil, Del -> Del))
59
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
val Chr = "Christian"
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
val HoD = "Michael Luck"
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
val Email = Pred("may_btain_email", List(Const(Chr)))
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
val AtLib = Pred("is_at_library", List(Const(Chr)))
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
val Chr_Staff = Pred("is_staff", List(Const(Chr)))
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
60
Christian Urban <urbanc@in.tum.de>
parents: 59
diff changeset
    95
val Policy_HoD = Says(HoD, Chr_Staff) -> Chr_Staff
Christian Urban <urbanc@in.tum.de>
parents: 59
diff changeset
    96
val Policy_Lib = And(Chr_Staff, AtLib) -> Email
59
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
val HoD_says = Says(HoD, Chr_Staff)
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
run (Judgement (List(AtLib, Policy_HoD, Policy_Lib, HoD_says), Email))
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
8b44bd114292 added slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
// consider the cases for true and false