Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
inox
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
LARA
inox
Commits
5516de65
Commit
5516de65
authored
9 years ago
by
Manos Koukoutos
Browse files
Options
Downloads
Patches
Plain Diff
Fix this benchmark
parent
439af53e
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
testcases/verification/list-algorithms/MergeSort.scala
+39
-54
39 additions, 54 deletions
testcases/verification/list-algorithms/MergeSort.scala
with
39 additions
and
54 deletions
testcases/verification/list-algorithms/MergeSort.scala
+
39
−
54
View file @
5516de65
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
))
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment