annotate hoareBinaryTree.agda @ 956:bfc7007177d0 default tip

safe and cubical compatible with no warning done
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 19 Oct 2024 09:48:48 +0900
parents f2a3f5707075
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
586
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff changeset
1 module hoareBinaryTree where
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff changeset
2
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff changeset
3 open import Level renaming (zero to Z ; suc to succ)
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff changeset
4
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff changeset
5 open import Data.Nat hiding (compare)
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff changeset
6 open import Data.Nat.Properties as NatProp
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff changeset
7 open import Data.Maybe
588
8627d35d4bff add data bt', and some function
ryokka
parents: 587
diff changeset
8 -- open import Data.Maybe.Properties
586
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff changeset
9 open import Data.Empty
933
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 782
diff changeset
10 open import Data.List hiding ( find )
586
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff changeset
11 open import Data.Product
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff changeset
12
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff changeset
13 open import Function as F hiding (const)
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff changeset
14
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff changeset
15 open import Relation.Binary
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff changeset
16 open import Relation.Binary.PropositionalEquality
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff changeset
17 open import Relation.Nullary
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff changeset
18 open import logic
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff changeset
19
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff changeset
20
588
8627d35d4bff add data bt', and some function
ryokka
parents: 587
diff changeset
21 _iso_ : {n : Level} {a : Set n} → ℕ → ℕ → Set
8627d35d4bff add data bt', and some function
ryokka
parents: 587
diff changeset
22 d iso d' = (¬ (suc d ≤ d')) ∧ (¬ (suc d' ≤ d))
8627d35d4bff add data bt', and some function
ryokka
parents: 587
diff changeset
23
8627d35d4bff add data bt', and some function
ryokka
parents: 587
diff changeset
24 iso-intro : {n : Level} {a : Set n} {x y : ℕ} → ¬ (suc x ≤ y) → ¬ (suc y ≤ x) → _iso_ {n} {a} x y
8627d35d4bff add data bt', and some function
ryokka
parents: 587
diff changeset
25 iso-intro = λ z z₁ → record { proj1 = z ; proj2 = z₁ }
8627d35d4bff add data bt', and some function
ryokka
parents: 587
diff changeset
26
590
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 589
diff changeset
27 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 589
diff changeset
28 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 589
diff changeset
29 -- no children , having left node , having right node , having both
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 589
diff changeset
30 --
597
ryokka
parents: 596
diff changeset
31 data bt {n : Level} (A : Set n) : Set n where
604
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
32 leaf : bt A
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
33 node : (key : ℕ) → (value : A) →
610
8239583dac0b add one more stack
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 609
diff changeset
34 (left : bt A ) → (right : bt A ) → bt A
600
016a8deed93d fix old binary tree
ryokka
parents: 597
diff changeset
35
620
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
36 node-key : {n : Level} {A : Set n} → bt A → Maybe ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
37 node-key (node key _ _ _) = just key
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
38 node-key _ = nothing
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
39
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
40 node-value : {n : Level} {A : Set n} → bt A → Maybe A
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
41 node-value (node _ value _ _) = just value
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
42 node-value _ = nothing
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
43
609
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
44 bt-depth : {n : Level} {A : Set n} → (tree : bt A ) → ℕ
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
45 bt-depth leaf = 0
618
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 617
diff changeset
46 bt-depth (node key value t t₁) = suc (Data.Nat._⊔_ (bt-depth t ) (bt-depth t₁ ))
606
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
47
716
a36147bb596d fix context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 710
diff changeset
48 find' : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (tree : bt A ) → List (bt A)
604
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
49 → (next : bt A → List (bt A) → t ) → (exit : bt A → List (bt A) → t ) → t
716
a36147bb596d fix context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 710
diff changeset
50 find' key leaf st _ exit = exit leaf st
a36147bb596d fix context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 710
diff changeset
51 find' key (node key₁ v1 tree tree₁) st next exit with <-cmp key key₁
a36147bb596d fix context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 710
diff changeset
52 find' key n st _ exit | tri≈ ¬a b ¬c = exit n st
a36147bb596d fix context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 710
diff changeset
53 find' key n@(node key₁ v1 tree tree₁) st next _ | tri< a ¬b ¬c = next tree (n ∷ st)
a36147bb596d fix context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 710
diff changeset
54 find' key n@(node key₁ v1 tree tree₁) st next _ | tri> ¬a ¬b c = next tree₁ (n ∷ st)
a36147bb596d fix context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 710
diff changeset
55
a36147bb596d fix context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 710
diff changeset
56 find : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (tree : bt A ) → (stack : List (bt A))
a36147bb596d fix context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 710
diff changeset
57 → (next : (tree1 : bt A) → (stack : List (bt A)) → t)
a36147bb596d fix context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 710
diff changeset
58 → (exit : (tree1 : bt A) → (stack : List (bt A)) → t) → t
a36147bb596d fix context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 710
diff changeset
59 find key leaf st _ exit = exit leaf st
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
60 find key (node key₁ v1 tree tree₁) st next exit with <-cmp key key₁
716
a36147bb596d fix context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 710
diff changeset
61 find key n st _ exit | tri≈ ¬a refl ¬c = exit n st
a36147bb596d fix context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 710
diff changeset
62 find {n} {_} {A} key (node key₁ v1 tree tree₁) st next _ | tri< a ¬b ¬c = next tree (tree ∷ st)
a36147bb596d fix context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 710
diff changeset
63 find key n@(node key₁ v1 tree tree₁) st next _ | tri> ¬a ¬b c = next tree₁ (tree₁ ∷ st)
597
ryokka
parents: 596
diff changeset
64
604
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
65 {-# TERMINATING #-}
609
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
66 find-loop : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → bt A → List (bt A) → (exit : bt A → List (bt A) → t) → t
611
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 610
diff changeset
67 find-loop {n} {m} {A} {t} key tree st exit = find-loop1 tree st where
604
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
68 find-loop1 : bt A → List (bt A) → t
716
a36147bb596d fix context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 710
diff changeset
69 find-loop1 tree st = find key tree st find-loop1 exit
600
016a8deed93d fix old binary tree
ryokka
parents: 597
diff changeset
70
611
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 610
diff changeset
71 replaceNode : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (value : A) → bt A → (bt A → t) → t
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
72 replaceNode k v1 leaf next = next (node k v1 leaf leaf)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
73 replaceNode k v1 (node key value t t₁) next = next (node k v1 t t₁)
611
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 610
diff changeset
74
609
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
75 replace : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (value : A) → bt A → List (bt A) → (next : ℕ → A → bt A → List (bt A) → t ) → (exit : bt A → t) → t
669
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 668
diff changeset
76 replace key value repl [] next exit = exit repl -- can't happen
690
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
77 replace key value repl (leaf ∷ []) next exit = exit repl
669
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 668
diff changeset
78 replace key value repl (node key₁ value₁ left right ∷ []) next exit with <-cmp key key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 668
diff changeset
79 ... | tri< a ¬b ¬c = exit (node key₁ value₁ repl right )
664
1f702351fd1f findP done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 663
diff changeset
80 ... | tri≈ ¬a b ¬c = exit (node key₁ value left right )
669
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 668
diff changeset
81 ... | tri> ¬a ¬b c = exit (node key₁ value₁ left repl )
716
a36147bb596d fix context
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 710
diff changeset
82 replace key value repl (leaf ∷ st) next exit = next key value repl st
669
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 668
diff changeset
83 replace key value repl (node key₁ value₁ left right ∷ st) next exit with <-cmp key key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 668
diff changeset
84 ... | tri< a ¬b ¬c = next key value (node key₁ value₁ repl right ) st
604
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
85 ... | tri≈ ¬a b ¬c = next key value (node key₁ value left right ) st
669
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 668
diff changeset
86 ... | tri> ¬a ¬b c = next key value (node key₁ value₁ left repl ) st
586
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff changeset
87
604
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
88 {-# TERMINATING #-}
609
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
89 replace-loop : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (value : A) → bt A → List (bt A) → (exit : bt A → t) → t
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
90 replace-loop {_} {_} {A} {t} key value tree st exit = replace-loop1 key value tree st where
604
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
91 replace-loop1 : (key : ℕ) → (value : A) → bt A → List (bt A) → t
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
92 replace-loop1 key value tree st = replace key value tree st replace-loop1 exit
586
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff changeset
93
609
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
94 insertTree : {n m : Level} {A : Set n} {t : Set m} → (tree : bt A) → (key : ℕ) → (value : A) → (next : bt A → t ) → t
662
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
95 insertTree tree key value exit = find-loop key tree ( tree ∷ [] ) $ λ t st → replaceNode key value t $ λ t1 → replace-loop key value t1 st exit
587
f103f07c0552 add insert code
ryokka
parents: 586
diff changeset
96
604
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
97 insertTest1 = insertTree leaf 1 1 (λ x → x )
611
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 610
diff changeset
98 insertTest2 = insertTree insertTest1 2 1 (λ x → x )
718
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 717
diff changeset
99 insertTest3 = insertTree insertTest2 3 3 (λ x → x )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 717
diff changeset
100 insertTest4 = insertTree insertTest3 1 4 (λ x → x ) -- this is wrong
587
f103f07c0552 add insert code
ryokka
parents: 586
diff changeset
101
710
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
102 updateTree : {n m : Level} {A : Set n} {t : Set m} → (tree : bt A) → (key : ℕ) → (value : A) → (empty : bt A → t ) → (next : A → bt A → t ) → t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
103 updateTree {_} {_} {A} {t} tree key value empty next = find-loop key tree ( tree ∷ [] )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
104 $ λ t st → replaceNode key value t $ λ t1 → replace-loop key value t1 st (found? st) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
105 found? : List (bt A) → bt A → t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
106 found? [] tree = empty tree -- can't happen
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
107 found? (leaf ∷ st) tree = empty tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
108 found? (node key value x x₁ ∷ st) tree = next value tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
109
933
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 782
diff changeset
110 open import Data.Unit hiding ( _≟_ ) -- ; _≤?_ ; _≤_)
605
f8cc98fcc34b define invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 604
diff changeset
111
620
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
112 data treeInvariant {n : Level} {A : Set n} : (tree : bt A) → Set n where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
113 t-leaf : treeInvariant leaf
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
114 t-single : (key : ℕ) → (value : A) → treeInvariant (node key value leaf leaf)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
115 t-right : {key key₁ : ℕ} → {value value₁ : A} → {t₁ t₂ : bt A} → (key < key₁) → treeInvariant (node key₁ value₁ t₁ t₂)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
116 → treeInvariant (node key value leaf (node key₁ value₁ t₁ t₂))
692
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
117 t-left : {key key₁ : ℕ} → {value value₁ : A} → {t₁ t₂ : bt A} → (key < key₁) → treeInvariant (node key value t₁ t₂)
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
118 → treeInvariant (node key₁ value₁ (node key value t₁ t₂) leaf )
620
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
119 t-node : {key key₁ key₂ : ℕ} → {value value₁ value₂ : A} → {t₁ t₂ t₃ t₄ : bt A} → (key < key₁) → (key₁ < key₂)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
120 → treeInvariant (node key value t₁ t₂)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
121 → treeInvariant (node key₂ value₂ t₃ t₄)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
122 → treeInvariant (node key₁ value₁ (node key value t₁ t₂) (node key₂ value₂ t₃ t₄))
605
f8cc98fcc34b define invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 604
diff changeset
123
662
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
124 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
125 -- stack always contains original top at end
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
126 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
127 data stackInvariant {n : Level} {A : Set n} (key : ℕ) : (top orig : bt A) → (stack : List (bt A)) → Set n where
675
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 671
diff changeset
128 s-single : {tree0 : bt A} → stackInvariant key tree0 tree0 (tree0 ∷ [])
653
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 652
diff changeset
129 s-right : {tree tree0 tree₁ : bt A} → {key₁ : ℕ } → {v1 : A } → {st : List (bt A)}
662
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
130 → key₁ < key → stackInvariant key (node key₁ v1 tree₁ tree) tree0 st → stackInvariant key tree tree0 (tree ∷ st)
653
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 652
diff changeset
131 s-left : {tree₁ tree0 tree : bt A} → {key₁ : ℕ } → {v1 : A } → {st : List (bt A)}
662
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
132 → key < key₁ → stackInvariant key (node key₁ v1 tree₁ tree) tree0 st → stackInvariant key tree₁ tree0 (tree₁ ∷ st)
639
5fe23f540726 replacedTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 638
diff changeset
133
677
681577b60c35 child-replaced
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 676
diff changeset
134 data replacedTree {n : Level} {A : Set n} (key : ℕ) (value : A) : (before after : bt A ) → Set n where
639
5fe23f540726 replacedTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 638
diff changeset
135 r-leaf : replacedTree key value leaf (node key value leaf leaf)
5fe23f540726 replacedTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 638
diff changeset
136 r-node : {value₁ : A} → {t t₁ : bt A} → replacedTree key value (node key value₁ t t₁) (node key value t t₁)
5fe23f540726 replacedTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 638
diff changeset
137 r-right : {k : ℕ } {v1 : A} → {t t1 t2 : bt A}
677
681577b60c35 child-replaced
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 676
diff changeset
138 → k < key → replacedTree key value t2 t → replacedTree key value (node k v1 t1 t2) (node k v1 t1 t)
639
5fe23f540726 replacedTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 638
diff changeset
139 r-left : {k : ℕ } {v1 : A} → {t t1 t2 : bt A}
687
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
140 → key < k → replacedTree key value t1 t → replacedTree key value (node k v1 t1 t2) (node k v1 t t2)
652
8c7446829b99 new stack invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 651
diff changeset
141
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
142 add< : { i : ℕ } (j : ℕ ) → i < suc i + j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
143 add< {i} j = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
144 suc i ≤⟨ m≤m+n (suc i) j ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
145 suc i + j ∎ where open ≤-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
146
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
147 treeTest1 : bt ℕ
692
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
148 treeTest1 = node 0 0 leaf (node 3 1 (node 2 5 (node 1 7 leaf leaf ) leaf) (node 5 5 leaf leaf))
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
149 treeTest2 : bt ℕ
692
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
150 treeTest2 = node 3 1 (node 2 5 (node 1 7 leaf leaf ) leaf) (node 5 5 leaf leaf)
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
151
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
152 treeInvariantTest1 : treeInvariant treeTest1
692
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
153 treeInvariantTest1 = t-right (m≤m+n _ 2) (t-node (add< 0) (add< 1) (t-left (add< 0) (t-single 1 7)) (t-single 5 5) )
605
f8cc98fcc34b define invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 604
diff changeset
154
639
5fe23f540726 replacedTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 638
diff changeset
155 stack-top : {n : Level} {A : Set n} (stack : List (bt A)) → Maybe (bt A)
5fe23f540726 replacedTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 638
diff changeset
156 stack-top [] = nothing
5fe23f540726 replacedTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 638
diff changeset
157 stack-top (x ∷ s) = just x
606
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
158
639
5fe23f540726 replacedTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 638
diff changeset
159 stack-last : {n : Level} {A : Set n} (stack : List (bt A)) → Maybe (bt A)
5fe23f540726 replacedTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 638
diff changeset
160 stack-last [] = nothing
5fe23f540726 replacedTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 638
diff changeset
161 stack-last (x ∷ []) = just x
5fe23f540726 replacedTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 638
diff changeset
162 stack-last (x ∷ s) = stack-last s
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
163
662
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
164 stackInvariantTest1 : stackInvariant 4 treeTest2 treeTest1 ( treeTest2 ∷ treeTest1 ∷ [] )
692
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
165 stackInvariantTest1 = s-right (add< 3) (s-single )
662
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
166
666
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 665
diff changeset
167 si-property0 : {n : Level} {A : Set n} {key : ℕ} {tree tree0 : bt A} → {stack : List (bt A)} → stackInvariant key tree tree0 stack → ¬ ( stack ≡ [] )
675
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 671
diff changeset
168 si-property0 (s-single ) ()
666
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 665
diff changeset
169 si-property0 (s-right x si) ()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 665
diff changeset
170 si-property0 (s-left x si) ()
665
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 664
diff changeset
171
666
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 665
diff changeset
172 si-property1 : {n : Level} {A : Set n} {key : ℕ} {tree tree0 tree1 : bt A} → {stack : List (bt A)} → stackInvariant key tree tree0 (tree1 ∷ stack)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 665
diff changeset
173 → tree1 ≡ tree
675
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 671
diff changeset
174 si-property1 (s-single ) = refl
666
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 665
diff changeset
175 si-property1 (s-right _ si) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 665
diff changeset
176 si-property1 (s-left _ si) = refl
662
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
177
663
cf5095488bbd stack contains original tree at end always
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 662
diff changeset
178 si-property-last : {n : Level} {A : Set n} (key : ℕ) (tree tree0 : bt A) → (stack : List (bt A)) → stackInvariant key tree tree0 stack
662
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
179 → stack-last stack ≡ just tree0
675
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 671
diff changeset
180 si-property-last key t t0 (t ∷ []) (s-single ) = refl
666
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 665
diff changeset
181 si-property-last key t t0 (.t ∷ x ∷ st) (s-right _ si ) with si-property1 si
663
cf5095488bbd stack contains original tree at end always
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 662
diff changeset
182 ... | refl = si-property-last key x t0 (x ∷ st) si
666
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 665
diff changeset
183 si-property-last key t t0 (.t ∷ x ∷ st) (s-left _ si ) with si-property1 si
663
cf5095488bbd stack contains original tree at end always
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 662
diff changeset
184 ... | refl = si-property-last key x t0 (x ∷ st) si
656
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 655
diff changeset
185
642
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 641
diff changeset
186 ti-right : {n : Level} {A : Set n} {tree₁ repl : bt A} → {key₁ : ℕ} → {v1 : A} → treeInvariant (node key₁ v1 tree₁ repl) → treeInvariant repl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 641
diff changeset
187 ti-right {_} {_} {.leaf} {_} {key₁} {v1} (t-single .key₁ .v1) = t-leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 641
diff changeset
188 ti-right {_} {_} {.leaf} {_} {key₁} {v1} (t-right x ti) = ti
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 641
diff changeset
189 ti-right {_} {_} {.(node _ _ _ _)} {_} {key₁} {v1} (t-left x ti) = t-leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 641
diff changeset
190 ti-right {_} {_} {.(node _ _ _ _)} {_} {key₁} {v1} (t-node x x₁ ti ti₁) = ti₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 641
diff changeset
191
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 641
diff changeset
192 ti-left : {n : Level} {A : Set n} {tree₁ repl : bt A} → {key₁ : ℕ} → {v1 : A} → treeInvariant (node key₁ v1 repl tree₁ ) → treeInvariant repl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 641
diff changeset
193 ti-left {_} {_} {.leaf} {_} {key₁} {v1} (t-single .key₁ .v1) = t-leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 641
diff changeset
194 ti-left {_} {_} {_} {_} {key₁} {v1} (t-right x ti) = t-leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 641
diff changeset
195 ti-left {_} {_} {_} {_} {key₁} {v1} (t-left x ti) = ti
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 641
diff changeset
196 ti-left {_} {_} {.(node _ _ _ _)} {_} {key₁} {v1} (t-node x x₁ ti ti₁) = ti
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 641
diff changeset
197
662
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
198 stackTreeInvariant : {n : Level} {A : Set n} (key : ℕ) (sub tree : bt A) → (stack : List (bt A))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
199 → treeInvariant tree → stackInvariant key sub tree stack → treeInvariant sub
675
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 671
diff changeset
200 stackTreeInvariant {_} {A} key sub tree (sub ∷ []) ti (s-single ) = ti
662
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
201 stackTreeInvariant {_} {A} key sub tree (sub ∷ st) ti (s-right _ si ) = ti-right (si1 si) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
202 si1 : {tree₁ : bt A} → {key₁ : ℕ} → {v1 : A} → stackInvariant key (node key₁ v1 tree₁ sub ) tree st → treeInvariant (node key₁ v1 tree₁ sub )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
203 si1 {tree₁ } {key₁ } {v1 } si = stackTreeInvariant key (node key₁ v1 tree₁ sub ) tree st ti si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
204 stackTreeInvariant {_} {A} key sub tree (sub ∷ st) ti (s-left _ si ) = ti-left ( si2 si) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
205 si2 : {tree₁ : bt A} → {key₁ : ℕ} → {v1 : A} → stackInvariant key (node key₁ v1 sub tree₁ ) tree st → treeInvariant (node key₁ v1 sub tree₁ )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
206 si2 {tree₁ } {key₁ } {v1 } si = stackTreeInvariant key (node key₁ v1 sub tree₁ ) tree st ti si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
207
639
5fe23f540726 replacedTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 638
diff changeset
208 rt-property1 : {n : Level} {A : Set n} (key : ℕ) (value : A) (tree tree1 : bt A ) → replacedTree key value tree tree1 → ¬ ( tree1 ≡ leaf )
5fe23f540726 replacedTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 638
diff changeset
209 rt-property1 {n} {A} key value .leaf .(node key value leaf leaf) r-leaf ()
5fe23f540726 replacedTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 638
diff changeset
210 rt-property1 {n} {A} key value .(node key _ _ _) .(node key value _ _) r-node ()
677
681577b60c35 child-replaced
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 676
diff changeset
211 rt-property1 {n} {A} key value .(node _ _ _ _) _ (r-right x rt) = λ ()
681577b60c35 child-replaced
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 676
diff changeset
212 rt-property1 {n} {A} key value .(node _ _ _ _) _ (r-left x rt) = λ ()
639
5fe23f540726 replacedTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 638
diff changeset
213
690
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
214 rt-property-leaf : {n : Level} {A : Set n} {key : ℕ} {value : A} {repl : bt A} → replacedTree key value leaf repl → repl ≡ node key value leaf leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
215 rt-property-leaf r-leaf = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
216
698
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
217 rt-property-¬leaf : {n : Level} {A : Set n} {key : ℕ} {value : A} {tree : bt A} → ¬ replacedTree key value tree leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
218 rt-property-¬leaf ()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
219
692
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
220 rt-property-key : {n : Level} {A : Set n} {key key₂ key₃ : ℕ} {value value₂ value₃ : A} {left left₁ right₂ right₃ : bt A}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
221 → replacedTree key value (node key₂ value₂ left right₂) (node key₃ value₃ left₁ right₃) → key₂ ≡ key₃
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
222 rt-property-key r-node = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
223 rt-property-key (r-right x ri) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
224 rt-property-key (r-left x ri) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
225
698
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
226 nat-≤> : { x y : ℕ } → x ≤ y → y < x → ⊥
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
227 nat-≤> (s≤s x<y) (s≤s y<x) = nat-≤> x<y y<x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
228 nat-<> : { x y : ℕ } → x < y → y < x → ⊥
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
229 nat-<> (s≤s x<y) (s≤s y<x) = nat-<> x<y y<x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
230
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
231 open _∧_
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
232
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
233
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
234 depth-1< : {i j : ℕ} → suc i ≤ suc (i Data.Nat.⊔ j )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
235 depth-1< {i} {j} = s≤s (m≤m⊔n _ j)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
236
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
237 depth-2< : {i j : ℕ} → suc i ≤ suc (j Data.Nat.⊔ i )
933
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 782
diff changeset
238 depth-2< {i} {j} = s≤s (m≤n⊔m j i)
611
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 610
diff changeset
239
649
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
240 depth-3< : {i : ℕ } → suc i ≤ suc (suc i)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
241 depth-3< {zero} = s≤s ( z≤n )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
242 depth-3< {suc i} = s≤s (depth-3< {i} )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
243
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
244
634
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
245 treeLeftDown : {n : Level} {A : Set n} {k : ℕ} {v1 : A} → (tree tree₁ : bt A )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
246 → treeInvariant (node k v1 tree tree₁)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
247 → treeInvariant tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
248 treeLeftDown {n} {A} {_} {v1} leaf leaf (t-single k1 v1) = t-leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
249 treeLeftDown {n} {A} {_} {v1} .leaf .(node _ _ _ _) (t-right x ti) = t-leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
250 treeLeftDown {n} {A} {_} {v1} .(node _ _ _ _) .leaf (t-left x ti) = ti
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
251 treeLeftDown {n} {A} {_} {v1} .(node _ _ _ _) .(node _ _ _ _) (t-node x x₁ ti ti₁) = ti
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
252
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
253 treeRightDown : {n : Level} {A : Set n} {k : ℕ} {v1 : A} → (tree tree₁ : bt A )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
254 → treeInvariant (node k v1 tree tree₁)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
255 → treeInvariant tree₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
256 treeRightDown {n} {A} {_} {v1} .leaf .leaf (t-single _ .v1) = t-leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
257 treeRightDown {n} {A} {_} {v1} .leaf .(node _ _ _ _) (t-right x ti) = ti
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
258 treeRightDown {n} {A} {_} {v1} .(node _ _ _ _) .leaf (t-left x ti) = t-leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
259 treeRightDown {n} {A} {_} {v1} .(node _ _ _ _) .(node _ _ _ _) (t-node x x₁ ti ti₁) = ti₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
260
704
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 703
diff changeset
261
718
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 717
diff changeset
262 -- record FindCond {n : Level} {A : Set n} (C : ℕ → bt A → Set n) : Set (Level.suc n) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 717
diff changeset
263 -- field
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 717
diff changeset
264 -- c1 : {key key₁ : ℕ} {v1 : A } { tree tree₁ : bt A } → C key (node key₁ v1 tree tree₁) → key < key₁ → C key tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 717
diff changeset
265 -- c2 : {key key₁ : ℕ} {v1 : A } { tree tree₁ : bt A } → C key (node key₁ v1 tree tree₁) → key > key₁ → C key tree₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 717
diff changeset
266 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 717
diff changeset
267 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 717
diff changeset
268 -- findP0 : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (tree : bt A ) → (stack : List (bt A))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 717
diff changeset
269 -- → {C : ℕ → bt A → Set n} → C key tree → FindCond C
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 717
diff changeset
270 -- → (next : (tree1 : bt A) → (stack : List (bt A)) → C key tree1 → bt-depth tree1 < bt-depth tree → t )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 717
diff changeset
271 -- → (exit : (tree1 : bt A) → (stack : List (bt A)) → C key tree1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 717
diff changeset
272 -- → (tree1 ≡ leaf ) ∨ ( node-key tree1 ≡ just key ) → t ) → t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 717
diff changeset
273 -- findP0 key leaf st Pre _ _ exit = exit leaf st Pre (case1 refl)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 717
diff changeset
274 -- findP0 key (node key₁ v1 tree tree₁) st Pre _ next exit with <-cmp key key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 717
diff changeset
275 -- findP0 key n st Pre e _ exit | tri≈ ¬a refl ¬c = exit n st Pre (case2 refl)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 717
diff changeset
276 -- findP0 {n} {_} {A} key (node key₁ v1 tree tree₁) st Pre e next _ | tri< a ¬b ¬c = next tree (tree ∷ st) (FindCond.c1 e Pre a) depth-1<
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 717
diff changeset
277 -- findP0 key n@(node key₁ v1 tree tree₁) st Pre e next _ | tri> ¬a ¬b c = next tree₁ (tree₁ ∷ st) (FindCond.c2 e Pre c) depth-2<
704
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 703
diff changeset
278
615
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 614
diff changeset
279 findP : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (tree tree0 : bt A ) → (stack : List (bt A))
662
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
280 → treeInvariant tree ∧ stackInvariant key tree tree0 stack
693
49dd82f49fa1 insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 692
diff changeset
281 → (next : (tree1 : bt A) → (stack : List (bt A)) → treeInvariant tree1 ∧ stackInvariant key tree1 tree0 stack → bt-depth tree1 < bt-depth tree → t )
49dd82f49fa1 insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 692
diff changeset
282 → (exit : (tree1 : bt A) → (stack : List (bt A)) → treeInvariant tree1 ∧ stackInvariant key tree1 tree0 stack
638
be6bd51c3f05 replaceTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 637
diff changeset
283 → (tree1 ≡ leaf ) ∨ ( node-key tree1 ≡ just key ) → t ) → t
693
49dd82f49fa1 insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 692
diff changeset
284 findP key leaf tree0 st Pre _ exit = exit leaf st Pre (case1 refl)
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
285 findP key (node key₁ v1 tree tree₁) tree0 st Pre next exit with <-cmp key key₁
693
49dd82f49fa1 insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 692
diff changeset
286 findP key n tree0 st Pre _ exit | tri≈ ¬a refl ¬c = exit n st Pre (case2 refl)
49dd82f49fa1 insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 692
diff changeset
287 findP {n} {_} {A} key (node key₁ v1 tree tree₁) tree0 st Pre next _ | tri< a ¬b ¬c = next tree (tree ∷ st)
663
cf5095488bbd stack contains original tree at end always
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 662
diff changeset
288 ⟪ treeLeftDown tree tree₁ (proj1 Pre) , findP1 a st (proj2 Pre) ⟫ depth-1< where
cf5095488bbd stack contains original tree at end always
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 662
diff changeset
289 findP1 : key < key₁ → (st : List (bt A)) → stackInvariant key (node key₁ v1 tree tree₁) tree0 st → stackInvariant key tree tree0 (tree ∷ st)
664
1f702351fd1f findP done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 663
diff changeset
290 findP1 a (x ∷ st) si = s-left a si
693
49dd82f49fa1 insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 692
diff changeset
291 findP key n@(node key₁ v1 tree tree₁) tree0 st Pre next _ | tri> ¬a ¬b c = next tree₁ (tree₁ ∷ st) ⟪ treeRightDown tree tree₁ (proj1 Pre) , s-right c (proj2 Pre) ⟫ depth-2<
606
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
292
638
be6bd51c3f05 replaceTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 637
diff changeset
293 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₁)
be6bd51c3f05 replaceTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 637
diff changeset
294 replaceTree1 k v1 value (t-single .k .v1) = t-single k value
be6bd51c3f05 replaceTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 637
diff changeset
295 replaceTree1 k v1 value (t-right x t) = t-right x t
be6bd51c3f05 replaceTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 637
diff changeset
296 replaceTree1 k v1 value (t-left x t) = t-left x t
be6bd51c3f05 replaceTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 637
diff changeset
297 replaceTree1 k v1 value (t-node x x₁ t t₁) = t-node x x₁ t t₁
be6bd51c3f05 replaceTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 637
diff changeset
298
649
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
299 open import Relation.Binary.Definitions
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
300
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
301 lemma3 : {i j : ℕ} → 0 ≡ i → j < i → ⊥
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
302 lemma3 refl ()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
303 lemma5 : {i j : ℕ} → i < 1 → j < i → ⊥
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
304 lemma5 (s≤s z≤n) ()
700
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 699
diff changeset
305 ¬x<x : {x : ℕ} → ¬ (x < x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 699
diff changeset
306 ¬x<x (s≤s lt) = ¬x<x lt
649
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
307
687
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
308 child-replaced : {n : Level} {A : Set n} (key : ℕ) (tree : bt A) → bt A
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
309 child-replaced key leaf = leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
310 child-replaced key (node key₁ value left right) with <-cmp key key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
311 ... | tri< a ¬b ¬c = left
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
312 ... | tri≈ ¬a b ¬c = node key₁ value left right
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
313 ... | tri> ¬a ¬b c = right
677
681577b60c35 child-replaced
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 676
diff changeset
314
671
b5fde9727830 use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 670
diff changeset
315 record replacePR {n : Level} {A : Set n} (key : ℕ) (value : A) (tree repl : bt A ) (stack : List (bt A)) (C : bt A → bt A → List (bt A) → Set n) : Set n where
b5fde9727830 use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 670
diff changeset
316 field
b5fde9727830 use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 670
diff changeset
317 tree0 : bt A
b5fde9727830 use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 670
diff changeset
318 ti : treeInvariant tree0
b5fde9727830 use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 670
diff changeset
319 si : stackInvariant key tree tree0 stack
687
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
320 ri : replacedTree key value (child-replaced key tree ) repl
671
b5fde9727830 use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 670
diff changeset
321 ci : C tree repl stack -- data continuation
b5fde9727830 use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 670
diff changeset
322
638
be6bd51c3f05 replaceTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 637
diff changeset
323 replaceNodeP : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (value : A) → (tree : bt A)
be6bd51c3f05 replaceTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 637
diff changeset
324 → (tree ≡ leaf ) ∨ ( node-key tree ≡ just key )
694
da42fe4eda54 complete insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 693
diff changeset
325 → (treeInvariant tree ) → ((tree1 : bt A) → treeInvariant tree1 → replacedTree key value (child-replaced key tree) tree1 → t) → t
da42fe4eda54 complete insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 693
diff changeset
326 replaceNodeP k v1 leaf C P next = next (node k v1 leaf leaf) (t-single k v1 ) r-leaf
da42fe4eda54 complete insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 693
diff changeset
327 replaceNodeP k v1 (node .k value t t₁) (case2 refl) P next = next (node k v1 t t₁) (replaceTree1 k value v1 P)
695
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 694
diff changeset
328 (subst (λ j → replacedTree k v1 j (node k v1 t t₁) ) repl00 r-node) where
694
da42fe4eda54 complete insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 693
diff changeset
329 repl00 : node k value t t₁ ≡ child-replaced k (node k value t t₁)
da42fe4eda54 complete insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 693
diff changeset
330 repl00 with <-cmp k k
da42fe4eda54 complete insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 693
diff changeset
331 ... | tri< a ¬b ¬c = ⊥-elim (¬b refl)
da42fe4eda54 complete insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 693
diff changeset
332 ... | tri≈ ¬a b ¬c = refl
da42fe4eda54 complete insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 693
diff changeset
333 ... | tri> ¬a ¬b c = ⊥-elim (¬b refl)
606
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
334
609
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
335 replaceP : {n m : Level} {A : Set n} {t : Set m}
671
b5fde9727830 use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 670
diff changeset
336 → (key : ℕ) → (value : A) → {tree : bt A} ( repl : bt A)
b5fde9727830 use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 670
diff changeset
337 → (stack : List (bt A)) → replacePR key value tree repl stack (λ _ _ _ → Lift n ⊤)
b5fde9727830 use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 670
diff changeset
338 → (next : ℕ → A → {tree1 : bt A } (repl : bt A) → (stack1 : List (bt A))
b5fde9727830 use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 670
diff changeset
339 → replacePR key value tree1 repl stack1 (λ _ _ _ → Lift n ⊤) → length stack1 < length stack → t)
613
eeb9eb38e5e2 data replacedTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 612
diff changeset
340 → (exit : (tree1 repl : bt A) → treeInvariant tree1 ∧ replacedTree key value tree1 repl → t) → t
675
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 671
diff changeset
341 replaceP key value {tree} repl [] Pre next exit = ⊥-elim ( si-property0 (replacePR.si Pre) refl ) -- can't happen
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 671
diff changeset
342 replaceP key value {tree} repl (leaf ∷ []) Pre next exit with si-property-last _ _ _ _ (replacePR.si Pre)-- tree0 ≡ leaf
677
681577b60c35 child-replaced
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 676
diff changeset
343 ... | refl = exit (replacePR.tree0 Pre) (node key value leaf leaf) ⟪ replacePR.ti Pre , r-leaf ⟫
689
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
344 replaceP key value {tree} repl (node key₁ value₁ left right ∷ []) Pre next exit with <-cmp key key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
345 ... | tri< a ¬b ¬c = exit (replacePR.tree0 Pre) (node key₁ value₁ repl right ) ⟪ replacePR.ti Pre , repl01 ⟫ where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
346 repl01 : replacedTree key value (replacePR.tree0 Pre) (node key₁ value₁ repl right )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
347 repl01 with si-property1 (replacePR.si Pre) | si-property-last _ _ _ _ (replacePR.si Pre)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
348 repl01 | refl | refl = subst (λ k → replacedTree key value (node key₁ value₁ k right ) (node key₁ value₁ repl right )) repl02 (r-left a repl03) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
349 repl03 : replacedTree key value ( child-replaced key (node key₁ value₁ left right)) repl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
350 repl03 = replacePR.ri Pre
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
351 repl02 : child-replaced key (node key₁ value₁ left right) ≡ left
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
352 repl02 with <-cmp key key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
353 ... | tri< a ¬b ¬c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
354 ... | tri≈ ¬a b ¬c = ⊥-elim ( ¬a a)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
355 ... | tri> ¬a ¬b c = ⊥-elim ( ¬a a)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
356 ... | tri≈ ¬a b ¬c = exit (replacePR.tree0 Pre) repl ⟪ replacePR.ti Pre , repl01 ⟫ where
678
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 677
diff changeset
357 repl01 : replacedTree key value (replacePR.tree0 Pre) repl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 677
diff changeset
358 repl01 with si-property1 (replacePR.si Pre) | si-property-last _ _ _ _ (replacePR.si Pre)
689
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
359 repl01 | refl | refl = subst (λ k → replacedTree key value k repl) repl02 (replacePR.ri Pre) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
360 repl02 : child-replaced key (node key₁ value₁ left right) ≡ node key₁ value₁ left right
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
361 repl02 with <-cmp key key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
362 ... | tri< a ¬b ¬c = ⊥-elim ( ¬b b)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
363 ... | tri≈ ¬a b ¬c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
364 ... | tri> ¬a ¬b c = ⊥-elim ( ¬b b)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
365 ... | tri> ¬a ¬b c = exit (replacePR.tree0 Pre) (node key₁ value₁ left repl ) ⟪ replacePR.ti Pre , repl01 ⟫ where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
366 repl01 : replacedTree key value (replacePR.tree0 Pre) (node key₁ value₁ left repl )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
367 repl01 with si-property1 (replacePR.si Pre) | si-property-last _ _ _ _ (replacePR.si Pre)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
368 repl01 | refl | refl = subst (λ k → replacedTree key value (node key₁ value₁ left k ) (node key₁ value₁ left repl )) repl02 (r-right c repl03) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
369 repl03 : replacedTree key value ( child-replaced key (node key₁ value₁ left right)) repl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
370 repl03 = replacePR.ri Pre
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
371 repl02 : child-replaced key (node key₁ value₁ left right) ≡ right
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
372 repl02 with <-cmp key key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
373 ... | tri< a ¬b ¬c = ⊥-elim ( ¬c c)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
374 ... | tri≈ ¬a b ¬c = ⊥-elim ( ¬c c)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
375 ... | tri> ¬a ¬b c = refl
690
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
376 replaceP {n} {_} {A} key value {tree} repl (leaf ∷ st@(tree1 ∷ st1)) Pre next exit = next key value repl st Post ≤-refl where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
377 Post : replacePR key value tree1 repl (tree1 ∷ st1) (λ _ _ _ → Lift n ⊤)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
378 Post with replacePR.si Pre
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
379 ... | s-right {_} {_} {tree₁} {key₂} {v1} x si = record { tree0 = replacePR.tree0 Pre ; ti = replacePR.ti Pre ; si = repl10 ; ri = repl12 ; ci = lift tt } where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
380 repl09 : tree1 ≡ node key₂ v1 tree₁ leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
381 repl09 = si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
382 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
383 repl10 with si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
384 ... | refl = si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
385 repl07 : child-replaced key (node key₂ v1 tree₁ leaf) ≡ leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
386 repl07 with <-cmp key key₂
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
387 ... | tri< a ¬b ¬c = ⊥-elim (¬c x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
388 ... | tri≈ ¬a b ¬c = ⊥-elim (¬c x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
389 ... | tri> ¬a ¬b c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
390 repl12 : replacedTree key value (child-replaced key tree1 ) repl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
391 repl12 = subst₂ (λ j k → replacedTree key value j k ) (sym (subst (λ k → child-replaced key k ≡ leaf) (sym repl09) repl07 ) ) (sym (rt-property-leaf (replacePR.ri Pre))) r-leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
392 ... | s-left {_} {_} {tree₁} {key₂} {v1} x si = record { tree0 = replacePR.tree0 Pre ; ti = replacePR.ti Pre ; si = repl10 ; ri = repl12 ; ci = lift tt } where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
393 repl09 : tree1 ≡ node key₂ v1 leaf tree₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
394 repl09 = si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
395 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
396 repl10 with si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
397 ... | refl = si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
398 repl07 : child-replaced key (node key₂ v1 leaf tree₁ ) ≡ leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
399 repl07 with <-cmp key key₂
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
400 ... | tri< a ¬b ¬c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
401 ... | tri≈ ¬a b ¬c = ⊥-elim (¬a x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
402 ... | tri> ¬a ¬b c = ⊥-elim (¬a x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
403 repl12 : replacedTree key value (child-replaced key tree1 ) repl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
404 repl12 = subst₂ (λ j k → replacedTree key value j k ) (sym (subst (λ k → child-replaced key k ≡ leaf) (sym repl09) repl07 ) ) (sym (rt-property-leaf (replacePR.ri Pre))) r-leaf
683
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 682
diff changeset
405 replaceP {n} {_} {A} key value {tree} repl (node key₁ value₁ left right ∷ st@(tree1 ∷ st1)) Pre next exit with <-cmp key key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 682
diff changeset
406 ... | tri< a ¬b ¬c = next key value (node key₁ value₁ repl right ) st Post ≤-refl where
675
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 671
diff changeset
407 Post : replacePR key value tree1 (node key₁ value₁ repl right ) st (λ _ _ _ → Lift n ⊤)
687
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
408 Post with replacePR.si Pre
688
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
409 ... | s-right {_} {_} {tree₁} {key₂} {v1} lt si = record { tree0 = replacePR.tree0 Pre ; ti = replacePR.ti Pre ; si = repl10 ; ri = repl12 ; ci = lift tt } where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
410 repl09 : tree1 ≡ node key₂ v1 tree₁ (node key₁ value₁ left right)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
411 repl09 = si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
412 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
413 repl10 with si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
414 ... | refl = si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
415 repl03 : child-replaced key (node key₁ value₁ left right) ≡ left
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
416 repl03 with <-cmp key key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
417 ... | tri< a1 ¬b ¬c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
418 ... | tri≈ ¬a b ¬c = ⊥-elim (¬a a)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
419 ... | tri> ¬a ¬b c = ⊥-elim (¬a a)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
420 repl02 : child-replaced key (node key₂ v1 tree₁ (node key₁ value₁ left right) ) ≡ node key₁ value₁ left right
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
421 repl02 with repl09 | <-cmp key key₂
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
422 ... | refl | tri< a ¬b ¬c = ⊥-elim (¬c lt)
689
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
423 ... | refl | tri≈ ¬a b ¬c = ⊥-elim (¬c lt)
688
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
424 ... | refl | tri> ¬a ¬b c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
425 repl04 : node key₁ value₁ (child-replaced key (node key₁ value₁ left right)) right ≡ child-replaced key tree1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
426 repl04 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
427 node key₁ value₁ (child-replaced key (node key₁ value₁ left right)) right ≡⟨ cong (λ k → node key₁ value₁ k right) repl03 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
428 node key₁ value₁ left right ≡⟨ sym repl02 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
429 child-replaced key (node key₂ v1 tree₁ (node key₁ value₁ left right) ) ≡⟨ cong (λ k → child-replaced key k ) (sym repl09) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
430 child-replaced key tree1 ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
431 repl12 : replacedTree key value (child-replaced key tree1 ) (node key₁ value₁ repl right)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
432 repl12 = subst (λ k → replacedTree key value k (node key₁ value₁ repl right) ) repl04 (r-left a (replacePR.ri Pre))
687
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
433 ... | s-left {_} {_} {tree₁} {key₂} {v1} lt si = record { tree0 = replacePR.tree0 Pre ; ti = replacePR.ti Pre ; si = repl10 ; ri = repl12 ; ci = lift tt } where
688
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
434 repl09 : tree1 ≡ node key₂ v1 (node key₁ value₁ left right) tree₁
683
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 682
diff changeset
435 repl09 = si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 682
diff changeset
436 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 682
diff changeset
437 repl10 with si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 682
diff changeset
438 ... | refl = si
687
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
439 repl03 : child-replaced key (node key₁ value₁ left right) ≡ left
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
440 repl03 with <-cmp key key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
441 ... | tri< a1 ¬b ¬c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
442 ... | tri≈ ¬a b ¬c = ⊥-elim (¬a a)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
443 ... | tri> ¬a ¬b c = ⊥-elim (¬a a)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
444 repl02 : child-replaced key (node key₂ v1 (node key₁ value₁ left right) tree₁) ≡ node key₁ value₁ left right
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
445 repl02 with repl09 | <-cmp key key₂
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
446 ... | refl | tri< a ¬b ¬c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
447 ... | refl | tri≈ ¬a b ¬c = ⊥-elim (¬a lt)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
448 ... | refl | tri> ¬a ¬b c = ⊥-elim (¬a lt)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
449 repl04 : node key₁ value₁ (child-replaced key (node key₁ value₁ left right)) right ≡ child-replaced key tree1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
450 repl04 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
451 node key₁ value₁ (child-replaced key (node key₁ value₁ left right)) right ≡⟨ cong (λ k → node key₁ value₁ k right) repl03 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
452 node key₁ value₁ left right ≡⟨ sym repl02 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
453 child-replaced key (node key₂ v1 (node key₁ value₁ left right) tree₁) ≡⟨ cong (λ k → child-replaced key k ) (sym repl09) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
454 child-replaced key tree1 ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
455 repl12 : replacedTree key value (child-replaced key tree1 ) (node key₁ value₁ repl right)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
456 repl12 = subst (λ k → replacedTree key value k (node key₁ value₁ repl right) ) repl04 (r-left a (replacePR.ri Pre))
705
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 704
diff changeset
457 ... | tri≈ ¬a b ¬c = next key value (node key₁ value left right ) st Post ≤-refl where
690
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
458 Post : replacePR key value tree1 (node key₁ value left right ) (tree1 ∷ st1) (λ _ _ _ → Lift n ⊤)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
459 Post with replacePR.si Pre
691
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 690
diff changeset
460 ... | s-right {_} {_} {tree₁} {key₂} {v1} x si = record { tree0 = replacePR.tree0 Pre ; ti = replacePR.ti Pre ; si = repl10 ; ri = repl12 b ; ci = lift tt } where
690
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
461 repl09 : tree1 ≡ node key₂ v1 tree₁ tree -- (node key₁ value₁ left right)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
462 repl09 = si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
463 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
464 repl10 with si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
465 ... | refl = si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
466 repl07 : child-replaced key (node key₂ v1 tree₁ tree) ≡ tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
467 repl07 with <-cmp key key₂
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
468 ... | tri< a ¬b ¬c = ⊥-elim (¬c x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
469 ... | tri≈ ¬a b ¬c = ⊥-elim (¬c x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
470 ... | tri> ¬a ¬b c = refl
691
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 690
diff changeset
471 repl12 : (key ≡ key₁) → replacedTree key value (child-replaced key tree1 ) (node key₁ value left right )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 690
diff changeset
472 repl12 refl with repl09
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 690
diff changeset
473 ... | refl = subst (λ k → replacedTree key value k (node key₁ value left right )) (sym repl07) r-node
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 690
diff changeset
474 ... | s-left {_} {_} {tree₁} {key₂} {v1} x si = record { tree0 = replacePR.tree0 Pre ; ti = replacePR.ti Pre ; si = repl10 ; ri = repl12 b ; ci = lift tt } where
690
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
475 repl09 : tree1 ≡ node key₂ v1 tree tree₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
476 repl09 = si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
477 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
478 repl10 with si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
479 ... | refl = si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
480 repl07 : child-replaced key (node key₂ v1 tree tree₁ ) ≡ tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
481 repl07 with <-cmp key key₂
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
482 ... | tri< a ¬b ¬c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
483 ... | tri≈ ¬a b ¬c = ⊥-elim (¬a x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
484 ... | tri> ¬a ¬b c = ⊥-elim (¬a x)
691
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 690
diff changeset
485 repl12 : (key ≡ key₁) → replacedTree key value (child-replaced key tree1 ) (node key₁ value left right )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 690
diff changeset
486 repl12 refl with repl09
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 690
diff changeset
487 ... | refl = subst (λ k → replacedTree key value k (node key₁ value left right )) (sym repl07) r-node
690
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
488 ... | tri> ¬a ¬b c = next key value (node key₁ value₁ left repl ) st Post ≤-refl where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
489 Post : replacePR key value tree1 (node key₁ value₁ left repl ) st (λ _ _ _ → Lift n ⊤)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
490 Post with replacePR.si Pre
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
491 ... | s-right {_} {_} {tree₁} {key₂} {v1} lt si = record { tree0 = replacePR.tree0 Pre ; ti = replacePR.ti Pre ; si = repl10 ; ri = repl12 ; ci = lift tt } where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
492 repl09 : tree1 ≡ node key₂ v1 tree₁ (node key₁ value₁ left right)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
493 repl09 = si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
494 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
495 repl10 with si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
496 ... | refl = si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
497 repl03 : child-replaced key (node key₁ value₁ left right) ≡ right
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
498 repl03 with <-cmp key key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
499 ... | tri< a1 ¬b ¬c = ⊥-elim (¬c c)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
500 ... | tri≈ ¬a b ¬c = ⊥-elim (¬c c)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
501 ... | tri> ¬a ¬b c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
502 repl02 : child-replaced key (node key₂ v1 tree₁ (node key₁ value₁ left right) ) ≡ node key₁ value₁ left right
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
503 repl02 with repl09 | <-cmp key key₂
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
504 ... | refl | tri< a ¬b ¬c = ⊥-elim (¬c lt)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
505 ... | refl | tri≈ ¬a b ¬c = ⊥-elim (¬c lt)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
506 ... | refl | tri> ¬a ¬b c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
507 repl04 : node key₁ value₁ left (child-replaced key (node key₁ value₁ left right)) ≡ child-replaced key tree1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
508 repl04 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
509 node key₁ value₁ left (child-replaced key (node key₁ value₁ left right)) ≡⟨ cong (λ k → node key₁ value₁ left k ) repl03 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
510 node key₁ value₁ left right ≡⟨ sym repl02 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
511 child-replaced key (node key₂ v1 tree₁ (node key₁ value₁ left right) ) ≡⟨ cong (λ k → child-replaced key k ) (sym repl09) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
512 child-replaced key tree1 ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
513 repl12 : replacedTree key value (child-replaced key tree1 ) (node key₁ value₁ left repl)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
514 repl12 = subst (λ k → replacedTree key value k (node key₁ value₁ left repl) ) repl04 (r-right c (replacePR.ri Pre))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
515 ... | s-left {_} {_} {tree₁} {key₂} {v1} lt si = record { tree0 = replacePR.tree0 Pre ; ti = replacePR.ti Pre ; si = repl10 ; ri = repl12 ; ci = lift tt } where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
516 repl09 : tree1 ≡ node key₂ v1 (node key₁ value₁ left right) tree₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
517 repl09 = si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
518 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
519 repl10 with si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
520 ... | refl = si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
521 repl03 : child-replaced key (node key₁ value₁ left right) ≡ right
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
522 repl03 with <-cmp key key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
523 ... | tri< a1 ¬b ¬c = ⊥-elim (¬c c)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
524 ... | tri≈ ¬a b ¬c = ⊥-elim (¬c c)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
525 ... | tri> ¬a ¬b c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
526 repl02 : child-replaced key (node key₂ v1 (node key₁ value₁ left right) tree₁) ≡ node key₁ value₁ left right
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
527 repl02 with repl09 | <-cmp key key₂
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
528 ... | refl | tri< a ¬b ¬c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
529 ... | refl | tri≈ ¬a b ¬c = ⊥-elim (¬a lt)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
530 ... | refl | tri> ¬a ¬b c = ⊥-elim (¬a lt)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
531 repl04 : node key₁ value₁ left (child-replaced key (node key₁ value₁ left right)) ≡ child-replaced key tree1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
532 repl04 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
533 node key₁ value₁ left (child-replaced key (node key₁ value₁ left right)) ≡⟨ cong (λ k → node key₁ value₁ left k ) repl03 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
534 node key₁ value₁ left right ≡⟨ sym repl02 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
535 child-replaced key (node key₂ v1 (node key₁ value₁ left right) tree₁) ≡⟨ cong (λ k → child-replaced key k ) (sym repl09) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
536 child-replaced key tree1 ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
537 repl12 : replacedTree key value (child-replaced key tree1 ) (node key₁ value₁ left repl)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
538 repl12 = subst (λ k → replacedTree key value k (node key₁ value₁ left repl) ) repl04 (r-right c (replacePR.ri Pre))
644
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 643
diff changeset
539
609
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
540 TerminatingLoopS : {l m : Level} {t : Set l} (Index : Set m ) → {Invraiant : Index → Set m } → ( reduce : Index → ℕ)
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
541 → (r : Index) → (p : Invraiant r)
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
542 → (loop : (r : Index) → Invraiant r → (next : (r1 : Index) → Invraiant r1 → reduce r1 < reduce r → t ) → t) → t
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
543 TerminatingLoopS {_} {_} {t} Index {Invraiant} reduce r p loop with <-cmp 0 (reduce r)
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
544 ... | tri≈ ¬a b ¬c = loop r p (λ r1 p1 lt → ⊥-elim (lemma3 b lt) )
933
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 782
diff changeset
545 ... | tri< a ¬b ¬c = loop r p (λ r1 p1 lt1 → TerminatingLoop1 (reduce r) r r1 (m≤n⇒m≤1+n lt1) p1 lt1 ) where
609
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
546 TerminatingLoop1 : (j : ℕ) → (r r1 : Index) → reduce r1 < suc j → Invraiant r1 → reduce r1 < reduce r → t
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
547 TerminatingLoop1 zero r r1 n≤j p1 lt = loop r1 p1 (λ r2 p1 lt1 → ⊥-elim (lemma5 n≤j lt1))
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
548 TerminatingLoop1 (suc j) r r1 n≤j p1 lt with <-cmp (reduce r1) (suc j)
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
549 ... | tri< a ¬b ¬c = TerminatingLoop1 j r r1 a p1 lt
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
550 ... | tri≈ ¬a b ¬c = loop r1 p1 (λ r2 p2 lt1 → TerminatingLoop1 j r1 r2 (subst (λ k → reduce r2 < k ) b lt1 ) p2 lt1 )
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
551 ... | tri> ¬a ¬b c = ⊥-elim ( nat-≤> c n≤j )
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
552
707
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 706
diff changeset
553 record LoopTermination {n : Level} {Index : Set n } { reduce : Index → ℕ }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 706
diff changeset
554 (r : Index ) (C : Set n) : Set (Level.suc n) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 706
diff changeset
555 field
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 706
diff changeset
556 rd : (r1 : Index) → reduce r1 < reduce r
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 706
diff changeset
557 ci : C -- data continuation
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 706
diff changeset
558
710
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
559 -- TerminatingLoopC : {l n : Level} {t : Set l} (Index : Set n ) → {C : Set n } → ( reduce : Index → ℕ)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
560 -- → (r : Index) → (P : LoopTermination r C )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
561 -- → (loop : (r : Index) → LoopTermination {_} {_} {reduce} r C → (next : (r1 : Index) → LoopTermination r1 C → t ) → t) → t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
562 -- TerminatingLoopC {_} {_} {t} Index {C} reduce r P loop with <-cmp 0 (reduce r)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
563 -- ... | tri≈ ¬a b ¬c = loop r P (λ r1 p1 → ⊥-elim (lemma3 b (LoopTermination.rd P r1)))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
564 -- ... | tri< a ¬b ¬c = loop r P (λ r1 p1 → TerminatingLoop1 (reduce r) r r1 (≤-step (LoopTermination.rd P r1)) p1 (LoopTermination.rd P r1)) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
565 -- TerminatingLoop1 : (j : ℕ) → (r r1 : Index) → reduce r1 < suc j → {!!} → reduce r1 < reduce r → t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
566 -- TerminatingLoop1 zero r r1 n≤j p1 lt = loop r1 {!!} (λ r2 P1 → ⊥-elim (lemma5 n≤j (LoopTermination.rd P1 r2)))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
567 -- TerminatingLoop1 (suc j) r r1 n≤j p1 lt with <-cmp (reduce r1) (suc j)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
568 -- ... | tri< a ¬b ¬c = TerminatingLoop1 j r r1 a p1 lt
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
569 -- ... | tri≈ ¬a b ¬c = loop r1 {!!} (λ r2 p2 → TerminatingLoop1 j r1 r2 (subst (λ k → reduce r2 < k ) b {!!} ) p2 {!!} )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
570 -- ... | tri> ¬a ¬b c = ⊥-elim ( nat-≤> c n≤j )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
571 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
572 -- record ReplCond {n : Level} {A : Set n} (C : ℕ → bt A → List (bt A) → Set n) : Set (Level.suc n) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
573 -- field
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
574 -- c1 : (key : ℕ) → (repl : bt A) → (stack : List (bt A)) → C key repl stack
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
575 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
576 -- replaceP0 : {n m : Level} {A : Set n} {t : Set m}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
577 -- → (key : ℕ) → (value : A) → ( repl : bt A)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
578 -- → (stack : List (bt A))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
579 -- → {C : ℕ → (repl : bt A ) → List (bt A) → Set n} → C key repl stack → ReplCond C
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
580 -- → (next : ℕ → A → (repl : bt A) → (stack1 : List (bt A))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
581 -- → C key repl stack → length stack1 < length stack → t)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
582 -- → (exit : (repl : bt A) → C key repl [] → t) → t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
583 -- replaceP0 key value repl [] Pre _ next exit = exit repl {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
584 -- replaceP0 key value repl (leaf ∷ []) Pre _ next exit = exit (node key value leaf leaf) {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
585 -- replaceP0 key value repl (node key₁ value₁ left right ∷ []) Pre e next exit with <-cmp key key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
586 -- ... | tri< a ¬b ¬c = exit (node key₁ value₁ repl right ) {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
587 -- ... | tri≈ ¬a b ¬c = exit repl {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
588 -- ... | tri> ¬a ¬b c = exit (node key₁ value₁ left repl ) {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
589 -- replaceP0 {n} {_} {A} key value repl (leaf ∷ st@(tree1 ∷ st1)) Pre e next exit = next key value repl st {!!} ≤-refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
590 -- replaceP0 {n} {_} {A} key value repl (node key₁ value₁ left right ∷ st@(tree1 ∷ st1)) Pre e next exit with <-cmp key key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
591 -- ... | tri< a ¬b ¬c = next key value (node key₁ value₁ repl right ) st {!!} ≤-refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
592 -- ... | tri≈ ¬a b ¬c = next key value (node key₁ value left right ) st {!!} ≤-refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
593 -- ... | tri> ¬a ¬b c = next key value (node key₁ value₁ left repl ) st {!!} ≤-refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
594 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 707
diff changeset
595 --
609
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
596 open _∧_
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
597
615
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 614
diff changeset
598 RTtoTI0 : {n : Level} {A : Set n} → (tree repl : bt A) → (key : ℕ) → (value : A) → treeInvariant tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 614
diff changeset
599 → replacedTree key value tree repl → treeInvariant repl
692
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
600 RTtoTI0 .leaf .(node key value leaf leaf) key value ti r-leaf = t-single key value
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
601 RTtoTI0 .(node key _ leaf leaf) .(node key value leaf leaf) key value (t-single .key _) r-node = t-single key value
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
602 RTtoTI0 .(node key _ leaf (node _ _ _ _)) .(node key value leaf (node _ _ _ _)) key value (t-right x ti) r-node = t-right x ti
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
603 RTtoTI0 .(node key _ (node _ _ _ _) leaf) .(node key value (node _ _ _ _) leaf) key value (t-left x ti) r-node = t-left x ti
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
604 RTtoTI0 .(node key _ (node _ _ _ _) (node _ _ _ _)) .(node key value (node _ _ _ _) (node _ _ _ _)) key value (t-node x x₁ ti ti₁) r-node = t-node x x₁ ti ti₁
701
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
605 -- r-right case
692
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
606 RTtoTI0 (node _ _ leaf leaf) (node _ _ leaf .(node key value leaf leaf)) key value (t-single _ _) (r-right x r-leaf) = t-right x (t-single key value)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
607 RTtoTI0 (node _ _ leaf right@(node _ _ _ _)) (node key₁ value₁ leaf leaf) key value (t-right x₁ ti) (r-right x ri) = t-single key₁ value₁
693
49dd82f49fa1 insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 692
diff changeset
608 RTtoTI0 (node key₁ _ leaf right@(node key₂ _ _ _)) (node key₁ value₁ leaf right₁@(node key₃ _ _ _)) key value (t-right x₁ ti) (r-right x ri) =
49dd82f49fa1 insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 692
diff changeset
609 t-right (subst (λ k → key₁ < k ) (rt-property-key ri) x₁) (RTtoTI0 _ _ key value ti ri)
692
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
610 RTtoTI0 (node key₁ _ (node _ _ _ _) leaf) (node key₁ _ (node key₃ value left right) leaf) key value₁ (t-left x₁ ti) (r-right x ())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
611 RTtoTI0 (node key₁ _ (node key₃ _ _ _) leaf) (node key₁ _ (node key₃ value₃ _ _) (node key value leaf leaf)) key value (t-left x₁ ti) (r-right x r-leaf) =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
612 t-node x₁ x ti (t-single key value)
693
49dd82f49fa1 insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 692
diff changeset
613 RTtoTI0 (node key₁ _ (node _ _ _ _) (node key₂ _ _ _)) (node key₁ _ (node _ _ _ _) (node key₃ _ _ _)) key value (t-node x₁ x₂ ti ti₁) (r-right x ri) =
49dd82f49fa1 insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 692
diff changeset
614 t-node x₁ (subst (λ k → key₁ < k) (rt-property-key ri) x₂) ti (RTtoTI0 _ _ key value ti₁ ri)
701
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
615 -- r-left case
700
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 699
diff changeset
616 RTtoTI0 .(node _ _ leaf leaf) .(node _ _ (node key value leaf leaf) leaf) key value (t-single _ _) (r-left x r-leaf) = t-left x (t-single _ _ )
701
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
617 RTtoTI0 .(node _ _ leaf (node _ _ _ _)) (node key₁ value₁ (node key value leaf leaf) (node _ _ _ _)) key value (t-right x₁ ti) (r-left x r-leaf) = t-node x x₁ (t-single key value) ti
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
618 RTtoTI0 (node key₃ _ (node key₂ _ _ _) leaf) (node key₃ _ (node key₁ value₁ left left₁) leaf) key value (t-left x₁ ti) (r-left x ri) =
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
619 t-left (subst (λ k → k < key₃ ) (rt-property-key ri) x₁) (RTtoTI0 _ _ key value ti ri) -- key₁ < key₃
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
620 RTtoTI0 (node key₁ _ (node key₂ _ _ _) (node _ _ _ _)) (node key₁ _ (node key₃ _ _ _) (node _ _ _ _)) key value (t-node x₁ x₂ ti ti₁) (r-left x ri) = t-node (subst (λ k → k < key₁ ) (rt-property-key ri) x₁) x₂ (RTtoTI0 _ _ key value ti ri) ti₁
615
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 614
diff changeset
621
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 614
diff changeset
622 RTtoTI1 : {n : Level} {A : Set n} → (tree repl : bt A) → (key : ℕ) → (value : A) → treeInvariant repl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 614
diff changeset
623 → replacedTree key value tree repl → treeInvariant tree
701
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
624 RTtoTI1 .leaf .(node key value leaf leaf) key value ti r-leaf = t-leaf
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
625 RTtoTI1 (node key value₁ leaf leaf) .(node key value leaf leaf) key value (t-single .key .value) r-node = t-single key value₁
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
626 RTtoTI1 .(node key _ leaf (node _ _ _ _)) .(node key value leaf (node _ _ _ _)) key value (t-right x ti) r-node = t-right x ti
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
627 RTtoTI1 .(node key _ (node _ _ _ _) leaf) .(node key value (node _ _ _ _) leaf) key value (t-left x ti) r-node = t-left x ti
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
628 RTtoTI1 .(node key _ (node _ _ _ _) (node _ _ _ _)) .(node key value (node _ _ _ _) (node _ _ _ _)) key value (t-node x x₁ ti ti₁) r-node = t-node x x₁ ti ti₁
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
629 -- r-right case
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
630 RTtoTI1 (node key₁ value₁ leaf leaf) (node key₁ _ leaf (node _ _ _ _)) key value (t-right x₁ ti) (r-right x r-leaf) = t-single key₁ value₁
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
631 RTtoTI1 (node key₁ value₁ leaf (node key₂ value₂ t2 t3)) (node key₁ _ leaf (node key₃ _ _ _)) key value (t-right x₁ ti) (r-right x ri) =
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
632 t-right (subst (λ k → key₁ < k ) (sym (rt-property-key ri)) x₁) (RTtoTI1 _ _ key value ti ri) -- key₁ < key₂
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
633 RTtoTI1 (node _ _ (node _ _ _ _) leaf) (node _ _ (node _ _ _ _) (node key value _ _)) key value (t-node x₁ x₂ ti ti₁) (r-right x r-leaf) =
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
634 t-left x₁ ti
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
635 RTtoTI1 (node key₄ _ (node key₃ _ _ _) (node key₁ value₁ n n₁)) (node key₄ _ (node key₃ _ _ _) (node key₂ _ _ _)) key value (t-node x₁ x₂ ti ti₁) (r-right x ri) = t-node x₁ (subst (λ k → key₄ < k ) (sym (rt-property-key ri)) x₂) ti (RTtoTI1 _ _ key value ti₁ ri) -- key₄ < key₁
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
636 -- r-left case
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
637 RTtoTI1 (node key₁ value₁ leaf leaf) (node key₁ _ _ leaf) key value (t-left x₁ ti) (r-left x ri) = t-single key₁ value₁
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
638 RTtoTI1 (node key₁ _ (node key₂ value₁ n n₁) leaf) (node key₁ _ (node key₃ _ _ _) leaf) key value (t-left x₁ ti) (r-left x ri) =
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
639 t-left (subst (λ k → k < key₁ ) (sym (rt-property-key ri)) x₁) (RTtoTI1 _ _ key value ti ri) -- key₂ < key₁
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
640 RTtoTI1 (node key₁ value₁ leaf _) (node key₁ _ _ _) key value (t-node x₁ x₂ ti ti₁) (r-left x r-leaf) = t-right x₂ ti₁
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
641 RTtoTI1 (node key₁ value₁ (node key₂ value₂ n n₁) _) (node key₁ _ _ _) key value (t-node x₁ x₂ ti ti₁) (r-left x ri) =
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
642 t-node (subst (λ k → k < key₁ ) (sym (rt-property-key ri)) x₁) x₂ (RTtoTI1 _ _ key value ti ri) ti₁ -- key₂ < key₁
614
0c174b6239a0 connected
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 613
diff changeset
643
611
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 610
diff changeset
644 insertTreeP : {n m : Level} {A : Set n} {t : Set m} → (tree : bt A) → (key : ℕ) → (value : A) → treeInvariant tree
696
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 695
diff changeset
645 → (exit : (tree repl : bt A) → treeInvariant repl ∧ replacedTree key value tree repl → t ) → t
693
49dd82f49fa1 insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 692
diff changeset
646 insertTreeP {n} {m} {A} {t} tree key value P0 exit =
49dd82f49fa1 insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 692
diff changeset
647 TerminatingLoopS (bt A ∧ List (bt A) ) {λ p → treeInvariant (proj1 p) ∧ stackInvariant key (proj1 p) tree (proj2 p) } (λ p → bt-depth (proj1 p)) ⟪ tree , tree ∷ [] ⟫ ⟪ P0 , s-single ⟫
696
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 695
diff changeset
648 $ λ p P loop → findP key (proj1 p) tree (proj2 p) P (λ t s P1 lt → loop ⟪ t , s ⟫ P1 lt )
693
49dd82f49fa1 insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 692
diff changeset
649 $ λ t s P C → replaceNodeP key value t C (proj1 P)
49dd82f49fa1 insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 692
diff changeset
650 $ λ t1 P1 R → TerminatingLoopS (List (bt A) ∧ bt A ∧ bt A )
49dd82f49fa1 insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 692
diff changeset
651 {λ p → replacePR key value (proj1 (proj2 p)) (proj2 (proj2 p)) (proj1 p) (λ _ _ _ → Lift n ⊤ ) }
696
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 695
diff changeset
652 (λ p → length (proj1 p)) ⟪ s , ⟪ t , t1 ⟫ ⟫ record { tree0 = tree ; ti = P0 ; si = proj2 P ; ri = R ; ci = lift tt }
693
49dd82f49fa1 insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 692
diff changeset
653 $ λ p P1 loop → replaceP key value (proj2 (proj2 p)) (proj1 p) P1
696
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 695
diff changeset
654 (λ key value {tree1} repl1 stack P2 lt → loop ⟪ stack , ⟪ tree1 , repl1 ⟫ ⟫ P2 lt )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 695
diff changeset
655 $ λ tree repl P → exit tree repl ⟪ RTtoTI0 _ _ _ _ (proj1 P) (proj2 P) , proj2 P ⟫
614
0c174b6239a0 connected
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 613
diff changeset
656
696
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 695
diff changeset
657 insertTestP1 = insertTreeP leaf 1 1 t-leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 695
diff changeset
658 $ λ _ x P → insertTreeP x 2 1 (proj1 P)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 695
diff changeset
659 $ λ _ x P → insertTreeP x 3 2 (proj1 P)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 695
diff changeset
660 $ λ _ x P → insertTreeP x 2 2 (proj1 P) (λ _ x _ → x )
694
da42fe4eda54 complete insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 693
diff changeset
661
609
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
662 top-value : {n : Level} {A : Set n} → (tree : bt A) → Maybe A
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
663 top-value leaf = nothing
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
664 top-value (node key value tree tree₁) = just value
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
665
703
23e0b9df7896 embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 702
diff changeset
666 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
618
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 617
diff changeset
667 field
619
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 618
diff changeset
668 tree0 : bt A
696
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 695
diff changeset
669 ti0 : treeInvariant tree0
695
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 694
diff changeset
670 ti : treeInvariant tree
662
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
671 si : stackInvariant key tree tree0 stack
703
23e0b9df7896 embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 702
diff changeset
672 ci : C key tree stack -- data continuation
702
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 701
diff changeset
673
703
23e0b9df7896 embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 702
diff changeset
674 record findExt {n : Level} {A : Set n} (key : ℕ) (C : ℕ → bt A → List (bt A) → Set n) : Set (Level.suc n) where
702
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 701
diff changeset
675 field
703
23e0b9df7896 embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 702
diff changeset
676 c1 : {key₁ : ℕ} {tree tree₁ : bt A } {st : List (bt A)} {v1 : A}
23e0b9df7896 embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 702
diff changeset
677 → findPR key (node key₁ v1 tree tree₁) st C → key < key₁ → C key tree (tree ∷ st)
23e0b9df7896 embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 702
diff changeset
678 c2 : {key₁ : ℕ} {tree tree₁ : bt A } {st : List (bt A)} {v1 : A}
23e0b9df7896 embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 702
diff changeset
679 → findPR key (node key₁ v1 tree tree₁) st C → key > key₁ → C key tree₁ (tree₁ ∷ st)
618
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 617
diff changeset
680
695
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 694
diff changeset
681 findPP : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (tree : bt A ) → (stack : List (bt A))
703
23e0b9df7896 embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 702
diff changeset
682 → {C : ℕ → bt A → List (bt A) → Set n } → findPR key tree stack C → findExt key C
702
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 701
diff changeset
683 → (next : (tree1 : bt A) → (stack : List (bt A)) → findPR key tree1 stack C → bt-depth tree1 < bt-depth tree → t )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 701
diff changeset
684 → (exit : (tree1 : bt A) → (stack : List (bt A)) → findPR key tree1 stack C
695
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 694
diff changeset
685 → (tree1 ≡ leaf ) ∨ ( node-key tree1 ≡ just key ) → t ) → t
702
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 701
diff changeset
686 findPP key leaf st Pre _ _ exit = exit leaf st Pre (case1 refl)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 701
diff changeset
687 findPP key (node key₁ v1 tree tree₁) st Pre _ next exit with <-cmp key key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 701
diff changeset
688 findPP key n st Pre _ _ exit | tri≈ ¬a refl ¬c = exit n st Pre (case2 refl)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 701
diff changeset
689 findPP {n} {_} {A} key (node key₁ v1 tree tree₁) st Pre e next _ | tri< a ¬b ¬c = next tree (tree ∷ st)
705
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 704
diff changeset
690 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
695
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 694
diff changeset
691 findP1 : key < key₁ → (st : List (bt A)) → stackInvariant key (node key₁ v1 tree tree₁)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 694
diff changeset
692 (findPR.tree0 Pre) st → stackInvariant key tree (findPR.tree0 Pre) (tree ∷ st)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 694
diff changeset
693 findP1 a (x ∷ st) si = s-left a si
702
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 701
diff changeset
694 findPP key n@(node key₁ v1 tree tree₁) st Pre e next _ | tri> ¬a ¬b c = next tree₁ (tree₁ ∷ st)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 701
diff changeset
695 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<
616
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 615
diff changeset
696
618
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 617
diff changeset
697 insertTreePP : {n m : Level} {A : Set n} {t : Set m} → (tree : bt A) → (key : ℕ) → (value : A) → treeInvariant tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 617
diff changeset
698 → (exit : (tree repl : bt A) → treeInvariant tree ∧ replacedTree key value tree repl → t ) → t
695
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 694
diff changeset
699 insertTreePP {n} {m} {A} {t} tree key value P0 exit =
703
23e0b9df7896 embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 702
diff changeset
700 TerminatingLoopS (bt A ∧ List (bt A) ) {λ p → findPR key (proj1 p) (proj2 p) (λ _ _ _ → Lift n ⊤) } (λ p → bt-depth (proj1 p)) ⟪ tree , tree ∷ [] ⟫
696
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 695
diff changeset
701 record { tree0 = tree ; ti = P0 ; ti0 = P0 ;si = s-single ; ci = lift tt }
703
23e0b9df7896 embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 702
diff changeset
702 $ λ 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 )
695
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 694
diff changeset
703 $ λ t s P C → replaceNodeP key value t C (findPR.ti P)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 694
diff changeset
704 $ λ t1 P1 R → TerminatingLoopS (List (bt A) ∧ bt A ∧ bt A )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 694
diff changeset
705 {λ p → replacePR key value (proj1 (proj2 p)) (proj2 (proj2 p)) (proj1 p) (λ _ _ _ → Lift n ⊤ ) }
696
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 695
diff changeset
706 (λ p → length (proj1 p)) ⟪ s , ⟪ t , t1 ⟫ ⟫ record { tree0 = findPR.tree0 P ; ti = findPR.ti0 P ; si = findPR.si P ; ri = R ; ci = lift tt }
695
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 694
diff changeset
707 $ λ p P1 loop → replaceP key value (proj2 (proj2 p)) (proj1 p) P1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 694
diff changeset
708 (λ key value {tree1} repl1 stack P2 lt → loop ⟪ stack , ⟪ tree1 , repl1 ⟫ ⟫ P2 lt ) exit
618
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 617
diff changeset
709
703
23e0b9df7896 embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 702
diff changeset
710 record findPC {n : Level} {A : Set n} (value : A) (key1 : ℕ) (tree : bt A ) (stack : List (bt A)) : Set n where
616
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 615
diff changeset
711 field
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 615
diff changeset
712 tree1 : bt A
698
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
713 ci : replacedTree key1 value tree1 tree
616
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 615
diff changeset
714
702
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 701
diff changeset
715 findPPC1 : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (value : A) → (tree : bt A ) → (stack : List (bt A))
703
23e0b9df7896 embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 702
diff changeset
716 → findPR key tree stack (findPC value )
23e0b9df7896 embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 702
diff changeset
717 → (next : (tree1 : bt A) → (stack : List (bt A)) → findPR key tree1 stack (findPC value ) → bt-depth tree1 < bt-depth tree → t )
23e0b9df7896 embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 702
diff changeset
718 → (exit : (tree1 : bt A) → (stack : List (bt A)) → findPR key tree1 stack (findPC value )
702
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 701
diff changeset
719 → (tree1 ≡ leaf ) ∨ ( node-key tree1 ≡ just key ) → t ) → t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 701
diff changeset
720 findPPC1 {n} {_} {A} key value tree stack Pr next exit = findPP key tree stack Pr findext next exit where
703
23e0b9df7896 embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 702
diff changeset
721 findext01 : {key₂ : ℕ} {tree₁ : bt A} {tree₂ : bt A} {st : List (bt A)} {v1 : A}
23e0b9df7896 embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 702
diff changeset
722 → (Pre : findPR key (node key₂ v1 tree₁ tree₂) st (findPC value) )
23e0b9df7896 embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 702
diff changeset
723 → key < key₂ → findPC value key tree₁ (tree₁ ∷ st)
933
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 782
diff changeset
724 findext01 Pre a with findPC.ci (findPR.ci Pre) | findPC.tree1 (findPR.ci Pre) in eqp
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 782
diff changeset
725 ... | r-leaf | leaf = ⊥-elim ( nat-≤> a ≤-refl)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 782
diff changeset
726 ... | r-node | node key value t1 t3 = ⊥-elim ( nat-≤> a ≤-refl )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 782
diff changeset
727 ... | r-right x t | t1 = ⊥-elim (nat-<> x a)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 782
diff changeset
728 ... | r-left {key₂} {v1} {_} {t2} {tree₂} x ri | node key value t1 t3 = record { tree1 = t1 ; ci = subst (λ k → replacedTree _ _ k _) (lemma01 eqp) ri } where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 782
diff changeset
729 lemma01 : {t2 : bt A} → node key₂ v1 t2 tree₂ ≡ node key value t1 t3 → t2 ≡ t1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 782
diff changeset
730 lemma01 refl = refl
703
23e0b9df7896 embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 702
diff changeset
731 findext02 : {key₂ : ℕ} {tree₁ : bt A} {tree₂ : bt A} {st : List (bt A)} {v1 : A}
23e0b9df7896 embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 702
diff changeset
732 → (Pre : findPR key (node key₂ v1 tree₁ tree₂) st (findPC value) )
23e0b9df7896 embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 702
diff changeset
733 → key > key₂ → findPC value key tree₂ (tree₂ ∷ st)
933
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 782
diff changeset
734 findext02 Pre c with findPC.ci (findPR.ci Pre) | findPC.tree1 (findPR.ci Pre) in eqp
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 782
diff changeset
735 ... | r-leaf | leaf = ⊥-elim ( nat-≤> c ≤-refl)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 782
diff changeset
736 ... | r-node | node key value t1 t3 = ⊥-elim ( nat-≤> c ≤-refl )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 782
diff changeset
737 ... | r-left x t | t1 = ⊥-elim (nat-<> x c)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 782
diff changeset
738 ... | r-right {key₂} {v1} {_} {tree₂} x ri | node key value t1 t3 = record { tree1 = t3 ; ci = subst (λ k → replacedTree _ _ k _) (lemma01 eqp) ri } where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 782
diff changeset
739 lemma01 : {t2 : bt A} → node key₂ v1 tree₂ t2 ≡ node key value t1 t3 → t2 ≡ t3
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 782
diff changeset
740 lemma01 refl = refl
703
23e0b9df7896 embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 702
diff changeset
741 findext : findExt key (findPC value )
23e0b9df7896 embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 702
diff changeset
742 findext = record { c1 = findext01 ; c2 = findext02 }
702
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 701
diff changeset
743
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 701
diff changeset
744 insertTreeSpec0 : {n : Level} {A : Set n} → (tree : bt A) → (value : A) → top-value tree ≡ just value → ⊤
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 701
diff changeset
745 insertTreeSpec0 _ _ _ = tt
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 701
diff changeset
746
700
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 699
diff changeset
747 containsTree : {n : Level} {A : Set n} → (tree tree1 : bt A) → (key : ℕ) → (value : A) → treeInvariant tree1 → replacedTree key value tree1 tree → ⊤
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 699
diff changeset
748 containsTree {n} {A} tree tree1 key value P RT =
617
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 616
diff changeset
749 TerminatingLoopS (bt A ∧ List (bt A) )
703
23e0b9df7896 embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 702
diff changeset
750 {λ p → findPR key (proj1 p) (proj2 p) (findPC value ) } (λ p → bt-depth (proj1 p))
698
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
751 ⟪ tree , tree ∷ [] ⟫ record { tree0 = tree ; ti0 = RTtoTI0 _ _ _ _ P RT ; ti = RTtoTI0 _ _ _ _ P RT ; si = s-single
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
752 ; ci = record { tree1 = tree1 ; ci = RT } }
702
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 701
diff changeset
753 $ λ p P loop → findPPC1 key value (proj1 p) (proj2 p) P (λ t s P1 lt → loop ⟪ t , s ⟫ P1 lt )
695
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 694
diff changeset
754 $ λ t1 s1 P2 found? → insertTreeSpec0 t1 value (lemma6 t1 s1 found? P2) where
703
23e0b9df7896 embedding invariant extentiion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 702
diff changeset
755 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
698
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
756 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
700
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 699
diff changeset
757 lemma8 : {tree1 t1 : bt A } → replacedTree key value tree1 t1 → node-key t1 ≡ just key → top-value t1 ≡ just value
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 699
diff changeset
758 lemma8 {.leaf} {node key value .leaf .leaf} r-leaf refl = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 699
diff changeset
759 lemma8 {.(node key _ t1 t2)} {node key value t1 t2} r-node refl = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 699
diff changeset
760 lemma8 {.(node key value t1 _)} {node key value t1 t2} (r-right x ri) refl = ⊥-elim (¬x<x x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 699
diff changeset
761 lemma8 {.(node key value _ t2)} {node key value t1 t2} (r-left x ri) refl = ⊥-elim (¬x<x x)
698
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
762 lemma7 : (t1 : bt A) ( s1 : List (bt A) ) (tree0 tree1 : bt A)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
763 → replacedTree key value tree1 t1 → stackInvariant key t1 tree0 s1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
764 → ( t1 ≡ leaf ) ∨ ( node-key t1 ≡ just key) → top-value t1 ≡ just value
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
765 lemma7 .leaf (.leaf ∷ []) .leaf tree1 () s-single (case1 refl)
700
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 699
diff changeset
766 lemma7 (node key value t1 t2) (.(node key value t1 t2) ∷ []) .(node key value t1 t2) tree1 ri s-single (case2 x) = lemma8 ri x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 699
diff changeset
767 lemma7 (node key value t1 t2) (.(node key value t1 t2) ∷ x₁ ∷ s1) tree0 tree1 ri (s-right x si) (case2 x₂) = lemma8 ri x₂
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 699
diff changeset
768 lemma7 (node key value t1 t2) (.(node key value t1 t2) ∷ x₁ ∷ s1) tree0 tree1 ri (s-left x si) (case2 x₂) = lemma8 ri x₂
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 699
diff changeset
769
615
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 614
diff changeset
770
700
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 699
diff changeset
771 containsTree1 : {n : Level} {A : Set n} → (tree : bt A) → (key : ℕ) → (value : A) → treeInvariant tree → ⊤
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 699
diff changeset
772 containsTree1 {n} {A} tree key value ti =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 699
diff changeset
773 insertTreeP tree key value ti
702
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 701
diff changeset
774 $ λ tree0 tree1 P → containsTree tree1 tree0 key value (RTtoTI1 _ _ _ _ (proj1 P) (proj2 P) ) (proj2 P)
700
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 699
diff changeset
775
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 699
diff changeset
776