Mercurial > hg > Members > Moririn
changeset 620:fe8c2d82c05c
...
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 07 Nov 2021 23:00:57 +0900 |
parents | a3fbc9b57015 |
children | 6861bcb9c54d |
files | hoareBinaryTree.agda |
diffstat | 1 files changed, 34 insertions(+), 20 deletions(-) [+] |
line wrap: on
line diff
--- a/hoareBinaryTree.agda Sun Nov 07 19:43:16 2021 +0900 +++ b/hoareBinaryTree.agda Sun Nov 07 23:00:57 2021 +0900 @@ -33,6 +33,14 @@ node : (key : ℕ) → (value : A) → (left : bt A ) → (right : bt A ) → bt A +node-key : {n : Level} {A : Set n} → bt A → Maybe ℕ +node-key (node key _ _ _) = just key +node-key _ = nothing + +node-value : {n : Level} {A : Set n} → bt A → Maybe A +node-value (node _ value _ _) = just value +node-value _ = nothing + bt-depth : {n : Level} {A : Set n} → (tree : bt A ) → ℕ bt-depth leaf = 0 bt-depth (node key value t t₁) = suc (Data.Nat._⊔_ (bt-depth t ) (bt-depth t₁ )) @@ -77,22 +85,28 @@ open import Data.Unit hiding ( _≟_ ; _≤?_ ; _≤_) -treeInvariant : {n : Level} {A : Set n} → (tree : bt A) → Set -treeInvariant leaf = ⊤ -treeInvariant (node key value leaf leaf) = ⊤ -treeInvariant (node key value leaf n@(node key₁ value₁ t₁ t₂)) = (key < key₁) ∧ treeInvariant n -treeInvariant (node key value n@(node key₁ value₁ t t₁) leaf) = treeInvariant n ∧ (key < key₁) -treeInvariant (node key value n@(node key₁ value₁ t t₁) m@(node key₂ value₂ t₂ t₃)) = treeInvariant n ∧ (key < key₁) ∧ (key₁ < key₂) ∧ treeInvariant m +data treeInvariant {n : Level} {A : Set n} : (tree : bt A) → Set n where + t-leaf : treeInvariant leaf + t-single : {key : ℕ} → {value : A} → treeInvariant (node key value leaf leaf) + t-right : {key key₁ : ℕ} → {value value₁ : A} → {t₁ t₂ : bt A} → (key < key₁) → treeInvariant (node key₁ value₁ t₁ t₂) → treeInvariant (node key value leaf (node key₁ value₁ t₁ t₂)) + t-left : {key key₁ : ℕ} → {value value₁ : A} → {t₁ t₂ : bt A} → (key₁ < key) → treeInvariant (node key value₁ t₁ t₂) → treeInvariant (node key₁ value₁ (node key value₁ t₁ t₂) leaf ) + t-node : {key key₁ key₂ : ℕ} → {value value₁ value₂ : A} → {t₁ t₂ t₃ t₄ : bt A} → (key < key₁) → (key₁ < key₂) + → treeInvariant (node key value t₁ t₂) + → treeInvariant (node key₂ value₂ t₃ t₄) + → treeInvariant (node key₁ value₁ (node key value t₁ t₂) (node key₂ value₂ t₃ t₄)) -treeInvariantTest1 = treeInvariant (node 3 0 leaf (node 1 1 leaf (node 3 5 leaf leaf))) +treeInvariantTest1 : treeInvariant (node 3 0 leaf (node 1 1 leaf (node 3 5 leaf leaf))) +treeInvariantTest1 = {!!} -stackInvariant : {n : Level} {A : Set n} → (tree : bt A) → (stack : List (bt A)) → Set n -stackInvariant {_} {A} _ [] = Lift _ ⊤ -stackInvariant {_} {A} tree (tree1 ∷ [] ) = tree1 ≡ tree -stackInvariant {_} {A} tree (x ∷ tail @ (node key value leaf right ∷ _) ) = (right ≡ x) ∧ stackInvariant tree tail -stackInvariant {_} {A} tree (x ∷ tail @ (node key value left leaf ∷ _) ) = (left ≡ x) ∧ stackInvariant tree tail -stackInvariant {_} {A} tree (x ∷ tail @ (node key value left right ∷ _ )) = ( (left ≡ x) ∧ stackInvariant tree tail) ∨ ( (right ≡ x) ∧ stackInvariant tree tail) -stackInvariant {_} {A} tree s = Lift _ ⊥ +data stackInvariant {n : Level} {A : Set n} : (tree0 : bt A) → (stack : List (bt A)) → Set n where + s-nil : stackInvariant leaf [] + s-single : (tree : bt A) → stackInvariant tree (tree ∷ [] ) + s-right : (tree : bt A) → {key : ℕ } → {value : A } { left : bt A} → stackInvariant (node key value left tree ) (tree ∷ node key value left tree ∷ []) + s-left : (tree : bt A) → {key : ℕ } → {value : A } { right : bt A} → stackInvariant (node key value tree right) (tree ∷ node key value tree right ∷ []) + s-< : (tree0 tree : bt A) → {key : ℕ } → {value : A } { left : bt A} → {st : List (bt A)} + → stackInvariant tree0 (tree ∷ st ) → stackInvariant tree0 ((node key value left tree ) ∷ tree ∷ st ) + s-> : (tree0 tree : bt A) → {key : ℕ } → {value : A } { right : bt A} → {st : List (bt A)} + → stackInvariant tree0 (tree ∷ st ) → stackInvariant tree0 ((node key value tree right ) ∷ 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) @@ -163,7 +177,7 @@ insertTreeP : {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 insertTreeP {n} {m} {A} {t} tree key value P exit = - TerminatingLoopS (bt A ∧ List (bt A) ) {λ p → treeInvariant (proj1 p) ∧ stackInvariant tree (proj2 p) } (λ p → bt-depth (proj1 p)) ⟪ tree , [] ⟫ ⟪ P , lift tt ⟫ + TerminatingLoopS (bt A ∧ List (bt A) ) {λ p → treeInvariant (proj1 p) ∧ stackInvariant tree (proj2 p) } (λ p → bt-depth (proj1 p)) ⟪ tree , [] ⟫ ⟪ P , {!!} ⟫ $ λ p P loop → findP key (proj1 p) tree (proj2 p) {!!} (λ t _ s P1 lt → loop ⟪ t , s ⟫ {!!} lt ) $ λ t _ s P → replaceNodeP key value t (proj1 P) $ λ t1 P1 R → TerminatingLoopS (List (bt A) ∧ (bt A ∧ bt A )) @@ -197,16 +211,16 @@ next tree (n ∷ st) (record {ti = findPP0 tree tree₁ (findPR.ti (Pre n st)) ; si = findPP2 st (findPR.si (Pre n st))} ) findPP1 where tree0 = findPR.tree0 (Pre n st) findPP0 : (tree tree₁ : bt A) → treeInvariant ( node key₁ v tree tree₁ ) → treeInvariant tree - findPP0 leaf t x = tt - findPP0 (node key value tree tree₁) leaf x = proj1 x - findPP0 (node key value tree tree₁) (node key₁ value₁ t t₁) x = proj1 x + findPP0 leaf t x = {!!} + findPP0 (node key value tree tree₁) leaf x = proj1 {!!} + findPP0 (node key value tree tree₁) (node key₁ value₁ t t₁) x = proj1 {!!} findPP2 : (st : List (bt A)) → stackInvariant tree0 st → stackInvariant tree0 (node key₁ v tree tree₁ ∷ st) - findPP2 [] (lift tt) = {!!} + findPP2 [] = {!!} findPP2 (leaf ∷ st) x = {!!} findPP2 (node key value leaf leaf ∷ st) x = {!!} findPP2 (node key value leaf (node key₁ value₁ x₂ x₃) ∷ st) x = {!!} findPP2 (node key value (node key₁ value₁ x₁ x₃) leaf ∷ st) x = {!!} - findPP2 (node key value (node key₁ value₁ x₁ x₃) (node key₂ value₂ x₂ x₄) ∷ st) x = case1 ⟪ {!!} , {!!} ⟫ + findPP2 (node key value (node key₁ value₁ x₁ x₃) (node key₂ value₂ x₂ x₄) ∷ st) x = {!!} findPP1 : suc ( bt-depth tree ) ≤ suc (bt-depth tree Data.Nat.⊔ bt-depth tree₁) findPP1 = {!!} findPP key n@(node key₁ v tree tree₁) st Pre next exit | tri> ¬a ¬b c = next tree₁ (n ∷ st) {!!} findPP2 where -- Cond n st → Cond tree₁ (n ∷ st)