Skip to content

Commit caa8bff

Browse files
committed
Debug flag for variable status
1 parent dd2c97f commit caa8bff

13 files changed

Lines changed: 105 additions & 81 deletions

File tree

dev/top_printers.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ let ppelim_constraints cstrs = pp (Sorts.ElimConstraints.pr qprinter cstrs)
318318
let ppnamedcontextval e =
319319
let env = Global.env () in
320320
let sigma = Evd.from_env env in
321-
pp (pr_named_context env sigma (named_context_of_val e))
321+
pp (pr_named_context_of (Environ.reset_with_named_context e env) sigma)
322322

323323
let ppaucontext auctx =
324324
let {quals = qnas; univs = unas} = AbstractContext.names auctx in

doc/sphinx/proof-engine/vernacular-commands.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,12 @@ Controlling display
10871087
after each tactic. The information is used by the Prooftree tool in Proof
10881088
General. (https://askra.de/software/prooftree)
10891089

1090+
.. flag:: Printing Variables Status
1091+
1092+
This debug :term:`flag` prints whether each variable in the context
1093+
is a section variable or an hypothesis local to the current proof.
1094+
It is off by default.
1095+
10901096
.. extracted from Gallina extensions chapter
10911097
10921098
.. _printing_constructions_full:

engine/eConstr.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1216,8 +1216,15 @@ let match_named_context_val :
12161216
match unsafe_eq, unsafe_relevance_eq with
12171217
| Refl, Refl -> match_named_context_val
12181218

1219+
let fold_named_context_val :
1220+
(named_context_val -> var_status -> named_declaration -> 'a -> 'a) ->
1221+
named_context_val -> init:'a -> 'a =
1222+
let Refl, Refl = unsafe_eq, unsafe_relevance_eq in
1223+
Environ.fold_named_context_val
1224+
12191225
let fold_named_context :
1220-
(env -> var_status -> named_declaration -> 'a -> 'a) -> env -> init:'a -> 'a =
1226+
(env -> var_status -> named_declaration -> 'a -> 'a) ->
1227+
env -> init:'a -> 'a =
12211228
let Refl, Refl = unsafe_eq, unsafe_relevance_eq in
12221229
Environ.fold_named_context
12231230

engine/eConstr.mli

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,13 @@ val val_of_named_context : (var_status * named_declaration) list -> named_contex
428428
val named_context_of_val : named_context_val -> named_context
429429
val named_context_of_val_with_status : named_context_val -> (var_status * named_declaration) list
430430

431+
val fold_named_context_val :
432+
(named_context_val -> var_status -> named_declaration -> 'a -> 'a) ->
433+
named_context_val -> init:'a -> 'a
434+
431435
val fold_named_context :
432-
(env -> var_status -> named_declaration -> 'a -> 'a) -> env -> init:'a -> 'a
436+
(env -> var_status -> named_declaration -> 'a -> 'a) ->
437+
env -> init:'a -> 'a
433438

434439
val lookup_rel : int -> env -> rel_declaration
435440
val lookup_named : variable -> env -> named_declaration

ide/rocqide/idetop.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ let process_goal short sigma g =
207207
if short then [] else
208208
let hyps =
209209
List.rev_map process_hyp
210-
(Ppconstr.compact_named_context sigma (EConstr.named_context env))
210+
(Ppconstr.compact_named_context sigma (Environ.named_context_val env))
211211
in
212212
hyps
213213
in

kernel/environ.ml

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ let set_rel_context_val v env =
191191
(* Named context *)
192192
type var_status = SecVar | ProofVar
193193

194+
let var_status_eq a b = match a, b with
195+
| SecVar, SecVar -> true
196+
| ProofVar, ProofVar -> true
197+
| (SecVar | ProofVar), _ -> false
198+
194199
let push_named_context_val status d ctxt =
195200
let id = NamedDecl.get_id d in
196201
(* assert (not (Id.Map.mem id ctxt.env_named_map)); *)
@@ -517,15 +522,18 @@ let pop_rel_context n env =
517522
env_rel_context = skip n ctxt;
518523
env_nb_rel = env.env_nb_rel - n }
519524

520-
let fold_named_context f env ~init =
521-
let rec fold_right env =
522-
match match_named_context_val env.env_named_context with
525+
let fold_named_context_val f sign ~init =
526+
let rec fold_right sign =
527+
match match_named_context_val sign with
523528
| None -> init
524529
| Some (status, d, rem) ->
525-
let env =
526-
reset_with_named_context rem env in
527-
f env status d (fold_right env)
528-
in fold_right env
530+
f rem status d (fold_right rem)
531+
in fold_right sign
532+
533+
let fold_named_context f env ~init =
534+
fold_named_context_val (fun sign status d acc ->
535+
f (reset_with_named_context sign env) status d acc)
536+
(named_context_val env) ~init
529537

