-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
9,779 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8,496 changes: 8,496 additions & 0 deletions
8,496
...ui/examples/heap/BoyerMoore/BM(BM__bm((I)).JML normal_behavior operation contract.0.proof
Large diffs are not rendered by default.
Oops, something went wrong.
319 changes: 319 additions & 0 deletions
319
.../examples/heap/BoyerMoore/BM(BM__count((I,_bigint,_bigint)).JML accessible clause.0.proof
Large diffs are not rendered by default.
Oops, something went wrong.
381 changes: 381 additions & 0 deletions
381
...oyerMoore/BM(BM__count((I,_bigint,_bigint)).JML model_behavior operation contract.0.proof
Large diffs are not rendered by default.
Oops, something went wrong.
422 changes: 422 additions & 0 deletions
422
...p/BoyerMoore/BM(BM__monoLemma((I,int,int)).JML normal_behavior operation contract.0.proof
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
example.name = Boyer Moore Majority | ||
example.file = BoyerMoore.key | ||
example.additionalFile.1 = src/BoyerMoore.java | ||
example.path = Algorithms | ||
|
||
This is a KeY verification example for the Boyer Moore | ||
majority vote algorithm. | ||
|
||
The challenge is as followed: | ||
Compute in linear time the majority | ||
of an array of integers if it | ||
exists, report its absence otherwise, . | ||
An element m is the majority element if more than half of the | ||
entries in the array hold m. | ||
|
||
Suggested by J.C. Filliâtre as an example during VerifyThis 24. | ||
|
||
Currently the proofs do not go through automatically, the proof | ||
files are checked in with the example. | ||
|
||
@see https://en.wikipedia.org/wiki/Boyer-Moore_majority_vote_algorithm | ||
@author Mattias Ulbrich |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
|
||
/** | ||
* This is a KeY verification example for the Boyer Moore | ||
* majority vote algorithm. | ||
* | ||
* The challenge is as followed: | ||
* Compute in linear time the majority | ||
* of an array of integers if it | ||
* exists, report its absence otherwise, . | ||
* An element m is the majority element if more than half of the | ||
* entries in the array hold m. | ||
* | ||
* Suggested by J.C. Filliâtre as an example during VerifyThis 24. | ||
* | ||
* @see https://en.wikipedia.org/wiki/Boyer-Moore_majority_vote_algorithm | ||
* @author Mattias Ulbrich | ||
*/ | ||
class BoyerMoore { | ||
|
||
/*@ private normal_behaviour | ||
@ requires 0 <= k <= a.length; | ||
@ ensures \result == (\num_of int l; 0<=l<k; a[l] == v); | ||
@ measured_by k; | ||
@ accessible a[*]; | ||
@ model int count(int[] a, \bigint k, \bigint v) { | ||
@ return k == 0 ? 0 : | ||
@ ((a[k-1] == v ? 1 : 0) + count(a, k-1, v)); | ||
@ } | ||
@*/ | ||
|
||
/*@ public normal_behaviour | ||
@ requires \static_invariant_for(IntOpt); | ||
@ ensures \result.present ==> count(a, a.length, \result.value) > a.length/2; | ||
@ | ||
@ ensures !\result.present ==> !(\exists int m; count(a, a.length, m) > a.length/2); | ||
@ | ||
@ assignable \nothing; | ||
@*/ | ||
public IntOpt bm(int[] a) { | ||
|
||
int mc = 0; | ||
int mx = 0; | ||
|
||
/*@ loop_invariant 0 <= k <= a.length; | ||
@ loop_invariant mc >= 0; | ||
@ loop_invariant 2 * count(a, k, mx) <= k + mc; | ||
@ loop_invariant (\forall int x; x != mx; 2 * count(a, k, x) <= k - mc); | ||
@ assignable \strictly_nothing; | ||
@ decreases a.length - k; | ||
@*/ | ||
for(int k=0; k < a.length; k++) { | ||
if(mc == 0) { | ||
mc = 1; | ||
mx = a[k]; | ||
} else if(mx == a[k]) { | ||
mc++; | ||
} else { | ||
mc--; | ||
} | ||
} | ||
|
||
if(mc == 0) return IntOpt.NONE; | ||
|
||
int cnt = 0; | ||
/*@ loop_invariant 0 <= r <= a.length; | ||
@ loop_invariant cnt == count(a, r, mx); | ||
@ loop_invariant cnt <= a.length / 2; | ||
@ assignable \strictly_nothing; | ||
@ decreases a.length - r; | ||
@*/ | ||
for(int r=0; r < a.length; r++) { | ||
if(mx == a[r]) { | ||
if(++cnt > a.length / 2) { | ||
// This should be a ghost call which | ||
// is currently unsupported. | ||
monoLemma(a, r + 1, mx); | ||
return new IntOpt(mx); | ||
} | ||
} | ||
} | ||
|
||
return IntOpt.NONE; | ||
} | ||
|
||
// This should be ghost which is currently unsupported. | ||
/*@ private normal_behaviour | ||
@ requires 0 <= k <= a.length; | ||
@ ensures count(a, k, v) <= count(a, a.length, v); | ||
@ assignable \strictly_nothing; | ||
@ measured_by a.length - k; | ||
@*/ | ||
private void monoLemma(int[] a, int k, int v) { | ||
if(k == a.length) return; | ||
monoLemma(a, k+1, v); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* This class is used to represent an optional integer value. | ||
* The field present indicates the presence of a result, | ||
* value contains that value. | ||
*/ | ||
final class IntOpt { | ||
public static final IntOpt NONE; | ||
static { | ||
NONE = new IntOpt(0); | ||
NONE.present = false; | ||
} | ||
|
||
//@ public static invariant !NONE.present; | ||
|
||
/*@ spec_public */ private boolean present; | ||
/*@ spec_public */ private int value; | ||
|
||
// No contract for this construtor, the code will be inlined. | ||
IntOpt(int value) { | ||
this.present = true; | ||
this.value = value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters