Families.agda 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. {-
  2. Types Summer School 2007
  3. Bertinoro
  4. Aug 19 - 31, 2007
  5. Agda
  6. Ulf Norell
  7. -}
  8. -- Now we're getting somewhere! Inductive families of datatypes.
  9. module Families where
  10. -- You can import modules defined in other files.
  11. -- More details later...
  12. -- open import Nat
  13. data Nat : Set where
  14. zero : Nat
  15. suc : Nat -> Nat
  16. _+_ : Nat -> Nat -> Nat
  17. zero + m = m
  18. suc n + m = suc (n + m)
  19. -- Think of an inductive family...
  20. data Vec (A : Set) : Nat -> Set where
  21. [] : Vec A zero
  22. _::_ : {n : Nat} -> A -> Vec A n -> Vec A (suc n)
  23. infixr 40 _::_
  24. -- Some simple functions
  25. head : {A : Set}{n : Nat} -> Vec A (suc n) -> A
  26. head (x :: _) = x -- no need for a [] case
  27. -- Does the definition look familiar?
  28. map : {A B : Set}{n : Nat} -> (A -> B) -> Vec A n -> Vec B n
  29. map f [] = []
  30. map f (x :: xs) = f x :: map f xs
  31. infixr 40 _++_
  32. _++_ : {A : Set}{n m : Nat} -> Vec A n -> Vec A m -> Vec A (n + m)
  33. [] ++ ys = ys
  34. (x :: xs) ++ ys = x :: (xs ++ ys)
  35. -- Why does this type check? Let's walk through it slowly.
  36. -- When pattern matching on the first vector, n is instantiated.
  37. -- What happens if we make the lengths explicit?
  38. cat : {A : Set}(n m : Nat) -> Vec A n -> Vec A m -> Vec A (n + m)
  39. cat .zero m [] ys = ys
  40. cat .(suc n) m (_::_ {n} x xs) ys = x :: (cat n m xs ys)
  41. -- Patterns which get instantiated by pattern matching on other stuff
  42. -- get tagged by a dot. If you erase all the dotted things you get a
  43. -- well-formed linear first-order pattern.
  44. -- Inside the dot we could have arbitrary terms. For instance,
  45. data Image_∋_ {A B : Set}(f : A -> B) : B -> Set where
  46. im : (x : A) -> Image f ∋ f x
  47. inv : {A B : Set}(f : A -> B)(y : B) -> Image f ∋ y -> A
  48. inv f .(f x) (im x) = x
  49. -- Let's do some other interesting families.
  50. -- The identity type.
  51. data _==_ {A : Set} : A -> A -> Set where
  52. refl : (x : A) -> x == x
  53. subst : {A : Set}(C : A -> Set)(x y : A) -> x == y -> C x -> C y
  54. subst C .x .x (refl x) cx = cx
  55. -- Finite sets
  56. {-
  57. Fin zero -
  58. Fin (suc zero) fzero
  59. Fin 2 fzero, fsuc fzero
  60. -}
  61. data Fin : Nat -> Set where
  62. fzero : {n : Nat} -> Fin (suc n)
  63. fsuc : {n : Nat} -> Fin n -> Fin (suc n)
  64. _!_ : {A : Set}{n : Nat} -> Vec A n -> Fin n -> A
  65. [] ! ()
  66. (x :: xs) ! fzero = x
  67. (x :: xs) ! fsuc i = xs ! i
  68. {-
  69. What's next?
  70. -}
  71. -- Actually, inductive families are sufficiently fun that
  72. -- you'll never get bored, but there's even more fun to be had.
  73. -- Move on to: Filter.agda