-
Notifications
You must be signed in to change notification settings - Fork 0
/
Examples.dfy
54 lines (45 loc) · 1.15 KB
/
Examples.dfy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// RUN: /nologo /rlimit:100000
module InsertionSort {
function insert(a: int, xs: seq<int>): seq<int>
{
if xs == [] then
[a]
else if a <= xs[0] then
[a] + xs
else
[xs[0]] + insert(a, xs[1..])
}
function sort(xs: seq<int>): seq<int>
{
if xs == [] then
[]
else
insert(xs[0], sort(xs[1..]))
}
predicate sorted(xs: seq<int>)
{
forall i | 0 <= i < |xs|-1 :: xs[i] <= xs[i+1]
}
// Note: This is not idiomatic Dafny; typically one would use lemmas,
// but using functions instead keeps things more expression-oriented.
function insert_sorted(a: int, xs: seq<int>): ()
requires sorted(xs)
ensures sorted(insert(a, xs))
{
if xs == [] then
()
else if a <= xs[0] then
()
else
insert_sorted(a, xs[1..])
}
function sort_sorted(xs: seq<int>): ()
ensures sorted(sort(xs))
{
if xs == [] then
()
else
var _ := sort_sorted(xs[1..]);
insert_sorted(xs[0], sort(xs[1..]))
}
}