annotate hoareBinaryTree1.agda @ 762:56de8e7dca7a

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Fri, 05 May 2023 10:14:53 +0900
parents 927c02120a73
children 799325a71422
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
722
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 718
diff changeset
1 module hoareBinaryTree1 where
586
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff changeset
2
727
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 726
diff changeset
3 open import Level hiding (suc ; zero ; _⊔_ )
586
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
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff changeset
10 open import Data.List
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
590
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 589
diff changeset
21 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 589
diff changeset
22 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 589
diff changeset
23 -- no children , having left node , having right node , having both
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 589
diff changeset
24 --
597
ryokka
parents: 596
diff changeset
25 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
26 leaf : bt A
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
27 node : (key : ℕ) → (value : A) →
610
8239583dac0b add one more stack
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 609
diff changeset
28 (left : bt A ) → (right : bt A ) → bt A
600
016a8deed93d fix old binary tree
ryokka
parents: 597
diff changeset
29
620
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
30 node-key : {n : Level} {A : Set n} → bt A → Maybe ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
31 node-key (node key _ _ _) = just key
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
32 node-key _ = nothing
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
33
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
34 node-value : {n : Level} {A : Set n} → bt A → Maybe A
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
35 node-value (node _ value _ _) = just value
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
36 node-value _ = nothing
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
37
609
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
38 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
39 bt-depth leaf = 0
727
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 726
diff changeset
40 bt-depth (node key value t t₁) = suc (bt-depth t ⊔ bt-depth t₁ )
606
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
41
605
f8cc98fcc34b define invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 604
diff changeset
42 open import Data.Unit hiding ( _≟_ ; _≤?_ ; _≤_)
f8cc98fcc34b define invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 604
diff changeset
43
620
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
44 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
45 t-leaf : treeInvariant leaf
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
46 t-single : (key : ℕ) → (value : A) → treeInvariant (node key value leaf leaf)
745
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 744
diff changeset
47 t-right : {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
48 → treeInvariant (node key value leaf (node key₁ value₁ t₁ t₂))
745
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 744
diff changeset
49 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
50 → treeInvariant (node key₁ value₁ (node key value t₁ t₂) leaf )
745
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 744
diff changeset
51 t-node : {key key₁ key₂ : ℕ} → {value value₁ value₂ : A} → {t₁ t₂ t₃ t₄ : bt A} → key < key₁ → key₁ < key₂
620
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
52 → treeInvariant (node key value t₁ t₂)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
53 → treeInvariant (node key₂ value₂ t₃ t₄)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
54 → 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
55
662
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
56 --
745
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 744
diff changeset
57 -- stack always contains original top at end (path of the tree)
662
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
58 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
59 data stackInvariant {n : Level} {A : Set n} (key : ℕ) : (top orig : bt A) → (stack : List (bt A)) → Set n where
729
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 727
diff changeset
60 s-nil : {tree0 : bt A} → stackInvariant key tree0 tree0 (tree0 ∷ [])
653
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 652
diff changeset
61 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
62 → 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
63 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
64 → 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
65
677
681577b60c35 child-replaced
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 676
diff changeset
66 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
67 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
68 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
69 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
70 → 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
71 r-left : {k : ℕ } {v1 : A} → {t t1 t2 : bt A}
687
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
72 → 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
73
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
74 add< : { i : ℕ } (j : ℕ ) → i < suc i + j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
75 add< {i} j = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
76 suc i ≤⟨ m≤m+n (suc i) j ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
77 suc i + j ∎ where open ≤-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
78
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
79 treeTest1 : bt ℕ
692
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
80 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
81 treeTest2 : bt ℕ
692
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
82 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
83
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
84 treeInvariantTest1 : treeInvariant treeTest1
692
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
85 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
86
639
5fe23f540726 replacedTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 638
diff changeset
87 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
88 stack-top [] = nothing
5fe23f540726 replacedTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 638
diff changeset
89 stack-top (x ∷ s) = just x
606
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
90
639
5fe23f540726 replacedTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 638
diff changeset
91 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
92 stack-last [] = nothing
5fe23f540726 replacedTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 638
diff changeset
93 stack-last (x ∷ []) = just x
5fe23f540726 replacedTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 638
diff changeset
94 stack-last (x ∷ s) = stack-last s
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
95
662
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
96 stackInvariantTest1 : stackInvariant 4 treeTest2 treeTest1 ( treeTest2 ∷ treeTest1 ∷ [] )
729
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 727
diff changeset
97 stackInvariantTest1 = s-right (add< 3) (s-nil )
662
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
98
666
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 665
diff changeset
99 si-property0 : {n : Level} {A : Set n} {key : ℕ} {tree tree0 : bt A} → {stack : List (bt A)} → stackInvariant key tree tree0 stack → ¬ ( stack ≡ [] )
729
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 727
diff changeset
100 si-property0 (s-nil ) ()
666
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 665
diff changeset
101 si-property0 (s-right x si) ()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 665
diff changeset
102 si-property0 (s-left x si) ()
665
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 664
diff changeset
103
666
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 665
diff changeset
104 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
105 → tree1 ≡ tree
729
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 727
diff changeset
106 si-property1 (s-nil ) = refl
666
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 665
diff changeset
107 si-property1 (s-right _ si) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 665
diff changeset
108 si-property1 (s-left _ si) = refl
662
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
109
663
cf5095488bbd stack contains original tree at end always
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 662
diff changeset
110 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
111 → stack-last stack ≡ just tree0
729
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 727
diff changeset
112 si-property-last key t t0 (t ∷ []) (s-nil ) = refl
666
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 665
diff changeset
113 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
114 ... | refl = si-property-last key x t0 (x ∷ st) si
666
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 665
diff changeset
115 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
116 ... | refl = si-property-last key x t0 (x ∷ st) si
656
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 655
diff changeset
117
639
5fe23f540726 replacedTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 638
diff changeset
118 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
119 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
120 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
121 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
122 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
123
690
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
124 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
125 rt-property-leaf r-leaf = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
126
698
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
127 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
128 rt-property-¬leaf ()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
129
692
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
130 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
131 → 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
132 rt-property-key r-node = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
133 rt-property-key (r-right x ri) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
134 rt-property-key (r-left x ri) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
135
698
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
136 nat-≤> : { x y : ℕ } → x ≤ y → y < x → ⊥
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
137 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
138 nat-<> : { x y : ℕ } → x < y → y < x → ⊥
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
139 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
140
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
141 open _∧_
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
142
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
143
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
144 depth-1< : {i j : ℕ} → suc i ≤ suc (i Data.Nat.⊔ j )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
145 depth-1< {i} {j} = s≤s (m≤m⊔n _ j)
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 depth-2< : {i j : ℕ} → suc i ≤ suc (j Data.Nat.⊔ i )
650
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 649
diff changeset
148 depth-2< {i} {j} = s≤s (m≤n⊔m j i)
611
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 610
diff changeset
149
649
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
150 depth-3< : {i : ℕ } → suc i ≤ suc (suc i)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
151 depth-3< {zero} = s≤s ( z≤n )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
152 depth-3< {suc i} = s≤s (depth-3< {i} )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
153
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
154
634
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
155 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
156 → treeInvariant (node k v1 tree tree₁)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
157 → treeInvariant tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
158 treeLeftDown {n} {A} {_} {v1} leaf leaf (t-single k1 v1) = t-leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
159 treeLeftDown {n} {A} {_} {v1} .leaf .(node _ _ _ _) (t-right x ti) = t-leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
160 treeLeftDown {n} {A} {_} {v1} .(node _ _ _ _) .leaf (t-left x ti) = ti
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
161 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
162
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
163 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
164 → treeInvariant (node k v1 tree tree₁)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
165 → treeInvariant tree₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
166 treeRightDown {n} {A} {_} {v1} .leaf .leaf (t-single _ .v1) = t-leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
167 treeRightDown {n} {A} {_} {v1} .leaf .(node _ _ _ _) (t-right x ti) = ti
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
168 treeRightDown {n} {A} {_} {v1} .(node _ _ _ _) .leaf (t-left x ti) = t-leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
169 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
170
615
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 614
diff changeset
171 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
172 → treeInvariant tree ∧ stackInvariant key tree tree0 stack
693
49dd82f49fa1 insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 692
diff changeset
173 → (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
174 → (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
175 → (tree1 ≡ leaf ) ∨ ( node-key tree1 ≡ just key ) → t ) → t
693
49dd82f49fa1 insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 692
diff changeset
176 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
177 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
178 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
179 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
180 ⟪ 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
181 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
182 findP1 a (x ∷ st) si = s-left a si
693
49dd82f49fa1 insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 692
diff changeset
183 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
184
638
be6bd51c3f05 replaceTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 637
diff changeset
185 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
186 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
187 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
188 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
189 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
190
649
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
191 open import Relation.Binary.Definitions
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
192
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
193 lemma3 : {i j : ℕ} → 0 ≡ i → j < i → ⊥
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
194 lemma3 refl ()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
195 lemma5 : {i j : ℕ} → i < 1 → j < i → ⊥
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
196 lemma5 (s≤s z≤n) ()
700
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 699
diff changeset
197 ¬x<x : {x : ℕ} → ¬ (x < x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 699
diff changeset
198 ¬x<x (s≤s lt) = ¬x<x lt
649
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
199
687
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
200 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
201 child-replaced key leaf = leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
202 child-replaced key (node key₁ value left right) with <-cmp key key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
203 ... | tri< a ¬b ¬c = left
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
204 ... | tri≈ ¬a b ¬c = node key₁ value left right
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
205 ... | tri> ¬a ¬b c = right
677
681577b60c35 child-replaced
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 676
diff changeset
206
671
b5fde9727830 use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 670
diff changeset
207 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
208 field
b5fde9727830 use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 670
diff changeset
209 tree0 : bt A
b5fde9727830 use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 670
diff changeset
210 ti : treeInvariant tree0
b5fde9727830 use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 670
diff changeset
211 si : stackInvariant key tree tree0 stack
687
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
212 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
213 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
214
638
be6bd51c3f05 replaceTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 637
diff changeset
215 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
216 → (tree ≡ leaf ) ∨ ( node-key tree ≡ just key )
694
da42fe4eda54 complete insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 693
diff changeset
217 → (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
218 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
219 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
220 (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
221 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
222 repl00 with <-cmp k k
da42fe4eda54 complete insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 693
diff changeset
223 ... | tri< a ¬b ¬c = ⊥-elim (¬b refl)
da42fe4eda54 complete insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 693
diff changeset
224 ... | tri≈ ¬a b ¬c = refl
da42fe4eda54 complete insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 693
diff changeset
225 ... | tri> ¬a ¬b c = ⊥-elim (¬b refl)
606
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
226
609
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
227 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
228 → (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
229 → (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
230 → (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
231 → 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
232 → (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
233 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
234 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
235 ... | 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
236 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
237 ... | 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
238 repl01 : replacedTree key value (replacePR.tree0 Pre) (node key₁ value₁ repl right )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
239 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
240 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
241 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
242 repl03 = replacePR.ri Pre
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
243 repl02 : child-replaced key (node key₁ value₁ left right) ≡ left
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
244 repl02 with <-cmp key key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
245 ... | tri< a ¬b ¬c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
246 ... | tri≈ ¬a b ¬c = ⊥-elim ( ¬a a)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
247 ... | tri> ¬a ¬b c = ⊥-elim ( ¬a a)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
248 ... | 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
249 repl01 : replacedTree key value (replacePR.tree0 Pre) repl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 677
diff changeset
250 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
251 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
252 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
253 repl02 with <-cmp key key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
254 ... | tri< a ¬b ¬c = ⊥-elim ( ¬b b)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
255 ... | tri≈ ¬a b ¬c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
256 ... | tri> ¬a ¬b c = ⊥-elim ( ¬b b)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
257 ... | 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
258 repl01 : replacedTree key value (replacePR.tree0 Pre) (node key₁ value₁ left repl )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
259 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
260 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
261 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
262 repl03 = replacePR.ri Pre
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
263 repl02 : child-replaced key (node key₁ value₁ left right) ≡ right
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
264 repl02 with <-cmp key key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
265 ... | tri< a ¬b ¬c = ⊥-elim ( ¬c c)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
266 ... | tri≈ ¬a b ¬c = ⊥-elim ( ¬c c)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
267 ... | tri> ¬a ¬b c = refl
690
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
268 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
269 Post : replacePR key value tree1 repl (tree1 ∷ st1) (λ _ _ _ → Lift n ⊤)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
270 Post with replacePR.si Pre
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
271 ... | 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
272 repl09 : tree1 ≡ node key₂ v1 tree₁ leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
273 repl09 = si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
274 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
275 repl10 with si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
276 ... | refl = si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
277 repl07 : child-replaced key (node key₂ v1 tree₁ leaf) ≡ leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
278 repl07 with <-cmp key key₂
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
279 ... | tri< a ¬b ¬c = ⊥-elim (¬c x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
280 ... | tri≈ ¬a b ¬c = ⊥-elim (¬c x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
281 ... | tri> ¬a ¬b c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
282 repl12 : replacedTree key value (child-replaced key tree1 ) repl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
283 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
284 ... | 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
285 repl09 : tree1 ≡ node key₂ v1 leaf tree₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
286 repl09 = si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
287 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
288 repl10 with si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
289 ... | refl = si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
290 repl07 : child-replaced key (node key₂ v1 leaf tree₁ ) ≡ leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
291 repl07 with <-cmp key key₂
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
292 ... | tri< a ¬b ¬c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
293 ... | tri≈ ¬a b ¬c = ⊥-elim (¬a x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
294 ... | tri> ¬a ¬b c = ⊥-elim (¬a x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
295 repl12 : replacedTree key value (child-replaced key tree1 ) repl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
296 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
297 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
298 ... | 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
299 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
300 Post with replacePR.si Pre
688
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
301 ... | 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
302 repl09 : tree1 ≡ node key₂ v1 tree₁ (node key₁ value₁ left right)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
303 repl09 = si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
304 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
305 repl10 with si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
306 ... | refl = si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
307 repl03 : child-replaced key (node key₁ value₁ left right) ≡ left
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
308 repl03 with <-cmp key key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
309 ... | tri< a1 ¬b ¬c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
310 ... | tri≈ ¬a b ¬c = ⊥-elim (¬a a)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
311 ... | tri> ¬a ¬b c = ⊥-elim (¬a a)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
312 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
313 repl02 with repl09 | <-cmp key key₂
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
314 ... | refl | tri< a ¬b ¬c = ⊥-elim (¬c lt)
689
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
315 ... | refl | tri≈ ¬a b ¬c = ⊥-elim (¬c lt)
688
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
316 ... | refl | tri> ¬a ¬b c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
317 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
318 repl04 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
319 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
320 node key₁ value₁ left right ≡⟨ sym repl02 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
321 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
322 child-replaced key tree1 ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
323 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
324 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
325 ... | 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
326 repl09 : tree1 ≡ node key₂ v1 (node key₁ value₁ left right) tree₁
683
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 682
diff changeset
327 repl09 = si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 682
diff changeset
328 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 682
diff changeset
329 repl10 with si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 682
diff changeset
330 ... | refl = si
687
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
331 repl03 : child-replaced key (node key₁ value₁ left right) ≡ left
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
332 repl03 with <-cmp key key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
333 ... | tri< a1 ¬b ¬c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
334 ... | tri≈ ¬a b ¬c = ⊥-elim (¬a a)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
335 ... | tri> ¬a ¬b c = ⊥-elim (¬a a)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
336 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
337 repl02 with repl09 | <-cmp key key₂
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
338 ... | refl | tri< a ¬b ¬c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
339 ... | refl | tri≈ ¬a b ¬c = ⊥-elim (¬a lt)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
340 ... | refl | tri> ¬a ¬b c = ⊥-elim (¬a lt)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
341 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
342 repl04 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
343 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
344 node key₁ value₁ left right ≡⟨ sym repl02 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
345 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
346 child-replaced key tree1 ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
347 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
348 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
349 ... | 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
350 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
351 Post with replacePR.si Pre
691
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 690
diff changeset
352 ... | 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
353 repl09 : tree1 ≡ node key₂ v1 tree₁ tree -- (node key₁ value₁ left right)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
354 repl09 = si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
355 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
356 repl10 with si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
357 ... | refl = si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
358 repl07 : child-replaced key (node key₂ v1 tree₁ tree) ≡ tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
359 repl07 with <-cmp key key₂
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
360 ... | tri< a ¬b ¬c = ⊥-elim (¬c x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
361 ... | tri≈ ¬a b ¬c = ⊥-elim (¬c x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
362 ... | tri> ¬a ¬b c = refl
691
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 690
diff changeset
363 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
364 repl12 refl with repl09
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 690
diff changeset
365 ... | 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
366 ... | 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
367 repl09 : tree1 ≡ node key₂ v1 tree tree₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
368 repl09 = si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
369 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
370 repl10 with si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
371 ... | refl = si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
372 repl07 : child-replaced key (node key₂ v1 tree tree₁ ) ≡ tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
373 repl07 with <-cmp key key₂
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
374 ... | tri< a ¬b ¬c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
375 ... | tri≈ ¬a b ¬c = ⊥-elim (¬a x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
376 ... | tri> ¬a ¬b c = ⊥-elim (¬a x)
691
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 690
diff changeset
377 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
378 repl12 refl with repl09
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 690
diff changeset
379 ... | 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
380 ... | 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
381 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
382 Post with replacePR.si Pre
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
383 ... | 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
384 repl09 : tree1 ≡ node key₂ v1 tree₁ (node key₁ value₁ left right)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
385 repl09 = si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
386 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
387 repl10 with si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
388 ... | refl = si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
389 repl03 : child-replaced key (node key₁ value₁ left right) ≡ right
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
390 repl03 with <-cmp key key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
391 ... | tri< a1 ¬b ¬c = ⊥-elim (¬c c)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
392 ... | tri≈ ¬a b ¬c = ⊥-elim (¬c c)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
393 ... | tri> ¬a ¬b c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
394 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
395 repl02 with repl09 | <-cmp key key₂
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
396 ... | refl | tri< a ¬b ¬c = ⊥-elim (¬c lt)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
397 ... | refl | tri≈ ¬a b ¬c = ⊥-elim (¬c lt)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
398 ... | refl | tri> ¬a ¬b c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
399 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
400 repl04 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
401 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
402 node key₁ value₁ left right ≡⟨ sym repl02 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
403 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
404 child-replaced key tree1 ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
405 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
406 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
407 ... | 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
408 repl09 : tree1 ≡ node key₂ v1 (node key₁ value₁ left right) tree₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
409 repl09 = si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
410 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
411 repl10 with si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
412 ... | refl = si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
413 repl03 : child-replaced key (node key₁ value₁ left right) ≡ right
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
414 repl03 with <-cmp key key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
415 ... | tri< a1 ¬b ¬c = ⊥-elim (¬c c)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
416 ... | tri≈ ¬a b ¬c = ⊥-elim (¬c c)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
417 ... | tri> ¬a ¬b c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
418 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
419 repl02 with repl09 | <-cmp key key₂
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
420 ... | refl | tri< a ¬b ¬c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
421 ... | refl | tri≈ ¬a b ¬c = ⊥-elim (¬a lt)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
422 ... | refl | tri> ¬a ¬b c = ⊥-elim (¬a lt)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
423 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
424 repl04 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
425 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
426 node key₁ value₁ left right ≡⟨ sym repl02 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
427 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
428 child-replaced key tree1 ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
429 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
430 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
431
609
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
432 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
433 → (r : Index) → (p : Invraiant r)
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
434 → (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
435 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
436 ... | tri≈ ¬a b ¬c = loop r p (λ r1 p1 lt → ⊥-elim (lemma3 b lt) )
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
437 ... | tri< a ¬b ¬c = loop r p (λ r1 p1 lt1 → TerminatingLoop1 (reduce r) r r1 (≤-step lt1) p1 lt1 ) where
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
438 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
439 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
440 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
441 ... | 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
442 ... | 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
443 ... | 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
444
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
445 open _∧_
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
446
615
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 614
diff changeset
447 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
448 → replacedTree key value tree repl → treeInvariant repl
692
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
449 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
450 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
451 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
452 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
453 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
454 -- r-right case
692
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
455 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
456 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
457 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
458 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
459 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
460 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
461 t-node x₁ x ti (t-single key value)
693
49dd82f49fa1 insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 692
diff changeset
462 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
463 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
464 -- r-left case
700
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 699
diff changeset
465 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
466 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
467 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
468 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
469 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
470
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 614
diff changeset
471 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
472 → replacedTree key value tree repl → treeInvariant tree
701
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
473 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
474 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
475 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
476 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
477 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
478 -- r-right case
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
479 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
480 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
481 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
482 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
483 t-left x₁ ti
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
484 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
485 -- r-left case
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
486 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
487 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
488 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
489 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
490 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
491 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
492
611
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 610
diff changeset
493 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
494 → (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
495 insertTreeP {n} {m} {A} {t} tree key value P0 exit =
729
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 727
diff changeset
496 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-nil ⟫
696
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 695
diff changeset
497 $ λ 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
498 $ λ 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
499 $ λ 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
500 {λ 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
501 (λ 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
502 $ λ 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
503 (λ 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
504 $ λ 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
505
696
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 695
diff changeset
506 insertTestP1 = insertTreeP leaf 1 1 t-leaf
722
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 718
diff changeset
507 $ λ _ x0 P0 → insertTreeP x0 2 1 (proj1 P0)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 718
diff changeset
508 $ λ _ x1 P1 → insertTreeP x1 3 2 (proj1 P1)
727
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 726
diff changeset
509 $ λ _ x2 P2 → insertTreeP x2 2 2 (proj1 P2) (λ _ x P → x )
694
da42fe4eda54 complete insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 693
diff changeset
510
609
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
511 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
512 top-value leaf = nothing
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
513 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
514
722
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 718
diff changeset
515 -- is realy inserted?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 718
diff changeset
516
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 718
diff changeset
517 -- other element is preserved?
702
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 701
diff changeset
518
722
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 718
diff changeset
519 -- deletion?
616
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 615
diff changeset
520
722
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 718
diff changeset
521 data Color : Set where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 718
diff changeset
522 Red : Color
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 718
diff changeset
523 Black : Color
618
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 617
diff changeset
524
746
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 745
diff changeset
525 data RBtreeInvariant {n : Level} {A : Set n} : (tree : bt (Color ∧ A)) → (bd : ℕ) → Set n where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 745
diff changeset
526 rb-leaf : RBtreeInvariant leaf 0
758
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
527 rb-single : (key : ℕ) → (value : A) → RBtreeInvariant (node key ⟪ Black , value ⟫ leaf leaf) 1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
528 rb-right-red : {key key₁ : ℕ} → {value value₁ : A} → {t t₁ : bt (Color ∧ A)} → key < key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
529 → RBtreeInvariant (node key₁ ⟪ Black , value₁ ⟫ t t₁) 1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
530 → RBtreeInvariant (node key ⟪ Red , value ⟫ leaf (node key₁ ⟪ Black , value₁ ⟫ t t₁)) 1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
531 rb-right-black : {key key₁ : ℕ} → {value value₁ : A} → {t t₁ : bt (Color ∧ A)} → key < key₁ → {c : Color}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
532 → RBtreeInvariant (node key₁ ⟪ c , value₁ ⟫ t t₁) 1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
533 → RBtreeInvariant (node key ⟪ Black , value ⟫ leaf (node key₁ ⟪ c , value₁ ⟫ t t₁)) 1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
534 rb-left-red : {key key₁ : ℕ} → {value value₁ : A} → {t t₁ : bt (Color ∧ A)} → key < key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
535 → RBtreeInvariant (node key₁ ⟪ Black , value₁ ⟫ t t₁) 1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
536 → RBtreeInvariant (node key₁ ⟪ Red , value ⟫ (node key ⟪ Black , value₁ ⟫ t t₁) leaf ) 1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
537 rb-left-black : {key key₁ : ℕ} → {value value₁ : A} → {t t₁ : bt (Color ∧ A)} → key < key₁ → {c : Color}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
538 → RBtreeInvariant (node key₁ ⟪ c , value₁ ⟫ t t₁) 1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
539 → RBtreeInvariant (node key₁ ⟪ Black , value ⟫ (node key ⟪ c , value₁ ⟫ t t₁) leaf) 1
746
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 745
diff changeset
540 rb-node-red : {key key₁ key₂ : ℕ} → {value value₁ value₂ : A} → {t₁ t₂ t₃ t₄ : bt (Color ∧ A)} → key < key₁ → key₁ < key₂ → {d : ℕ}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 745
diff changeset
541 → RBtreeInvariant (node key ⟪ Black , value ⟫ t₁ t₂) d
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 745
diff changeset
542 → RBtreeInvariant (node key₂ ⟪ Black , value₂ ⟫ t₃ t₄) d
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 745
diff changeset
543 → RBtreeInvariant (node key₁ ⟪ Red , value₁ ⟫ (node key ⟪ Black , value ⟫ t₁ t₂) (node key₂ ⟪ Black , value₂ ⟫ t₃ t₄)) d
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 745
diff changeset
544 rb-node-black : {key key₁ key₂ : ℕ} → {value value₁ value₂ : A} → {t₁ t₂ t₃ t₄ : bt (Color ∧ A)} → key < key₁ → key₁ < key₂ → {d : ℕ}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 745
diff changeset
545 → {c c₁ : Color}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 745
diff changeset
546 → RBtreeInvariant (node key ⟪ c , value ⟫ t₁ t₂) d
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 745
diff changeset
547 → RBtreeInvariant (node key₂ ⟪ c₁ , value₂ ⟫ t₃ t₄) d
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 745
diff changeset
548 → RBtreeInvariant (node key₁ ⟪ Black , value₁ ⟫ (node key ⟪ c , value ⟫ t₁ t₂) (node key₂ ⟪ c₁ , value₂ ⟫ t₃ t₄)) (suc d)
745
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 744
diff changeset
549
758
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
550 tesr-rb-0 : RBtreeInvariant {_} {ℕ} leaf 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
551 tesr-rb-0 = rb-leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
552
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
553 tesr-rb-1 : RBtreeInvariant {_} {ℕ} (node 10 ⟪ Black , 10 ⟫ leaf leaf) 1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
554 tesr-rb-1 = rb-single 10 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
555
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
556 tesr-rb-2 : RBtreeInvariant {_} {ℕ} (node 10 ⟪ Red , 10 ⟫ (node 5 ⟪ Black , 5 ⟫ leaf leaf) leaf) 1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
557 tesr-rb-2 = rb-left-red (add< _) ( rb-single 10 5)
746
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 745
diff changeset
558
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 745
diff changeset
559 -- This one can be very difficult
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 745
diff changeset
560 -- data replacedRBTree {n : Level} {A : Set n} (key : ℕ) (value : A) : (before after : bt (Color ∧ A) ) → Set n where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 745
diff changeset
561 -- rb-leaf : replacedRBTree key value leaf (node key ⟪ Black , value ⟫ leaf leaf)
745
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 744
diff changeset
562
747
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 746
diff changeset
563 color : {n : Level} (A : Set n) → (rb : bt (Color ∧ A)) → Color
749
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 748
diff changeset
564 color {n} A leaf = Red
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 748
diff changeset
565 color {n} A (node key ⟪ c , v ⟫ rb rb₁) = c
723
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 722
diff changeset
566
747
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 746
diff changeset
567 RB→bt : {n : Level} (A : Set n) → (rb : bt (Color ∧ A)) → bt A
746
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 745
diff changeset
568 RB→bt {n} A leaf = leaf
747
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 746
diff changeset
569 RB→bt {n} A (node key ⟪ c , value ⟫ rb rb₁) = node key value (RB→bt A rb) (RB→bt A rb₁)
723
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 722
diff changeset
570
754
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
571 data ParentGrand {n : Level} {A : Set n} (self : bt A) : (parent uncle grand : bt A) → Set n where
740
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
572 s2-s1p2 : {kp kg : ℕ} {vp vg : A} → {n1 n2 : bt A} {parent grand : bt A }
754
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
573 → parent ≡ node kp vp self n1 → grand ≡ node kg vg parent n2 → ParentGrand self parent n2 grand
740
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
574 s2-1sp2 : {kp kg : ℕ} {vp vg : A} → {n1 n2 : bt A} {parent grand : bt A }
754
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
575 → parent ≡ node kp vp n1 self → grand ≡ node kg vg parent n2 → ParentGrand self parent n2 grand
740
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
576 s2-s12p : {kp kg : ℕ} {vp vg : A} → {n1 n2 : bt A} {parent grand : bt A }
754
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
577 → parent ≡ node kp vp self n1 → grand ≡ node kg vg n2 parent → ParentGrand self parent n2 grand
740
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
578 s2-1s2p : {kp kg : ℕ} {vp vg : A} → {n1 n2 : bt A} {parent grand : bt A }
754
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
579 → parent ≡ node kp vp n1 self → grand ≡ node kg vg n2 parent → ParentGrand self parent n2 grand
734
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 733
diff changeset
580
746
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 745
diff changeset
581 -- with color
736
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 735
diff changeset
582 data rotatedTree {n : Level} {A : Set n} : (before after : bt A ) → Set n where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 735
diff changeset
583 rr-node : {t : bt A} → rotatedTree t t
746
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 745
diff changeset
584 -- a b
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 745
diff changeset
585 -- b c d a
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 745
diff changeset
586 -- d e e c
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 745
diff changeset
587 rr-right : {ka kb : ℕ } {va vb : A} → {c c₁ d d₁ e e₁ : bt A}
736
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 735
diff changeset
588 → ka < kb
746
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 745
diff changeset
589 → rotatedTree d d₁ → rotatedTree e e₁ → rotatedTree c c₁
747
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 746
diff changeset
590 → rotatedTree (node ka va (node kb vb d e) c) (node kb vb d₁ (node ka va e₁ c₁) )
745
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 744
diff changeset
591 -- b a
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 744
diff changeset
592 -- d a b c
746
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 745
diff changeset
593 -- e c d e
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 745
diff changeset
594 rr-left : {ka kb : ℕ } {va vb : A} → {c c₁ d d₁ e e₁ : bt A}
736
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 735
diff changeset
595 → ka < kb
746
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 745
diff changeset
596 → rotatedTree d d₁ → rotatedTree e e₁ → rotatedTree c c₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 745
diff changeset
597 → rotatedTree (node kb vb d (node ka va e c) ) (node ka va (node kb vb d₁ e₁) c₁)
734
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 733
diff changeset
598
749
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 748
diff changeset
599 record PG {n : Level } (A : Set n) (self : bt A) (stack : List (bt A)) : Set n where
740
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
600 field
754
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
601 parent grand uncle : bt A
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
602 pg : ParentGrand self parent uncle grand
740
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
603 rest : List (bt A)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
604 stack=gp : stack ≡ ( self ∷ parent ∷ grand ∷ rest )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
605
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
606 stackToPG : {n : Level} {A : Set n} → {key : ℕ } → (tree orig : bt A )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
607 → (stack : List (bt A)) → stackInvariant key tree orig stack
749
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 748
diff changeset
608 → ( stack ≡ orig ∷ [] ) ∨ ( stack ≡ tree ∷ orig ∷ [] ) ∨ PG A tree stack
740
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
609 stackToPG {n} {A} {key} tree .tree .(tree ∷ []) s-nil = case1 refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
610 stackToPG {n} {A} {key} tree .(node _ _ _ tree) .(tree ∷ node _ _ _ tree ∷ []) (s-right x s-nil) = case2 (case1 refl)
741
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 740
diff changeset
611 stackToPG {n} {A} {key} tree .(node k2 v2 t5 (node k1 v1 t2 tree)) (tree ∷ node _ _ _ tree ∷ .(node k2 v2 t5 (node k1 v1 t2 tree) ∷ [])) (s-right {.tree} {.(node k2 v2 t5 (node k1 v1 t2 tree))} {t2} {k1} {v1} x (s-right {.(node k1 v1 t2 tree)} {.(node k2 v2 t5 (node k1 v1 t2 tree))} {t5} {k2} {v2} x₁ s-nil)) = case2 (case2
749
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 748
diff changeset
612 record { parent = node k1 v1 t2 tree ; grand = _ ; pg = s2-1s2p refl refl ; rest = _ ; stack=gp = refl } )
741
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 740
diff changeset
613 stackToPG {n} {A} {key} tree orig (tree ∷ node _ _ _ tree ∷ .(node k2 v2 t5 (node k1 v1 t2 tree) ∷ _)) (s-right {.tree} {.orig} {t2} {k1} {v1} x (s-right {.(node k1 v1 t2 tree)} {.orig} {t5} {k2} {v2} x₁ (s-right x₂ si))) = case2 (case2
749
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 748
diff changeset
614 record { parent = node k1 v1 t2 tree ; grand = _ ; pg = s2-1s2p refl refl ; rest = _ ; stack=gp = refl } )
741
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 740
diff changeset
615 stackToPG {n} {A} {key} tree orig (tree ∷ node _ _ _ tree ∷ .(node k2 v2 t5 (node k1 v1 t2 tree) ∷ _)) (s-right {.tree} {.orig} {t2} {k1} {v1} x (s-right {.(node k1 v1 t2 tree)} {.orig} {t5} {k2} {v2} x₁ (s-left x₂ si))) = case2 (case2
749
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 748
diff changeset
616 record { parent = node k1 v1 t2 tree ; grand = _ ; pg = s2-1s2p refl refl ; rest = _ ; stack=gp = refl } )
742
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 741
diff changeset
617 stackToPG {n} {A} {key} tree .(node k2 v2 (node k1 v1 t1 tree) t2) .(tree ∷ node k1 v1 t1 tree ∷ node k2 v2 (node k1 v1 t1 tree) t2 ∷ []) (s-right {_} {_} {t1} {k1} {v1} x (s-left {_} {_} {t2} {k2} {v2} x₁ s-nil)) = case2 (case2
749
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 748
diff changeset
618 record { parent = node k1 v1 t1 tree ; grand = _ ; pg = s2-1sp2 refl refl ; rest = _ ; stack=gp = refl } )
742
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 741
diff changeset
619 stackToPG {n} {A} {key} tree orig .(tree ∷ node k1 v1 t1 tree ∷ node k2 v2 (node k1 v1 t1 tree) t2 ∷ _) (s-right {_} {_} {t1} {k1} {v1} x (s-left {_} {_} {t2} {k2} {v2} x₁ (s-right x₂ si))) = case2 (case2
749
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 748
diff changeset
620 record { parent = node k1 v1 t1 tree ; grand = _ ; pg = s2-1sp2 refl refl ; rest = _ ; stack=gp = refl } )
742
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 741
diff changeset
621 stackToPG {n} {A} {key} tree orig .(tree ∷ node k1 v1 t1 tree ∷ node k2 v2 (node k1 v1 t1 tree) t2 ∷ _) (s-right {_} {_} {t1} {k1} {v1} x (s-left {_} {_} {t2} {k2} {v2} x₁ (s-left x₂ si))) = case2 (case2
749
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 748
diff changeset
622 record { parent = node k1 v1 t1 tree ; grand = _ ; pg = s2-1sp2 refl refl ; rest = _ ; stack=gp = refl } )
742
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 741
diff changeset
623 stackToPG {n} {A} {key} tree .(node _ _ tree _) .(tree ∷ node _ _ tree _ ∷ []) (s-left {_} {_} {t1} {k1} {v1} x s-nil) = case2 (case1 refl)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 741
diff changeset
624 stackToPG {n} {A} {key} tree .(node _ _ _ (node k1 v1 tree t1)) .(tree ∷ node k1 v1 tree t1 ∷ node _ _ _ (node k1 v1 tree t1) ∷ []) (s-left {_} {_} {t1} {k1} {v1} x (s-right x₁ s-nil)) = case2 (case2
749
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 748
diff changeset
625 record { parent = node k1 v1 tree t1 ; grand = _ ; pg = s2-s12p refl refl ; rest = _ ; stack=gp = refl } )
742
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 741
diff changeset
626 stackToPG {n} {A} {key} tree orig .(tree ∷ node k1 v1 tree t1 ∷ node _ _ _ (node k1 v1 tree t1) ∷ _) (s-left {_} {_} {t1} {k1} {v1} x (s-right x₁ (s-right x₂ si))) = case2 (case2
749
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 748
diff changeset
627 record { parent = node k1 v1 tree t1 ; grand = _ ; pg = s2-s12p refl refl ; rest = _ ; stack=gp = refl } )
742
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 741
diff changeset
628 stackToPG {n} {A} {key} tree orig .(tree ∷ node k1 v1 tree t1 ∷ node _ _ _ (node k1 v1 tree t1) ∷ _) (s-left {_} {_} {t1} {k1} {v1} x (s-right x₁ (s-left x₂ si))) = case2 (case2
749
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 748
diff changeset
629 record { parent = node k1 v1 tree t1 ; grand = _ ; pg = s2-s12p refl refl ; rest = _ ; stack=gp = refl } )
742
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 741
diff changeset
630 stackToPG {n} {A} {key} tree .(node _ _ (node k1 v1 tree t1) _) .(tree ∷ node k1 v1 tree t1 ∷ node _ _ (node k1 v1 tree t1) _ ∷ []) (s-left {_} {_} {t1} {k1} {v1} x (s-left x₁ s-nil)) = case2 (case2
749
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 748
diff changeset
631 record { parent = node k1 v1 tree t1 ; grand = _ ; pg = s2-s1p2 refl refl ; rest = _ ; stack=gp = refl } )
742
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 741
diff changeset
632 stackToPG {n} {A} {key} tree orig .(tree ∷ node k1 v1 tree t1 ∷ node _ _ (node k1 v1 tree t1) _ ∷ _) (s-left {_} {_} {t1} {k1} {v1} x (s-left x₁ (s-right x₂ si))) = case2 (case2
749
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 748
diff changeset
633 record { parent = node k1 v1 tree t1 ; grand = _ ; pg = s2-s1p2 refl refl ; rest = _ ; stack=gp = refl } )
742
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 741
diff changeset
634 stackToPG {n} {A} {key} tree orig .(tree ∷ node k1 v1 tree t1 ∷ node _ _ (node k1 v1 tree t1) _ ∷ _) (s-left {_} {_} {t1} {k1} {v1} x (s-left x₁ (s-left x₂ si))) = case2 (case2
749
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 748
diff changeset
635 record { parent = node k1 v1 tree t1 ; grand = _ ; pg = s2-s1p2 refl refl ; rest = _ ; stack=gp = refl } )
740
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
636
760
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 759
diff changeset
637 stackCase1 : {n : Level} {A : Set n} → {key : ℕ } → {tree orig : bt A }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 759
diff changeset
638 → {stack : List (bt A)} → stackInvariant key tree orig stack
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 759
diff changeset
639 → stack ≡ orig ∷ [] → tree ≡ orig
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 759
diff changeset
640 stackCase1 s-nil refl = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 759
diff changeset
641
749
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 748
diff changeset
642 PGtoRBinvariant : {n : Level} {A : Set n} → {key d0 ds dp dg : ℕ } → (tree orig : bt (Color ∧ A) )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 748
diff changeset
643 → RBtreeInvariant orig d0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 748
diff changeset
644 → (stack : List (bt (Color ∧ A))) → (pg : PG (Color ∧ A) tree stack )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 748
diff changeset
645 → RBtreeInvariant tree ds ∧ RBtreeInvariant (PG.parent pg) dp ∧ RBtreeInvariant (PG.grand pg) dg
758
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
646 PGtoRBinvariant = {!!}
734
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 733
diff changeset
647
748
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 747
diff changeset
648 findRBT : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (tree tree0 : bt (Color ∧ A) ) → (stack : List (bt (Color ∧ A))) → {d d0 : ℕ}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 747
diff changeset
649 → RBtreeInvariant tree0 d0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 747
diff changeset
650 → RBtreeInvariant tree d ∧ stackInvariant key tree tree0 stack
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 747
diff changeset
651 → (next : (tree1 : bt (Color ∧ A)) → (stack : List (bt (Color ∧ A))) → {d1 : ℕ} → RBtreeInvariant tree1 d1 ∧ stackInvariant key tree1 tree0 stack → bt-depth tree1 < bt-depth tree → t )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 747
diff changeset
652 → (exit : (tree1 : bt (Color ∧ A)) → (stack : List (bt (Color ∧ A))) → {d1 : ℕ} → RBtreeInvariant tree1 d1 ∧ stackInvariant key tree1 tree0 stack
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 747
diff changeset
653 → (tree1 ≡ leaf ) ∨ ( node-key tree1 ≡ just key ) → t ) → t
758
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
654 findRBT = {!!}
723
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 722
diff changeset
655
754
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
656 rotateLeft : {n m : Level} {A : Set n} {t : Set m}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
657 → (key : ℕ) → (value : A) → {d0 : ℕ}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
658 → (orig tree rot repl : bt (Color ∧ A)) {d0 : ℕ}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
659 → RBtreeInvariant orig d0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
660 → {d : ℕ} → RBtreeInvariant tree d → {dr : ℕ} → RBtreeInvariant repl dr
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
661 → (stack : List (bt (Color ∧ A))) → (si : stackInvariant key tree orig stack )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
662 → rotatedTree tree rot
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
663 → {c : Color} → replacedTree key ⟪ c , value ⟫ (child-replaced key rot) repl
756
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 755
diff changeset
664 → (next : {key2 d2 : ℕ} {c2 : Color}
754
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
665 → (to tr rot rr : bt (Color ∧ A))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
666 → RBtreeInvariant orig d0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
667 → {d : ℕ} → RBtreeInvariant tree d → {dr : ℕ} → RBtreeInvariant rr dr
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
668 → (stack1 : List (bt (Color ∧ A))) → stackInvariant key tr to stack1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
669 → rotatedTree tr rot
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
670 → {c : Color} → replacedTree key ⟪ c , value ⟫ (child-replaced key rot) rr
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
671 → length stack1 < length stack → t )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
672 → (exit : {k1 d1 : ℕ} {c1 : Color} → (rot repl : bt (Color ∧ A) )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
673 → {d : ℕ} → RBtreeInvariant repl d
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
674 → (ri : rotatedTree orig rot ) → {c : Color} → replacedTree key ⟪ c , value ⟫ rot repl → t ) → t
756
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 755
diff changeset
675 rotateLeft {n} {m} {A} {t} key value orig tree rot repl rbo rbt rbr stack si ri rr next exit = rotateLeft1 where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 755
diff changeset
676 rotateLeft1 : t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 755
diff changeset
677 rotateLeft1 with stackToPG tree orig stack si
758
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
678 ... | case1 x = exit {!!} {!!} {!!} {!!} rr
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
679 ... | case2 (case1 x) = {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
680 ... | case2 (case2 pg) = {!!}
754
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
681
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
682 rotateRight : {n m : Level} {A : Set n} {t : Set m}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
683 → (key : ℕ) → (value : A) → {d0 : ℕ}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
684 → (orig tree rot repl : bt (Color ∧ A)) {d0 : ℕ}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
685 → RBtreeInvariant orig d0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
686 → {d : ℕ} → RBtreeInvariant tree d → {dr : ℕ} → RBtreeInvariant repl dr
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
687 → (stack : List (bt (Color ∧ A))) → (si : stackInvariant key tree orig stack )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
688 → rotatedTree tree rot
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
689 → {c : Color} → replacedTree key ⟪ c , value ⟫ (child-replaced key rot) repl
756
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 755
diff changeset
690 → (next : {key2 d2 : ℕ} {c2 : Color}
754
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
691 → (to tr rot rr : bt (Color ∧ A))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
692 → RBtreeInvariant orig d0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
693 → {d : ℕ} → RBtreeInvariant tree d → {dr : ℕ} → RBtreeInvariant rr dr
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
694 → (stack1 : List (bt (Color ∧ A))) → stackInvariant key tr to stack1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
695 → rotatedTree tr rot
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
696 → {c : Color} → replacedTree key ⟪ c , value ⟫ (child-replaced key rot) rr
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
697 → length stack1 < length stack → t )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
698 → (exit : {k1 d1 : ℕ} {c1 : Color} → (rot repl : bt (Color ∧ A) )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
699 → {d : ℕ} → RBtreeInvariant repl d
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
700 → (ri : rotatedTree orig rot ) → {c : Color} → replacedTree key ⟪ c , value ⟫ rot repl → t ) → t
756
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 755
diff changeset
701 rotateRight {n} {m} {A} {t} key value orig tree rot repl rbo rbt rbr stack si ri rr next exit = rotateRight1 where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 755
diff changeset
702 rotateRight1 : t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 755
diff changeset
703 rotateRight1 with stackToPG tree orig stack si
758
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
704 ... | case1 x = {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
705 ... | case2 (case1 x) = {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
706 ... | case2 (case2 pg) = {!!}
754
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
707
729
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 727
diff changeset
708 insertCase5 : {n m : Level} {A : Set n} {t : Set m}
751
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 750
diff changeset
709 → (key : ℕ) → (value : A) → {d0 : ℕ}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 750
diff changeset
710 → (orig tree rot repl : bt (Color ∧ A)) {d0 : ℕ}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 750
diff changeset
711 → RBtreeInvariant orig d0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 750
diff changeset
712 → {d : ℕ} → RBtreeInvariant tree d → {dr : ℕ} → RBtreeInvariant repl dr
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 750
diff changeset
713 → (stack : List (bt (Color ∧ A))) → (si : stackInvariant key tree orig stack )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 750
diff changeset
714 → rotatedTree tree rot
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 750
diff changeset
715 → {c : Color} → replacedTree key ⟪ c , value ⟫ (child-replaced key rot) repl
756
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 755
diff changeset
716 → (next : {key2 d2 : ℕ} {c2 : Color}
751
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 750
diff changeset
717 → (to tr rot rr : bt (Color ∧ A))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 750
diff changeset
718 → RBtreeInvariant orig d0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 750
diff changeset
719 → {d : ℕ} → RBtreeInvariant tree d → {dr : ℕ} → RBtreeInvariant rr dr
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 750
diff changeset
720 → (stack1 : List (bt (Color ∧ A))) → stackInvariant key tr to stack1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 750
diff changeset
721 → rotatedTree tr rot
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 750
diff changeset
722 → {c : Color} → replacedTree key ⟪ c , value ⟫ (child-replaced key rot) rr
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 750
diff changeset
723 → length stack1 < length stack → t )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 750
diff changeset
724 → (exit : {k1 d1 : ℕ} {c1 : Color} → (rot repl : bt (Color ∧ A) )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 750
diff changeset
725 → {d : ℕ} → RBtreeInvariant repl d
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 750
diff changeset
726 → (ri : rotatedTree orig rot ) → {c : Color} → replacedTree key ⟪ c , value ⟫ rot repl → t ) → t
755
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 754
diff changeset
727 insertCase5 {n} {m} {A} {t} key value orig tree rot repl rbo rbt rbr stack si ri rr next exit = insertCase51 where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 754
diff changeset
728 insertCase51 : t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 754
diff changeset
729 insertCase51 with stackToPG tree orig stack si
758
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
730 ... | case1 eq = {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
731 ... | case2 (case1 eq ) = {!!}
755
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 754
diff changeset
732 ... | case2 (case2 pg) with PG.pg pg
758
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
733 ... | s2-s1p2 x x₁ = rotateRight {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} next exit
755
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 754
diff changeset
734 -- x : PG.parent pg ≡ node kp vp tree n1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 754
diff changeset
735 -- x₁ : PG.grand pg ≡ node kg vg (PG.parent pg) (PG.uncle pg)
758
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
736 ... | s2-1sp2 x x₁ = rotateLeft {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} next exit
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
737 ... | s2-s12p x x₁ = rotateLeft {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} next exit
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
738 ... | s2-1s2p x x₁ = rotateLeft {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} next exit
755
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 754
diff changeset
739 -- = insertCase2 tree (PG.parent pg) (PG.uncle pg) (PG.grand pg) stack si (PG.pg pg)
751
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 750
diff changeset
740
723
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 722
diff changeset
741
749
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 748
diff changeset
742 -- if we have replacedNode on RBTree, we have RBtreeInvariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 748
diff changeset
743
724
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 723
diff changeset
744 replaceRBP : {n m : Level} {A : Set n} {t : Set m}
759
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 758
diff changeset
745 → (key : ℕ) → (value : A)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 758
diff changeset
746 → (to tr rot rr : bt (Color ∧ A))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 758
diff changeset
747 → {d0 : ℕ} → RBtreeInvariant to d0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 758
diff changeset
748 → {d : ℕ} → RBtreeInvariant tr d → {dr : ℕ} → RBtreeInvariant rr dr
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 758
diff changeset
749 → (stack : List (bt (Color ∧ A))) → stackInvariant key tr to stack
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 758
diff changeset
750 → rotatedTree tr rot
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 758
diff changeset
751 → {c : Color} → replacedTree key ⟪ c , value ⟫ (child-replaced key rot) rr
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 758
diff changeset
752 → (next : {key2 d2 : ℕ} {c2 : Color}
755
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 754
diff changeset
753 → (to tr rot rr : bt (Color ∧ A))
759
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 758
diff changeset
754 → RBtreeInvariant to d0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 758
diff changeset
755 → {d : ℕ} → RBtreeInvariant tr d → {dr : ℕ} → RBtreeInvariant rr dr
747
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 746
diff changeset
756 → (stack1 : List (bt (Color ∧ A))) → stackInvariant key tr to stack1
755
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 754
diff changeset
757 → rotatedTree tr rot
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 754
diff changeset
758 → {c : Color} → replacedTree key ⟪ c , value ⟫ (child-replaced key rot) rr
737
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 736
diff changeset
759 → length stack1 < length stack → t )
760
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 759
diff changeset
760 → (exit : (rot repl : bt (Color ∧ A) )
748
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 747
diff changeset
761 → {d : ℕ} → RBtreeInvariant repl d
760
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 759
diff changeset
762 → (ri : rotatedTree to rot ) → {c : Color} → replacedTree key ⟪ c , value ⟫ (child-replaced key rot) repl → t ) → t
759
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 758
diff changeset
763 replaceRBP {n} {m} {A} {t} key value orig tree rot repl rbio rbit rbir stack si roti {c} ri next exit = ? where
754
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
764 insertCase2 : (tree parent uncle grand : bt (Color ∧ A))
749
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 748
diff changeset
765 → (stack : List (bt (Color ∧ A))) → (si : stackInvariant key tree orig stack )
754
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
766 → (pg : ParentGrand tree parent uncle grand ) → t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
767 insertCase2 tree leaf uncle grand stack si (s2-s1p2 () x₁)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
768 insertCase2 tree leaf uncle grand stack si (s2-1sp2 () x₁)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
769 insertCase2 tree leaf uncle grand stack si (s2-s12p () x₁)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
770 insertCase2 tree leaf uncle grand stack si (s2-1s2p () x₁)
758
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
771 insertCase2 tree parent@(node kp ⟪ Red , _ ⟫ _ _) uncle grand stack si pg = next {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
772 insertCase2 tree parent@(node kp ⟪ Black , _ ⟫ _ _) leaf grand stack si pg = {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
773 insertCase2 tree parent@(node kp ⟪ Black , _ ⟫ _ _) (node ku ⟪ Red , _ ⟫ _ _ ) grand stack si pg = next {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!} {!!}
754
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
774 insertCase2 tree parent@(node kp ⟪ Black , _ ⟫ _ _) (node ku ⟪ Black , _ ⟫ _ _) grand stack si (s2-s1p2 x x₁) =
758
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
775 insertCase5 key value orig tree {!!} repl rbio {!!} {!!} stack si {!!} ri {!!} {!!} next exit
754
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
776 -- tree is left of parent, parent is left of grand
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
777 -- node kp ⟪ Black , proj3 ⟫ left right ≡ node kp₁ vp tree n1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
778 -- grand ≡ node kg vg (node kp ⟪ Black , proj3 ⟫ left right) (node ku ⟪ Black , proj4 ⟫ left₁ right₁)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
779 insertCase2 tree parent@(node kp ⟪ Black , _ ⟫ _ _) (node ku ⟪ Black , _ ⟫ _ _) grand stack si (s2-1sp2 x x₁) =
758
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
780 rotateLeft key value orig tree {!!} repl rbio {!!} {!!} stack si {!!} ri {!!} {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
781 (λ a b c d e f h i j k l m → insertCase5 key value a b c d {!!} {!!} h i j k l {!!} {!!} next exit ) exit
754
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
782 -- tree is right of parent, parent is left of grand rotateLeft
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
783 -- node kp ⟪ Black , proj3 ⟫ left right ≡ node kp₁ vp n1 tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
784 -- grand ≡ node kg vg (node kp ⟪ Black , proj3 ⟫ left right) (node ku ⟪ Black , proj4 ⟫ left₁ right₁)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
785 insertCase2 tree parent@(node kp ⟪ Black , _ ⟫ _ _) (node ku ⟪ Black , _ ⟫ _ _) grand stack si (s2-s12p x x₁) =
758
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
786 rotateRight key value orig tree {!!} repl rbio {!!} {!!} stack si {!!} ri {!!} {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
787 (λ a b c d e f h i j k l m → insertCase5 key value a b c d {!!} {!!} h i j k l {!!} {!!} next exit ) exit
754
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
788 -- tree is left of parent, parent is right of grand, rotateRight
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
789 -- node kp ⟪ Black , proj3 ⟫ left right ≡ node kp₁ vp tree n1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
790 -- grand ≡ node kg vg (node ku ⟪ Black , proj4 ⟫ left₁ right₁) (node kp ⟪ Black , proj3 ⟫ left right)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
791 insertCase2 tree parent@(node kp ⟪ Black , _ ⟫ _ _) (node ku ⟪ Black , _ ⟫ _ _) grand stack si (s2-1s2p x x₁) =
758
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 756
diff changeset
792 insertCase5 key value orig tree {!!} repl rbio {!!} {!!} stack si {!!} ri {!!} {!!} next exit
754
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
793 -- tree is right of parent, parent is right of grand
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
794 -- node kp ⟪ Black , proj3 ⟫ left right ≡ node kp₁ vp n1 tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
795 -- grand ≡ node kg vg (node ku ⟪ Black , proj4 ⟫ left₁ right₁) (node kp ⟪ Black , proj3 ⟫ left right)
749
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 748
diff changeset
796 insertCase1 : t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 748
diff changeset
797 insertCase1 with stackToPG tree orig stack si
760
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 759
diff changeset
798 ... | case1 eq = exit rot repl rbir (subst (λ k → rotatedTree k rot) (stackCase1 si eq) roti) ri
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 759
diff changeset
799 ... | case2 (case1 eq ) = ? where
762
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 760
diff changeset
800 insertCase12 : (orig : bt (Color ∧ A))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 760
diff changeset
801 → {stack : List (bt (Color ∧ A))} → (si : stackInvariant key tree orig stack )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 760
diff changeset
802 → stack ≡ tree ∷ orig ∷ [] → t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 760
diff changeset
803 insertCase12 (node k1 ⟪ Red , v1 ⟫ t1 tree) (s-right x s-nil) refl = exit rot repl rbir ? ?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 760
diff changeset
804 insertCase12 (node k1 ⟪ Black , v1 ⟫ t1 tree) (s-right x s-nil) refl = ?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 760
diff changeset
805 insertCase12 (node k1 ⟪ Red , v1 ⟫ tree t1) (s-left x s-nil) refl = ?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 760
diff changeset
806 insertCase12 (node k1 ⟪ Black , v1 ⟫ tree t1) (s-left x s-nil) refl = ?
760
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 759
diff changeset
807 -- exit rot repl rbir ? ?
754
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 753
diff changeset
808 ... | case2 (case2 pg) = insertCase2 tree (PG.parent pg) (PG.uncle pg) (PG.grand pg) stack si (PG.pg pg)
724
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 723
diff changeset
809
740
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
810