Mercurial > hg > Gears > GearsAgda
changeset 655:d0394c191d84
...
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 21 Nov 2021 10:54:13 +0900 |
parents | 48c6e6961ea5 |
children | 30690aed1819 |
files | hoareBinaryTree.agda |
diffstat | 1 files changed, 42 insertions(+), 50 deletions(-) [+] |
line wrap: on
line diff
--- a/hoareBinaryTree.agda Sun Nov 21 10:09:05 2021 +0900 +++ b/hoareBinaryTree.agda Sun Nov 21 10:54:13 2021 +0900 @@ -103,14 +103,12 @@ → treeInvariant (node key₁ value₁ (node key value t₁ t₂) (node key₂ value₂ t₃ t₄)) data stackInvariant {n : Level} {A : Set n} (key : ℕ) : (top orig : bt A) → (stack : List (bt A)) → Set n where - s-left0 : {tree tree₁ : bt A} → {key₁ : ℕ } → {v1 : A } - → key < key₁ → stackInvariant key (node key₁ v1 tree tree₁) (node key₁ v1 tree tree₁) (node key₁ v1 tree tree₁ ∷ []) - s-right0 : {tree₁ tree : bt A} → {key₁ : ℕ } → {v1 : A } - → key₁ < key → stackInvariant key (node key₁ v1 tree tree₁) (node key₁ v1 tree tree₁) (node key₁ v1 tree tree₁ ∷ []) + s-nil : {tree : bt A} → stackInvariant key tree tree [] + s-single : {tree : bt A} → stackInvariant key tree tree [] → stackInvariant key tree tree (tree ∷ []) s-right : {tree tree0 tree₁ : bt A} → {key₁ : ℕ } → {v1 : A } → {st : List (bt A)} - → key₁ < key → stackInvariant key (node key₁ v1 tree₁ tree) tree0 st → stackInvariant key tree tree0 (tree ∷ st) + → key₁ < key → stackInvariant key (node key₁ v1 tree₁ tree) tree0 st → ¬ (st ≡ []) → stackInvariant key tree tree0 (tree ∷ st) s-left : {tree₁ tree0 tree : bt A} → {key₁ : ℕ } → {v1 : A } → {st : List (bt A)} - → key < key₁ → stackInvariant key (node key₁ v1 tree₁ tree) tree0 st → stackInvariant key tree₁ tree0 (tree₁ ∷ st) + → key < key₁ → stackInvariant key (node key₁ v1 tree₁ tree) tree0 st → ¬ (st ≡ []) → stackInvariant key tree₁ tree0 (tree₁ ∷ st) data replacedTree {n : Level} {A : Set n} (key : ℕ) (value : A) : (tree tree1 : bt A ) → Set n where r-leaf : replacedTree key value leaf (node key value leaf leaf) @@ -121,16 +119,10 @@ → k > key → replacedTree key value t1 t2 → replacedTree key value (node k v1 t1 t) (node k v1 t2 t) replFromStack : {n : Level} {A : Set n} {key : ℕ} {top orig : bt A} → {stack : List (bt A)} → stackInvariant key top orig stack → bt A -replFromStack (s-right0 {tree} x) = tree -replFromStack (s-left0 {tree} x) = tree -replFromStack (s-right {tree} x st) = tree -replFromStack (s-left {tree} x st) = tree - -stackInvariant-leaf : {n : Level} {A : Set n} {key : ℕ} {top orig : bt A} → {stack : List (bt A)} → stackInvariant key top orig stack → ¬ (orig ≡ leaf) -stackInvariant-leaf {_} {_} {_} {_} {_} (s-right0 x) () -stackInvariant-leaf {_} {_} {_} {_} {_} (s-left0 x) () -stackInvariant-leaf {_} {_} {_} {_} {_} (s-right x st) eq = stackInvariant-leaf st eq -stackInvariant-leaf {_} {_} {_} {_} {_} (s-left x st) eq = stackInvariant-leaf st eq +replFromStack (s-nil {tree} ) = tree +replFromStack (s-single {tree} _ ) = tree +replFromStack (s-right {tree} x _ st) = tree +replFromStack (s-left {tree} x _ st) = tree add< : { i : ℕ } (j : ℕ ) → i < suc i + j add< {i} j = begin @@ -155,24 +147,25 @@ stack-last (x ∷ s) = stack-last s stackInvariantTest1 : stackInvariant 4 treeTest2 treeTest1 ( treeTest2 ∷ treeTest1 ∷ [] ) -stackInvariantTest1 = s-right (add< 2) (s-right0 (add< 2)) +stackInvariantTest1 = s-right (add< 2) (s-single s-nil) (λ ()) -si-property1 : {n : Level} {A : Set n} (key : ℕ) (tree tree0 : bt A) → (stack : List (bt A)) → stackInvariant key tree tree0 stack +si-property1 : {n : Level} {A : Set n} (key : ℕ) (tree tree0 : bt A) → (stack : List (bt A)) → ¬ (stack ≡ []) → stackInvariant key tree tree0 stack → stack-top stack ≡ just tree -si-property1 key t t0 (t ∷ st) (s-right0 _ ) = refl -si-property1 key t t0 (t ∷ st) (s-left0 _ ) = refl -si-property1 key t t0 (t ∷ st) (s-right _ si) = refl -si-property1 key t t0 (t ∷ st) (s-left _ si) = refl +si-property1 key t t0 [] ne (s-nil ) = ⊥-elim ( ne refl ) +si-property1 key t t0 (t ∷ []) ne (s-single _) = refl +si-property1 key t t0 (t ∷ st) _ (s-right _ _ si) = refl +si-property1 key t t0 (t ∷ st) _ (s-left _ _ si) = refl -si-property-last : {n : Level} {A : Set n} (key : ℕ) (tree tree0 : bt A) → (stack : List (bt A)) → stackInvariant key tree tree0 stack +si-property-last : {n : Level} {A : Set n} (key : ℕ) (tree tree0 : bt A) → (stack : List (bt A)) → ¬ (stack ≡ []) → stackInvariant key tree tree0 stack → stack-last stack ≡ just tree0 -si-property-last key t t0 (.t ∷ []) (s-right0 _ ) = refl -si-property-last key t t0 (.t ∷ []) (s-left0 _ ) = refl -si-property-last key t t0 (.t ∷ x ∷ st) (s-right _ si) with si-property1 key _ _ (x ∷ st) si -... | refl = si-property-last key x t0 (x ∷ st) si -si-property-last key t t0 (.t ∷ x ∷ st) (s-left _ si) with si-property1 key _ _ (x ∷ st) si -... | refl = si-property-last key x t0 (x ∷ st) si - +si-property-last key t t0 [] ne s-nil = ⊥-elim ( ne refl ) +si-property-last key t t0 (t ∷ []) _ (s-single _) = refl +si-property-last key t t0 (t ∷ []) _ (s-right _ _ ne) = ⊥-elim ( ne refl ) +si-property-last key t t0 (t ∷ []) _ (s-left _ _ ne) = ⊥-elim ( ne refl ) +si-property-last key t t0 (.t ∷ x ∷ st) ne (s-right _ si _) with si-property1 key _ _ (x ∷ st) (λ ()) si +... | refl = si-property-last key x t0 (x ∷ st) (λ ()) si +si-property-last key t t0 (.t ∷ x ∷ st) ne (s-left _ si _) with si-property1 key _ _ (x ∷ st) (λ ()) si +... | refl = si-property-last key x t0 (x ∷ st) (λ ()) si ti-right : {n : Level} {A : Set n} {tree₁ repl : bt A} → {key₁ : ℕ} → {v1 : A} → treeInvariant (node key₁ v1 tree₁ repl) → treeInvariant repl ti-right {_} {_} {.leaf} {_} {key₁} {v1} (t-single .key₁ .v1) = t-leaf ti-right {_} {_} {.leaf} {_} {key₁} {v1} (t-right x ti) = ti @@ -187,12 +180,12 @@ stackTreeInvariant : {n : Level} {A : Set n} (key : ℕ) (sub tree : bt A) → (stack : List (bt A)) → treeInvariant tree → stackInvariant key sub tree stack → treeInvariant sub -stackTreeInvariant {_} {A} key sub tree (sub ∷ []) ti (s-right0 _ ) = ti -stackTreeInvariant {_} {A} key sub tree (sub ∷ []) ti (s-left0 _ ) = ti -stackTreeInvariant {_} {A} key sub tree (sub ∷ st) ti (s-right _ si) = ti-right (si1 si) where +stackTreeInvariant {_} {A} key sub tree [] ti s-nil = ti +stackTreeInvariant {_} {A} key sub tree (sub ∷ []) ti (s-single _) = ti +stackTreeInvariant {_} {A} key sub tree (sub ∷ st) ti (s-right _ si _) = ti-right (si1 si) where si1 : {tree₁ : bt A} → {key₁ : ℕ} → {v1 : A} → stackInvariant key (node key₁ v1 tree₁ sub ) tree st → treeInvariant (node key₁ v1 tree₁ sub ) si1 {tree₁ } {key₁ } {v1 } si = stackTreeInvariant key (node key₁ v1 tree₁ sub ) tree st ti si -stackTreeInvariant {_} {A} key sub tree (sub ∷ st) ti (s-left _ si) = ti-left ( si2 si) where +stackTreeInvariant {_} {A} key sub tree (sub ∷ st) ti (s-left _ si _) = ti-left ( si2 si) where si2 : {tree₁ : bt A} → {key₁ : ℕ} → {v1 : A} → stackInvariant key (node key₁ v1 sub tree₁ ) tree st → treeInvariant (node key₁ v1 sub tree₁ ) si2 {tree₁ } {key₁ } {v1 } si = stackTreeInvariant key (node key₁ v1 sub tree₁ ) tree st ti si @@ -240,10 +233,11 @@ findP key leaf tree0 st Pre _ exit = exit leaf tree0 st Pre (case1 refl) findP key (node key₁ v1 tree tree₁) tree0 st Pre next exit with <-cmp key key₁ findP key n tree0 st Pre _ exit | tri≈ ¬a refl ¬c = exit n tree0 st Pre (case2 refl) -findP key n@(node key₁ v1 tree tree₁) tree0 st Pre next _ | tri< a ¬b ¬c = next tree tree0 (tree ∷ st) ⟪ treeLeftDown tree tree₁ (proj1 Pre) , findP1 a (proj2 Pre) ⟫ depth-1< where - findP1 : key < key₁ → stackInvariant key (node key₁ v1 tree tree₁) tree0 st → stackInvariant key tree tree0 (tree ∷ st) - findP1 a si = s-left a si -findP key n@(node key₁ v1 tree tree₁) tree0 st Pre next _ | tri> ¬a ¬b c = next tree₁ tree0 (tree₁ ∷ st) ⟪ treeRightDown tree tree₁ (proj1 Pre) , s-right c (proj2 Pre) ⟫ depth-2< +findP {_} {_} {A} key n@(node key₁ v1 tree tree₁) tree0 st Pre next _ | tri< a ¬b ¬c = next tree tree0 (tree ∷ st) ⟪ treeLeftDown tree tree₁ (proj1 Pre) , findP1 a st (proj2 Pre) ⟫ depth-1< where + findP1 : key < key₁ → (st : List (bt A)) → stackInvariant key (node key₁ v1 tree tree₁) tree0 st → stackInvariant key tree tree0 (tree ∷ st) + findP1 a [] s-nil = {!!} + findP1 a (x ∷ st) si = s-left a si (λ ()) +findP key n@(node key₁ v1 tree tree₁) tree0 st Pre next _ | tri> ¬a ¬b c = next tree₁ tree0 (tree₁ ∷ st) ⟪ treeRightDown tree tree₁ (proj1 Pre) , s-right c (proj2 Pre) {!!} ⟫ depth-2< replaceTree1 : {n : Level} {A : Set n} {t t₁ : bt A } → ( k : ℕ ) → (v1 value : A ) → treeInvariant (node k v1 t t₁) → treeInvariant (node k value t t₁) replaceTree1 k v1 value (t-single .k .v1) = t-single k value @@ -275,30 +269,28 @@ → treeInvariant tree0 ∧ stackInvariant key tree-st tree0 stack1 ∧ replacedTree key value tree1 repl → length stack1 < length stack → t) → (exit : (tree1 repl : bt A) → treeInvariant tree1 ∧ replacedTree key value tree1 repl → t) → t replaceP key value {tree0} {tree} {tree-st} repl [] Pre next exit with proj1 (proj2 Pre) -... | () +... | t = {!!} replaceP {_} {_} {A} key value {tree0} {tree} {tree-st} repl (leaf ∷ []) Pre next exit with proj1 (proj2 Pre) -... | s-right x () -... | s-left x () +... | s-right x t _ = {!!} +... | s-left x t _ = {!!} replaceP key value {tree0} {tree} {tree-st} repl (leaf ∷ leaf ∷ st) Pre next exit with proj1 (proj2 Pre) -... | s-right x () -... | s-left x () +... | s-right x t _ = {!!} +... | s-left x t _ = {!!} replaceP {_} {_} {A} key value {tree0} {tree} {tree-st} repl (leaf ∷ node key₁ value₁ left right ∷ st) Pre next exit with <-cmp key key₁ ... | tri< a ¬b ¬c = next key value (node key₁ value₁ repl right ) (node key₁ value₁ tree right ∷ st) ⟪ proj1 Pre , ⟪ repl5 (proj1 (proj2 Pre)) , r-left a (proj2 (proj2 Pre)) ⟫ ⟫ ≤-refl where repl5 : stackInvariant key tree-st tree0 (leaf ∷ node key₁ value₁ left right ∷ st) → stackInvariant key (node key₁ value₁ tree right) tree0 (node key₁ value₁ tree right ∷ st ) - repl5 si with si-property1 _ _ _ _ si - repl5 (s-right x si) | refl = s-left a {!!} - repl5 (s-left x si) | refl = s-left a {!!} + repl5 si with si-property1 _ _ _ _ {!!} si + repl5 (s-right x si _) | refl = s-left a {!!} {!!} + repl5 (s-left x si _) | refl = s-left a {!!} {!!} ... | tri≈ ¬a b ¬c = next key value (node key₁ value left right) st {!!} depth-3< ... | tri> ¬a ¬b c = next key value (node key₁ value₁ repl right) st {!!} depth-3< replaceP key value {tree0} {tree} {tree-st} repl (node key₁ value₁ left right ∷ st) Pre next exit with <-cmp key key₁ ... | tri> ¬a ¬b c = next key value (node key₁ value₁ repl right ) st {!!} ≤-refl ... | tri≈ ¬a b ¬c = next key value (node key value left right ) st {!!} ≤-refl where -- this case won't happen ... | tri< a ¬b ¬c with proj1 (proj2 Pre) -... | s-right0 x = {!!} -... | s-left0 x = {!!} -... | s-right x si1 = {!!} -... | s-left x si1 = next key value (node key₁ value₁ repl right ) st ⟪ proj1 Pre , ⟪ si1 , r-left a (proj2 (proj2 Pre)) ⟫ ⟫ ≤-refl +... | s-right x si1 _ = {!!} +... | s-left x si1 _ = next key value (node key₁ value₁ repl right ) st ⟪ proj1 Pre , ⟪ si1 , r-left a (proj2 (proj2 Pre)) ⟫ ⟫ ≤-refl -- = next key value (node key₁ value₁ repl right ) st ⟪ proj1 Pre , ⟪ repl2 (proj1 (proj2 Pre)) , r-left a {!!} ⟫ ⟫ ≤-refl where -- repl2 : stackInvariant key tree tree0 (node key₁ value₁ left right ∷ st) → stackInvariant key (node key₁ value₁ left right) tree0 st -- repl2 (s-single .(node key₁ value₁ left right)) = {!!}