-
Notifications
You must be signed in to change notification settings - Fork 30
Add min heap public test case #189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
CooperRoalson
wants to merge
2
commits into
main
Choose a base branch
from
CooperRoalson-patch-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
| // The heap invariant represents the central property of a min heap, allowing all other methods to verify | ||
| predicate {:induction false} HeapInvariant(heap: MinHeap) | ||
| { | ||
|
|
||
| } |
This file contains hidden or 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,116 @@ | ||
| // Proof that a min heap binary tree implementation successfully implements its spec | ||
|
|
||
| // --------- Min Heap Specification --------- | ||
|
|
||
| // Written as predicates that check the output instead of functions that produce the output | ||
| // because it's surprising hard to find the min of a multiset in a function :( | ||
|
|
||
| // Size should give the size of the heap | ||
| ghost predicate CheckSizeSpec(heap: multiset<int>, result: int) | ||
| { | ||
| result == |heap| | ||
| } | ||
|
|
||
| // Insert should add an element to the heap | ||
| ghost predicate CheckInsertSpec(heap: multiset<int>, x: int, result : multiset<int>) | ||
| { | ||
| result == heap + multiset{x} | ||
| } | ||
|
|
||
| // Top should return the min element of the heap | ||
| ghost predicate CheckTopSpec(heap: multiset<int>, result: int) | ||
| requires |heap| > 0 | ||
| { | ||
| forall x :: x in heap ==> result <= x | ||
| } | ||
|
|
||
| // Pop should remove the min element of the heap | ||
| ghost predicate CheckPopSpec(heap: multiset<int>, top: int, result: multiset<int>) | ||
| requires |heap| > 0 | ||
| { | ||
| CheckTopSpec(heap, top) && result == heap - multiset{top} | ||
| } | ||
|
|
||
|
|
||
| // --------- Min Heap Implementation --------- | ||
|
|
||
| // Uses a binary tree to organize the elements in descending order | ||
|
|
||
| // Every method requires and ensures the "heap invariant", and checks its output against the spec | ||
|
|
||
| datatype MinHeap = Nil | HeapNode(val: int, left: MinHeap, right: MinHeap) | ||
|
|
||
| // This converts the heap data structure to the theoretical/spec representation so we can check it | ||
| ghost function {:induction false} Contents(heap: MinHeap) : multiset<int> | ||
| requires HeapInvariant(heap) | ||
|
|
||
| ensures heap.HeapNode? ==> |Contents(heap)| > 0 && CheckTopSpec(Contents(heap), heap.val) // the root should be the "top" | ||
| { | ||
| match heap | ||
| case Nil => multiset{} | ||
| case HeapNode(val, left, right) => multiset{val} + Contents(left) + Contents(right) | ||
|
|
||
| } | ||
|
|
||
| function {:induction false} Size(heap: MinHeap) : nat | ||
| requires HeapInvariant(heap) | ||
| ensures CheckSizeSpec(Contents(heap), Size(heap)) // check against spec | ||
| { | ||
| match heap | ||
| case Nil => 0 | ||
| case HeapNode(val, left, right) => 1 + Size(left) + Size(right) | ||
| } | ||
|
|
||
| method {:induction false} Insert(heapIn: MinHeap, x: int) returns (heapOut: MinHeap) | ||
| requires HeapInvariant(heapIn) | ||
| ensures HeapInvariant(heapOut) | ||
| ensures CheckInsertSpec(Contents(heapIn), x, Contents(heapOut)) // check against spec | ||
|
|
||
| ensures heapOut.HeapNode? | ||
| ensures heapOut.val == x || (heapIn.HeapNode? && heapOut.val == heapIn.val) | ||
| { | ||
| match heapIn | ||
| case Nil => heapOut := HeapNode(x, Nil, Nil); | ||
| case HeapNode(val, left, right) => { | ||
| var lesser := if x < val then x else val; | ||
| var greater := if x < val then val else x; | ||
|
|
||
| if (Size(left) > Size(right)) { | ||
| var newRight := Insert(right, greater); | ||
| heapOut := HeapNode(lesser, left, newRight); | ||
| } else { | ||
| var newLeft := Insert(left, greater); | ||
| heapOut := HeapNode(lesser, newLeft, right); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function {:induction false} Top(heap: MinHeap) : int | ||
| requires HeapInvariant(heap) | ||
| requires Size(heap) > 0 | ||
| ensures CheckTopSpec(Contents(heap), Top(heap)) // check against spec | ||
| { | ||
| heap.val | ||
| } | ||
|
|
||
| method {:induction false} Pop(heapIn: MinHeap) returns (heapOut: MinHeap) | ||
| requires HeapInvariant(heapIn) | ||
| requires Size(heapIn) > 0 | ||
|
|
||
| ensures HeapInvariant(heapOut) | ||
| ensures CheckPopSpec(Contents(heapIn), Top(heapIn), Contents(heapOut)) // check against spec | ||
| { | ||
| if (heapIn.left.Nil?) { | ||
| heapOut := heapIn.right; | ||
| } else if (heapIn.right.Nil?) { | ||
| heapOut := heapIn.left; | ||
| } else if (heapIn.left.val <= heapIn.right.val) { | ||
| var newVal := heapIn.left.val; | ||
| var newLeft := Pop(heapIn.left); | ||
| heapOut := HeapNode(newVal, newLeft, heapIn.right); | ||
| } else { | ||
| var newVal := heapIn.right.val; | ||
| var newRight := Pop(heapIn.right); | ||
| heapOut := HeapNode(newVal, heapIn.left, newRight); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spelling error: "surprising hard" should be "surprisingly hard".