changeset 703:23e0b9df7896

embedding invariant extentiion
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 05 Dec 2021 14:50:04 +0900
parents 5959f7aa0960
children aad148b5037d
files hoareBinaryTree.agda
diffstat 1 files changed, 34 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- a/hoareBinaryTree.agda	Sun Dec 05 11:46:20 2021 +0900
+++ b/hoareBinaryTree.agda	Sun Dec 05 14:50:04 2021 +0900
@@ -585,23 +585,23 @@
 top-value leaf = nothing
 top-value (node key value tree tree₁) = just value
 
-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
+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
      ti0 : treeInvariant tree0
      ti : treeInvariant tree
      si : stackInvariant key tree tree0 stack
-     ci : C tree stack     -- data continuation
+     ci : C key tree stack     -- data continuation
 
-record findExt {n : Level} {A : Set n} (C : bt A → List (bt A) → Set n) : Set (Level.suc n) where
+record findExt {n : Level} {A : Set n} (key : ℕ) (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)
+      c1 : {key₁ : ℕ} {tree tree₁ : bt A } {st : List (bt A)} {v1 : A}
+        → findPR key (node key₁ v1 tree tree₁) st C → key < key₁  → C key tree (tree ∷ st)
+      c2 : {key₁ : ℕ} {tree tree₁ : bt A } {st : List (bt A)} {v1 : A}
+        → findPR key (node key₁ v1 tree tree₁) st C → key > key₁  → C key tree₁ (tree₁ ∷ st)
    
 findPP : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (tree : bt A ) → (stack : List (bt A))
-           →  {C : bt A → List (bt A) → Set n } → findPR key tree stack C  → findExt C
+           →  {C : ℕ → bt A → List (bt A) → Set n } → findPR key tree stack C  → findExt key 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
@@ -619,9 +619,9 @@
 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 ∷ [] ⟫
+   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 record { c1 = λ _ _ → lift tt ; c2 =  λ _ _ → lift tt } (λ 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 ⊤ ) }
@@ -629,51 +629,35 @@
        $  λ p P1 loop → replaceP key value  (proj2 (proj2 p)) (proj1 p) P1
             (λ key value {tree1} repl1 stack P2 lt → loop ⟪ stack , ⟪ tree1 , repl1  ⟫ ⟫ P2 lt )  exit 
 
-record findPC {n : Level} {A : Set n} (key1 : ℕ) (value : A) (tree : bt A ) (stack : List (bt A)) : Set n where
+record findPC {n : Level} {A : Set n} (value : A) (key1 : ℕ) (tree : bt A ) (stack : List (bt A)) : Set n where
    field
      tree1 : bt A
      ci : replacedTree key1 value tree1 tree
    
