|
| 1 | +Require Import RGref.DSL.DSL. |
| 2 | + |
| 3 | +(** * A Mutable Binary Tree *) |
| 4 | + |
| 5 | +(** ** A Pure Tree of Non-Zero Naturals |
| 6 | + To further familiarize ourselves, we implement a pure-functional tree containing arbitrary non-zero nats. *) |
| 7 | + |
| 8 | +Section PureTree. |
| 9 | +Inductive btree' : Set := |
| 10 | + | lf' : btree' |
| 11 | + | nd' : forall (C:Set) (P:C->Prop) (a b:C) (pa:P a) (pb:P b), nat -> btree'. |
| 12 | +Inductive gtz' : btree' -> Prop := |
| 13 | + | gtzlf : gtz' lf' |
| 14 | + | gtznd : forall (C:Set) (P:C->Prop) (a b:C) (pa:P a) (pb:P b) (n:nat), |
| 15 | + n > 0 -> gtz' (nd' C P a b pa pb n). |
| 16 | +Inductive PTCheck : btree' -> Prop := |
| 17 | + | lf_check : PTCheck lf' |
| 18 | + | nd_check : forall (n:nat) (a b:btree') (pa:PTCheck a) (pb:PTCheck b) (gta:gtz' a) (gtb:gtz' b), |
| 19 | + PTCheck (nd' btree' gtz' a b gta gtb n). |
| 20 | +Definition btree := {t:btree'|PTCheck t}. |
| 21 | +Definition gtz (t:btree) := gtz' (proj1_sig t). |
| 22 | +Definition lf := exist _ lf' lf_check. |
| 23 | +Definition nd n (a b:btree) pa pb := exist _ (nd' btree' gtz' (proj1_sig a) (proj1_sig b) pa pb n) |
| 24 | + (nd_check n (proj1_sig a) (proj1_sig b) (proj2_sig a) (proj2_sig b) pa pb). |
| 25 | + |
| 26 | +End PureTree. |
| 27 | +(* |
| 28 | +Axiom sref_axiom : forall (A:Type), Type. |
| 29 | +Inductive sref (A:Type) : Type := |
| 30 | + | mksref : nat -> sref A. |
| 31 | +Axiom sref_read : forall {A}, sref A -> A. |
| 32 | +(*CoInductive natskel (C:Type) (P:hpred C) (R G:hrel C) : Set := |
| 33 | + | leaf : natskel C P R G |
| 34 | + | node : nat -> ref{C|P}[R,G] -> natskel C P R G.*) |
| 35 | +Inductive natskel (C:Set) (P:C->Prop): Set := |
| 36 | + | leaf : natskel C P |
| 37 | + | node : nat -> sref (natskel C P) -> natskel C P. |
| 38 | +
|
| 39 | +Fixpoint ll_of_len (n:nat) (P:forall {A}, nat->A->Prop): Set := |
| 40 | + match n with |
| 41 | + | 0 => natskel unit (fun _ => True) |
| 42 | + | S n' => natskel (ll_of_len n' P) (P _ n') |
| 43 | + end. |
| 44 | +Check ll_of_len. |
| 45 | +Fixpoint sorted {A} (n:nat) : A -> Prop := |
| 46 | + fun l => |
| 47 | + match (n,l) with |
| 48 | + | (0,y) => True |
| 49 | + | (S n',leaf) => True |
| 50 | + | (S n',node val tl) => val > 0 /\ sorted (sref_read tl) |
| 51 | + end. |
| 52 | +
|
| 53 | +(*CoInductive cotype : Type -> Type := |
| 54 | + | μ : forall (τ:Set), cotype (natskel τ) -> cotype τ.*) |
| 55 | +
|
| 56 | +Inductive sorted : forall {C P}, natskel C P -> Prop := |
| 57 | + | sleaf : forall C P, sorted (leaf C P) |
| 58 | + | snode : forall C n (tl:natskel C (sorted C sorted), n > 0 -> sorted (sref_read tl) -> sorted (node _ _ n tl) |
| 59 | +. |
| 60 | +
|
| 61 | +
|
| 62 | +Fixpoint btree |
| 63 | +
|
| 64 | +Definition bound {C P R G}: hpred (natskel C P R G) := |
| 65 | + fun node => fun h => |
| 66 | + match node with |
| 67 | + | leaf => True |
| 68 | + | node n tl => n > 0 |
| 69 | + end. |
| 70 | +*) |
0 commit comments