Basics.agda 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. {-
  2. Types Summer School 2007
  3. Bertinoro
  4. Aug 19 - 31, 2007
  5. Agda
  6. Ulf Norell
  7. -}
  8. {-
  9. Learn more about Agda on the Agda wiki:
  10. http://www.cs.chalmers.se/~ulfn/Agda
  11. This is where you find the exercises for the afternoon.
  12. -}
  13. -- Each Agda file contains a top-level module, whose
  14. -- name corresponds to the file name.
  15. module Basics where
  16. {-
  17. Expressions (types and terms)
  18. -}
  19. -- The expression language of Agda is your favorite dependently
  20. -- typed λ-calculus.
  21. -- For instance:
  22. id₁ : (A : Set) -> A -> A
  23. id₁ = \ A x -> x
  24. id₂ : (A : Set) -> A -> A
  25. id₂ = \ A x -> id₁ A (id₁ A x)
  26. -- Note: Agda likes white space. This is not correct:
  27. -- id:(A:Set)->A->A
  28. -- Why not? In Agda the following strings are valid identifiers:
  29. -- id:
  30. -- A:Set
  31. -- ->A->A
  32. -- Another useful function, featuring telescopes
  33. -- and typed λs.
  34. compose : (A B C : Set) -> (B -> C) -> (A -> B) -> A -> C
  35. compose = \(A B C : Set) f g x -> f (g x)
  36. compose' : (A B : Set)(C : B -> Set)
  37. (f : (x : B) -> C x)(g : A -> B) ->
  38. (x : A) -> C (g x)
  39. compose' = \A B C f g x -> f (g x)
  40. {-
  41. Implicit arguments
  42. -}
  43. -- Writing down type arguments explicitly soon gets old.
  44. -- Enter implicit arguments.
  45. -- Note the curlies in the telescope. And A mysteriously disappeares
  46. -- in the definition.
  47. id₃ : {A : Set} -> A -> A
  48. id₃ = \ x -> x
  49. -- And it's not there when applying the function.
  50. id₄ : {A : Set} -> A -> A
  51. id₄ = \ x -> (id₃ (id₃ x))
  52. -- If you think the type checker should figure out the value of
  53. -- something explicit, you write _.
  54. id₆ : {A : Set} -> A -> A
  55. id₆ x = id₁ _ x
  56. -- Interesting though it is, eventually you'll get bored
  57. -- with the λ-calculus...
  58. -- Move on to: Datatypes.agda