530538
let fold_named_context_reverse f ~init env =
531539
Context.Named.fold_inside f ~init:init (named_context env)

kernel/environ.mli

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ val fold_rel_context :
113113

114114
type var_status = SecVar | ProofVar
115115

116+
val var_status_eq : var_status -> var_status -> bool
117+
116118
val var_status_ctxt : ?check:bool -> Id.t -> named_context_val -> var_status
117119
val var_status : ?check:bool -> Id.t -> env -> var_status
118120

@@ -149,8 +151,13 @@ val named_body : variable -> env -> constr option
149151

150152
(** {6 Recurrence on [named_context]: older declarations processed first } *)
151153

154+
val fold_named_context_val :
155+
(named_context_val -> var_status -> Constr.named_declaration -> 'a -> 'a) ->
156+
named_context_val -> init:'a -> 'a
157+
152158
val fold_named_context :
153-
(env -> var_status -> Constr.named_declaration -> 'a -> 'a) -> env -> init:'a -> 'a
159+
(env -> var_status -> Constr.named_declaration -> 'a -> 'a) ->
160+
env -> init:'a -> 'a
154161

155162
val match_named_context_val : named_context_val -> (var_status * named_declaration * named_context_val) option
156163

printing/ppconstr.ml

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -858,39 +858,40 @@ let pr_binders ~flags env sigma l : Pp.t = pr_undelimited_binders ~flags spc tru
858858

859859
module CompactedDecl = struct
860860
type t =
861-
| LocalAssum of Id.t EConstr.binder_annot list * EConstr.types
862-
| LocalDef of Id.t EConstr.binder_annot list * EConstr.constr * EConstr.types
861+
| LocalAssum of Environ.var_status * Id.t EConstr.binder_annot list * EConstr.types
862+
| LocalDef of Environ.var_status * Id.t EConstr.binder_annot list * EConstr.constr * EConstr.types
863863

864-
let of_named_decl = function
864+
let of_named_decl status = function
865865
| Context.Named.Declaration.LocalAssum (id,t) ->
866-
LocalAssum ([id],t)
866+
LocalAssum (status, [id], t)
867867
| Context.Named.Declaration.LocalDef (id,v,t) ->
868-
LocalDef ([id],v,t)
868+
LocalDef (status, [id], v, t)
869869

870870
let to_tuple = function
871-
| LocalAssum (ids, t) -> ids, None, t
872-
| LocalDef (ids, b, t) -> ids, Some b, t
871+
| LocalAssum (_, ids, t) -> ids, None, t
872+
| LocalDef (_, ids, b, t) -> ids, Some b, t
873873
end
874874

