-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathp_fmt.ml
328 lines (307 loc) · 10.6 KB
/
p_fmt.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
(*
* proof/fmt.ml --- proofs (pretty printing)
*
*
* Copyright (C) 2008-2010 INRIA and Microsoft Corporation
*)
(* START dflags
*
* "proof" goals when printing proofs (i.e., --debug rep)
*
* END dflags *)
open Ext
open Property
open Expr.T
open Expr.Fmt
open Format
open Fmtutil
open P_t
let step_name x = match Property.query x Props.step with
| None -> "<?>?"
| Some sn -> string_of_stepno sn
let step_dot x = match Property.query x Props.step with
| Some (Unnamed _) -> ""
| _ -> "."
let rec extend_with (hx, vx) hs = match Deque.front hs with
| None -> (hx, vx)
| Some (h, hs) ->
let cx = match h.core with
| Flex nm
| Fresh (nm, _, _, _)
| Defn ({core = Operator (nm, _)}, _, _, _) ->
(hx, Ctx.push vx nm.core)
| Fact (_, _, _) ->
(hx, Ctx.bump vx)
| _ -> failwith "Proof_fmt.extend_with"
in
extend_with cx hs
let pp_print_obligation ff ob =
ignore (pp_print_sequent (Deque.empty, Ctx.dot) ff ob.obl.core)
let prmeth what ff =
let f m =
Format.pp_print_string ff " " ;
Method.pp_print_method ff m;
in
Option.iter (List.iter f) (query what Props.meth)
let is_omitted prf = match prf.core with
| Omitted _ -> true
| _ -> false
let rec pp_print_proof cx ff prf =
match query prf Props.orig_proof with
| Some orig_prf when not (Params.debugging "noby") ->
let orig_prf = match prf.core with
| Steps (first :: _, qed) ->
if has first Props.goal then
assign orig_prf Props.goal (get first Props.goal)
else orig_prf
| Steps ([], qed) ->
if has qed Props.goal then
assign orig_prf Props.goal (get qed Props.goal)
else orig_prf
| _ ->
orig_prf
in
pp_print_proof cx ff orig_prf
| _ ->
fprintf ff "@[<v0>" ;
if not (is_omitted prf) && (Params.debugging "proof" || Params.debugging "leafproofs") then begin
let not_steps = match prf.core with
| Steps _ -> false
| _ -> true
in
if not_steps && has prf Props.goal then begin
fprintf ff
"@[<hv3>(* goal =@ @[%a@] *)@]@,(* %s *)@,"
(Expr.Fmt.pp_print_expr (Deque.empty, Ctx.dot)) (Sequent (get prf Props.goal) @@ prf)
(Util.location prf) ;
end ;
if has prf Props.obs then begin
fprintf ff
"@[<h>(* #obligations = %d *)@]@,"
(List.length (get prf Props.obs))
end
end ;
let supp = if has prf Props.supp then"(*{_}*)" else "" in
begin match prf.core with
| By (us, onl) ->
fprintf ff "%sBY %s@[<b0>%a%t@]"
supp
(if onl then "ONLY " else "")
(pp_print_usable cx) us
(prmeth prf)
| Obvious ->
fprintf ff "%sOBVIOUS%t" supp (prmeth prf)
| Omitted h ->
fprintf ff "%sOMITTED%s" supp begin
match h with
| Explicit -> ""
| Implicit -> " (* implicit *)"
| Elsewhere loc -> Printf.sprintf " (* see %s *)" (Loc.string_of_locus ~cap:false loc)
end
| Steps (inits, qed) ->
let cx =
pp_print_delimited_fold
~sep:(fun ff () -> ())
(fun cx ff stp ->
fprintf ff "@[<hv2>%s%s "
(step_name stp) (step_dot stp) ;
let cx = pp_print_step cx ff stp in
fprintf ff "@]@," ;
cx)
cx ff inits
in
fprintf ff "@[<hv2>%s%s QED %a@]"
(step_name prf) (step_dot prf)
(pp_print_qed_step(*proof_nl*) cx) qed
| Error msg ->
fprintf ff "%sERROR (*%s*)" supp msg
end;
fprintf ff "@]"
and pp_print_step cx ff stp =
if Params.debugging "proof" then begin
if has stp Props.goal then begin
if not (has stp Props.orig_step) then pp_force_newline ff () ;
fprintf ff
"@[<hv3>(* goal =@ @[%a@] *)@]@\n(* %s *)@\n"
(Expr.Fmt.pp_print_expr (Deque.empty, Ctx.dot)) (Sequent (get stp Props.goal) @@ stp)
(Util.location stp) ;
end ;
(* if has stp Props.obs then begin *)
(* fprintf ff *)
(* "@[<h>(\* #obligations = %d *\)@]@\n" *)
(* (List.length (get stp Props.obs)) *)
(* end *)
end ;
begin match query stp Props.orig_step with
| None -> ()
| Some stp -> begin
(* pp_force_newline ff () ; *)
match stp.core with
| Have e ->
fprintf ff "(* @[<h>HAVE %a@] *)@\n" (pp_print_expr cx) e
| Take bs ->
let (_, nbs) = extend_bounds cx bs in
fprintf ff "(* @[<h>TAKE @[<b0>%a@]@] *)@\n"
(pp_print_delimited (pp_print_bound cx)) nbs
| Witness es ->
fprintf ff "(* @[<h>WITNESS @[<b0>%a@]@] *)@\n"
(pp_print_delimited (pp_print_expr cx)) es
| Pcase (e, _) ->
fprintf ff "(* @[<h>CASE %a@] *)@\n" (pp_print_expr cx) e
| Pick (bs, e, _) ->
let (ecx, nbs) = extend_bounds cx bs in
fprintf ff "(* @[<b2>PICK %a :@ %a@] *)@\n"
(pp_print_delimited (pp_print_bound cx)) nbs
(pp_print_expr ecx) e
| _ -> ()
end
end ;
let stepnm = step_name stp @@ stp in
let cx = match stp.core with
| Forget k ->
fprintf ff "FORGET EXCEPT %d" k ;
cx
| Hide us ->
fprintf ff "HIDE @[<b0>%a@]" (pp_print_usable cx) us ;
cx
| Use (us, onl) ->
if has stp Props.supp then fprintf ff "(*{_}*)" ;
fprintf ff "@[<b2>USE %s@[<b0>%a@]%t@]"
(if onl then "ONLY " else "")
(pp_print_usable cx) us
(prmeth stp) ;
List.fold_left (fun cx _ -> bump cx) cx us.facts
| Define dfs ->
pp_open_vbox ff 0 ;
let cx = pp_print_defns cx ff dfs in
pp_close_box ff () ;
cx
| Assert (sq, p) ->
ignore (pp_print_sequent cx ff sq) ;
begin
(* assumptions *)
let pcx = extend_with cx sq.context in
(* negation of old goal *)
let pcx = bump pcx in
(* step name *)
let (pcx, _) = adj pcx stepnm in
(* hidden fact that it is true *)
let pcx = bump pcx in
pp_print_proof_nl pcx ff p ;
end ;
let (cx, _) = adj cx stepnm in
bump cx
| Pcase (e, p) ->
fprintf ff "@[<b2>CASE@ %a@]" (pp_print_expr cx) e ;
begin
(* negation of old goal + new assumption *)
let pcx = bump (bump cx) in
(* step name definition and fact *)
let (pcx, _) = adj pcx stepnm in
(* hidden fact that it is true *)
let pcx = bump pcx in
pp_print_proof_nl pcx ff p ;
end ;
let (cx, _) = adj cx stepnm in
bump cx
| Suffices (sq, p) ->
fprintf ff "@[<b2>SUFFICES@ %t@]"
(fun ff -> ignore (pp_print_sequent cx ff sq)) ;
begin match Deque.null sq.context, p.core with
| false, _
| _, Steps _ -> pp_force_newline ff ()
| _ -> pp_print_space ff ()
end ;
begin
(* step name definition and fact *)
let (cx, _) = adj cx stepnm in
let cx = bump cx in
pp_print_proof cx ff p ;
end ;
(* new context *)
let cx = extend_with cx sq.context in
(* negation of old goal *)
let cx = bump cx in
(* step name definition *)
let (cx, _) = adj cx stepnm in
(* hidden fact that it is true *)
bump cx
| Have e ->
if has stp Props.supp then fprintf ff "(*{_}*) " ;
fprintf ff "@[<b2>HAVE@ %a%t@]" (pp_print_expr cx) e (prmeth stp) ;
(* negation of old goal + hidden fact *)
let cx = bump (bump cx) in
(* step name definition *)
let (cx, _) = adj cx stepnm in
(* hidden fact that it is true *)
bump cx
| Take bs ->
if has stp Props.supp then fprintf ff "(*{_}*)" ;
(* assumptions *)
let (cx, bsf) = fmt_bounds cx bs in
fprintf ff "@[<b2>TAKE@ %t%t@]" bsf (prmeth stp) ;
(* negation of old goal *)
let cx = bump cx in
(* step name definition *)
let (cx, _) = adj cx stepnm in
(* hidden fact that it is true *)
bump cx
| Witness es ->
if has stp Props.supp then fprintf ff "(*{_}*)" ;
fprintf ff "@[<b2>WITNESS@ @[<b0>%a%t@]@]"
(pp_print_delimited (pp_print_expr cx)) es (prmeth stp) ;
(* no new assumptions *)
(* negation of old goal *)
let cx = bump cx in
(* step name definition *)
let (cx, _) = adj cx stepnm in
(* hidden fact that it is true *)
bump cx
| Pick (bs, e, p) ->
let (ecx, bsf) = fmt_bounds cx bs in
fprintf ff "@[<b2>PICK %t :@ %a@]%a"
bsf
(pp_print_expr ecx) e
(pp_print_proof_nl (bump (bump (bump cx)))) p ;
(* step name + fact for existential *)
let cx = bump (bump cx) in
(* identifiers for PICK *)
let (cx, _) = extend_bounds cx bs in
(* body of PICK *)
let cx = bump cx in
(* negation of old goal *)
let cx = bump cx in
(* step name definition for the SUFFICES *)
let cx = bump cx in
(* conjunction of nondom facts in the SUFFICES *)
bump cx
in cx
and pp_print_qed_step cx ff q =
match q.core with Qed p -> pp_print_proof cx ff p
and pp_print_usable cx ff us = match us.facts, List.map defop us.defs with
| [], [] -> assert false
| [], defs ->
fprintf ff "DEF%s @[%a@]"
(if List.length defs = 1 then "" else "S")
(pp_print_delimited (pp_print_expr cx)) defs
| facts, [] ->
fprintf ff "%a" (pp_print_delimited (pp_print_expr cx)) facts
| facts, defs ->
fprintf ff "@[%a@]@ DEF%s @[%a@]"
(pp_print_delimited (pp_print_expr cx)) facts
(if List.length defs = 1 then "" else "S")
(pp_print_delimited (pp_print_expr cx)) defs
and defop dw = match dw.core with
| Dvar id -> { dw with core = Opaque id }
| Dx n -> { dw with core = Ix n }
and pp_print_proof_nl cx ff prf =
let () = match prf.core with
| Steps _ -> pp_force_newline ff () ;
| _ -> pp_print_space ff ()
in pp_print_proof cx ff prf
let string_of_step cx e =
let b = Buffer.create 10 in
let fmt = Format.formatter_of_buffer b in
ignore (pp_print_step (cx, Ctx.dot) fmt e);
Format.pp_print_flush fmt () ;
Buffer.contents b