14
let set_debug b = debug := b
16
let buf = Buffer.create 1024
18
let string_of_global env ref =
19
Libnames.string_of_qualid (Nametab.shortest_qualid_of_global env ref)
23
(* we cannot interpret the terms as we read them (since some lemmas
24
may need other lemmas to be already interpreted) *)
25
type lemma = { l_id : string; l_type : string; l_proof : string }
26
type zenon_proof = lemma list * string
30
let ident = ['a'-'z' 'A'-'Z' '_' '0'-'9' '\'']+
31
let space = [' ' '\t' '\r']
34
| "(* BEGIN-PROOF *)" "\n" { scan lexbuf }
36
| eof { anomaly "malformed Zenon proof term" }
38
(* here we read the lemmas and the main proof term;
39
meanwhile we maintain the set of axioms that were used *)
42
| "Let" space (ident as id) space* ":"
43
{ let t = read_coq_term lexbuf in
44
let p = read_lemma_proof lexbuf in
45
let l,pr = scan lexbuf in
46
{ l_id = id; l_type = t; l_proof = p } :: l, pr }
47
| "Definition theorem:"
48
{ let t = read_main_proof lexbuf in [], t }
50
{ anomaly "malformed Zenon proof term" }
52
and read_coq_term = parse
54
{ let s = Buffer.contents buf in Buffer.clear buf; s }
55
| "coq__" (ident as id) (* a Why keyword renamed *)
56
{ Buffer.add_string buf id; read_coq_term lexbuf }
57
| ("dp_axiom__" ['0'-'9']+) as id
58
{ axioms := id :: !axioms; Buffer.add_string buf id; read_coq_term lexbuf }
60
{ Buffer.add_char buf c; read_coq_term lexbuf }
62
{ anomaly "malformed Zenon proof term" }
64
and read_lemma_proof = parse
66
{ read_coq_term lexbuf }
68
{ anomaly "malformed Zenon proof term" }
70
(* skip the main proof statement and then read its term *)
71
and read_main_proof = parse
73
{ read_coq_term lexbuf }
75
{ read_main_proof lexbuf }
77
{ anomaly "malformed Zenon proof term" }
82
let read_zenon_proof f =
85
let lb = from_channel c in
88
if not !debug then begin try Sys.remove f with _ -> () end;
91
let constr_of_string gl s =
92
let parse_constr = Pcoq.parse_string Pcoq.Constr.constr in
93
Constrintern.interp_constr (project gl) (pf_env gl) (parse_constr s)
95
(* we are lazy here: we build strings containing Coq terms using a *)
96
(* pretty-printer Fol -> Coq *)
101
let rec print_list sep print fmt = function
104
| x :: r -> print fmt x; sep fmt (); print_list sep print fmt r
106
let space fmt () = fprintf fmt "@ "
107
let comma fmt () = fprintf fmt ",@ "
109
let rec print_typ fmt = function
110
| Tvar x -> fprintf fmt "%s" x
111
| Tid ("int", []) -> fprintf fmt "Z"
112
| Tid (x, []) -> fprintf fmt "%s" x
113
| Tid (x, [t]) -> fprintf fmt "(%s %a)" x print_typ t
115
fprintf fmt "(%s %a)" x (print_list comma print_typ) tl
117
let rec print_term fmt = function
121
fprintf fmt "@[(Zplus %a %a)@]" print_term a print_term b
123
fprintf fmt "@[(Zminus %a %a)@]" print_term a print_term b
125
fprintf fmt "@[(Zmult %a %a)@]" print_term a print_term b
127
fprintf fmt "@[(Zdiv %a %a)@]" print_term a print_term b
131
fprintf fmt "@[(%s %a)@]" id print_terms tl
133
and print_terms fmt tl =
134
print_list space print_term fmt tl
136
(* builds the text for "forall vars, f vars = t" *)
137
let fun_def_axiom f vars t =
138
let binder fmt (x,t) = fprintf fmt "(%s: %a)" x print_typ t in
139
fprintf str_formatter
140
"@[(forall %a, %s %a = %a)@]@."
141
(print_list space binder) vars f
142
(print_list space (fun fmt (x,_) -> pp_print_string fmt x)) vars
144
flush_str_formatter ()
148
let prove_axiom id = match Dp_why.find_proof id with
151
| Fun_def (f, vars, ty, t) ->
154
let s = Coq.fun_def_axiom f vars t in
155
if !debug then Format.eprintf "axiom fun def = %s@." s;
156
let c = constr_of_string gl s in
157
assert_tac (Name (id_of_string id)) c gl)
158
[tclTHEN intros reflexivity; tclIDTAC]
160
let exact_string s gl =
161
let c = constr_of_string gl s in
164
let interp_zenon_proof (ll,p) =
165
let interp_lemma l gl =
166
let ty = constr_of_string gl l.l_type in
168
(assert_tac (Name (id_of_string l.l_id)) ty)
169
[exact_string l.l_proof; tclIDTAC]
172
tclTHEN (tclMAP interp_lemma ll) (exact_string p)
174
let proof_from_file f =
176
msgnl (str "proof_from_file " ++ str f);
177
let zp = read_zenon_proof f in
178
msgnl (str "proof term is " ++ str (snd zp));
179
tclTHEN (tclMAP prove_axiom !axioms) (interp_zenon_proof zp)