-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathtree.agda
More file actions
50 lines (41 loc) Β· 1.94 KB
/
tree.agda
File metadata and controls
50 lines (41 loc) Β· 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
module tree where
open import bool
open import eq
open import level
open import list
open import list-to-string
open import nat
open import nat-thms
open import string
----------------------------------------------------------------------
-- datatype
----------------------------------------------------------------------
-- nonempty trees
data π {β} (A : Set β) : Set β where
node : A β π (π A) β π A
----------------------------------------------------------------------
-- tree operations
----------------------------------------------------------------------
leaf : β {β}{A : Set β} β A β π A
leaf a = node a []
π-to-string : β {β}{A : Set β} β (f : A β string) β (t : π A) β string
πs-to-string : β {β}{A : Set β} β (f : A β string) β (ts : π (π A)) β string
π-to-string f (node a []) = f a
π-to-string f (node a ts) = "(" ^ (f a) ^ (πs-to-string f ts) ^ ")"
πs-to-string f [] = ""
πs-to-string f (t :: ts) = " " ^ (π-to-string f t) ^ (πs-to-string f ts)
perfect-binary-tree : β {β}{A : Set β} β β β A β π A
perfect-binary-tree 0 a = (node a [])
perfect-binary-tree (suc n) a = let t = perfect-binary-tree n a in
(node a (t :: t :: []))
sizeπ : β {β}{A : Set β} β π A β β
sizeπs : β {β}{A : Set β} β π (π A) β β
sizeπ (node a ts) = suc (sizeπs ts)
sizeπs [] = 0
sizeπs (t :: ts) = sizeπ t + sizeπs ts
size-perfect : β {β}{A : Set β}(n : β)(a : A) β (sizeπ (perfect-binary-tree n a)) β‘ pred (2 pow (suc n))
size-perfect 0 a = refl
size-perfect (suc n) a with (sizeπ (perfect-binary-tree n a)) | size-perfect n a
... | s | ps rewrite ps with 2 pow n | nonzero-pow 2 n refl
... | x | px rewrite +0 x with x + x | (iszerosum2 x x px)
... | x2 | q rewrite +0 x2 | +0 (pred x2) | sym (+suc (pred x2) (pred x2)) | sucpred q | pred+ x2 x2 q = refl