Source file pretty.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
(** Pretty-printing the parser-level AST.
This module defines functions that allow printing elements of syntax found
in the parser-level abstract syntax. This is used, for example, to print a
file in the Lambdapi syntax, given the AST obtained when parsing a file in
the Dedukti syntax. *)
open Lplib open Base
open Common open Pos
open Syntax
open Format
open Core
(** Keywords table. *)
let keyword_table = Hashtbl.create 59
let is_keyword : string -> bool = Hashtbl.mem keyword_table
let _ = let open LpLexer in
List.iter (fun (s, k) -> Hashtbl.add keyword_table s k)
[ "abort", ABORT
; "admit", ADMIT
; "admitted", ADMITTED
; "apply", APPLY
; "as", AS
; "assert", ASSERT false
; "assertnot", ASSERT true
; "associative", ASSOCIATIVE
; "assume", ASSUME
; "assumption", ASSUMPTION
; "begin", BEGIN
; "builtin", BUILTIN
; "commutative", COMMUTATIVE
; "compute", COMPUTE
; "constant", CONSTANT
; "debug", DEBUG
; "end", END
; "fail", FAIL
; "flag", FLAG
; "focus", FOCUS
; "generalize", GENERALIZE
; "have", HAVE
; "in", IN
; "induction", INDUCTION
; "inductive", INDUCTIVE
; "infix", INFIX
; "injective", INJECTIVE
; "left", SIDE(Pratter.Left)
; "let", LET
; "off", SWITCH(false)
; "on", SWITCH(true)
; "opaque", OPAQUE
; "open", OPEN
; "postfix", POSTFIX
; "prefix", PREFIX
; "print", PRINT
; "private", PRIVATE
; "proofterm", PROOFTERM
; "protected", PROTECTED
; "prover", PROVER
; "prover_timeout", PROVER_TIMEOUT
; "quantifier", QUANTIFIER
; "refine", REFINE
; "reflexivity", REFLEXIVITY
; "require", REQUIRE
; "rewrite", REWRITE
; "right", SIDE(Pratter.Right)
; "rule", RULE
; "sequential", SEQUENTIAL
; "simplify", SIMPLIFY
; "solve", SOLVE
; "symbol", SYMBOL
; "symmetry", SYMMETRY
; "type", TYPE_QUERY
; "TYPE", TYPE_TERM
; "unif_rule", UNIF_RULE
; "verbose", VERBOSE
; "why3", WHY3
; "with", WITH ]
let raw_ident : string pp = fun ppf s ->
string ppf (if is_keyword s then Escape.escape s else s)
let ident : p_ident pp = fun ppf {elt;_} -> raw_ident ppf elt
let param_id : p_ident option pp = fun ppf idopt ->
match idopt with
| Some(id) -> out ppf "%a" ident id
| None -> out ppf "_"
let param_ids : p_ident option list pp = List.pp param_id " "
let raw_path : Path.t pp = List.pp raw_ident "."
let path : p_path pp = fun ppf {elt;_} -> raw_path ppf elt
let qident : p_qident pp = fun ppf {elt=(mp,s);_} ->
if mp = [] || mp = Sign.Ghost.path then raw_ident ppf s
else out ppf "%a.%a" raw_path mp raw_ident s
let modifier : p_modifier pp = fun ppf {elt; _} ->
match elt with
| P_expo(e) -> Print.expo ppf e
| P_mstrat(s) -> Print.match_strat ppf s
| P_prop(p) -> Print.prop ppf p
| P_opaq -> out ppf "opaque "
let modifiers : p_modifier list pp = List.pp modifier ""
let rec needs_wrap t =
match t.elt with
| P_Arro(_,u) -> needs_wrap u
| P_Prod _ -> false
| _ -> true
let no_named_param (ids,_,_) = List.for_all ((=) None) ids
let rec term : bool -> p_term pp = fun ec ppf t ->
match t.elt with
| P_Type -> out ppf "TYPE"
| P_Iden(qid,false) -> out ppf "%a" qident qid
| P_Iden(qid,true) -> out ppf "@@%a" qident qid
| P_Wild -> out ppf "_"
| P_Meta({elt=id;_},ts) ->
out ppf "?%d%a" id (env ec) (Some ts)
| P_Patt(idopt,ts) ->
out ppf "$%a%a" param_id idopt (env ec) ts
| P_NLit([],i) -> out ppf "%s" i
| P_NLit(p,i) -> out ppf "%a.%s" raw_path p i
| P_SLit s -> string ppf s
| P_Wrap t -> out ppf "(@[<hv2>%a@])" (term ec) t
| P_Expl t -> out ppf "[@[<hv2>%a@]]" (term ec) t
| P_Appl(t,u) -> out ppf "@[%a@ %a@]" (term ec) t (wrap ec) u
| P_Arro(a,b) -> out ppf "@[%a@ → %a@]" (wrap ec) a (term ec) b
| P_Abst(xs,t) ->
let ec = ec && List.for_all no_named_param xs in
out ppf "@[<2>λ%a,@ %a@]" (params_list ec) xs (term ec) t
| P_Prod(xs,b) -> out ppf "@[<2>Π%a,@ %a@]" (params_list ec) xs (term ec) b
| P_LLet(x,xs,a,t,u) ->
out ppf "@[@[<hv2>let @[<2>%a%a%a@] ≔@ %a@ @]in@ %a@]"
ident x (params_list ec) xs (typ ec) a (term ec) t (term ec) u
and wrap ec ppf t =
match t.elt with
| P_Type
| P_Iden _
| P_Wild
| P_Meta _
| P_Patt _
| P_NLit _
| P_SLit _
| P_Expl _
| P_Wrap _
-> term ec ppf t
| P_Appl _
| P_Arro _
| P_Prod _
| P_Abst _
| P_LLet _
-> out ppf "(@[<hv2>%a@])" (term ec) t
and env : bool -> p_term array option pp = fun ec ppf ts ->
match ts with
| None -> ()
| Some [||] when ec -> ()
| Some ts -> out ppf ".[%a]" (Array.pp (term ec) ";") ts
and params : bool -> p_params pp = fun ec ppf (ids, t, b) ->
if b then out ppf "@[[@,@[<2>%a%a@]@,]@]" param_ids ids (typ ec) t
else out ppf "@[(@,@[<2>%a%a@]@,)@]" param_ids ids (typ ec) t
and params_list : bool -> p_params list pp = fun ec ppf ->
List.iter (out ppf "@ %a" (params ec))
and typ : bool -> p_term option pp = fun ec ppf t ->
match t with
| None -> ()
| Some t -> out ppf "@ : %a" (if needs_wrap t then term ec else wrap ec) t
let term = term true
let wrap = wrap true
let env = env true
let typ = typ true
let params = params true
let params_list = params_list true
let rule : string -> p_rule pp = fun kw ppf {elt=(l,r);_} ->
out ppf "%s %a ↪ %a" kw term l term r
let inductive : string -> p_inductive pp =
let cons ppf (id,a) = out ppf "@,| %a : %a" ident id term a in
fun kw ppf {elt=(id,a,cs);_} ->
out ppf "@[<v>%s %a : %a ≔%a@]" kw ident id term a (List.pp cons "") cs
let equiv : (p_term * p_term) pp = fun ppf (l,r) ->
out ppf "%a ≡ %a" term l term r
(** [unpack eqs] transforms a p_term of the form [LpLexer.cons
(LpLexer.equiv t u) (LpLexer.cons (LpLexer.equiv v w) ...)] into a
list [[(t,u); (v,w); ...]]. See unif_rule.ml. *)
let rec unpack : p_term -> (p_term * p_term) list = fun eqs ->
let is (p,s) id = p = Sign.Ghost.path && s = id.Term.sym_name in
match Syntax.p_get_args eqs with
| ({elt=P_Iden({elt;_},_); _}, [v; w]) ->
if is elt Unif_rule.cons then
match Syntax.p_get_args v with
| ({elt=P_Iden({elt;_},_); _}, [t; u])
when is elt Unif_rule.equiv -> (t, u) :: unpack w
| _ -> assert false
else if is elt Unif_rule.equiv then [(v, w)]
else assert false
| _ -> assert false
let unif_rule : p_rule pp = fun ppf {elt=(l,r);_} ->
let lhs =
match Syntax.p_get_args l with
| (_, [t; u]) -> (t, u)
| _ -> assert false
in
out ppf "%a ↪ [%a]" equiv lhs (List.pp equiv "; ") (unpack r)
let proof_end : p_proof_end pp = fun ppf pe ->
out ppf (match pe.elt with
| P_proof_end -> "end"
| P_proof_admitted -> "admitted"
| P_proof_abort -> "abort")
let rwpatt : p_rwpatt pp = fun ppf p ->
match p.elt with
| Rw_Term(t) -> term ppf t
| Rw_InTerm(t) -> out ppf "in %a" term t
| Rw_InIdInTerm(x,t) -> out ppf "in %a in %a" ident x term t
| Rw_IdInTerm(x,t) -> out ppf "%a in %a" ident x term t
| Rw_TermInIdInTerm(u,(x,t)) ->
out ppf "%a in %a in %a" term u ident x term t
| Rw_TermAsIdInTerm(u,(x,t)) ->
out ppf "%a as %a in %a" term u ident x term t
let assertion : p_assertion pp = fun ppf a ->
match a with
| P_assert_typing (t, a) -> out ppf "@[%a@ : %a@]" term t term a
| P_assert_conv (t, u) -> out ppf "@[%a@ ≡ %a@]" term t term u
let relation ppf s =
string ppf (match s with Exact -> " = " | Inside -> " > ")
let where elt ppf = function
| Spine x -> out ppf "spine%a" elt x
| Conclusion x -> out ppf "concl%a" elt x
| Hypothesis x -> out ppf "hyp%a" elt x
let f ppf x = string ppf x
let search_constr ppf = function
| QType x -> out ppf "%a" (Option.pp
(where (Option.pp ~none_case:(fun _ -> string ppf " >= ") relation))) x
| QXhs(i,None) -> out ppf "%a"
(Option.pp ~none_case:(fun _ -> string ppf " >= ") relation) i
| QXhs(i,Some Lhs) -> out ppf "%a"
(Option.pp ~none_case:(fun _ -> string ppf " >= ") relation) i
| QXhs(i,Some Rhs) -> out ppf "%a"
(Option.pp ~none_case:(fun _ -> string ppf " >= ") relation) i
let generalize ppf b = if b then string ppf " generalize"
let search_base ppf = function
| QName s -> out ppf "name = %s" s
| QSearch(t,g,None) -> out ppf "anywhere%a%a" generalize g term t
| QSearch(t,g,Some c) ->
out ppf "%a%a%a" search_constr c generalize g term t
let op ppf o = string ppf (match o with
Union -> " | "
| Intersect -> " with ")
let filter ppf f =
match f with
| Path s -> out ppf " in %s" s
| RegExp s -> out ppf " in \"%s\"" s
let rec search ppf = function
| QBase b -> search_base ppf b
| QOp(q1,o,q2) -> out ppf "%a%a%a" search q1 op o search q2
| QFilter(q,f) -> out ppf "%a%a" search q filter f
let print : print pp = fun ppf p ->
match p with
| Verbose -> out ppf " verbose"
| Debug -> out ppf " debug"
| Flag -> out ppf " flag"
| Prover -> out ppf " prover"
| Prover_timeout -> out ppf " prover_timeout"
| Goal -> ()
| Symbol qid -> out ppf " %a" qident qid
| Unif_rule -> out ppf " unif_rule"
| Coerce_rule -> out ppf " coerce_rule"
| String s -> out ppf " %s" (String.add_quotes s)
| Builtin -> out ppf " builtin"
| Notation -> out ppf " notation"
let query : p_query pp = fun ppf { elt; _ } ->
match elt with
| P_query_assert(true, a) -> out ppf "assertnot ⊢ %a" assertion a
| P_query_assert(false,a) -> out ppf "assert ⊢ %a" assertion a
| P_query_debug(_,"") -> out ppf "debug"
| P_query_debug(true ,s) -> out ppf "debug +%s" s
| P_query_debug(false,s) -> out ppf "debug -%s" s
| P_query_flag("", _) -> out ppf "flag"
| P_query_flag(s, b) ->
out ppf "flag \"%s\" %s" s (if b then "on" else "off")
| P_query_infer(t, _) -> out ppf "type %a" term t
| P_query_normalize(t, _) -> out ppf "compute %a" term t
| P_query_prover s -> out ppf "prover \"%s\"" s
| P_query_prover_timeout n -> out ppf "prover_timeout %s" n
| P_query_print p -> out ppf "print%a" print p
| P_query_proofterm -> out ppf "proofterm"
| P_query_verbose i -> out ppf "verbose %s" i
| P_query_search q -> out ppf "search %a" search q
let rec tactic : p_tactic pp = fun ppf { elt; _ } ->
match elt with
| P_tac_admit -> out ppf "admit"
| P_tac_and(t1,t2) -> out ppf "%a; %a" tactic t1 tactic t2
| P_tac_all_hyps t -> out ppf "all_hyps %a" term t
| P_tac_apply t -> out ppf "apply %a" term t
| P_tac_assume ids ->
out ppf "assume%a" (List.pp (unit " " |+ param_id) "") ids
| P_tac_assumption -> out ppf "assumption"
| P_tac_change t -> out ppf "change %a" term t
| P_tac_eval t -> out ppf "eval %a" term t
| P_tac_fail -> out ppf "fail"
| P_tac_first_hyp t -> out ppf "first_hyp %a" term t
| P_tac_focus n -> out ppf "focus %s" n
| P_tac_generalize id -> out ppf "generalize %a" ident id
| P_tac_have (id, t) -> out ppf "have %a: %a" ident id term t
| P_tac_induction -> out ppf "induction"
| P_tac_orelse(t1,t2) -> out ppf "orelse %a %a" tactic t1 tactic t2
| P_tac_query q -> query ppf q
| P_tac_refine t -> out ppf "refine %a" term t
| P_tac_refl -> out ppf "reflexivity"
| P_tac_remove ids ->
out ppf "remove%a" (List.pp (unit " " |+ ident) "") ids
| P_tac_repeat t -> out ppf "repeat %a" tactic t
| P_tac_rewrite(b,p,t) ->
let dir ppf b = if not b then out ppf " left" in
let pat ppf p = out ppf " .[%a]" rwpatt p in
out ppf "rewrite%a%a %a" dir b (Option.pp pat) p term t
| P_tac_set (id, t) -> out ppf "set %a ≔ %a" ident id term t
| P_tac_simpl SimpAll -> out ppf "simplify"
| P_tac_simpl SimpBetaOnly -> out ppf "simplify rule off"
| P_tac_simpl (SimpSym qid) -> out ppf "simplify %a" qident qid
| P_tac_solve -> out ppf "solve"
| P_tac_sym -> out ppf "symmetry"
| P_tac_try t -> out ppf "try %a" tactic t
| P_tac_why3 p ->
let prover ppf s = out ppf " \"%s\"" s in
out ppf "why3%a" (Option.pp prover) p
let rec subproof : p_subproof pp = fun ppf sp ->
out ppf "{@[<hv2>@ %a@ @]}" proofsteps sp
and subproofs : p_subproof list pp = fun ppf spl ->
out ppf "@[<hv>%a@]" (pp_print_list ~pp_sep:pp_print_space subproof) spl
and proofsteps : p_proofstep list pp = fun ppf psl ->
pp_print_list ~pp_sep:pp_print_space proofstep ppf psl
and proofstep : p_proofstep pp = fun ppf (Tactic (t, spl)) ->
out ppf "@[<hv2>%a@,%a;@]" tactic t subproofs spl
let proof : (p_proof * p_proof_end) pp = fun ppf (p, pe) ->
out ppf "begin@ @[<hv2>%a@]@ %a"
(fun ppf -> function
| [block] -> proofsteps ppf block
| blocks -> subproofs ppf blocks) p
proof_end pe
let command : p_command pp = fun ppf { elt; _ } ->
begin match elt with
| P_builtin (s, qid) -> out ppf "@[builtin \"%s\"@ ≔ %a@]" s qident qid
| P_inductive (_, _, _, []) -> assert false
| P_inductive (_, ms, xs, i :: il) ->
let with_ind ppf i = out ppf "@,%a" (inductive "with") i in
out ppf "@[<v>@[%a%a@]%a%a@]"
modifiers ms (List.pp params " ") xs
(inductive "inductive") i (List.pp with_ind "") il
| P_notation (qid, n) ->
out ppf "notation %a %a" qident qid (Print.notation string) n
| P_open(_,false,ps) -> out ppf "open %a" (List.pp path " ") ps
| P_open(_,true,ps) -> out ppf "private open %a" (List.pp path " ") ps
| P_query q -> query ppf q
| P_require (None, ps) -> out ppf "require %a" (List.pp path " ") ps
| P_require (Some false, ps) ->
out ppf "require open %a" (List.pp path " ") ps
| P_require (Some true, ps) ->
out ppf "require private open %a" (List.pp path " ") ps
| P_require_as (p,i) -> out ppf "@[require %a@ as %a@]" path p ident i
| P_rules [] -> assert false
| P_rules (r :: rs) ->
let with_rule ppf r = out ppf "@.%a" (rule "with") r in
rule "rule" ppf r; List.iter (with_rule ppf) rs
| P_symbol
{ p_sym_mod; p_sym_kw=_; p_sym_nam; p_sym_arg; p_sym_typ;
p_sym_trm; p_sym_prf; p_sym_def } ->
begin
out ppf "@[<v>@[<2>%asymbol %a%a%a%a%a@]%a@]"
modifiers p_sym_mod
ident p_sym_nam
params_list p_sym_arg
typ p_sym_typ
(pp_if p_sym_def (unit "@ ≔")) ()
(Option.pp (sep " " |+ term)) p_sym_trm
(Option.pp (unit "@," |+ proof)) p_sym_prf
end
| P_unif_rule ur -> out ppf "unif_rule %a" unif_rule ur
| P_coercion c -> out ppf "%a" (rule "coerce_rule") c
| P_opaque qid -> out ppf "opaque %a" qident qid
end;
out ppf ";"
let commands : p_commands pp = fun ppf ->
Stream.iter ((command +| unit "@.") ppf)