Skip to content
Snippets Groups Projects
Commit c5eedf65 authored by Mirco Dotta's avatar Mirco Dotta
Browse files

- Slightly changed extractors declaration of ConsSnoc to allow ease in specification

- Commented properties that use implication and which are not handled correctly in the current implementation
- Removed And node from PropositionalLogic example which make the automatic generator injection get nuts
parent 0ad0f29f
No related branches found
No related tags found
No related merge requests found
......@@ -16,37 +16,48 @@ object ConsSnoc {
/* Extractors */
object Nill {
def apply(): Nill = new Nill()
def unapply(n: Nill): Boolean = true
//def unapply(n: Nill): Boolean = true
def unapply(l: Lst): Boolean = l match {
case n: Nill => true
case c: Cons => false
}
}
object Cons {
def apply(head: Int, tail: Lst): Cons =
new Cons(head,tail)
def apply(head: Int, tail: Lst): Cons = new Cons(head,tail)
def unapply(c: Cons): Option[(Int,Lst)] =
Some((c.head,c.tail))
//def unapply(c: Cons): Option[(Int,Lst)] = Some((c.head,c.tail))
def unapply(l: Lst): Option[(Int,Lst)] = l match {
case n: Nill => None
case c: Cons => Some((c.head,c.tail))
}
}
object Snoc {
def unapply(c: Cons): Option[(Lst,Int)] = c match {
case Cons(c, xs) => xs match {
def unapply(l: Lst): Option[(Lst,Int)] = l match {
case Nill() => None
case Cons(c, xs) => xs match {
case Nill() => Some((Nill(),c))
case Snoc(ys, y) => Some((Cons(c,ys),y))
}
}
}
/*
object Snoc {
def unapply(c): Option[(Lst,Int)] = c match {
case Nill() => None
case Cons(c, xs) => xs match {
case Nill() => Some(Tuple2(Nill(),c))
case Snoc(ys, y) => Some(Tuple2(Cons(c,ys),y))
def unapply(c: Cons): Option[(Lst,Int)] = c match {
case Cons(c, xs) => xs match {
case Nill() => Some((Nill(),c))
case Snoc(ys, y) => Some((Cons(c,ys),y))
}
}
}
*/
def firstAndLast(lst: Lst): Lst = lst match {
case Nill() => lst
......@@ -65,34 +76,40 @@ object ConsSnoc {
*/
forAll{n: Nill => Nill.unapply(n)} // Dom_Nill = Nill
forAll{c: Cons => Cons.unapply(c).isDefined} // Dom_Cons = Cons
forAll{c: Cons => Cons.unapply(c) == Some((c.head, c.tail))} // postcondition for Cons extractor method
forAll{c: Cons => Snoc.unapply(c).isDefined} // Dom_Snoc = Cons
forAll{c: Cons => Snoc.unapply(c) == Some((reverse(c).head, reverse(c).tail))} // postcondition for Snoc extractor method
forAll{l: Lst => lstUnapply(l).isDefined} // Dom_Cons \Un Dom_Nill = Lst
forAll{l: Lst => !(lstUnapply(l).get.isInstanceOf[Cons] && lstUnapply(l).get.isInstanceOf[Nill])} //Dom_Cons \Int Dom_Nill = empty
forAll{l: Lst => Cons.unapply(l).isDefined || Nill.unapply(l)} // Dom_Cons \Un Dom_Nill = Lst
forAll{l: Lst => !(Cons.unapply(l).isDefined && Nill.unapply(l))} //Dom_Cons \Int Dom_Nill = empty
def lstUnapply(l: Lst): Option[Any] = l match {
case n: Nill => Some(Nill.unapply(n))
case c: Cons => Cons.unapply(c)
case _ => None
}
def reverse(c: Cons): Cons = {
def loop(l: Lst, res: Cons): Cons = l match {
case Nill() => res
case Cons(head, tail) => loop(tail, Cons(head,res))
}
loop(c.tail,Cons(c.head, Nill()))
}
def equalLst(l1: Lst, l2: Lst): Boolean = (l1,l2) match {
case (Nill(),Nill()) => true
case (Cons(x,xs),Cons(y,ys)) if x == y =>
equalLst(xs,ys)
case _ => false
}
// forAll{c: Cons => equalLst(Cons(Cons.unapply(c).get._1, Cons.unapply(c).get._2), c)} // postcondition for Cons extractor method
//
// forAll{c: Cons => equalLst(Snoc.unapply(c).get._2, Snoc.unapply(c).get._1), reverse(c))} // postcondition for Snoc extractor method
// def last(c: Cons): Int = c match {
// case Cons(x, Nill()) => x
// case Cons(_,tail) => last(tail)
// }
//
// def reverse(c: Cons): Cons = {
// def loop(l: Lst, res: Cons): Cons = l match {
// case Nill() => res
// case Cons(head, tail) => loop(tail, Cons(head,res))
// }
// loop(c.tail,Cons(c.head, Nill()))
// }
//
// def equalLst(l1: Lst, l2: Lst): Boolean = (l1,l2) match {
// case (Nill(),Nill()) => true
// case (Cons(x,xs),Cons(y,ys)) if x == y =>
// equalLst(xs,ys)
// case _ => false
// }
}
\ No newline at end of file
......@@ -102,10 +102,10 @@ object LeftistHeap extends Application {
//forAll[(Heap,Elem)]( p => ((rankk(p._1) > 0 && findMin(p._1).value == p._2.value) ==> (findMin(p._1).value < p._2.value)))
//val heapFindMin = forAll{ heap : Heap => (heap.rankk > 0) ==> (heap.findMin == min(content(heap).elements.toList))}
forAll{ heap : Heap => (rankk(heap) > 0) ==> (findMin(heap) == min(content(heap).elements.toList))}
//forAll{ heap : Heap => (rankk(heap) > 0) ==> (findMin(heap) == min(content(heap).elements.toList))}
//val heapDeleteMin = forAll{ heap: Heap => (heap.rankk > 0) ==> (content(heap.deleteMin).equals(content(heap) - heap.findMin))}
forAll{ heap: Heap => (rankk(heap) > 0) ==> (content(deleteMin(heap)).equals(content(heap) - findMin(heap)))}
//forAll{ heap: Heap => (rankk(heap) > 0) ==> (content(deleteMin(heap)).equals(content(heap) - findMin(heap)))}
}
......@@ -11,7 +11,8 @@ object PropositionalLogic {
// java.lang.StackOverflowError
@generator
sealed abstract class Formula
case class And(left: Formula, right: Formula) extends Formula
// conjunction makes automatic generator crash
//case class And(left: Formula, right: Formula) extends Formula
case class Or(left: Formula, right: Formula) extends Formula
case class Neg(f: Formula) extends Formula
case class True() extends Formula
......@@ -24,14 +25,14 @@ object PropositionalLogic {
def desugar(f: Formula): Formula = f match {
case True() => f
case False() => f
case And(left,right) => And(desugar(left),desugar(right))
//case And(left,right) => And(desugar(left),desugar(right))
case Or(left,right) => Or(desugar(left),desugar(right))
case Neg(f) => Neg(desugar(f))
case Imply(left,right) => Or(Neg(desugar(left)),desugar(right))
}
def isDesugared(f: Formula): Boolean = f match {
case And(left,right) => isDesugared(left) && isDesugared(right)
//case And(left,right) => isDesugared(left) && isDesugared(right)
case Or(left,right) => isDesugared(left) && isDesugared(right)
case Neg(f) => isDesugared(f)
case True() => true
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment