annotate hoareBinaryTree.agda @ 606:61a0491a627b

with Hoare condition
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 03 Nov 2021 16:14:09 +0900
parents f8cc98fcc34b
children 79418701a283
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
586
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff changeset
1 module hoareBinaryTree where
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff changeset
2
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff changeset
3 open import Level renaming (zero to Z ; suc to succ)
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff changeset
4
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff changeset
5 open import Data.Nat hiding (compare)
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff changeset
6 open import Data.Nat.Properties as NatProp
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff changeset
7 open import Data.Maybe
588
8627d35d4bff add data bt', and some function
ryokka
parents: 587
diff changeset
8 -- open import Data.Maybe.Properties
586
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff changeset
9 open import Data.Empty
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
588
8627d35d4bff add data bt', and some function
ryokka
parents: 587
diff changeset
21 _iso_ : {n : Level} {a : Set n} → ℕ → ℕ → Set
8627d35d4bff add data bt', and some function
ryokka
parents: 587
diff changeset
22 d iso d' = (¬ (suc d ≤ d')) ∧ (¬ (suc d' ≤ d))
8627d35d4bff add data bt', and some function
ryokka
parents: 587
diff changeset
23
8627d35d4bff add data bt', and some function
ryokka
parents: 587
diff changeset
24 iso-intro : {n : Level} {a : Set n} {x y : ℕ} → ¬ (suc x ≤ y) → ¬ (suc y ≤ x) → _iso_ {n} {a} x y
8627d35d4bff add data bt', and some function
ryokka
parents: 587
diff changeset
25 iso-intro = λ z z₁ → record { proj1 = z ; proj2 = z₁ }
8627d35d4bff add data bt', and some function
ryokka
parents: 587
diff changeset
26
590
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 589
diff changeset
27 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 589
diff changeset
28 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 589
diff changeset
29 -- no children , having left node , having right node , having both
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 589
diff changeset
30 --
597
ryokka
parents: 596
diff changeset
31 data bt {n : Level} (A : Set n) : Set n where
604
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
32 leaf : bt A
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
33 node : (key : ℕ) → (value : A) →
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
34 (left : bt A ) → (write : bt A ) → bt A
600
016a8deed93d fix old binary tree
ryokka
parents: 597
diff changeset
35
606
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
36 bt-length : {n : Level} {A : Set n} → (tree : bt A ) → ℕ
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
37 bt-length leaf = 0
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
38 bt-length (node key value t t₁) = Data.Nat._⊔_ (bt-length t ) (bt-length t₁ )
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
39
604
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
40 find : {n : Level} {A : Set n} {t : Set n} → (key : ℕ) → (tree : bt A ) → List (bt A)
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
41 → (next : bt A → List (bt A) → t ) → (exit : bt A → List (bt A) → t ) → t
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
42 find key leaf st _ exit = exit leaf st
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
43 find key (node key₁ v tree tree₁) st next exit with <-cmp key key₁
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
44 find key n st _ exit | tri≈ ¬a b ¬c = exit n st
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
45 find key n@(node key₁ v tree tree₁) st next _ | tri< a ¬b ¬c = next tree (n ∷ st)
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
46 find key n@(node key₁ v tree tree₁) st next _ | tri> ¬a ¬b c = next tree₁ (n ∷ st)
597
ryokka
parents: 596
diff changeset
47
604
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
48 {-# TERMINATING #-}
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
49 find-loop : {n : Level} {A : Set n} {t : Set n} → (key : ℕ) → bt A → List (bt A) → (exit : bt A → List (bt A) → t) → t
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
50 find-loop {_} {A} {t} key tree st exit = find-loop1 tree st where
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
51 find-loop1 : bt A → List (bt A) → t
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
52 find-loop1 tree st = find key tree st find-loop1 exit
600
016a8deed93d fix old binary tree
ryokka
parents: 597
diff changeset
53
604
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
54 replace : {n : Level} {A : Set n} {t : Set n} → (key : ℕ) → (value : A) → bt A → List (bt A) → (next : ℕ → A → bt A → List (bt A) → t ) → (exit : bt A → t) → t
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
55 replace key value tree [] next exit = exit tree
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
56 replace key value tree (leaf ∷ st) next exit = next key value tree st
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
57 replace key value tree (node key₁ value₁ left right ∷ st) next exit with <-cmp key key₁
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
58 ... | tri< a ¬b ¬c = next key value (node key₁ value₁ tree right ) st
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
59 ... | tri≈ ¬a b ¬c = next key value (node key₁ value left right ) st
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
60 ... | tri> ¬a ¬b c = next key value (node key₁ value₁ left tree ) st
586
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff changeset
61
604
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
62 {-# TERMINATING #-}
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
63 replace-loop : {n : Level} {A : Set n} {t : Set n} → (key : ℕ) → (value : A) → bt A → List (bt A) → (exit : bt A → t) → t
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
64 replace-loop {_} {A} {t} key value tree st exit = replace-loop1 key value tree st where
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
65 replace-loop1 : (key : ℕ) → (value : A) → bt A → List (bt A) → t
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
66 replace-loop1 key value tree st = replace key value tree st replace-loop1 exit
586
0ddfa505d612 isolate search function problem, and add hoareBinaryTree.agda.
ryokka
parents:
diff changeset
67
604
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
68 insertTree : {n : Level} {A : Set n} {t : Set n} → (tree : bt A) → (key : ℕ) → (value : A) → (next : bt A → t ) → t
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
69 insertTree tree key value exit = find-loop key tree [] ( λ t st → replace-loop key value t st exit )
587
f103f07c0552 add insert code
ryokka
parents: 586
diff changeset
70
604
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
71 insertTest1 = insertTree leaf 1 1 (λ x → x )
587
f103f07c0552 add insert code
ryokka
parents: 586
diff changeset
72
605
f8cc98fcc34b define invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 604
diff changeset
73 open import Data.Unit hiding ( _≟_ ; _≤?_ ; _≤_)
f8cc98fcc34b define invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 604
diff changeset
74
f8cc98fcc34b define invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 604
diff changeset
75 treeInvariant : {n : Level} {A : Set n} → (tree : bt A) → Set
f8cc98fcc34b define invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 604
diff changeset
76 treeInvariant leaf = ⊤
f8cc98fcc34b define invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 604
diff changeset
77 treeInvariant (node key value leaf leaf) = ⊤
f8cc98fcc34b define invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 604
diff changeset
78 treeInvariant (node key value leaf n@(node key₁ value₁ t₁ t₂)) = (key < key₁) ∧ treeInvariant n
f8cc98fcc34b define invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 604
diff changeset
79 treeInvariant (node key value n@(node key₁ value₁ t t₁) leaf) = treeInvariant n ∧ (key < key₁)
f8cc98fcc34b define invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 604
diff changeset
80 treeInvariant (node key value n@(node key₁ value₁ t t₁) m@(node key₂ value₂ t₂ t₃)) = treeInvariant n ∧ (key < key₁) ∧ (key₁ < key₂) ∧ treeInvariant m
f8cc98fcc34b define invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 604
diff changeset
81
f8cc98fcc34b define invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 604
diff changeset
82 treeInvariantTest1 = treeInvariant (node 3 0 leaf (node 1 1 leaf (node 3 5 leaf leaf)))
f8cc98fcc34b define invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 604
diff changeset
83
f8cc98fcc34b define invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 604
diff changeset
84 stackInvariant : {n : Level} {A : Set n} → (stack : List (bt A)) → Set n
f8cc98fcc34b define invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 604
diff changeset
85 stackInvariant {_} {A} [] = Lift _ ⊤
f8cc98fcc34b define invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 604
diff changeset
86 stackInvariant {_} {A} (leaf ∷ stack) = Lift _ ⊤
f8cc98fcc34b define invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 604
diff changeset
87 stackInvariant {_} {A} (node key value leaf leaf ∷ []) = Lift _ ⊤
f8cc98fcc34b define invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 604
diff changeset
88 stackInvariant {_} {A} (node key value _ (node _ _ _ _) ∷ []) = Lift _ ⊥
f8cc98fcc34b define invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 604
diff changeset
89 stackInvariant {_} {A} (node key value (node _ _ _ _) _ ∷ []) = Lift _ ⊥
f8cc98fcc34b define invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 604
diff changeset
90 stackInvariant {_} {A} (x ∷ node key value leaf leaf ∷ tail ) = Lift _ ⊥
f8cc98fcc34b define invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 604
diff changeset
91 stackInvariant {_} {A} (x ∷ tail @ (node key value leaf tree ∷ _) ) = (tree ≡ x) ∧ stackInvariant tail
f8cc98fcc34b define invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 604
diff changeset
92 stackInvariant {_} {A} (x ∷ tail @ (node key value tree leaf ∷ _) ) = (tree ≡ x) ∧ stackInvariant tail
f8cc98fcc34b define invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 604
diff changeset
93 stackInvariant {_} {A} (x ∷ tail @ (node key value left right ∷ _ )) = ( (left ≡ x) ∧ stackInvariant tail) ∨ ( (right ≡ x) ∧ stackInvariant tail)
f8cc98fcc34b define invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 604
diff changeset
94 stackInvariant {_} {A} s = Lift _ ⊥
606
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
95
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
96 findP : {n : Level} {A : Set n} {t : Set n} → (key : ℕ) → (tree : bt A ) → (stack : List (bt A))
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
97 → treeInvariant tree → stackInvariant stack
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
98 → (next : (tree1 : bt A) → (stack : List (bt A)) → treeInvariant tree1 ∧ stackInvariant stack → bt-length tree1 < bt-length tree → t )
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
99 → (exit : (tree1 : bt A) → (stack : List (bt A)) → treeInvariant tree1 ∧ stackInvariant stack → t ) → t
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
100 findP = {!!}
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
101
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
102 replaceP : {n : Level} {A : Set n} {t : Set n}
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
103 → (key : ℕ) → (value : A) → (tree : bt A) → (stack : List (bt A)) → treeInvariant tree ∧ stackInvariant stack
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
104 → (next : (tree1 : bt A) → (stack : List (bt A)) → treeInvariant tree1 ∧ stackInvariant stack → bt-length tree1 < bt-length tree → t )
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
105 → (exit : (tree1 : bt A) → treeInvariant tree1 → t) → t
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
106 replaceP = {!!}
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
107
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
108
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
109
61a0491a627b with Hoare condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 605
diff changeset
110