annotate hoareBinaryTree1.agda @ 740:9ff79715588e

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 23 Apr 2023 14:29:09 +0900
parents 3443703a68cc
children c44edea35126
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)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
47 t-right : {key key₁ : ℕ} → {value value₁ : A} → {t₁ t₂ : bt A} → (key < key₁) → treeInvariant (node key₁ value₁ t₁ t₂)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
48 → treeInvariant (node key value leaf (node key₁ value₁ t₁ t₂))
692
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
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 )
620
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
51 t-node : {key key₁ key₂ : ℕ} → {value value₁ value₂ : A} → {t₁ t₂ t₃ t₄ : bt A} → (key < key₁) → (key₁ < key₂)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 619
diff changeset
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 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
57 -- stack always contains original top at end
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
642
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 641
diff changeset
118 ti-right : {n : Level} {A : Set n} {tree₁ repl : bt A} → {key₁ : ℕ} → {v1 : A} → treeInvariant (node key₁ v1 tree₁ repl) → treeInvariant repl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 641
diff changeset
119 ti-right {_} {_} {.leaf} {_} {key₁} {v1} (t-single .key₁ .v1) = t-leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 641
diff changeset
120 ti-right {_} {_} {.leaf} {_} {key₁} {v1} (t-right x ti) = ti
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 641
diff changeset
121 ti-right {_} {_} {.(node _ _ _ _)} {_} {key₁} {v1} (t-left x ti) = t-leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 641
diff changeset
122 ti-right {_} {_} {.(node _ _ _ _)} {_} {key₁} {v1} (t-node x x₁ ti ti₁) = ti₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 641
diff changeset
123
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 641
diff changeset
124 ti-left : {n : Level} {A : Set n} {tree₁ repl : bt A} → {key₁ : ℕ} → {v1 : A} → treeInvariant (node key₁ v1 repl tree₁ ) → treeInvariant repl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 641
diff changeset
125 ti-left {_} {_} {.leaf} {_} {key₁} {v1} (t-single .key₁ .v1) = t-leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 641
diff changeset
126 ti-left {_} {_} {_} {_} {key₁} {v1} (t-right x ti) = t-leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 641
diff changeset
127 ti-left {_} {_} {_} {_} {key₁} {v1} (t-left x ti) = ti
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 641
diff changeset
128 ti-left {_} {_} {.(node _ _ _ _)} {_} {key₁} {v1} (t-node x x₁ ti ti₁) = ti
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 641
diff changeset
129
662
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
130 stackTreeInvariant : {n : Level} {A : Set n} (key : ℕ) (sub tree : bt A) → (stack : List (bt A))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
131 → treeInvariant tree → stackInvariant key sub tree stack → treeInvariant sub
729
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 727
diff changeset
132 stackTreeInvariant {_} {A} key sub tree (sub ∷ []) ti (s-nil ) = ti
662
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
133 stackTreeInvariant {_} {A} key sub tree (sub ∷ st) ti (s-right _ si ) = ti-right (si1 si) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
134 si1 : {tree₁ : bt A} → {key₁ : ℕ} → {v1 : A} → stackInvariant key (node key₁ v1 tree₁ sub ) tree st → treeInvariant (node key₁ v1 tree₁ sub )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
135 si1 {tree₁ } {key₁ } {v1 } si = stackTreeInvariant key (node key₁ v1 tree₁ sub ) tree st ti si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
136 stackTreeInvariant {_} {A} key sub tree (sub ∷ st) ti (s-left _ si ) = ti-left ( si2 si) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
137 si2 : {tree₁ : bt A} → {key₁ : ℕ} → {v1 : A} → stackInvariant key (node key₁ v1 sub tree₁ ) tree st → treeInvariant (node key₁ v1 sub tree₁ )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
138 si2 {tree₁ } {key₁ } {v1 } si = stackTreeInvariant key (node key₁ v1 sub tree₁ ) tree st ti si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 661
diff changeset
139
639
5fe23f540726 replacedTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 638
diff changeset
140 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
141 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
142 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
143 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
144 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
145
690
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
146 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
147 rt-property-leaf r-leaf = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
148
698
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
149 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
150 rt-property-¬leaf ()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
151
692
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
152 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
153 → 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
154 rt-property-key r-node = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
155 rt-property-key (r-right x ri) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
156 rt-property-key (r-left x ri) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
157
698
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
158 nat-≤> : { x y : ℕ } → x ≤ y → y < x → ⊥
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
159 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
160 nat-<> : { x y : ℕ } → x < y → y < x → ⊥
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
161 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
162
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
163 open _∧_
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
164
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 697
diff changeset
165
632
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
166 depth-1< : {i j : ℕ} → suc i ≤ suc (i Data.Nat.⊔ j )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
167 depth-1< {i} {j} = s≤s (m≤m⊔n _ j)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
168
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 631
diff changeset
169 depth-2< : {i j : ℕ} → suc i ≤ suc (j Data.Nat.⊔ i )
650
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 649
diff changeset
170 depth-2< {i} {j} = s≤s (m≤n⊔m j i)
611
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 610
diff changeset
171
649
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
172 depth-3< : {i : ℕ } → suc i ≤ suc (suc i)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
173 depth-3< {zero} = s≤s ( z≤n )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
174 depth-3< {suc i} = s≤s (depth-3< {i} )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
175
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
176
634
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
177 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
178 → treeInvariant (node k v1 tree tree₁)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
179 → treeInvariant tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
180 treeLeftDown {n} {A} {_} {v1} leaf leaf (t-single k1 v1) = t-leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
181 treeLeftDown {n} {A} {_} {v1} .leaf .(node _ _ _ _) (t-right x ti) = t-leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
182 treeLeftDown {n} {A} {_} {v1} .(node _ _ _ _) .leaf (t-left x ti) = ti
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
183 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
184
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
185 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
186 → treeInvariant (node k v1 tree tree₁)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
187 → treeInvariant tree₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
188 treeRightDown {n} {A} {_} {v1} .leaf .leaf (t-single _ .v1) = t-leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
189 treeRightDown {n} {A} {_} {v1} .leaf .(node _ _ _ _) (t-right x ti) = ti
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
190 treeRightDown {n} {A} {_} {v1} .(node _ _ _ _) .leaf (t-left x ti) = t-leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 633
diff changeset
191 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
192
615
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 614
diff changeset
193 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
194 → treeInvariant tree ∧ stackInvariant key tree tree0 stack
693
49dd82f49fa1 insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 692
diff changeset
195 → (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
196 → (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
197 → (tree1 ≡ leaf ) ∨ ( node-key tree1 ≡ just key ) → t ) → t
693
49dd82f49fa1 insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 692
diff changeset
198 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
199 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
200 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
201 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
202 ⟪ 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
203 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
204 findP1 a (x ∷ st) si = s-left a si
693
49dd82f49fa1 insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 692
diff changeset
205 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
206
638
be6bd51c3f05 replaceTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 637
diff changeset
207 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
208 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
209 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
210 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
211 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
212
649
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
213 open import Relation.Binary.Definitions
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
214
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
215 lemma3 : {i j : ℕ} → 0 ≡ i → j < i → ⊥
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
216 lemma3 refl ()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
217 lemma5 : {i j : ℕ} → i < 1 → j < i → ⊥
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
218 lemma5 (s≤s z≤n) ()
700
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 699
diff changeset
219 ¬x<x : {x : ℕ} → ¬ (x < x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 699
diff changeset
220 ¬x<x (s≤s lt) = ¬x<x lt
649
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 648
diff changeset
221
687
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
222 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
223 child-replaced key leaf = leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
224 child-replaced key (node key₁ value left right) with <-cmp key key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
225 ... | tri< a ¬b ¬c = left
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
226 ... | tri≈ ¬a b ¬c = node key₁ value left right
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
227 ... | tri> ¬a ¬b c = right
677
681577b60c35 child-replaced
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 676
diff changeset
228
671
b5fde9727830 use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 670
diff changeset
229 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
230 field
b5fde9727830 use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 670
diff changeset
231 tree0 : bt A
b5fde9727830 use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 670
diff changeset
232 ti : treeInvariant tree0
b5fde9727830 use record invariant for replace
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 670
diff changeset
233 si : stackInvariant key tree tree0 stack
687
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
234 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
235 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
236
638
be6bd51c3f05 replaceTree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 637
diff changeset
237 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
238 → (tree ≡ leaf ) ∨ ( node-key tree ≡ just key )
694
da42fe4eda54 complete insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 693
diff changeset
239 → (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
240 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
241 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
242 (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
243 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
244 repl00 with <-cmp k k
da42fe4eda54 complete insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 693
diff changeset
245 ... | tri< a ¬b ¬c = ⊥-elim (¬b refl)
da42fe4eda54 complete insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 693
diff changeset
246 ... | tri≈ ¬a b ¬c = refl
da42fe4eda54 complete insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 693
diff changeset
247 ... | tri> ¬a ¬b c = ⊥-elim (¬b refl)
606
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
248
609
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
249 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
250 → (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
251 → (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
252 → (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
253 → 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
254 → (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
255 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
256 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
257 ... | 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
258 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
259 ... | 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
260 repl01 : replacedTree key value (replacePR.tree0 Pre) (node key₁ value₁ repl right )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
261 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
262 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
263 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
264 repl03 = replacePR.ri Pre
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
265 repl02 : child-replaced key (node key₁ value₁ left right) ≡ left
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
266 repl02 with <-cmp key key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
267 ... | tri< a ¬b ¬c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
268 ... | tri≈ ¬a b ¬c = ⊥-elim ( ¬a a)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
269 ... | tri> ¬a ¬b c = ⊥-elim ( ¬a a)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
270 ... | 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
271 repl01 : replacedTree key value (replacePR.tree0 Pre) repl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 677
diff changeset
272 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
273 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
274 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
275 repl02 with <-cmp key key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
276 ... | tri< a ¬b ¬c = ⊥-elim ( ¬b b)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
277 ... | tri≈ ¬a b ¬c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
278 ... | tri> ¬a ¬b c = ⊥-elim ( ¬b b)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
279 ... | 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
280 repl01 : replacedTree key value (replacePR.tree0 Pre) (node key₁ value₁ left repl )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
281 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
282 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
283 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
284 repl03 = replacePR.ri Pre
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
285 repl02 : child-replaced key (node key₁ value₁ left right) ≡ right
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
286 repl02 with <-cmp key key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
287 ... | tri< a ¬b ¬c = ⊥-elim ( ¬c c)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
288 ... | tri≈ ¬a b ¬c = ⊥-elim ( ¬c c)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
289 ... | tri> ¬a ¬b c = refl
690
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
290 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
291 Post : replacePR key value tree1 repl (tree1 ∷ st1) (λ _ _ _ → Lift n ⊤)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
292 Post with replacePR.si Pre
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
293 ... | 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
294 repl09 : tree1 ≡ node key₂ v1 tree₁ leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
295 repl09 = si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
296 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
297 repl10 with si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
298 ... | refl = si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
299 repl07 : child-replaced key (node key₂ v1 tree₁ leaf) ≡ leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
300 repl07 with <-cmp key key₂
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
301 ... | tri< a ¬b ¬c = ⊥-elim (¬c x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
302 ... | tri≈ ¬a b ¬c = ⊥-elim (¬c x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
303 ... | tri> ¬a ¬b c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
304 repl12 : replacedTree key value (child-replaced key tree1 ) repl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
305 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
306 ... | 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
307 repl09 : tree1 ≡ node key₂ v1 leaf tree₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
308 repl09 = si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
309 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
310 repl10 with si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
311 ... | refl = si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
312 repl07 : child-replaced key (node key₂ v1 leaf tree₁ ) ≡ leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
313 repl07 with <-cmp key key₂
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
314 ... | tri< a ¬b ¬c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
315 ... | tri≈ ¬a b ¬c = ⊥-elim (¬a x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
316 ... | tri> ¬a ¬b c = ⊥-elim (¬a x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
317 repl12 : replacedTree key value (child-replaced key tree1 ) repl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
318 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
319 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
320 ... | 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
321 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
322 Post with replacePR.si Pre
688
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
323 ... | 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
324 repl09 : tree1 ≡ node key₂ v1 tree₁ (node key₁ value₁ left right)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
325 repl09 = si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
326 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
327 repl10 with si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
328 ... | refl = si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
329 repl03 : child-replaced key (node key₁ value₁ left right) ≡ left
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
330 repl03 with <-cmp key key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
331 ... | tri< a1 ¬b ¬c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
332 ... | tri≈ ¬a b ¬c = ⊥-elim (¬a a)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
333 ... | tri> ¬a ¬b c = ⊥-elim (¬a a)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
334 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
335 repl02 with repl09 | <-cmp key key₂
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
336 ... | refl | tri< a ¬b ¬c = ⊥-elim (¬c lt)
689
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 688
diff changeset
337 ... | refl | tri≈ ¬a b ¬c = ⊥-elim (¬c lt)
688
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
338 ... | refl | tri> ¬a ¬b c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
339 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
340 repl04 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
341 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
342 node key₁ value₁ left right ≡⟨ sym repl02 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
343 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
344 child-replaced key tree1 ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 687
diff changeset
345 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
346 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
347 ... | 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
348 repl09 : tree1 ≡ node key₂ v1 (node key₁ value₁ left right) tree₁
683
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 682
diff changeset
349 repl09 = si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 682
diff changeset
350 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 682
diff changeset
351 repl10 with si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 682
diff changeset
352 ... | refl = si
687
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
353 repl03 : child-replaced key (node key₁ value₁ left right) ≡ left
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
354 repl03 with <-cmp key key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
355 ... | tri< a1 ¬b ¬c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
356 ... | tri≈ ¬a b ¬c = ⊥-elim (¬a a)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
357 ... | tri> ¬a ¬b c = ⊥-elim (¬a a)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
358 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
359 repl02 with repl09 | <-cmp key key₂
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
360 ... | refl | tri< a ¬b ¬c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
361 ... | refl | tri≈ ¬a b ¬c = ⊥-elim (¬a lt)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
362 ... | refl | tri> ¬a ¬b c = ⊥-elim (¬a lt)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
363 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
364 repl04 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
365 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
366 node key₁ value₁ left right ≡⟨ sym repl02 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
367 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
368 child-replaced key tree1 ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 686
diff changeset
369 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
370 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
371 ... | 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
372 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
373 Post with replacePR.si Pre
691
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 690
diff changeset
374 ... | 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
375 repl09 : tree1 ≡ node key₂ v1 tree₁ tree -- (node key₁ value₁ left right)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
376 repl09 = si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
377 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
378 repl10 with si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
379 ... | refl = si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
380 repl07 : child-replaced key (node key₂ v1 tree₁ tree) ≡ tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
381 repl07 with <-cmp key key₂
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
382 ... | tri< a ¬b ¬c = ⊥-elim (¬c x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
383 ... | tri≈ ¬a b ¬c = ⊥-elim (¬c x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
384 ... | tri> ¬a ¬b c = refl
691
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 690
diff changeset
385 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
386 repl12 refl with repl09
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 690
diff changeset
387 ... | 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
388 ... | 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
389 repl09 : tree1 ≡ node key₂ v1 tree tree₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
390 repl09 = si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
391 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
392 repl10 with si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
393 ... | refl = si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
394 repl07 : child-replaced key (node key₂ v1 tree tree₁ ) ≡ tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
395 repl07 with <-cmp key key₂
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
396 ... | tri< a ¬b ¬c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
397 ... | tri≈ ¬a b ¬c = ⊥-elim (¬a x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
398 ... | tri> ¬a ¬b c = ⊥-elim (¬a x)
691
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 690
diff changeset
399 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
400 repl12 refl with repl09
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 690
diff changeset
401 ... | 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
402 ... | 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
403 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
404 Post with replacePR.si Pre
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
405 ... | 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
406 repl09 : tree1 ≡ node key₂ v1 tree₁ (node key₁ value₁ left right)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
407 repl09 = si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
408 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
409 repl10 with si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
410 ... | refl = si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
411 repl03 : child-replaced key (node key₁ value₁ left right) ≡ right
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
412 repl03 with <-cmp key key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
413 ... | tri< a1 ¬b ¬c = ⊥-elim (¬c c)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
414 ... | tri≈ ¬a b ¬c = ⊥-elim (¬c c)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
415 ... | tri> ¬a ¬b c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
416 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
417 repl02 with repl09 | <-cmp key key₂
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
418 ... | refl | tri< a ¬b ¬c = ⊥-elim (¬c lt)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
419 ... | refl | tri≈ ¬a b ¬c = ⊥-elim (¬c lt)
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 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
422 repl04 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
423 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
424 node key₁ value₁ left right ≡⟨ sym repl02 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
425 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
426 child-replaced key tree1 ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
427 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
428 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
429 ... | 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
430 repl09 : tree1 ≡ node key₂ v1 (node key₁ value₁ left right) tree₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
431 repl09 = si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
432 repl10 : stackInvariant key tree1 (replacePR.tree0 Pre) (tree1 ∷ st1)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
433 repl10 with si-property1 si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
434 ... | refl = si
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
435 repl03 : child-replaced key (node key₁ value₁ left right) ≡ right
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
436 repl03 with <-cmp key key₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
437 ... | tri< a1 ¬b ¬c = ⊥-elim (¬c c)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
438 ... | tri≈ ¬a b ¬c = ⊥-elim (¬c c)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
439 ... | tri> ¬a ¬b c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
440 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
441 repl02 with repl09 | <-cmp key key₂
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
442 ... | refl | tri< a ¬b ¬c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
443 ... | refl | tri≈ ¬a b ¬c = ⊥-elim (¬a lt)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
444 ... | refl | tri> ¬a ¬b c = ⊥-elim (¬a lt)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
445 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
446 repl04 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
447 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
448 node key₁ value₁ left right ≡⟨ sym repl02 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
449 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
450 child-replaced key tree1 ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 689
diff changeset
451 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
452 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
453
609
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
454 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
455 → (r : Index) → (p : Invraiant r)
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
456 → (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
457 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
458 ... | 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
459 ... | 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
460 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
461 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
462 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
463 ... | 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
464 ... | 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
465 ... | 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
466
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
467 open _∧_
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
468
615
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 614
diff changeset
469 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
470 → replacedTree key value tree repl → treeInvariant repl
692
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
471 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
472 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
473 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
474 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
475 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
476 -- r-right case
692
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 691
diff changeset
477 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
478 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
479 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
480 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
481 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
482 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
483 t-node x₁ x ti (t-single key value)
693
49dd82f49fa1 insertTreeP
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 692
diff changeset
484 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
485 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
486 -- r-left case
700
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 699
diff changeset
487 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
488 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
489 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
490 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
491 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
492
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 614
diff changeset
493 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
494 → replacedTree key value tree repl → treeInvariant tree
701
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
495 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
496 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
497 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
498 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
499 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
500 -- r-right case
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
501 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
502 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
503 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
504 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
505 t-left x₁ ti
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
506 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
507 -- r-left case
690da797cf40 hoareBinaryTree done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 700
diff changeset
508 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
509 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
510 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
511 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
512 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
513 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
514
611
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 610
diff changeset
515 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
516 → (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
517 insertTreeP {n} {m} {A} {t} tree key value P0 exit =
729
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 727
diff changeset
518 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
519 $ λ 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
520 $ λ 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
521 $ λ 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
522 {λ 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
523 (λ 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
524 $ λ 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
525 (λ 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
526 $ λ 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
527
696
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 695
diff changeset
528 insertTestP1 = insertTreeP leaf 1 1 t-leaf
722
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 718
diff changeset
529 $ λ _ x0 P0 → insertTreeP x0 2 1 (proj1 P0)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 718
diff changeset
530 $ λ _ x1 P1 → insertTreeP x1 3 2 (proj1 P1)
727
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 726
diff changeset
531 $ λ _ 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
532
609
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
533 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
534 top-value leaf = nothing
79418701a283 add test and speciication
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 606
diff changeset
535 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
536
722
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 718
diff changeset
537 -- is realy inserted?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 718
diff changeset
538
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 718
diff changeset
539 -- other element is preserved?
702
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 701
diff changeset
540
722
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 718
diff changeset
541 -- deletion?
616
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 615
diff changeset
542
722
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 718
diff changeset
543 data Color : Set where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 718
diff changeset
544 Red : Color
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 718
diff changeset
545 Black : Color
618
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 617
diff changeset
546
723
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 722
diff changeset
547 data RBTree {n : Level} (A : Set n) : (key : ℕ) → Color → (b-depth : ℕ) → Set n where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 722
diff changeset
548 rb-leaf : (key : ℕ) → RBTree A key Black 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 722
diff changeset
549 rb-single : (key : ℕ) → (value : A) → (c : Color) → RBTree A key c 1
725
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 724
diff changeset
550 t-right-red : (key : ℕ) {key₁ : ℕ} → (value : A) → key < key₁ → {d : ℕ } → RBTree A key₁ Black d → RBTree A key Red d
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 724
diff changeset
551 t-right-black : (key : ℕ) {key₁ : ℕ} → (value : A) → key < key₁ → {c : Color} → {d : ℕ }→ RBTree A key₁ c d
723
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 722
diff changeset
552 → RBTree A key Black (suc d)
725
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 724
diff changeset
553 t-left-red : (key₁ : ℕ) { key : ℕ} → (value : A) → key < key₁ → {d : ℕ} → RBTree A key Black d
723
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 722
diff changeset
554 → RBTree A key₁ Red d
725
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 724
diff changeset
555 t-left-black : (key₁ : ℕ) {key : ℕ} → (value : A) → key < key₁ → {c : Color} → {d : ℕ} → RBTree A key c d
723
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 722
diff changeset
556 → RBTree A key₁ Black (suc d)
725
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 724
diff changeset
557 t-node-red : (key₁ : ℕ) { key key₂ : ℕ} → (value : A) → key < key₁ → key₁ < key₂ → {d : ℕ}
723
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 722
diff changeset
558 → RBTree A key Black d
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 722
diff changeset
559 → RBTree A key₂ Black d
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 722
diff changeset
560 → RBTree A key₁ Red d
725
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 724
diff changeset
561 t-node-black : (key₁ : ℕ) {key key₂ : ℕ} → (value : A) → key < key₁ → key₁ < key₂ → {c c1 : Color} {d : ℕ}
723
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 722
diff changeset
562 → RBTree A key c d
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 722
diff changeset
563 → RBTree A key₂ c1 d
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 722
diff changeset
564 → RBTree A key₁ Black (suc d)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 722
diff changeset
565
738
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 737
diff changeset
566 color : {n : Level} (A : Set n) {key d : ℕ} {c : Color } → (rb : RBTree A key c d ) → Color
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 737
diff changeset
567 color {n} A {k} {d} {c} rb = c
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 737
diff changeset
568
739
3443703a68cc it is no good to develop all invariant at once
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 738
diff changeset
569 rb-key : {n : Level} (A : Set n) {key d : ℕ} {c : Color } → (rb : RBTree A key c d ) → ℕ
3443703a68cc it is no good to develop all invariant at once
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 738
diff changeset
570 rb-key {n} A {k} {d} {c} rb = k
3443703a68cc it is no good to develop all invariant at once
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 738
diff changeset
571
723
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 722
diff changeset
572 RB→bt : {n : Level} (A : Set n) {key d : ℕ} {c : Color } → (rb : RBTree A key c d ) → bt A
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 722
diff changeset
573 RB→bt {n} A (rb-leaf _) = leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 722
diff changeset
574 RB→bt {n} A (rb-single key value _) = node key value leaf leaf
725
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 724
diff changeset
575 RB→bt {n} A (t-right-red key value x rb) = node key value leaf (RB→bt A rb)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 724
diff changeset
576 RB→bt {n} A (t-right-black key value x rb) = node key value leaf (RB→bt A rb)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 724
diff changeset
577 RB→bt {n} A (t-left-red key value x rb) = node key value (RB→bt A rb) leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 724
diff changeset
578 RB→bt {n} A (t-left-black key value x rb) = node key value (RB→bt A rb) leaf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 724
diff changeset
579 RB→bt {n} A (t-node-red key value x x₁ rb rb₁) = node key value (RB→bt A rb) (RB→bt A rb₁)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 724
diff changeset
580 RB→bt {n} A (t-node-black key value x x₁ 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
581
724
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 723
diff changeset
582 rbt-depth : {n : Level} (A : Set n) {key : ℕ} {c : Color} {d : ℕ} → RBTree A key c d → ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 723
diff changeset
583 rbt-depth A (rb-leaf _) = zero
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 723
diff changeset
584 rbt-depth A (rb-single _ value _) = 1
725
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 724
diff changeset
585 rbt-depth A (t-right-red _ value x ab) = suc ( rbt-depth A ab)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 724
diff changeset
586 rbt-depth A (t-right-black _ value x ab) = suc ( rbt-depth A ab)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 724
diff changeset
587 rbt-depth A (t-left-red _ value x ab) = suc ( rbt-depth A ab)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 724
diff changeset
588 rbt-depth A (t-left-black _ value x ab) = suc ( rbt-depth A ab)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 724
diff changeset
589 rbt-depth A (t-node-red _ value x x₁ ab ab₁) = suc (Data.Nat._⊔_ (rbt-depth A ab ) (rbt-depth A ab₁ ))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 724
diff changeset
590 rbt-depth A (t-node-black _ value x x₁ ab ab₁) = suc (Data.Nat._⊔_ (rbt-depth A ab ) (rbt-depth A ab₁ ))
723
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 722
diff changeset
591
725
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 724
diff changeset
592 rbt-key : {n : Level} (A : Set n) {key : ℕ} {c : Color} {d : ℕ} → RBTree A key c d → Maybe ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 724
diff changeset
593 rbt-key {n} A (rb-leaf _) = nothing
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 724
diff changeset
594 rbt-key {n} A (rb-single key value _) = just key
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 724
diff changeset
595 rbt-key {n} A (t-right-red key value x rb) = just key
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 724
diff changeset
596 rbt-key {n} A (t-right-black key value x rb) = just key
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 724
diff changeset
597 rbt-key {n} A (t-left-red key value x rb) = just key
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 724
diff changeset
598 rbt-key {n} A (t-left-black key value x rb) = just key
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 724
diff changeset
599 rbt-key {n} A (t-node-red key value x x₁ rb rb₁) = just key
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 724
diff changeset
600 rbt-key {n} A (t-node-black key value x x₁ rb rb₁) = just key
723
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 722
diff changeset
601
737
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 736
diff changeset
602 data ParentGrand {n : Level} {A : Set n} (self : bt A) : (parent grand : bt A) → Set n where
740
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
603 s2-s1p2 : {kp kg : ℕ} {vp vg : A} → {n1 n2 : bt A} {parent grand : bt A }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
604 → parent ≡ node kp vp self n1 → grand ≡ node kg vg parent n2 → ParentGrand self parent grand
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
605 s2-1sp2 : {kp kg : ℕ} {vp vg : A} → {n1 n2 : bt A} {parent grand : bt A }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
606 → parent ≡ node kp vp n1 self → grand ≡ node kg vg parent n2 → ParentGrand self parent grand
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
607 s2-s12p : {kp kg : ℕ} {vp vg : A} → {n1 n2 : bt A} {parent grand : bt A }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
608 → parent ≡ node kp vp self n1 → grand ≡ node kg vg n2 parent → ParentGrand self parent grand
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
609 s2-1s2p : {kp kg : ℕ} {vp vg : A} → {n1 n2 : bt A} {parent grand : bt A }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
610 → parent ≡ node kp vp n1 self → grand ≡ node kg vg n2 parent → ParentGrand self parent grand
734
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 733
diff changeset
611
736
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 735
diff changeset
612 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
613 rr-node : {t : bt A} → rotatedTree t t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 735
diff changeset
614 rr-right : {ka kb : ℕ } {va vb : A} → {ta tb tc ta1 tb1 tc1 : bt A}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 735
diff changeset
615 → ka < kb
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 735
diff changeset
616 → rotatedTree ta ta1 → rotatedTree tb tb1 → rotatedTree tc tc1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 735
diff changeset
617 → rotatedTree (node ka va (node kb vb ta tb) tc) (node kb vb ta1 (node ka va tb1 tc1) )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 735
diff changeset
618 rr-left : {ka kb : ℕ } {va vb : A} → {ta tb tc ta1 tb1 tc1 : bt A}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 735
diff changeset
619 → ka < kb
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 735
diff changeset
620 → rotatedTree ta ta1 → rotatedTree tb tb1 → rotatedTree tc tc1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 735
diff changeset
621 → rotatedTree (node kb vb ta (node ka va tb tc) ) (node ka va (node kb vb ta1 tb1) tc1)
734
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 733
diff changeset
622
740
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
623 record PG {n : Level } (A : Set n) (stack : List (bt A)) : Set n where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
624 field
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
625 self parent grand : bt A
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
626 pg : ParentGrand self parent grand
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
627 rest : List (bt A)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
628 stack=gp : stack ≡ ( self ∷ parent ∷ grand ∷ rest )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
629
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
630 stackToPG : {n : Level} {A : Set n} → {key : ℕ } → (tree orig : bt A )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
631 → (stack : List (bt A)) → stackInvariant key tree orig stack
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
632 → ( stack ≡ orig ∷ [] ) ∨ ( stack ≡ tree ∷ orig ∷ [] ) ∨ PG A stack
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
633 stackToPG {n} {A} {key} tree .tree .(tree ∷ []) s-nil = case1 refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
634 stackToPG {n} {A} {key} tree .(node _ _ _ tree) .(tree ∷ node _ _ _ tree ∷ []) (s-right x s-nil) = case2 (case1 refl)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
635 stackToPG {n} {A} {key} tree orig (tree ∷ node _ _ _ tree ∷ grand ∷ rest) (s-right {t0} {t1} {t2} {k1} {v1} x (s-right {t3} {t4} {t5} {k2} {v2} x₁ si)) = case2 (case2
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
636 record { self = tree ; parent = node k1 v1 t2 tree ; grand = grand ; pg = s2-1s2p refl ? ; rest = rest ; stack=gp = refl } )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
637 stackToPG {n} {A} {key} tree orig .(tree ∷ node _ _ _ tree ∷ _) (s-right x (s-left x₁ si)) = ?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
638 stackToPG {n} {A} {key} tree orig .(tree ∷ _) (s-left x si) = ?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
639
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
640
736
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 735
diff changeset
641 rbsi-len : {n : Level} {A : Set n} {orig parent grand : bt A}
737
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 736
diff changeset
642 → ParentGrand orig parent grand → ℕ
736
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 735
diff changeset
643 rbsi-len {n} {A} {s} {p} {g} st = ?
734
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 733
diff changeset
644
737
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 736
diff changeset
645 findRBP : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) {key1 d d1 : ℕ} → {c c1 : Color} → (tree : RBTree A key c d ) (orig : RBTree A key1 c1 d1 )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 736
diff changeset
646 → (stack : List (bt A)) → stackInvariant key (RB→bt A tree) (RB→bt A orig) stack
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 736
diff changeset
647 → (next : {key0 d0 : ℕ} {c0 : Color} → (tree1 : RBTree A key0 c0 d0 ) → (stack : List (bt A)) → stackInvariant key (RB→bt A tree1) (RB→bt A orig) stack → rbt-depth A tree1 < rbt-depth A tree → t )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 736
diff changeset
648 → (exit : {key0 d0 : ℕ} {c0 : Color} → (tree1 : RBTree A key0 c0 d0 ) → (stack : List (bt A)) → stackInvariant key (RB→bt A tree1) (RB→bt A orig) stack
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 736
diff changeset
649 → (rbt-depth A tree1 ≡ 0 ) ∨ ( rbt-key A tree1 ≡ just key ) → t ) → t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 736
diff changeset
650 findRBP {n} {m} {A} {t} key {key1} tree orig st si next exit = ?
723
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 722
diff changeset
651
731
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 730
diff changeset
652 rotateRight : ?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 730
diff changeset
653 rotateRight = ?
733
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 732
diff changeset
654
731
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 730
diff changeset
655 rotateLeft : ?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 730
diff changeset
656 rotateLeft = ?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 730
diff changeset
657
729
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 727
diff changeset
658 insertCase5 : {n m : Level} {A : Set n} {t : Set m}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 727
diff changeset
659 → (key : ℕ) → (value : A) → {key0 key1 key2 d0 d1 d2 : ℕ} {c0 c1 c2 : Color}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 727
diff changeset
660 → (orig : RBTree A key1 c1 d1 ) → (tree : RBTree A key1 c1 d1 ) ( repl : RBTree A key2 c2 d2 )
737
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 736
diff changeset
661 → (si : ParentGrand ? ? ?)
736
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 735
diff changeset
662 → (ri : rotatedTree (RB→bt A tree) (RB→bt A repl))
729
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 727
diff changeset
663 → (next : ℕ → A → {k1 k2 d1 d2 : ℕ} {c1 c2 : Color} → (tree1 : RBTree A k1 c1 d1 ) (repl1 : RBTree A k2 c2 d2 )
737
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 736
diff changeset
664 → (si1 : ParentGrand ? ? ?)
736
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 735
diff changeset
665 → (ri : rotatedTree (RB→bt A tree1) (RB→bt A repl1))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 735
diff changeset
666 → rbsi-len si1 < rbsi-len si → t )
729
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 727
diff changeset
667 → (exit : {k1 k2 d1 d2 : ℕ} {c1 c2 : Color} (tree1 : RBTree A k1 c1 d1 ) → (repl1 : RBTree A k2 c2 d2 )
736
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 735
diff changeset
668 → (ri : rotatedTree (RB→bt A orig) (RB→bt A repl1))
729
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 727
diff changeset
669 → t ) → t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 727
diff changeset
670 insertCase5 {n} {m} {A} {t} key value orig tree repl si ri next exit = ? where
737
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 736
diff changeset
671 insertCase51 : (key1 : ℕ) (si : ParentGrand ? ? ? ) → t
729
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 727
diff changeset
672 insertCase51 = ?
723
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 722
diff changeset
673
724
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 723
diff changeset
674 replaceRBP : {n m : Level} {A : Set n} {t : Set m}
737
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 736
diff changeset
675 → (key : ℕ) → (value : A) → {key0 key1 d0 d1 : ℕ} {c0 c1 : Color}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 736
diff changeset
676 → (orig : RBTree A key0 c0 d0 ) → (tree : RBTree A key1 c1 d1 )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 736
diff changeset
677 → (stack : List (bt A)) → (si : stackInvariant key (RB→bt A tree) (RB→bt A orig) stack )
738
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 737
diff changeset
678 → (next : {key2 d2 : ℕ} {c2 : Color} → (tree2 : RBTree A key2 c2 d2 )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 737
diff changeset
679 → {tr to : bt A} → RB→bt A tree2 ≡ tr → RB→bt A orig ≡ to
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 737
diff changeset
680 → (stack1 : List (bt A)) → stackInvariant key tr to stack1
737
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 736
diff changeset
681 → length stack1 < length stack → t )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 736
diff changeset
682 → (exit : {k1 d1 : ℕ} {c1 : Color} → (repl1 : RBTree A k1 c1 d1 ) → (rot : bt A )
738
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 737
diff changeset
683 → (ri : rotatedTree (RB→bt A orig) rot ) → replacedTree key value rot (RB→bt A repl1) → t ) → t
740
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
684 replaceRBP {n} {m} {A} {t} key value {_} {key1} orig tree stack si next exit = insertCase1 stack _ _ refl refl si where
738
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 737
diff changeset
685 insertCase2 : {k0 k1 d0 d1 d2 : ℕ} {c0 c1 c2 : Color} → (tree : RBTree A k0 c0 d0)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 737
diff changeset
686 → (parent : RBTree A k1 c1 d1) → (grand : RBTree A key1 c2 d2)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 737
diff changeset
687 → (stack : List (bt A)) → (tr to : bt A) → RB→bt A grand ≡ tr → RB→bt A orig ≡ to → (si : stackInvariant key tr to stack )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 737
diff changeset
688 → (pg : ParentGrand (RB→bt A tree) (RB→bt A parent) tr ) → t
739
3443703a68cc it is no good to develop all invariant at once
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 738
diff changeset
689 insertCase2 tree parent grand stack tr to treq toeq si pg = ?
740
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
690 insertCase1 : (stack : List (bt A)) → (tr to : bt A) → RB→bt A tree ≡ tr → RB→bt A orig ≡ to → (si : stackInvariant key tr to stack ) → t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
691 insertCase1 = ?
724
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 723
diff changeset
692
740
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 739
diff changeset
693