FunctionsInIndices.agda 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. module FunctionsInIndices where
  2. open import Prelude
  3. open import Eq
  4. data Tree (a : Set) : ℕ -> Set where
  5. leaf : a -> Tree a 1
  6. node : forall n₁ n₂ -> Tree a n₁ -> Tree a n₂ -> Tree a (n₁ + n₂)
  7. -- This does not work:
  8. -- leftmost : forall {a} n -> Tree a (suc n) -> a
  9. -- leftmost .0 (leaf x) = x
  10. -- leftmost .n₂ (node zero (suc n₂) l r) = leftmost l
  11. -- leftmost .(n₁ + n₂) (node (suc n₁) n₂ l r) = leftmost l
  12. module Workaround1 where
  13. private
  14. T : Set -> ℕ -> Set
  15. T a zero = ⊤
  16. T a (suc n) = a
  17. leftmost' : forall {a n} -> Tree a n -> T a n
  18. leftmost' (leaf x) = x
  19. leftmost' (node zero zero l r) = tt
  20. leftmost' (node zero (suc n₂) l r) = leftmost' r
  21. leftmost' (node (suc n₁) n₂ l r) = leftmost' l
  22. leftmost : forall {a n} -> Tree a (suc n) -> a
  23. leftmost = leftmost'
  24. module Workaround2 where
  25. private
  26. leftmost' : forall {a n m} -> Tree a m -> m ≡ suc n -> a
  27. leftmost' (leaf x) _ = x
  28. leftmost' (node zero (suc n₂) l r) _ = leftmost' r refl
  29. leftmost' (node (suc n₁) n₂ l r) _ = leftmost' l refl
  30. leftmost : forall {a n} -> Tree a (suc n) -> a
  31. leftmost t = leftmost' t refl