-findPPC : {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
-findPPC key value leaf st Pre _ exit = exit leaf st Pre (case1 refl)
-findPPC key value (node key₁ v1 tree tree₁) st Pre next exit with <-cmp key key₁
-findPPC key value n st Pre _ exit | tri≈ ¬a refl ¬c = exit n st Pre (case2 refl)
-findPPC {n} {_} {A} key value (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 =  s-left a (findPR.si Pre)
-          ; ci = findP2 } depth-1< where
-   findP2 : findPC key value tree (tree ∷ st)
-   findP2 with findPC.ci (findPR.ci Pre) | findPC.tree1 (findPR.ci Pre) | inspect  findPC.tree1 (findPR.ci Pre) 
-   findP2 | r-node | leaf | _ = ⊥-elim ( nat-≤> a ≤-refl )
-   findP2 | r-node | node key value t t₁ | _ = ⊥-elim ( nat-≤> a ≤-refl )
-   findP2 | (r-right x ri) | t | _ = ⊥-elim (nat-<> x a)
-   findP2 | (r-left x ri) | node key value t t₁ | record { eq = refl } = record { tree1 = t ; ci =  ri }
-   findP2 | r-left x ri | leaf | record { eq = () }
-   findP2 | r-leaf | leaf | record { eq = eq } = ⊥-elim ( nat-≤> a ≤-refl )
-findPPC key value 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 = findP2 }  depth-2< where
-   findP2 : findPC key value tree₁ (tree₁ ∷ st)
-   findP2 with findPC.ci (findPR.ci Pre) | findPC.tree1 (findPR.ci Pre) | inspect  findPC.tree1 (findPR.ci Pre) 
-   findP2 | r-node | node key value ti ti₁ | eq = ⊥-elim ( nat-≤> c ≤-refl )
-   findP2 | r-left x ri | ti | eq = ⊥-elim ( nat-<> x c )
-   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 )
+   →  findPR key tree stack (findPC value )
+   → (next : (tree1 : bt A) → (stack : List (bt A)) → findPR key tree1 stack (findPC value ) → bt-depth tree1 < bt-depth tree   → t )
+   → (exit : (tree1 : bt A) → (stack : List (bt A)) → findPR key tree1 stack (findPC 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 = {!!} }
+   findext01 :  {key₂ : ℕ} {tree₁ : bt A} {tree₂ : bt A} {st : List (bt A)} {v1 : A}
+      → (Pre : findPR key (node key₂ v1 tree₁ tree₂) st (findPC value) )
+      → key < key₂ → findPC value key tree₁ (tree₁ ∷ st)
+   findext01 Pre a with findPC.ci (findPR.ci Pre) | findPC.tree1 (findPR.ci Pre) | inspect  findPC.tree1 (findPR.ci Pre)
+   ... | r-leaf | leaf | record { eq = refl } = ⊥-elim ( nat-≤> a  ≤-refl) 
+   ... | r-node | node key value t1 t3 | record { eq = refl } = ⊥-elim ( nat-≤> a  ≤-refl ) 
+   ... | r-right x t | t1 | t2 = ⊥-elim (nat-<> x a)
+   ... | r-left x ri | node key value t1 t3 | record { eq = refl } = record { tree1 = t1 ; ci = ri }
+   findext02 :  {key₂ : ℕ} {tree₁ : bt A} {tree₂ : bt A} {st : List (bt A)} {v1 : A}
+      → (Pre : findPR key (node key₂ v1 tree₁ tree₂) st (findPC value) )
+      → key > key₂ → findPC value key tree₂ (tree₂ ∷ st)
+   findext02 Pre c with findPC.ci (findPR.ci Pre) | findPC.tree1 (findPR.ci Pre) | inspect  findPC.tree1 (findPR.ci Pre)
+   ... | r-leaf | leaf | record { eq = refl } = ⊥-elim ( nat-≤> c  ≤-refl) 
+   ... | r-node | node key value t1 t3 | record { eq = refl } = ⊥-elim ( nat-≤> c  ≤-refl ) 
+   ... | r-left x t | t1 | t2 = ⊥-elim (nat-<> x c)
+   ... | r-right x ri | node key value t1 t3 | record { eq = refl } = record { tree1 = t3 ; ci = ri }
+   findext : findExt key (findPC value )
+   findext = record { c1 = findext01 ; c2 = findext02 }
 
 insertTreeSpec0 : {n : Level} {A : Set n} → (tree : bt A) → (value : A) → top-value tree ≡ just value → ⊤
 insertTreeSpec0 _ _ _ = tt
@@ -681,12 +665,12 @@
 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)) 
+     {λ p → findPR key (proj1 p) (proj2 p) (findPC 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 → 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 : bt A) (s1 : List (bt A)) (found? : (t1 ≡ leaf) ∨ (node-key t1 ≡ just key)) (P2 : findPR key t1 s1 (findPC 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
               lemma8 : {tree1 t1 : bt A } → replacedTree key  value tree1 t1 → node-key t1 ≡ just key → top-value t1 ≡ just value
               lemma8 {.leaf} {node key value .leaf .leaf} r-leaf refl = refl