-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathBinarySearch.dfy
More file actions
50 lines (40 loc) · 1.5 KB
/
BinarySearch.dfy
File metadata and controls
50 lines (40 loc) · 1.5 KB
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
// RUN: %run "%s" > "%t"
// OutputCheck is broken, see https://github.com/dafny-lang/libraries/issues/164
// not working: %OutputCheck --file-to-check "%t" "%s"
// CHECK-L: [-6, 0, 1, 3, 7, 7, 9]
// CHECK-NEXT-L: 3
/*******************************************************************************
* Copyright by the contributors to the Dafny Project
* SPDX-License-Identifier: MIT
*******************************************************************************/
include "../../../src/Relations.dfy"
include "../../../src/Collections/Arrays/BinarySearch.dfy"
include "../../../src/Collections/Sequences/Seq.dfy"
include "../../../src/Collections/Sequences/MergeSort.dfy"
module BinarySearchExamples {
import BinarySearch
import opened Seq
import opened Seq.MergeSort
import opened Relations
lemma SortedByLessThanOrEqualTo(s: seq<int>)
requires SortedBy(s, (x, y) => x <= y)
ensures SortedBy(s, (x, y) => x < y || x == y)
{}
method {:vcs_split_on_every_assert} SortAndSearch() {
var input := [1, 7, 7, 3, 9, 0, -6];
var sortedInput := MergeSortBy(input, (x, y) => x <= y);
print sortedInput, "\n";
assert SortedBy(sortedInput, (x, y) => x <= y);
var sortedArray := ToArray(sortedInput);
SortedByLessThanOrEqualTo(sortedArray[..]);
var indexOfThree := BinarySearch.BinarySearch(sortedArray, 3, (x, y) => x < y);
if indexOfThree.Some? {
print indexOfThree.value, "\n";
} else {
print "Not found\n";
}
}
method Main() {
SortAndSearch();
}
}