Parameterised.agda 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. -- This module introduces parameterised datatypes.
  2. module Introduction.Data.Parameterised where
  3. -- First some of our old friends.
  4. data Nat : Set where
  5. zero : Nat
  6. suc : Nat -> Nat
  7. data Bool : Set where
  8. false : Bool
  9. true : Bool
  10. -- A datatype can be parameterised over a telescope, (A : Set) in the case of
  11. -- lists. The parameters are bound in the types of the constructors.
  12. data List (A : Set) : Set where
  13. nil : List A
  14. cons : A -> List A -> List A
  15. -- When using the constructors the parameters to the datatype becomes implicit
  16. -- arguments. In this case, the types of the constructors are :
  17. -- nil : {A : Set} -> List A
  18. -- cons : {A : Set} -> A -> List A -> List A
  19. -- So, we can write
  20. nilNat = nil {Nat} -- the type of this will be List Nat
  21. -- When pattern matching on elements of a parameterised datatype you cannot
  22. -- refer to the parameters--it wouldn't make sense to pattern match on the
  23. -- element type of the list. So you can say
  24. null : {A : Set} -> List A -> Bool
  25. null nil = true
  26. null (cons _ _) = false
  27. -- but not
  28. -- null (nil {A}) = true
  29. -- null (cons {A} _ _) = false