| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Properties of binary trees |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.Tree.Binary.Properties where |
| 10 |
|
| 11 |
open import Function.Base using (_∘_) |
| 12 |
open import Function.Nary.NonDependent using (congₙ) |
| 13 |
open import Level using (Level) |
| 14 |
open import Data.Nat.Base using (suc; _+_) |
| 15 |
open import Data.Tree.Binary |
| 16 |
open import Function.Base using (id; flip) |
| 17 |
open import Relation.Binary.PropositionalEquality.Core |
| 18 |
using (_≡_; refl; cong; cong₂; _≗_) |
| 19 |
|
| 20 |
private |
| 21 |
variable |
| 22 |
a n n₁ n₂ l l₁ l₂ : Level |
| 23 |
A : Set a |
| 24 |
N : Set n |
| 25 |
N₁ : Set n₁ |
| 26 |
N₂ : Set n₂ |
| 27 |
L : Set l |
| 28 |
L₁ : Set l₁ |
| 29 |
L₂ : Set l₂ |
| 30 |
|
| 31 |
#nodes-map : ∀ (f : N → N₁) (g : L → L₁) t → #nodes (map f g t) ≡ #nodes t |
| 32 |
#nodes-map f g (leaf x) = refl |
| 33 |
#nodes-map f g (node l m r) = |
| 34 |
cong₂ (λ l r → l + suc r) (#nodes-map f g l) (#nodes-map f g r) |
| 35 |
|
| 36 |
#nodes-mapₙ : ∀ (f : N → N₁) (t : Tree N L) → #nodes (mapₙ f t) ≡ #nodes t |
| 37 |
#nodes-mapₙ f = #nodes-map f id |
| 38 |
|
| 39 |
#nodes-mapₗ : ∀ (g : L → L₁) (t : Tree N L) → #nodes (mapₗ g t) ≡ #nodes t |
| 40 |
#nodes-mapₗ = #nodes-map id |
| 41 |
|
| 42 |
#leaves-map : ∀ (f : N → N₁) (g : L → L₁) t → #leaves (map f g t) ≡ #leaves t |
| 43 |
#leaves-map f g (leaf x) = refl |
| 44 |
#leaves-map f g (node l m r) = |
| 45 |
cong₂ _+_ (#leaves-map f g l) (#leaves-map f g r) |
| 46 |
|
| 47 |
#leaves-mapₙ : ∀ (f : N → N₁) (t : Tree N L) → #leaves (mapₙ f t) ≡ #leaves t |
| 48 |
#leaves-mapₙ f = #leaves-map f id |
| 49 |
|
| 50 |
#leaves-mapₗ : ∀ (g : L → L₁) (t : Tree N L) → #leaves (mapₗ g t) ≡ #leaves t |
| 51 |
#leaves-mapₗ = #leaves-map id |
| 52 |
|
| 53 |
map-id : ∀ (t : Tree N L) → map id id t ≡ t |
| 54 |
map-id (leaf x) = refl |
| 55 |
map-id (node l v r) = cong₂ (flip node v) (map-id l) (map-id r) |
| 56 |
|
| 57 |
map-compose : ∀ {f₁ : N₁ → N₂} {f₂ : N → N₁} {g₁ : L₁ → L₂} {g₂ : L → L₁} → |
| 58 |
map (f₁ ∘ f₂) (g₁ ∘ g₂) ≗ map f₁ g₁ ∘ map f₂ g₂ |
| 59 |
map-compose (leaf x) = refl |
| 60 |
map-compose (node l v r) = cong₂ (λ l r → node l _ r) (map-compose l) (map-compose r) |
| 61 |
|
| 62 |
map-cong : ∀ {f₁ f₂ : N → N₁} {g₁ g₂ : L → L₁} → f₁ ≗ f₂ → g₁ ≗ g₂ → map f₁ g₁ ≗ map f₂ g₂ |
| 63 |
map-cong p q (leaf x) = cong leaf (q x) |
| 64 |
map-cong p q (node l v r) = congₙ 3 node (map-cong p q l) (p v) (map-cong p q r) |