comparison src/Reflection.v @ 142:6a00d49e85fb

Start of Reflection
author Adam Chlipala <adamc@hcoop.net>
date Tue, 28 Oct 2008 11:29:14 -0400
parents
children f3e018e167f8
comparison
equal deleted inserted replaced
141:ce4cc7fa9b2b 142:6a00d49e85fb
1 (* Copyright (c) 2008, Adam Chlipala
2 *
3 * This work is licensed under a
4 * Creative Commons Attribution-Noncommercial-No Derivative Works 3.0
5 * Unported License.
6 * The license text is available at:
7 * http://creativecommons.org/licenses/by-nc-nd/3.0/
8 *)
9
10 (* begin hide *)
11 Require Import List.
12
13 Require Import Tactics MoreSpecif.
14
15 Set Implicit Arguments.
16 (* end hide *)
17
18
19 (** %\chapter{Proof by Reflection}% *)
20
21 (** The last chapter highlighted a very heuristic approach to proving. In this chapter, we will study an alternative technique, %\textit{%#<i>#proof by reflection#</i>#%}%. We will write, in Gallina, decision procedures with proofs of correctness, and we will appeal to these procedures in writing very short proofs. Such a proof is checked by running the decision procedure. The term %\textit{%#<i>#reflection#</i>#%}% applies because we will need to translate Gallina propositions into values of inductive types representing syntax, so that Gallina programs may analyze them. *)
22
23
24 (** * Proving Evenness *)
25
26 (** Proving that particular natural number constants are even is certainly something we would rather have happen automatically. The Ltac-programming techniques that we learned in the last chapter make it easy to implement such a procedure. *)
27
28 Inductive isEven : nat -> Prop :=
29 | Even_O : isEven O
30 | Even_SS : forall n, isEven n -> isEven (S (S n)).
31
32 Ltac prove_even := repeat constructor.
33
34 Theorem even_256 : isEven 256.
35 prove_even.
36 Qed.
37
38 Print even_256.
39 (** [[
40
41 even_256 =
42 Even_SS
43 (Even_SS
44 (Even_SS
45 (Even_SS
46 ]]
47
48 ...and so on. This procedure always works (at least on machines with infinite resources), but it has a serious drawback, which we see when we print the proof it generates that 256 is even. The final proof term has length linear in the input value. This seems like a shame, since we could write a trivial and trustworthy program to verify evenness of constants. The proof checker could simply call our program where needed.
49
50 It is also unfortunate not to have static typing guarantees that our tactic always behaves appropriately. Other invocations of similar tactics might fail with dynamic type errors, and we would not know about the bugs behind these errors until we happened to attempt to prove complex enough goals.
51
52 The techniques of proof by reflection address both complaints. We will be able to write proofs like this with constant size overhead beyond the size of the input, and we will do it with verified decision procedures written in Gallina.
53
54 For this example, we begin by using a type from the [MoreSpecif] module to write a certified evenness checker. *)
55
56 Print partial.
57 (** [[
58
59 Inductive partial (P : Prop) : Set := Proved : P -> [P] | Uncertain : [P]
60 ]] *)
61
62 (** A [partial P] value is an optional proof of [P]. The notation [[P]] stands for [partial P]. *)
63
64 Open Local Scope partial_scope.
65
66 (** We bring into scope some notations for the [partial] type. These overlap with some of the notations we have seen previously for specification types, so they were placed in a separate scope that needs separate opening. *)
67
68 Definition check_even (n : nat) : [isEven n].
69 Hint Constructors isEven.
70
71 refine (fix F (n : nat) : [isEven n] :=
72 match n return [isEven n] with
73 | 0 => Yes
74 | 1 => No
75 | S (S n') => Reduce (F n')
76 end); auto.
77 Defined.
78
79 (** We can use dependent pattern-matching to write a function that performs a surprising feat. When given a [partial P], this function [partialOut] returns a proof of [P] if the [partial] value contains a proof, and it returns a (useless) proof of [True] otherwise. From the standpoint of ML and Haskell programming, it seems impossible to write such a type, but it is trivial with a [return] annotation. *)
80
81 Definition partialOut (P : Prop) (x : [P]) :=
82 match x return (match x with
83 | Proved _ => P
84 | Uncertain => True
85 end) with
86 | Proved pf => pf
87 | Uncertain => I
88 end.
89
90 (** It may seem strange to define a function like this. However, it turns out to be very useful in writing a reflective verison of our earlier [prove_even] tactic: *)
91
92 Ltac prove_even_reflective :=
93 match goal with
94 | [ |- isEven ?N] => exact (partialOut (check_even N))
95 end.
96
97 (** We identify which natural number we are considering, and we "prove" its evenness by pulling the proof out of the appropriate [check_even] call. *)
98
99 Theorem even_256' : isEven 256.
100 prove_even_reflective.
101 Qed.
102
103 Print even_256'.
104 (** [[
105
106 even_256' = partialOut (check_even 256)
107 : isEven 256
108 ]]
109
110 We can see a constant wrapper around the object of the proof. For any even number, this form of proof will suffice. What happens if we try the tactic with an odd number? *)
111
112 Theorem even_255 : isEven 255.
113 (** [[
114
115 prove_even_reflective.
116
117 [[
118
119 User error: No matching clauses for match goal
120 ]]
121
122 Thankfully, the tactic fails. To see more precisely what goes wrong, we can run manually the body of the [match].
123
124 [[
125
126 exact (partialOut (check_even 255)).
127
128 [[
129
130 Error: The term "partialOut (check_even 255)" has type
131 "match check_even 255 with
132 | Yes => isEven 255
133 | No => True
134 end" while it is expected to have type "isEven 255"
135 ]]
136
137 As usual, the type-checker performs no reductions to simplify error messages. If we reduced the first term ourselves, we would see that [check_even 255] reduces to a [No], so that the first term is equivalent to [True], which certainly does not unify with [isEven 255]. *)
138 Abort.