annotate hoareBinaryTree1.agda @ 602:0dbbcab02864

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 07 Jun 2020 15:44:39 +0900
parents 7ae0c25d2b58
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
598
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
1 module hoareBinaryTree1 where
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
2
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
3 open import Level renaming (zero to Z ; suc to succ)
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
4
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
5 open import Data.Nat hiding (compare)
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
6 open import Data.Nat.Properties as NatProp
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
7 open import Data.Maybe
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
8 -- open import Data.Maybe.Properties
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
9 open import Data.Empty
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
10 open import Data.List
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
11 open import Data.Product
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
12
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
13 open import Function as F hiding (const)
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
14
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
15 open import Relation.Binary
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
16 open import Relation.Binary.PropositionalEquality
599
7ae0c25d2b58 writing invaliant
ryokka
parents: 598
diff changeset
17 open import Relation.Nullary hiding (proof)
598
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
18 open import logic
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
19
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
20
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
21 data bt {n : Level} (A : Set n) : Set n where
602
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
22 bt-empty : bt A
598
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
23 bt-node : (key : ℕ) → A →
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
24 (ltree : bt A) → (rtree : bt A) → bt A
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
25
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
26 bt-find : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (tree : bt A ) → List (bt A)
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
27 → ( bt A → List (bt A) → t ) → t
602
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
28 bt-find {n} {m} {A} {t} key leaf@(bt-empty) stack exit = exit leaf stack
598
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
29 bt-find {n} {m} {A} {t} key (bt-node key₁ AA tree tree₁) stack next with <-cmp key key₁
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
30 bt-find {n} {m} {A} {t} key node@(bt-node key₁ AA tree tree₁) stack exit | tri≈ ¬a b ¬c = exit node stack
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
31 bt-find {n} {m} {A} {t} key node@(bt-node key₁ AA ltree rtree) stack next | tri< a ¬b ¬c = bt-find key ltree (node ∷ stack) next
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
32 bt-find {n} {m} {A} {t} key node@(bt-node key₁ AA ltree rtree) stack next | tri> ¬a ¬b c = bt-find key rtree (node ∷ stack) next
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
33
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
34 bt-replace : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → A → bt A → List (bt A) → (bt A → t ) → t
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
35 bt-replace {n} {m} {A} {t} ikey a otree stack next = bt-replace0 otree where
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
36 bt-replace1 : bt A → List (bt A) → t
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
37 bt-replace1 tree [] = next tree
602
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
38 bt-replace1 node ((bt-empty) ∷ stack) = bt-replace1 node stack
598
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
39 bt-replace1 node ((bt-node key₁ b x x₁) ∷ stack) = bt-replace1 (bt-node key₁ b node x₁) stack
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
40 bt-replace0 : (tree : bt A) → t
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
41 bt-replace0 tree@(bt-node key _ ltr rtr) = bt-replace1 (bt-node ikey a ltr rtr) stack -- find case
602
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
42 bt-replace0 bt-empty = bt-replace1 (bt-node ikey a bt-empty bt-empty) stack
598
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
43
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
44
602
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
45 bt-Empty : {n : Level} {A : Set n} → bt A
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
46 bt-Empty = bt-empty
598
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
47
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
48 bt-insert : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → A → bt A → (bt A → t ) → t
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
49 bt-insert key a tree next = bt-find key tree [] (λ mtree stack → bt-replace key a mtree stack (λ tree → next tree) )
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
50
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
51 find-test : bt ℕ
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
52 find-test = bt-find 5 bt-empty [] (λ x y → x)
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
53
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
54
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
55 insert-test : bt ℕ
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
56 insert-test = bt-insert 5 7 bt-empty (λ x → x)
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
57
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
58 insert-test1 : bt ℕ
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
59 insert-test1 = bt-insert 5 7 bt-empty (λ x → bt-insert 15 17 x (λ y → y))
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
60
602
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
61 insert-test2 : {n : Level} {t : Set n} → ( bt ℕ → t ) → t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
62 insert-test2 next = bt-insert 15 17 bt-empty
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
63 $ λ x1 → bt-insert 5 7 x1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
64 $ λ x2 → bt-insert 1 3 x2
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
65 $ λ x3 → bt-insert 4 2 x3
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
66 $ λ x4 → bt-insert 1 4 x4
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
67 $ λ y → next y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
68
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
69 insert-test3 : bt ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
70 insert-test3 = bt-insert 15 17 bt-empty
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
71 $ λ x1 → bt-insert 5 7 x1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
72 $ λ x2 → bt-insert 1 3 x2
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
73 $ λ x3 → bt-insert 4 2 x3
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
74 $ λ x4 → bt-insert 1 4 x4
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
75 $ λ y → y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
76
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
77 insert-find0 : bt ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
78 insert-find0 = insert-test2 $ λ tree → bt-find 1 tree [] $ λ x y → x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
79
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
80 insert-find1 : List (bt ℕ)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
81 insert-find1 = insert-test2 $ λ tree → bt-find 1 tree [] $ λ x y → y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
82
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
83 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
84 -- 1 After insert, all node except inserted node is preserved
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
85 -- 2 After insert, specified key node is inserted
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
86 -- 3 tree node order is consistent
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
87 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
88 -- 4 noes on stack + current node = original top node .... invriant bt-find
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
89 -- 5 noes on stack + current node = original top node with replaced node .... invriant bt-replace
598
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
90
599
7ae0c25d2b58 writing invaliant
ryokka
parents: 598
diff changeset
91 tree+stack0 : {n : Level} {A : Set n} → (tree mtree : bt A) → (stack : List (bt A)) → Set n
7ae0c25d2b58 writing invaliant
ryokka
parents: 598
diff changeset
92 tree+stack0 {n} {A} tree mtree [] = {!!}
7ae0c25d2b58 writing invaliant
ryokka
parents: 598
diff changeset
93 tree+stack0 {n} {A} tree mtree (x ∷ stack) = {!!}
7ae0c25d2b58 writing invaliant
ryokka
parents: 598
diff changeset
94
598
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
95 tree+stack : {n : Level} {A : Set n} → (tree mtree : bt A) → (stack : List (bt A)) → Set n
602
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
96 tree+stack {n} {A} bt-empty mtree stack = (mtree ≡ bt-empty) ∧ (stack ≡ [])
598
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
97 tree+stack {n} {A} (bt-node key x tree tree₁) mtree stack = bt-replace key x mtree stack (λ ntree → ntree ≡ tree)
40ffa0833d03 add new BinaryTree
ryokka
parents:
diff changeset
98
599
7ae0c25d2b58 writing invaliant
ryokka
parents: 598
diff changeset
99 data _implies_ (A B : Set ) : Set (succ Z) where
7ae0c25d2b58 writing invaliant
ryokka
parents: 598
diff changeset
100 proof : ( A → B ) → A implies B
7ae0c25d2b58 writing invaliant
ryokka
parents: 598
diff changeset
101
7ae0c25d2b58 writing invaliant
ryokka
parents: 598
diff changeset
102 implies2p : {A B : Set } → A implies B → A → B
7ae0c25d2b58 writing invaliant
ryokka
parents: 598
diff changeset
103 implies2p (proof x) = x
7ae0c25d2b58 writing invaliant
ryokka
parents: 598
diff changeset
104
7ae0c25d2b58 writing invaliant
ryokka
parents: 598
diff changeset
105
7ae0c25d2b58 writing invaliant
ryokka
parents: 598
diff changeset
106 bt-find-hoare1 : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (tree mtree : bt A ) → (stack : List (bt A))
7ae0c25d2b58 writing invaliant
ryokka
parents: 598
diff changeset
107 → (tree+stack tree mtree stack)
7ae0c25d2b58 writing invaliant
ryokka
parents: 598
diff changeset
108 → ( (ntree : bt A) → (nstack : List (bt A)) → (tree+stack tree ntree nstack) → t ) → t
602
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
109 bt-find-hoare1 {n} {m} {A} {t} key leaf@(bt-empty) mtree stack t+s exit = exit leaf stack {!!}
599
7ae0c25d2b58 writing invaliant
ryokka
parents: 598
diff changeset
110 bt-find-hoare1 {n} {m} {A} {t} key (bt-node key₁ AA tree tree₁) mtree stack t+s next with <-cmp key key₁
7ae0c25d2b58 writing invaliant
ryokka
parents: 598
diff changeset
111 bt-find-hoare1 {n} {m} {A} {t} key node@(bt-node key₁ AA tree tree₁) mtree stack t+s exit | tri≈ ¬a b ¬c = exit node stack {!!}
7ae0c25d2b58 writing invaliant
ryokka
parents: 598
diff changeset
112 bt-find-hoare1 {n} {m} {A} {t} key node@(bt-node key₁ AA ltree rtree) mtree stack t+s next | tri< a ¬b ¬c = bt-find-hoare1 {n} {m} {A} {t} key ltree {!!} (node ∷ stack) {!!} {!!}
7ae0c25d2b58 writing invaliant
ryokka
parents: 598
diff changeset
113 bt-find-hoare1 {n} {m} {A} {t} key node@(bt-node key₁ AA ltree rtree) mtree stack t+s next | tri> ¬a ¬b c = bt-find-hoare1 key rtree {!!} (node ∷ stack) {!!} {!!}
7ae0c25d2b58 writing invaliant
ryokka
parents: 598
diff changeset
114
7ae0c25d2b58 writing invaliant
ryokka
parents: 598
diff changeset
115
7ae0c25d2b58 writing invaliant
ryokka
parents: 598
diff changeset
116 bt-find-hoare : {n m : Level} {A : Set n} {t : Set m} → (key : ℕ) → (tree : bt A ) → ( (ntree : bt A) → (nstack : List (bt A)) → (tree+stack tree ntree nstack) → t ) → t
7ae0c25d2b58 writing invaliant
ryokka
parents: 598
diff changeset
117 bt-find-hoare {n} {m} {A} {t} key node exit = bt-find-hoare1 {n} {m} {A} {t} key node bt-empty [] ({!!}) exit