Skip to content
Snippets Groups Projects
Commit 5516de65 authored by Manos Koukoutos's avatar Manos Koukoutos
Browse files

Fix this benchmark

parent 439af53e
No related branches found
No related tags found
No related merge requests found
import leon.lang._ import leon.lang._
import leon.annotation._ import synthesis._
import leon.collection._
object MergeSort { object MergeSort {
sealed abstract class List
case class Cons(head:Int,tail:List) extends List
case class Nil() extends List
case class Pair(fst:List,snd:List) def isSorted(l: List[BigInt]): Boolean = l match {
case Cons(x, t@Cons(y, _)) => x <= y && isSorted(t)
def contents(l: List): Set[Int] = l match { case _ => true
case Nil() => Set.empty
case Cons(x,xs) => contents(xs) ++ Set(x)
} }
def is_sorted(l: List): Boolean = l match { def merge(l1: List[BigInt], l2: List[BigInt]): List[BigInt] = {
case Nil() => true require(isSorted(l1) && isSorted(l2))
case Cons(x,xs) => xs match { (l1, l2) match {
case Nil() => true case (Nil(), _) => l2
case Cons(y, ys) => x <= y && is_sorted(Cons(y, ys)) case (_, Nil()) => l1
case (Cons(h1,t1), Cons(h2, t2)) =>
if (h1 <= h2) Cons(h1, merge(t1, l2))
else Cons(h2, merge(l1, t2))
} }
} ensuring {
(res: List[BigInt]) =>
isSorted(res) &&
res.content == l1.content ++ l2.content &&
res.size == l1.size + l2.size
} }
def length(list:List): Int = list match { def split(l: List[BigInt]): (List[BigInt], List[BigInt]) = {
case Nil() => 0 l match {
case Cons(x,xs) => 1 + length(xs) case Cons(h1, Cons(h2, tail)) =>
} val (rec1, rec2) = split(tail)
(h1 :: rec1, h2 :: rec2)
def splithelper(aList:List,bList:List,n:Int): Pair = case _ => (l, Nil[BigInt]())
if (n <= 0) Pair(aList,bList)
else
bList match {
case Nil() => Pair(aList,bList)
case Cons(x,xs) => splithelper(Cons(x,aList),xs,n-1)
} }
} ensuring { (res: (List[BigInt], List[BigInt])) =>
val (r1, r2) = res
r1.size + r2.size == l.size &&
r1.content ++ r2.content == l.content &&
r1.size - r2.size <= 1 &&
r2.size - r1.size <= 1
}
def split(list:List,n:Int): Pair = splithelper(Nil(),list,n) def mergeSort(l: List[BigInt]): List[BigInt] = {
val (l1, l2) = split(l)
def merge(aList:List, bList:List):List = (bList match { merge(mergeSort(l1), mergeSort(l2))
case Nil() => aList } ensuring ( res =>
case Cons(x,xs) => isSorted(res) &&
aList match { res.content == l.content &&
case Nil() => bList res.size == l.size
case Cons(y,ys) => )
if (y < x)
Cons(y,merge(ys, bList))
else
Cons(x,merge(aList, xs))
}
}) ensuring(res => contents(res) == contents(aList) ++ contents(bList))
def mergeSort(list:List):List = (list match {
case Nil() => list
case Cons(x,Nil()) => list
case _ =>
val p = split(list,length(list)/2)
merge(mergeSort(p.fst), mergeSort(p.snd))
}) ensuring(res => contents(res) == contents(list) && is_sorted(res))
@ignore
def main(args: Array[String]): Unit = {
val ls: List = Cons(5, Cons(2, Cons(4, Cons(5, Cons(1, Cons(8,Nil()))))))
println(ls)
println(mergeSort(ls))
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment