Advertisement
Guest User

church numerals and coq and universes

a guest
Sep 3rd, 2015
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1.  
  2.  
  3. (** **** Exercise: 4 stars, advanced (church_numerals) *)
  4.  
  5. Module Church.
  6.  
  7. (** In this exercise, we will explore an alternative way of defining
  8. natural numbers, using the so-called _Church numerals_, named
  9. after mathematician Alonzo Church. We can represent a natural
  10. number [n] as a function that takes a function [f] as a parameter
  11. and returns [f] iterated [n] times. More formally, *)
  12.  
  13. Definition nat := forall X : Type, (X -> X) -> X -> X.
  14.  
  15. (** Let's see how to write some numbers with this notation. Any
  16. function [f] iterated once shouldn't change. Thus, *)
  17.  
  18. Definition one : nat :=
  19. fun (X : Type) (f : X -> X) (x : X) => f x.
  20.  
  21. (** [two] should apply [f] twice to its argument: *)
  22.  
  23. Definition two : nat :=
  24. fun (X : Type) (f : X -> X) (x : X) => f (f x).
  25. Definition three : nat :=
  26. fun (X : Type) (f : X -> X) (x : X) => f (f (f x)).
  27.  
  28. (** [zero] is somewhat trickier: how can we apply a function zero
  29. times? The answer is simple: just leave the argument untouched. *)
  30.  
  31. Definition zero : nat :=
  32. fun (X : Type) (f : X -> X) (x : X) => x.
  33.  
  34. (** More generally, a number [n] will be written as [fun X f x => f (f
  35. ... (f x) ...)], with [n] occurrences of [f]. Notice in particular
  36. how the [doit3times] function we've defined previously is actually
  37. just the representation of [3]. *)
  38.  
  39.  
  40. (** Complete the definitions of the following functions. Make sure
  41. that the corresponding unit tests pass by proving them with
  42. [reflexivity]. *)
  43.  
  44. (** Successor of a natural number *)
  45.  
  46. Definition succ (n : nat) : nat :=
  47. (* FILL IN HERE *) fun (X : Type) (f : X -> X) (x : X) => f (n X f x).
  48.  
  49.  
  50. Example succ_1 : succ zero = one.
  51. Proof. (* FILL IN HERE *) Proof. reflexivity. Qed.
  52.  
  53. Example succ_2 : succ one = two.
  54. Proof. (* FILL IN HERE *) Proof. reflexivity. Qed.
  55.  
  56. Example succ_3 : succ two = three.
  57. Proof. (* FILL IN HERE *) Proof. reflexivity. Qed.
  58.  
  59. (** Addition of two natural numbers *)
  60.  
  61. Eval compute in (three bool negb false).
  62.  
  63. Check nat.
  64. Check succ.
  65. (* Check three _ succ. <- this fails *)
  66.  
  67. (* this also fails *)
  68. Definition plus (n m : nat) : nat := n _ succ m.
  69.  
  70. (*
  71. Example plus_1 : plus zero one = one.
  72. Proof. (* FILL IN HERE *) Proof. reflexivity. Qed.
  73.  
  74. Example plus_2 : plus two three = plus three two.
  75. Proof. (* FILL IN HERE *) Proof. reflexivity. Qed.
  76.  
  77. Example plus_3 :
  78. plus (plus two two) three = plus one (plus three three).
  79. Proof. (* FILL IN HERE *) Proof. reflexivity. Qed.
  80. *)
  81.  
  82. (** Multiplication *)
  83.  
  84. Definition mult (n m : nat) : nat := n _ (plus m) zero.
  85. (* FILL IN HERE *)
  86.  
  87. Example mult_1 : mult one one = one.
  88. Proof. (* FILL IN HERE *) Proof. reflexivity. Qed.
  89.  
  90. Example mult_2 : mult zero (plus three three) = zero.
  91. Proof. (* FILL IN HERE *) Proof. reflexivity. Qed.
  92.  
  93. Example mult_3 : mult two three = plus three three.
  94. Proof. (* FILL IN HERE *) Proof. reflexivity. Qed.
  95.  
  96. (** Exponentiation *)
  97.  
  98. (** Hint: Polymorphism plays a crucial role here. However, choosing
  99. the right type to iterate over can be tricky. If you hit a
  100. "Universe inconsistency" error, try iterating over a different
  101. type: [nat] itself is usually problematic. *)
  102.  
  103. Definition exp (n m : nat) : nat := m _ (mult n) one.
  104. (* FILL IN HERE *)
  105.  
  106. Example exp_1 : exp two two = plus two two.
  107. Proof. (* FILL IN HERE *) Proof. reflexivity. Qed.
  108.  
  109. Example exp_2 : exp three two = plus (mult two (mult two two)) one.
  110. Proof. (* FILL IN HERE *) Proof. reflexivity. Qed.
  111.  
  112. Example exp_3 : exp three zero = one.
  113. Proof. (* FILL IN HERE *) Proof. reflexivity. Qed.
  114. End Church.
  115.  
  116. (** [] *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement