Mercurial > hg > Gears > GearsAgda
changeset 702:5959f7aa0960
...
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 05 Dec 2021 11:46:20 +0900 |
parents | 690da797cf40 |
children | 23e0b9df7896 |
files | hoareBinaryTree.agda |
diffstat | 1 files changed, 35 insertions(+), 16 deletions(-) [+] |
line wrap: on
line diff
--- a/hoareBinaryTree.agda Sun Dec 05 10:40:44 2021 +0900 +++ b/hoareBinaryTree.agda Sun Dec 05 11:46:20 2021 +0900 @@ -585,9 +585,6 @@ top-value leaf = nothing top-value (node key value tree tree₁) = just value -insertTreeSpec0 : {n : Level} {A : Set n} → (tree : bt A) → (value : A) → top-value tree ≡ just value → ⊤ -insertTreeSpec0 _ _ _ = tt - record findPR {n : Level} {A : Set n} (key : ℕ) (tree : bt A ) (stack : List (bt A)) (C : bt A → List (bt A) → Set n) : Set n where field tree0 : bt A @@ -595,29 +592,36 @@ ti : treeInvariant tree si : stackInvariant key tree tree0 stack ci : C tree stack -- data continuation + +record findExt {n : Level} {A : Set n} (C : bt A → List (bt A) → Set n) : Set (Level.suc n) where + field + c1 : {key key₁ : ℕ} {tree tree₁ : bt A } {st : List (bt A)} {v1 : A} + → findPR key (node key₁ v1 tree tree₁) st C → key < key₁ → C tree (tree ∷ st) + c2 : {key key₁ : ℕ} {tree tree₁ : bt A } {st : List (bt A)} {v1 : A} + → findPR key (node key₁ v1 tree tree₁) st C → key > key₁ → C tree₁ (tree₁ ∷ st) findPP : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (tree : bt A ) → (stack : List (bt A)) - → findPR key tree stack (λ _ _ → Lift n ⊤) - → (next : (tree1 : bt A) → (stack : List (bt A)) → findPR key tree1 stack (λ _ _ → Lift n ⊤) → bt-depth tree1 < bt-depth tree → t ) - → (exit : (tree1 : bt A) → (stack : List (bt A)) → findPR key tree1 stack (λ _ _ → Lift n ⊤) + → {C : bt A → List (bt A) → Set n } → findPR key tree stack C → findExt C + → (next : (tree1 : bt A) → (stack : List (bt A)) → findPR key tree1 stack C → bt-depth tree1 < bt-depth tree → t ) + → (exit : (tree1 : bt A) → (stack : List (bt A)) → findPR key tree1 stack C → (tree1 ≡ leaf ) ∨ ( node-key tree1 ≡ just key ) → t ) → t -findPP key leaf st Pre _ exit = exit leaf st Pre (case1 refl) -findPP key (node key₁ v1 tree tree₁) st Pre next exit with <-cmp key key₁ -findPP key n st Pre _ exit | tri≈ ¬a refl ¬c = exit n st Pre (case2 refl) -findPP {n} {_} {A} key (node key₁ v1 tree tree₁) st Pre next _ | tri< a ¬b ¬c = next tree (tree ∷ st) - record { tree0 = findPR.tree0 Pre ; ti0 = findPR.ti0 Pre ; ti = treeLeftDown tree tree₁ (findPR.ti Pre) ; si = findP1 a st (findPR.si Pre) ; ci = lift tt } depth-1< where +findPP key leaf st Pre _ _ exit = exit leaf st Pre (case1 refl) +findPP key (node key₁ v1 tree tree₁) st Pre _ next exit with <-cmp key key₁ +findPP key n st Pre _ _ exit | tri≈ ¬a refl ¬c = exit n st Pre (case2 refl) +findPP {n} {_} {A} key (node key₁ v1 tree tree₁) st Pre e next _ | tri< a ¬b ¬c = next tree (tree ∷ st) + record { tree0 = findPR.tree0 Pre ; ti0 = findPR.ti0 Pre ; ti = treeLeftDown tree tree₁ (findPR.ti Pre) ; si = findP1 a st (findPR.si Pre) ; ci = findExt.c1 e Pre a } depth-1< where -- findPR key (node key₁ v1 tree tree₁) st C → key < key₁ → C tree (tree ∷ st) findP1 : key < key₁ → (st : List (bt A)) → stackInvariant key (node key₁ v1 tree tree₁) (findPR.tree0 Pre) st → stackInvariant key tree (findPR.tree0 Pre) (tree ∷ st) findP1 a (x ∷ st) si = s-left a si -findPP key n@(node key₁ v1 tree tree₁) st Pre next _ | tri> ¬a ¬b c = next tree₁ (tree₁ ∷ st) - record { tree0 = findPR.tree0 Pre ; ti0 = findPR.ti0 Pre ; ti = treeRightDown tree tree₁ (findPR.ti Pre) ; si = s-right c (findPR.si Pre) ; ci = lift tt } depth-2< +findPP key n@(node key₁ v1 tree tree₁) st Pre e next _ | tri> ¬a ¬b c = next tree₁ (tree₁ ∷ st) + record { tree0 = findPR.tree0 Pre ; ti0 = findPR.ti0 Pre ; ti = treeRightDown tree tree₁ (findPR.ti Pre) ; si = s-right c (findPR.si Pre) ; ci = findExt.c2 e Pre c } depth-2< insertTreePP : {n m : Level} {A : Set n} {t : Set m} → (tree : bt A) → (key : ℕ) → (value : A) → treeInvariant tree → (exit : (tree repl : bt A) → treeInvariant tree ∧ replacedTree key value tree repl → t ) → t insertTreePP {n} {m} {A} {t} tree key value P0 exit = TerminatingLoopS (bt A ∧ List (bt A) ) {λ p → findPR key (proj1 p) (proj2 p) (λ _ _ → Lift n ⊤) } (λ p → bt-depth (proj1 p)) ⟪ tree , tree ∷ [] ⟫ record { tree0 = tree ; ti = P0 ; ti0 = P0 ;si = s-single ; ci = lift tt } - $ λ p P loop → findPP key (proj1 p) (proj2 p) P (λ t s P1 lt → loop ⟪ t , s ⟫ P1 lt ) + $ λ p P loop → findPP key (proj1 p) (proj2 p) P record { c1 = λ _ _ → lift tt ; c2 = λ _ _ → lift tt } (λ t s P1 lt → loop ⟪ t , s ⟫ P1 lt ) $ λ t s P C → replaceNodeP key value t C (findPR.ti P) $ λ t1 P1 R → TerminatingLoopS (List (bt A) ∧ bt A ∧ bt A ) {λ p → replacePR key value (proj1 (proj2 p)) (proj2 (proj2 p)) (proj1 p) (λ _ _ _ → Lift n ⊤ ) } @@ -659,13 +663,28 @@ findP2 | r-right x ri | node key value t t₁ | record { eq = refl } = record { tree1 = t₁ ; ci = ri } findP2 | r-leaf | leaf | record { eq = eq } = ⊥-elim ( nat-≤> c ≤-refl ) +findPPC1 : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (value : A) → (tree : bt A ) → (stack : List (bt A)) + → findPR key tree stack (findPC key value ) + → (next : (tree1 : bt A) → (stack : List (bt A)) → findPR key tree1 stack (findPC key value ) → bt-depth tree1 < bt-depth tree → t ) + → (exit : (tree1 : bt A) → (stack : List (bt A)) → findPR key tree1 stack (findPC key value ) + → (tree1 ≡ leaf ) ∨ ( node-key tree1 ≡ just key ) → t ) → t +findPPC1 {n} {_} {A} key value tree stack Pr next exit = findPP key tree stack Pr findext next exit where + findext01 : {key₁ : ℕ} {key₂ : ℕ} {tree₁ : bt A} {tree₂ : bt A} {st : List (bt A)} {v1 : A} → + findPR key₁ (node key₂ v1 tree₁ tree₂) st (findPC key value) → key₁ < key₂ → findPC key value tree₁ (tree₁ ∷ st) + findext01 = {!!} + findext : findExt (findPC key value) + findext = record { c1 = findext01 ; c2 = {!!} } + +insertTreeSpec0 : {n : Level} {A : Set n} → (tree : bt A) → (value : A) → top-value tree ≡ just value → ⊤ +insertTreeSpec0 _ _ _ = tt + containsTree : {n : Level} {A : Set n} → (tree tree1 : bt A) → (key : ℕ) → (value : A) → treeInvariant tree1 → replacedTree key value tree1 tree → ⊤ containsTree {n} {A} tree tree1 key value P RT = TerminatingLoopS (bt A ∧ List (bt A) ) {λ p → findPR key (proj1 p) (proj2 p) (findPC key value ) } (λ p → bt-depth (proj1 p)) ⟪ tree , tree ∷ [] ⟫ record { tree0 = tree ; ti0 = RTtoTI0 _ _ _ _ P RT ; ti = RTtoTI0 _ _ _ _ P RT ; si = s-single ; ci = record { tree1 = tree1 ; ci = RT } } - $ λ p P loop → findPPC key value (proj1 p) (proj2 p) P (λ t s P1 lt → loop ⟪ t , s ⟫ P1 lt ) + $ λ p P loop → findPPC1 key value (proj1 p) (proj2 p) P (λ t s P1 lt → loop ⟪ t , s ⟫ P1 lt ) $ λ t1 s1 P2 found? → insertTreeSpec0 t1 value (lemma6 t1 s1 found? P2) where lemma6 : (t1 : bt A) (s1 : List (bt A)) (found? : (t1 ≡ leaf) ∨ (node-key t1 ≡ just key)) (P2 : findPR key t1 s1 (findPC key value )) → top-value t1 ≡ just value lemma6 t1 s1 found? P2 = lemma7 t1 s1 (findPR.tree0 P2) ( findPC.tree1 (findPR.ci P2)) (findPC.ci (findPR.ci P2)) (findPR.si P2) found? where @@ -686,6 +705,6 @@ containsTree1 : {n : Level} {A : Set n} → (tree : bt A) → (key : ℕ) → (value : A) → treeInvariant tree → ⊤ containsTree1 {n} {A} tree key value ti = insertTreeP tree key value ti - $ λ tree0 tree1 P → containsTree tree1 tree0 key value (RTtoTI1 _ _ _ _ (proj1 P) (proj2 P) ) (proj2 P) -- (proj1 P) (proj2 P) + $ λ tree0 tree1 P → containsTree tree1 tree0 key value (RTtoTI1 _ _ _ _ (proj1 P) (proj2 P) ) (proj2 P)