Skip to content

Commit

Permalink
Add version of queue pop w/o lemma & duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
dc-mak committed Jun 17, 2024
1 parent 912baec commit bf4b455
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/examples/queue_pop_unified.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "queue_headers.h"

/*@
type_synonym result = { datatype seq after, datatype seq before }
predicate (result) Queue_pop_lemma(pointer front, pointer back, i32 popped) {
if (is_null(front)) {
return { after: Seq_Nil{}, before: snoc(Seq_Nil{}, popped) };
} else {
take B = Owned<struct int_queueCell>(back);
assert (is_null(B.next));
take L = IntQueueAux (front, back);
return { after: snoc(L, B.first), before: snoc(Seq_Cons {head: popped, tail: L}, B.first) };
}
}
@*/

void snoc_fact(struct int_queueCell *front, struct int_queueCell *back, int x)
/*@
requires
take Q = IntQueueAux(front, back);
take B = Owned<struct int_queueCell>(back);
ensures
take NewQ = IntQueueAux(front, back);
take NewB = Owned<struct int_queueCell>(back);
Q == NewQ; B == NewB;
let L = snoc (Seq_Cons{head: x, tail: Q}, B.first);
hd(L) == x;
tl(L) == snoc (Q, B.first);
@*/
{
/*@ unfold snoc (Seq_Cons{head: x, tail: Q}, B.first); @*/
}

void snoc_fact_unified(struct int_queueCell *front, struct int_queueCell *back, int x)
/*@
requires
take AB = Queue_pop_lemma(front, back, x);
ensures
take NewAB = Queue_pop_lemma(front, back, x);
AB == NewAB;
AB.after == tl(AB.before);
x == hd(AB.before);
@*/
{
if (!front) {
/*@ unfold snoc(Seq_Nil{}, x); @*/
} else {
snoc_fact(front, back, x);
}
}

int IntQueue_pop (struct int_queue *q)
/*@ requires take before = IntQueuePtr(q);
before != Seq_Nil{};
ensures take after = IntQueuePtr(q);
after == tl(before);
return == hd(before);
@*/
{
/*@ split_case is_null((*q).front); @*/
struct int_queueCell* h = q->front;
/*@ split_case ptr_eq(h,(*q).back); @*/
int x = h->first;
q->front = h->next;
freeIntQueueCell(h);
if (!q->front) q->back = 0;
snoc_fact_unified(q->front, q->back, x);
return x;
}

0 comments on commit bf4b455

Please sign in to comment.