-
Notifications
You must be signed in to change notification settings - Fork 30
Add two-stack queue test #188
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
ireneyrzd
wants to merge
1
commit into
main
Choose a base branch
from
two-stack-queue-test
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
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,20 @@ | ||
| lemma {:induction false} EnqueueCorrect<T>(q: TwoStackQueue<T>, x: T) | ||
| ensures QueueContents(Enqueue(q, x)) == QueueContents(q) + [x] | ||
| { | ||
|
|
||
| } | ||
|
|
||
| lemma {:induction false} DequeueCorrect<T>(q: TwoStackQueue<T>) | ||
| requires |QueueContents(q)| > 0 | ||
| ensures var (q', x) := Dequeue(q); | ||
| x == QueueContents(q)[0] && QueueContents(q') == QueueContents(q)[1..] | ||
| { | ||
|
|
||
| } | ||
|
|
||
| lemma {:induction false} TransferCorrect<T>(q: TwoStackQueue<T>) | ||
| requires |q.outbox| == 0 | ||
| ensures QueueContents(Transfer(q)) == QueueContents(q) | ||
| { | ||
|
|
||
| } | ||
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,76 @@ | ||
| // Two-Stack Queue: Simulating a Queue with Two Stacks | ||
| // Proves that FIFO queue behavior can be implemented using two LIFO stacks | ||
|
|
||
| // Two-stack queue state | ||
| datatype TwoStackQueue<T> = Queue(inbox: seq<T>, outbox: seq<T>) | ||
|
|
||
| // Get the logical queue contents (FIFO order) | ||
| function {:induction false} QueueContents<T>(q: TwoStackQueue<T>): seq<T> | ||
| { | ||
| q.outbox + Reverse(q.inbox) | ||
| } | ||
|
|
||
| // Reverse a sequence | ||
| function {:induction false} Reverse<T>(s: seq<T>): seq<T> | ||
| decreases |s| | ||
| { | ||
| if |s| == 0 then [] | ||
| else Reverse(s[1..]) + [s[0]] | ||
| } | ||
|
|
||
| // Enqueue: push to inbox stack | ||
| function {:induction false} Enqueue<T>(q: TwoStackQueue<T>, x: T): TwoStackQueue<T> | ||
| { | ||
| Queue(q.inbox + [x], q.outbox) | ||
| } | ||
|
|
||
| // Transfer: move all elements from inbox to outbox (reverses order) | ||
| function {:induction false} Transfer<T>(q: TwoStackQueue<T>): TwoStackQueue<T> | ||
| requires |q.outbox| == 0 | ||
| { | ||
| Queue([], Reverse(q.inbox)) | ||
| } | ||
|
|
||
| // Dequeue: pop from outbox, transfer if needed | ||
| function {:induction false} Dequeue<T>(q: TwoStackQueue<T>): (TwoStackQueue<T>, T) | ||
| requires |QueueContents(q)| > 0 | ||
| { | ||
| if |q.outbox| > 0 then | ||
| (Queue(q.inbox, q.outbox[..|q.outbox|-1]), q.outbox[|q.outbox|-1]) | ||
| else | ||
| var q' := Transfer(q); | ||
| (Queue(q'.inbox, q'.outbox[..|q'.outbox|-1]), q'.outbox[|q'.outbox|-1]) | ||
| } | ||
|
|
||
| // Test: Enqueue preserves FIFO semantics | ||
| method {:induction false} EnqueueTest<T>(q: TwoStackQueue<T>, x: T) | ||
| { | ||
| var q' := Enqueue(q, x); | ||
| assert QueueContents(q') == QueueContents(q) + [x] by { | ||
| EnqueueCorrect(q, x); | ||
| } | ||
| } | ||
|
|
||
| // Test: Dequeue removes from front | ||
| method {:induction false} DequeueTest<T>(q: TwoStackQueue<T>) | ||
| requires |QueueContents(q)| > 0 | ||
| { | ||
| var (q', x) := Dequeue(q); | ||
| var contents := QueueContents(q); | ||
| assert x == contents[0] by { | ||
| DequeueCorrect(q); | ||
| } | ||
| assert QueueContents(q') == contents[1..] by { | ||
| DequeueCorrect(q); | ||
| } | ||
| } | ||
|
|
||
| // Test: Transfer maintains queue contents | ||
| method {:induction false} TransferTest<T>(q: TwoStackQueue<T>) | ||
| requires |q.outbox| == 0 | ||
| { | ||
| var q' := Transfer(q); | ||
| assert QueueContents(q') == QueueContents(q) by { | ||
| TransferCorrect(q); | ||
| } | ||
| } |
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.
The lemma references
TwoStackQueueandQueueContents/Enqueuewithout importing or including the definitions fromtestcases/two_stack_queue.dfy. This file needs anincludestatement at the top to reference the main definitions.