875875
let compact_named_context sigma sign =
876876
let module NamedDecl = Context.Named.Declaration in
877-
let compact l decl =
877+
let compact l status decl =
878878
match decl, l with
879879
| NamedDecl.LocalAssum (i,t), [] ->
880-
[CompactedDecl.LocalAssum ([i],t)]
880+
[CompactedDecl.LocalAssum (status,[i],t)]
881881
| NamedDecl.LocalDef (i,c,t), [] ->
882-
[CompactedDecl.LocalDef ([i],c,t)]
883-
| NamedDecl.LocalAssum (i1,t1), CompactedDecl.LocalAssum (li,t2) :: q ->
884-
if EConstr.eq_constr sigma t1 t2
885-
then CompactedDecl.LocalAssum (i1::li, t2) :: q
886-
else CompactedDecl.LocalAssum ([i1],t1) :: CompactedDecl.LocalAssum (li,t2) :: q
887-
| NamedDecl.LocalDef (i1,c1,t1), CompactedDecl.LocalDef (li,c2,t2) :: q ->
888-
if EConstr.eq_constr sigma c1 c2 && EConstr.eq_constr sigma t1 t2
889-
then CompactedDecl.LocalDef (i1::li, c2, t2) :: q
890-
else CompactedDecl.LocalDef ([i1],c1,t1) :: CompactedDecl.LocalDef (li,c2,t2) :: q
882+
[CompactedDecl.LocalDef (status,[i],c,t)]
883+
| NamedDecl.LocalAssum (i1,t1), CompactedDecl.LocalAssum (status',li,t2) :: q ->
884+
if Environ.var_status_eq status status' && EConstr.eq_constr sigma t1 t2
885+
then CompactedDecl.LocalAssum (status, i1::li, t2) :: q
886+
else CompactedDecl.LocalAssum (status, [i1],t1) :: CompactedDecl.LocalAssum (status',li,t2) :: q
887+
| NamedDecl.LocalDef (i1,c1,t1), CompactedDecl.LocalDef (status',li,c2,t2) :: q ->
888+
if Environ.var_status_eq status status' &&
889+
EConstr.eq_constr sigma c1 c2 && EConstr.eq_constr sigma t1 t2
890+
then CompactedDecl.LocalDef (status, i1::li, c2, t2) :: q
891+
else CompactedDecl.LocalDef (status, [i1],c1,t1) :: CompactedDecl.LocalDef (status',li,c2,t2) :: q
891892
| NamedDecl.LocalAssum (i,t), q ->
892-
CompactedDecl.LocalAssum ([i],t) :: q
893+
CompactedDecl.LocalAssum (status,[i],t) :: q
893894
| NamedDecl.LocalDef (i,c,t), q ->
894-
CompactedDecl.LocalDef ([i],c,t) :: q
895+
CompactedDecl.LocalDef (status,[i],c,t) :: q
895896
in
896-
sign |> Context.Named.fold_inside compact ~init:[] |> List.rev
897+
EConstr.fold_named_context_val (fun _ status d acc -> compact acc status d) sign ~init:[]

printing/ppconstr.mli

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,15 @@ val modular_constr_pr :
9999

100100
module CompactedDecl : sig
101101
type t =
102-
| LocalAssum of Id.t EConstr.binder_annot list * EConstr.types
103-
| LocalDef of Id.t EConstr.binder_annot list * EConstr.constr * EConstr.types
102+
| LocalAssum of Environ.var_status * Id.t EConstr.binder_annot list * EConstr.types
103+
| LocalDef of Environ.var_status * Id.t EConstr.binder_annot list * EConstr.constr * EConstr.types
104104

105-
val of_named_decl : EConstr.named_declaration -> t
105+
val of_named_decl : Environ.var_status -> EConstr.named_declaration -> t
106106

107107
val to_tuple : t ->
108108
Id.t EConstr.binder_annot list *
109109
EConstr.constr option *
110110
EConstr.types
111111
end
112-
val compact_named_context : Evd.evar_map -> EConstr.named_context -> CompactedDecl.t list
112+
113+
val compact_named_context : Evd.evar_map -> Environ.named_context_val -> CompactedDecl.t list

printing/printer.ml

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -359,32 +359,40 @@ let pr_pattern t = pr_pattern_env (Global.env()) empty_names_context t*)
359359

360360
(* Flag for compact display of goals *)
361361

362-
let get_compact_context,set_compact_context =
363-
let compact_context = ref false in
364-
(fun () -> !compact_context),(fun b -> compact_context := b)
362+
let { Goptions.get = get_compact_context } =
363+
Goptions.declare_bool_option_and_ref ~key:["Printing";"Compact";"Contexts"] ~value:false ()
364+
365+
let { Goptions.get = print_var_status } =
366+
Goptions.declare_bool_option_and_ref ~key:["Printing";"Variables";"Status"] ~value:false ()
365367

366368
let pr_ecompacted_decl ?flags env sigma decl =
367-
let ids, pbody, typ = match decl with
368-
| CompactedDecl.LocalAssum (ids, typ) ->
369-
ids, None, typ
370-
| CompactedDecl.LocalDef (ids,c,typ) ->
371-
(* Force evaluation *)
372-
let pb = pr_leconstr_env ?flags ~inctx:true env sigma c in
373-
let pb = if EConstr.isCast sigma c then surround pb else pb in
374-
ids, Some pb, typ in
369+
let status, ids, pbody, typ = match decl with
370+
| CompactedDecl.LocalAssum (status, ids, typ) ->
371+
status, ids, None, typ
372+
| CompactedDecl.LocalDef (status, ids, c, typ) ->
373+
(* Force evaluation *)
374+
let pb = pr_leconstr_env ?flags ~inctx:true env sigma c in
375+
let pb = if EConstr.isCast sigma c then surround pb else pb in
376+
status, ids, Some pb, typ in
375377
let pids =
376378
hov 0 (prlist_with_sep pr_comma (fun id -> pr_id id.binder_name) ids) in
377379
let pt = pr_letype_env ?flags env sigma typ in
380+
let pstatus = if print_var_status() then
381+
match status with
382+
| SecVar -> spc() ++ pr_in_comment (str "section variable")
383+
| ProofVar -> spc() ++ pr_in_comment (str "hypothesis")
384+
else mt()
385+
in
378386
match pbody with
379-
| None -> hov 2 (pids ++ str" :" ++ spc () ++ pt)
387+
| None -> hov 2 (pids ++ str" :" ++ spc () ++ pt ++ pstatus)
380388
| Some pbody ->
381-
hov 2 (pids ++ str" :=" ++ spc () ++ pbody ++ spc () ++ str": " ++ pt)
389+
hov 2 (pids ++ str" :=" ++ spc () ++ pbody ++ spc () ++ str": " ++ pt ++ pstatus)
382390

383-
let pr_enamed_decl ?flags env sigma decl =
384-
decl |> CompactedDecl.of_named_decl |> pr_ecompacted_decl ?flags env sigma
391+
let pr_enamed_decl ?flags env sigma status decl =
392+
decl |> CompactedDecl.of_named_decl status |> pr_ecompacted_decl ?flags env sigma
385393

386-
let pr_named_decl ?flags env sigma (decl:Constr.named_declaration) =
387-
pr_enamed_decl ?flags env sigma (EConstr.of_named_decl decl)
394+
let pr_named_decl ?flags env sigma status (decl:Constr.named_declaration) =
395+
pr_enamed_decl ?flags env sigma status (EConstr.of_named_decl decl)
388396

389397
let pr_rel_decl ?flags env sigma decl =
390398
let na = RelDecl.get_name decl in
@@ -413,18 +421,13 @@ let pr_erel_decl ?flags env sigma (decl:EConstr.rel_declaration) =
413421

414422
(* Prints a signature, all declarations on the same line if possible *)
415423
let pr_named_context_of ?flags env sigma =
416-
let make_decl_list env _status d pps = pr_named_decl ?flags env sigma d :: pps in
424+
let make_decl_list env status d pps = pr_named_decl ?flags env sigma status d :: pps in
417425
let psl = List.rev (fold_named_context make_decl_list env ~init:[]) in
418426
hv 0 (prlist_with_sep (fun _ -> ws 2) (fun x -> x) psl)
419427

420428
let pr_var_list_decl ?flags env sigma decl =
421429
hov 0 (pr_ecompacted_decl ?flags env sigma decl)
422430

423-
let pr_named_context ?flags env sigma ne_context =
424-
hv 0 (Context.Named.fold_outside
425-
(fun d pps -> pps ++ ws 2 ++ pr_named_decl ?flags env sigma d)
426-
ne_context ~init:(mt ()))
427-
428431
let pr_rel_context ?(flags=current_combined()) env sigma rel_context =
429432
let ppflags = Ppconstr.of_printing_flags flags in
430433
let rel_context = EConstr.of_rel_context rel_context in
@@ -440,7 +443,7 @@ let pr_context_unlimited ?flags env sigma =
440443
(fun d pps ->
441444
let pidt = pr_ecompacted_decl ?flags env sigma d in
442445
(pps ++ fnl () ++ pidt))
443-
(compact_named_context sigma (EConstr.named_context env)) (mt ())
446+
(compact_named_context sigma (Environ.named_context_val env)) (mt ())
444447
in
445448
let db_env =
446449
fold_rel_context
@@ -469,7 +472,7 @@ let should_compact env sigma typ =
469472
let rec bld_sign_env ?flags env sigma ctxt pps =
470473
match ctxt with
471474
| [] -> pps
472-
| CompactedDecl.LocalAssum (ids,typ)::ctxt' when should_compact env sigma typ ->
475+
| CompactedDecl.LocalAssum (_,ids,typ)::ctxt' when should_compact env sigma typ ->
473476
let pps',ctxt' = bld_sign_env_id ?flags env sigma ctxt (mt ()) true in
474477
(* putting simple hyps in a more horizontal flavor *)
475478
bld_sign_env ?flags env sigma ctxt' (pps ++ brk (0,0) ++ hov 0 pps')
@@ -480,7 +483,7 @@ let rec bld_sign_env ?flags env sigma ctxt pps =
480483
and bld_sign_env_id ?flags env sigma ctxt pps is_start =
481484
match ctxt with
482485
| [] -> pps,ctxt
483-
| CompactedDecl.LocalAssum(ids,typ) as d :: ctxt' when should_compact env sigma typ ->
486+
| CompactedDecl.LocalAssum(_,ids,typ) as d :: ctxt' when should_compact env sigma typ ->
484487
let pidt = pr_var_list_decl ?flags env sigma d in
485488
let pps' = pps ++ (if not is_start then brk (3,0) else (mt ())) ++ pidt in
486489
bld_sign_env_id ?flags env sigma ctxt' pps' false
@@ -490,7 +493,7 @@ and bld_sign_env_id ?flags env sigma ctxt pps is_start =
490493
(* compact printing an env (variables and de Bruijn). Separator: three
491494
spaces between simple hyps, and newline otherwise *)
492495
let pr_context_limit_compact ?n ?flags env sigma =
493-
let ctxt = EConstr.named_context env in
496+
let ctxt = Environ.named_context_val env in
494497
let ctxt = compact_named_context sigma ctxt in
495498
let lgth = List.length ctxt in
496499
let n_capped =

0 commit comments

Comments
 (0)