annotate hoareBinaryTree.agda @ 604:2075785a124a

new approach
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 03 Nov 2021 10:32:56 +0900
parents 803c423c2855
children f8cc98fcc34b
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
8627d35d4bff add data bt', and some function
ryokka
parents: 587
diff changeset
27
590
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 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 589
diff changeset
30 -- no children , having left node , having right node , having both
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 589
diff changeset
31 --
597
ryokka
parents: 596
diff changeset
32 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
33 leaf : bt A
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
34 node : (key : ℕ) → (value : A) →
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
35 (left : bt A ) → (write : bt A ) → bt A
600
016a8deed93d fix old binary tree
ryokka
parents: 597
diff changeset
36
604
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
37 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
38 → (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
39 find key leaf st _ exit = exit leaf st
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
40 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
41 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
42 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
43 find key n@(node key₁ v tree tree₁) st next _ | tri> ¬a ¬b c = next tree₁ (n ∷ st)
597
ryokka
parents: 596
diff changeset
44
604
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
45 {-# TERMINATING #-}
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
46 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
47 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
48 find-loop1 : bt A → List (bt A) → t
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
49 find-loop1 tree st = find key tree st find-loop1 exit
600
016a8deed93d fix old binary tree
ryokka
parents: 597
diff changeset
50
604
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
51 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
52 replace key value tree [] next exit = exit tree
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
53 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
54 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
55 ... | 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
56 ... | 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
57 ... | 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
58
604
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
59 {-# TERMINATING #-}
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
60 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
61 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
62 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
63 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
64
604
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
65 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
66 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
67
604
2075785a124a new approach
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 601
diff changeset
68 insertTest1 = insertTree leaf 1 1 (λ x → x )
587
f103f07c0552 add insert code
ryokka
parents: 586
diff changeset
69