Problem1.agda 925 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. module Problem1 where
  2. -- 1.1
  3. data Nat : Set where
  4. zero : Nat
  5. suc : Nat -> Nat
  6. -- 1.2
  7. infixl 60 _+_
  8. _+_ : Nat -> Nat -> Nat
  9. zero + m = m
  10. suc n + m = suc (n + m)
  11. -- 1.3
  12. infixl 70 _*_
  13. _*_ : Nat -> Nat -> Nat
  14. zero * m = zero
  15. suc n * m = m + n * m
  16. -- 1.4
  17. infix 30 _==_
  18. data _==_ {A : Set}(x : A) : A -> Set where
  19. refl : x == x
  20. cong : {A B : Set}(f : A -> B){x y : A} -> x == y -> f x == f y
  21. cong f refl = refl
  22. assoc : (x y z : Nat) -> x + (y + z) == (x + y) + z
  23. assoc zero y z = refl
  24. assoc (suc x) y z = cong suc (assoc x y z)
  25. -- Alternative solution using 'with'. Note that in order
  26. -- to be able to pattern match on the induction hypothesis
  27. -- we have to abstract (using with) over the left hand side
  28. -- of the equation.
  29. assoc' : (x y z : Nat) -> x + (y + z) == (x + y) + z
  30. assoc' zero y z = refl
  31. assoc' (suc x) y z with x + (y + z) | assoc x y z
  32. ... | .((x + y) + z) | refl = refl