Source file prettyp.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
open Pp
open CErrors
open Util
open CAst
open Names
open Termops
open Declarations
open Environ
open Impargs
open Libobject
open Libnames
open Globnames
open Printer
open Context.Rel.Declaration
module RelDecl = Context.Rel.Declaration
module NamedDecl = Context.Named.Declaration
let print_module mp = Printmod.print_module ~with_body:true mp
let print_modtype = Printmod.print_modtype
(** Utilities *)
let print_closed_sections = ref false
let pr_infos_list l = v 0 (prlist_with_sep cut (fun x -> x) l)
let with_line_skip l = if List.is_empty l then mt() else fnl() ++ fnl () ++ pr_infos_list l
let blankline = mt()
let add_colon prefix = if ismt prefix then mt () else prefix ++ str ": "
let int_or_no n = if Int.equal n 0 then str "no" else int n
(** Basic printing *)
let print_basename cst = pr_global (GlobRef.ConstRef cst)
let print_ref env reduce ref udecl =
let typ, univs = Typeops.type_of_global_in_context env ref in
let inst = UVars.make_abstract_instance univs in
let bl = Printer.universe_binders_with_opt_names (Environ.universes_of_global env ref) udecl in
let sigma = Evd.from_ctx (UState.of_names bl) in
let typ =
if reduce then
let ctx,ccl = Reductionops.whd_decompose_prod_decls env sigma (EConstr.of_constr typ)
in EConstr.to_constr sigma (EConstr.it_mkProd_or_LetIn ccl ctx)
else typ in
let typ = Arguments_renaming.rename_type typ ref in
let impargs = select_stronger_impargs (implicits_of_global ref) in
let impargs = List.map binding_kind_of_status impargs in
let variance = let open GlobRef in match ref with
| VarRef _ | ConstRef _ -> None
| IndRef (ind,_) | ConstructRef ((ind,_),_) ->
let mind = Environ.lookup_mind ind env in
mind.Declarations.mind_variance
in
let inst =
if Environ.is_polymorphic env ref
then Printer.pr_universe_instance sigma inst
else mt ()
in
let priv = None in
hov 0 (pr_global ref ++ inst ++ str " :" ++ spc () ++ pr_ltype_env env sigma ~impargs typ ++
Printer.pr_abstract_universe_ctx sigma ?variance univs ?priv)
(** Command [Print Implicit], somehow subsumed by [About] *)
let pr_impl_name imp = Id.print (name_of_implicit imp)
let print_impargs_by_name max = function
| [] -> []
| impls ->
let n = List.length impls in
[hov 0 (str (String.plural n "Argument") ++ spc() ++
prlist_with_sep pr_comma pr_impl_name impls ++ spc() ++
str (String.conjugate_verb_to_be n) ++ str" implicit" ++
(if max then strbrk " and maximally inserted" else mt()))]
let print_one_impargs_list l =
let imps = List.filter is_status_implicit l in
let maximps = List.filter Impargs.maximal_insertion_of imps in
let nonmaximps = List.subtract (=) imps maximps in
print_impargs_by_name false nonmaximps @
print_impargs_by_name true maximps
let print_impargs_list prefix l =
let l = extract_impargs_data l in
List.flatten (List.map (fun (cond,imps) ->
match cond with
| None ->
List.map (fun pp -> add_colon prefix ++ pp)
(print_one_impargs_list imps)
| Some (n1,n2) ->
[v 2 (prlist_with_sep cut (fun x -> x)
[(if ismt prefix then str "When" else prefix ++ str ", when") ++
str " applied to " ++
(if Int.equal n1 n2 then int_or_no n2 else
if Int.equal n1 0 then str "no more than " ++ int n2
else int n1 ++ str " to " ++ int_or_no n2) ++
str (String.plural n2 " argument") ++ str ":";
v 0 (prlist_with_sep cut (fun x -> x)
(if List.exists is_status_implicit imps
then print_one_impargs_list imps
else [str "No implicit arguments"]))])]) l)
let need_expansion env impl ref =
let typ, _ = Typeops.type_of_global_in_context env ref in
let ctx = Term.prod_decls typ in
let nprods = List.count is_local_assum ctx in
not (List.is_empty impl) && List.length impl >= nprods &&
let _,lastimpl = List.chop nprods impl in
List.exists is_status_implicit lastimpl
let print_impargs env ref =
let impl = implicits_of_global ref in
let has_impl = not (List.is_empty impl) in
pr_infos_list
([ print_ref env (need_expansion env (select_impargs_size 0 impl) ref) ref None;
blankline ] @
(if has_impl then print_impargs_list (mt()) impl
else [str "No implicit arguments"]))
(** Printing reduction behavior *)
let print_reduction_behaviour = function
| GlobRef.ConstRef ref -> let p = Reductionops.ReductionBehaviour.print ref in if Pp.ismt p then [] else [p]
| _ -> []
(** Printing opacity status *)
type opacity =
| FullyOpaque
| TransparentMaybeOpacified of Conv_oracle.level
let opacity env =
function
| GlobRef.VarRef v when NamedDecl.is_local_def (Environ.lookup_named v env) ->
Some(TransparentMaybeOpacified
(Conv_oracle.get_strategy (Environ.oracle env) (Conv_oracle.EvalVarRef v)))
| GlobRef.ConstRef cst ->
let cb = Environ.lookup_constant cst env in
(match cb.const_body with
| Undef _ | Primitive _ | Symbol _ -> None
| OpaqueDef _ -> Some FullyOpaque
| Def _ -> Some
(TransparentMaybeOpacified
(Conv_oracle.get_strategy (Environ.oracle env) (Conv_oracle.EvalConstRef cst))))
| _ -> None
let print_opacity env ref =
match opacity env ref with
| None -> []
| Some s ->
[pr_global ref ++ str " is " ++
match s with
| FullyOpaque -> str "opaque"
| TransparentMaybeOpacified Conv_oracle.Opaque ->
str "basically transparent but considered opaque for reduction"
| TransparentMaybeOpacified lev when Conv_oracle.is_transparent lev ->
str "transparent"
| TransparentMaybeOpacified (Conv_oracle.Level n) ->
str "transparent (with expansion weight " ++ int n ++ str ")"
| TransparentMaybeOpacified Conv_oracle.Expand ->
str "transparent (with minimal expansion weight)"]
(** Printing coercion status *)
let print_if_is_coercion ref =
if Coercionops.coercion_exists ref then
let i = Coercionops.coercion_info ref in
let r = if i.Coercionops.coe_reversible then " reversible" else "" in
[pr_global ref ++ str " is a" ++ str r ++ str " coercion"]
else []
(** Printing polymorphic status *)
let pr_template_variables = function
| [] -> mt ()
| vars -> str " on " ++ prlist_with_sep spc UnivNames.pr_level_with_global_universes vars
let print_polymorphism env ref =
let poly = Environ.is_polymorphic env ref in
let template_poly = Environ.is_template_polymorphic env ref in
let template_variables = Environ.get_template_polymorphic_variables env ref in
[ pr_global ref ++ str " is " ++
(if poly then str "universe polymorphic"
else if template_poly then
str "template universe polymorphic"
++ if !Detyping.print_universes then h (pr_template_variables template_variables) else mt()
else str "not universe polymorphic") ]
let print_prop_but_default_dep_elim ref =
match ref with
| GlobRef.IndRef ind ->
if Indrec.is_prop_but_default_dependent_elim ind
then [pr_global ref ++ str " is in Prop but its eliminators are declared dependent by default"]
else []
| _ -> []
(** Print projection status *)
let print_projection env ref =
match ref with
| GlobRef.ConstRef cst ->
begin
match Structures.PrimitiveProjections.find_opt cst with
| Some p -> [pr_global ref ++ str " is a primitive projection of " ++ pr_global (IndRef (Projection.Repr.inductive p))]
| None ->
try
let ind = (Structures.Structure.find_from_projection cst).name in
[pr_global ref ++ str " is a projection of " ++ pr_global (IndRef ind)]
with Not_found -> []
end
| _ -> []
(** Printing type-in-type status *)
let print_type_in_type env ref =
let unsafe = Environ.is_type_in_type env ref in
if unsafe then
[ pr_global ref ++ str " relies on an unsafe universe hierarchy"]
else []
(** Printing primitive projection status *)
let print_primitive_record recflag mipv = function
| PrimRecord _ ->
let eta = match recflag with
| CoFinite | Finite -> str" without eta conversion"
| BiFinite -> str " with eta conversion"
in
[Id.print mipv.(0).mind_typename ++ str" has primitive projections" ++ eta ++ str"."]
| FakeRecord | NotRecord -> []
let print_primitive env ref =
match ref with
| GlobRef.IndRef ind ->
let mib = Environ.lookup_mind (fst ind) env in
print_primitive_record mib.mind_finite mib.mind_packets mib.mind_record
| _ -> []
(** Printing arguments status (scopes, implicit, names) *)
let env ref scopes =
let open Constr in
let rec aux env t = function
| [] -> false
| _::scopes -> match kind (Reduction.whd_all env t) with
| Prod (na,dom,codom) -> aux (push_rel (RelDecl.LocalAssum (na,dom)) env) codom scopes
| _ -> true
in
let ty, _ctx = Typeops.type_of_global_in_context env ref in
aux env ty scopes
let implicit_kind_of_status = function
| None -> Anonymous, Glob_term.Explicit
| Some imp -> pi1 imp.impl_pos, if imp.impl_max then Glob_term.MaxImplicit else Glob_term.NonMaxImplicit
let imp =
let _,imp = implicit_kind_of_status imp in
(Anonymous, imp)
let dummy = {
Vernacexpr.implicit_status = Glob_term.Explicit;
name = Anonymous;
recarg_like = false;
notation_scope = [];
}
let is_dummy = function
| Vernacexpr.(RealArg {implicit_status; name; recarg_like; notation_scope}) ->
name = Anonymous && not recarg_like && notation_scope = [] && implicit_status = Glob_term.Explicit
| _ -> false
let rec main_implicits i renames recargs scopes impls =
if renames = [] && recargs = [] && scopes = [] && impls = [] then []
else
let recarg_like, recargs = match recargs with
| j :: recargs when i = j -> true, recargs
| _ -> false, recargs
in
let (name, implicit_status) =
match renames, impls with
| _, (Some _ as i) :: _ -> implicit_kind_of_status i
| name::_, _ -> (name,Glob_term.Explicit)
| [], (None::_ | []) -> (Anonymous, Glob_term.Explicit)
in
let notation_scope = match scopes with
| scope :: _ -> List.map (fun s -> CAst.make (Constrexpr.DelimUnboundedScope, s)) scope
| [] -> []
in
let status = {Vernacexpr.implicit_status; name; recarg_like; notation_scope} in
let tl = function [] -> [] | _::tl -> tl in
let rest = main_implicits (i+1) (tl renames) recargs (tl scopes) (tl impls) in
status :: rest
let rec insert_fake_args volatile bidi impls =
let open Vernacexpr in
match volatile, bidi with
| Some 0, _ -> VolatileArg :: insert_fake_args None bidi impls
| _, Some 0 -> BidiArg :: insert_fake_args volatile None impls
| None, None -> List.map (fun a -> RealArg a) impls
| _, _ ->
let hd, tl = match impls with
| impl :: impls -> impl, impls
| [] -> dummy, []
in
let f = Option.map pred in
RealArg hd :: insert_fake_args (f volatile) (f bidi) tl
let print_arguments env ref =
let qid = Nametab.shortest_qualid_of_global Id.Set.empty ref in
let flags, recargs, nargs_for_red =
match ref with
| ConstRef ref ->
begin match Reductionops.ReductionBehaviour.get ref with
| None -> [], [], None
| Some NeverUnfold -> [`ReductionNeverUnfold], [], None
| Some (UnfoldWhen { nargs; recargs }) -> [], recargs, nargs
| Some (UnfoldWhenNoMatch { nargs; recargs }) -> [`ReductionDontExposeCase], recargs, nargs
end
| _ -> [], [], None
in
let names, not_renamed =
try Arguments_renaming.arguments_names ref, false
with Not_found ->
let ty, _ = Typeops.type_of_global_in_context env ref in
List.map pi1 (Impargs.compute_implicits_names env (Evd.from_env env) (EConstr.of_constr ty)), true in
let scopes = Notation.find_arguments_scope ref in
let flags = if needs_extra_scopes env ref scopes then `ExtraScopes::flags else flags in
let impls = Impargs.extract_impargs_data (Impargs.implicits_of_global ref) in
let impls, moreimpls = match impls with
| (_, impls) :: rest -> impls, rest
| [] -> assert false
in
let impls = main_implicits 0 names recargs scopes impls in
let moreimpls = List.map (fun (_,i) -> List.map extra_implicit_kind_of_status i) moreimpls in
let bidi = Pretyping.get_bidirectionality_hint ref in
let impls = insert_fake_args nargs_for_red bidi impls in
if List.for_all is_dummy impls && moreimpls = [] && flags = [] then []
else
let open Constrexpr in
let open Vernacexpr in
[Ppvernac.pr_vernac_expr
(VernacSynPure (VernacArguments (CAst.make (AN qid), impls, moreimpls, flags))) ++
(if not_renamed then mt () else
fnl () ++ str " (where some original arguments have been renamed)")]
(** Printing dependencies in section variables *)
let print_section_deps env ref =
let hyps = let open GlobRef in match ref with
| VarRef _ -> None
| ConstRef c ->
let bd = Environ.lookup_constant c env in
Some bd.const_hyps
| IndRef (mind,_) | ConstructRef ((mind,_),_) ->
let mb = Environ.lookup_mind mind env in
Some mb.mind_hyps
in
let hyps = Option.map (List.filter NamedDecl.is_local_assum) hyps in
match hyps with
| None | Some [] -> []
| Some hyps ->
[hov 0 (pr_global ref ++ str (String.plural (List.length hyps) " uses section variable") ++ spc () ++
hv 1 (prlist_with_sep spc (fun d -> Id.print (NamedDecl.get_id d)) (List.rev hyps)) ++ str ".")]
(** Printing bidirectionality status *)
let print_bidi_hints gr =
match Pretyping.get_bidirectionality_hint gr with
| None -> []
| Some nargs ->
[str "Using typing information from context after typing the " ++ int nargs ++ str " first arguments"]
(** Printing basic information about references (common to Print and About) *)
let print_name_infos env ref =
let type_info_for_implicit =
if need_expansion env (select_impargs_size 0 (implicits_of_global ref)) ref then
[str "Expanded type for implicit arguments";
print_ref env true ref None; blankline]
else
[] in
print_type_in_type env ref @
print_prop_but_default_dep_elim ref @
print_projection env ref @
print_primitive env ref @
type_info_for_implicit @
print_arguments env ref @
print_section_deps env ref @
print_if_is_coercion ref
let print_typed_value_in_env env sigma (trm,typ) =
(pr_leconstr_env ~inctx:true env sigma trm ++ fnl () ++
str " : " ++ pr_letype_env env sigma typ)
let print_named_def env sigma ~impargs name body typ =
let pbody = pr_lconstr_env ~inctx:true env sigma body in
let ptyp = pr_ltype_env env sigma ~impargs typ in
let pbody = if Constr.isCast body then surround pbody else pbody in
(str "*** [" ++ str name ++ str " " ++
hov 0 (str ":=" ++ brk (1,2) ++ pbody ++ spc () ++
str ":" ++ brk (1,2) ++ ptyp) ++
str "]")
let print_named_assum env sigma ~impargs name typ =
str "*** [" ++ str name ++ str " : " ++ pr_ltype_env env sigma ~impargs typ ++ str "]"
let print_named_decl env sigma with_implicit id =
let open Context.Named.Declaration in
let impargs = if with_implicit then select_stronger_impargs (implicits_of_global (VarRef id)) else [] in
let impargs = List.map binding_kind_of_status impargs in
match lookup_named id env with
| LocalAssum (id, typ) ->
print_named_assum env sigma ~impargs (Id.to_string id.Context.binder_name) typ
| LocalDef (id, body, typ) ->
print_named_def env sigma ~impargs (Id.to_string id.Context.binder_name) body typ
let assumptions_for_print lna =
List.fold_right (fun na env -> add_name na env) lna empty_names_context
let print_inductive_args env mind mib =
let flatmapi f v = List.flatten (Array.to_list (Array.mapi f v)) in
let mips =
try
Array.mapi (fun i mip ->
let projs = Option.List.flatten (Structures.Structure.find_projections (mind,i)) in
(mip.mind_consnames, Array.of_list projs)) mib.mind_packets
with
Not_found ->
Array.map (fun mip -> (mip.mind_consnames,[||])) mib.mind_packets in
flatmapi
(fun i (constructs, projs) ->
print_arguments env (GlobRef.IndRef (mind,i)) @
flatmapi (fun j _ -> print_arguments env (GlobRef.ConstructRef ((mind,i),j+1))) constructs @
flatmapi (fun _ cst -> print_arguments env (GlobRef.ConstRef cst)) projs)
mips
let print_inductive env mind udecl =
let mib = Environ.lookup_mind mind env in
Printmod.pr_mutual_inductive_body env mind mib udecl
let print_inductive_with_infos env mind udecl =
let mib = Environ.lookup_mind mind env in
let mipv = mib.mind_packets in
Printmod.pr_mutual_inductive_body env mind mib udecl ++
with_line_skip
(print_primitive_record mib.mind_finite mipv mib.mind_record @
print_inductive_args env mind mib)
let print_section_variable_with_infos env sigma id =
print_named_decl env sigma true id ++
with_line_skip (print_name_infos env (GlobRef.VarRef id))
let print_typed_body env evd ~impargs (val_0,typ) =
(pr_lconstr_env ~inctx:true env evd val_0 ++ fnl () ++ str " : " ++ pr_ltype_env env evd ~impargs typ)
let print_instance sigma cb =
if Declareops.constant_is_polymorphic cb then
let univs = Declareops.constant_polymorphic_context cb in
let inst = UVars.make_abstract_instance univs in
pr_universe_instance sigma inst
else mt()
let print_constant env ~with_values with_implicit cst udecl =
let cb = Environ.lookup_constant cst env in
let typ = cb.const_type in
let univs = cb.const_universes in
let uctx =
UState.of_names
(Printer.universe_binders_with_opt_names (Declareops.constant_polymorphic_context cb) udecl)
in
let val_0 = match cb.const_body with
| Undef _ | Symbol _ | Primitive _ -> None
| Def c -> Some ((if Option.has_some with_values then Some c else None), None)
| OpaqueDef o ->
match with_values with
| None -> Some (None, None)
| Some access ->
let c, priv = Global.force_proof access o in
let priv = match priv with
| PrivateMonomorphic () -> None
| PrivatePolymorphic priv -> Some priv
in
Some (Some c, priv)
in
let sigma = Evd.from_ctx uctx in
let impargs = if with_implicit then select_stronger_impargs (implicits_of_global (ConstRef cst)) else [] in
let impargs = List.map binding_kind_of_status impargs in
let pr_ltype = pr_ltype_env env sigma in
hov 0 (
match val_0 with
| None ->
str"*** [ " ++
print_basename cst ++ print_instance sigma cb ++ str " :" ++ spc () ++ pr_ltype ~impargs typ ++
str" ]" ++
Printer.pr_universes sigma univs
| Some (optbody, priv) ->
print_basename cst ++ print_instance sigma cb ++
str (if Option.has_some optbody then " =" else " :") ++ spc() ++
(match optbody with Some c-> print_typed_body env sigma ~impargs (c,typ) | None -> pr_ltype ~impargs typ)++
Printer.pr_universes sigma univs ?priv)
let print_constant_with_infos env access cst udecl =
print_constant env ~with_values:(Some access) true cst udecl ++
with_line_skip (print_name_infos env (GlobRef.ConstRef cst))
let print_global_reference access env sigma gref udecl =
let open GlobRef in
match gref with
| ConstRef cst ->
(match Structures.PrimitiveProjections.find_opt cst with
| Some p -> print_inductive_with_infos env (fst (Projection.Repr.inductive p)) udecl
| None -> print_constant_with_infos env access cst udecl)
| IndRef (mind,_) -> print_inductive_with_infos env mind udecl
| ConstructRef ((mind,_),_) -> print_inductive_with_infos env mind udecl
| VarRef id -> print_section_variable_with_infos env sigma id
let glob_constr_of_abbreviation kn =
let (vars,a) = Abbreviation.search_abbreviation kn in
(List.map fst vars, Notation_ops.glob_constr_of_notation_constr a)
let print_abbreviation_body env kn (vars,c) =
let qid = Nametab.shortest_qualid_of_abbreviation Id.Set.empty kn in
hov 2
(hov 4
(str "Notation " ++ pr_qualid qid ++
prlist (fun id -> spc () ++ Id.print id) vars ++
spc () ++ str ":=") ++
spc () ++
Vernacstate.System.protect (fun () ->
Abbreviation.toggle_abbreviation ~on:false ~use:ParsingAndPrinting kn;
pr_glob_constr_env env (Evd.from_env env) c) ())
let print_abbreviation access env sigma kn =
let (vars,c) = glob_constr_of_abbreviation kn in
let pp = match DAst.get c with
| GRef (gref,_udecl) -> [print_global_reference access env sigma gref None]
| _ -> [] in
print_abbreviation_body env kn (vars,c) ++
with_line_skip pp
(** Unused outside? *)
let pr_prefix_name prefix = Id.print (snd (split_dirpath prefix.Nametab.obj_dir))
let print_library_node = function
| Lib.OpenedSection (prefix, _) ->
str " >>>>>>> Section " ++ pr_prefix_name prefix
| Lib.OpenedModule (_,_,prefix,_) ->
str " >>>>>>> Module " ++ pr_prefix_name prefix
| Lib.CompilingLibrary { Nametab.obj_dir; _ } ->
str " >>>>>>> Library " ++ DirPath.print obj_dir
(** Printing part of command [Check] *)
let print_judgment env sigma {uj_val=trm;uj_type=typ} =
print_typed_value_in_env env sigma (trm, typ)
let print_safe_judgment {Safe_typing.jdg_env=senv; jdg_val=trm; jdg_type=typ} =
let env = Safe_typing.env_of_safe_env senv in
let sigma = Evd.from_env env in
let trm = EConstr.of_constr trm in
let typ = EConstr.of_constr typ in
print_typed_value_in_env env sigma (trm, typ)
(** Command [Print All] *)
module DynHandle = Libobject.Dyn.Map(struct type 'a t = 'a -> Pp.t option end)
let handle h (Libobject.Dyn.Dyn (tag, o)) = match DynHandle.find tag h with
| f -> f o
| exception Not_found -> None
let print_library_leaf env sigma ~with_values mp lobj =
match lobj with
| AtomicObject o ->
let handler =
DynHandle.add Declare.Internal.objVariable begin fun id ->
(try Some(print_named_decl env sigma false id) with Not_found -> None)
end @@
DynHandle.add Declare.Internal.Constant.tag begin fun (id,_) ->
let kn = Constant.make2 mp (Label.of_id id) in
Some (print_constant env ~with_values false kn None)
end @@
DynHandle.add DeclareInd.Internal.objInductive begin fun (id,_) ->
let kn = MutInd.make2 mp (Label.of_id id) in
Some (print_inductive env kn None)
end @@
DynHandle.empty
in
handle handler o
| ModuleObject (id,_) ->
Some (Printmod.print_module ~with_body:(Option.has_some with_values) (MPdot (mp,Label.of_id id)))
| ModuleTypeObject (id,_) ->
Some (print_modtype (MPdot (mp, Label.of_id id)))
| IncludeObject _ | KeepObject _ | ExportObject _ -> None
let decr = Option.map ((+) (-1))
let is_done = Option.equal Int.equal (Some 0)
let print_leaves env sigma ~with_values mp n leaves =
let rec prec n = function
| [] -> n, []
| o :: rest ->
if is_done n then n, []
else begin match print_library_leaf env sigma ~with_values mp o with
| Some pp ->
let n, prest = prec (decr n) rest in
n, pp :: prest
| None -> prec n rest
end
in
let n, l = prec n leaves in
n, v 0 (pr_sequence (fun x -> x) (List.rev l))
let print_context env sigma ~with_values =
let rec prec n = function
| [] -> mt()
| (node, leaves) :: rest ->
if is_done n then mt()
else
let mp = (Lib.node_prefix node).Nametab.obj_mp in
let n, pleaves = print_leaves env sigma ~with_values mp n leaves in
if is_done n then pleaves
else prec n rest ++ pleaves
in
prec
let print_full_context access env sigma =
print_context env sigma ~with_values:(Some access) None (Lib.contents ())
let print_full_context_typ env sigma =
print_context env sigma ~with_values:None None (Lib.contents ())
(** Command line [-output-context] *)
module DynHandleF = Libobject.Dyn.Map(struct type 'a t = 'a -> Pp.t end)
let handleF h (Libobject.Dyn.Dyn (tag, o)) = match DynHandleF.find tag h with
| f -> f o
| exception Not_found -> mt ()
let print_full_pure_atomic access env sigma mp lobj =
let handler =
DynHandleF.add Declare.Internal.Constant.tag begin fun (id,_) ->
let kn = KerName.make mp (Label.of_id id) in
let con = Global.constant_of_delta_kn kn in
let cb = Global.lookup_constant con in
let typ = cb.const_type in
hov 0 (
match cb.const_body with
| Undef _ ->
str "Parameter " ++
print_basename con ++ str " :" ++ spc () ++ pr_ltype_env env sigma typ
| OpaqueDef lc ->
str "Theorem " ++ print_basename con ++ cut () ++
str " : " ++ pr_ltype_env env sigma typ ++ str "." ++ fnl () ++
str "Proof " ++ pr_lconstr_env env sigma
(fst (Global.force_proof access lc))
| Def c ->
str "Definition " ++ print_basename con ++ cut () ++
str " : " ++ pr_ltype_env env sigma typ ++ cut () ++ str " := " ++
pr_lconstr_env env sigma c
| Primitive _ ->
str "Primitive " ++
print_basename con ++ str " :" ++ spc () ++ pr_ltype_env env sigma typ
| Symbol _ ->
str "Symbol " ++
print_basename con ++ str " :" ++ spc () ++ pr_ltype_env env sigma typ)
++ str "." ++ fnl () ++ fnl ()
end @@
DynHandleF.add DeclareInd.Internal.objInductive begin fun (id,_) ->
let kn = KerName.make mp (Label.of_id id) in
let mind = Global.mind_of_delta_kn kn in
let mib = Global.lookup_mind mind in
Printmod.pr_mutual_inductive_body (Global.env()) mind mib None ++
str "." ++ fnl () ++ fnl ()
end @@
DynHandleF.empty
in
handleF handler lobj
let print_full_pure_leaf access env sigma mp = function
| AtomicObject lobj -> print_full_pure_atomic access env sigma mp lobj
| ModuleObject (id, _) ->
print_module (MPdot (mp,Label.of_id id)) ++ str "." ++ fnl () ++ fnl ()
| ModuleTypeObject (id, _) ->
print_modtype (MPdot (mp,Label.of_id id)) ++ str "." ++ fnl () ++ fnl ()
| _ -> mt()
let print_full_pure_context access env sigma =
let rec prec = function
| (node,leaves)::rest ->
let mp = (Lib.node_prefix node).Nametab.obj_mp in
let pp = Pp.prlist (print_full_pure_leaf access env sigma mp) leaves in
prec rest ++ pp
| [] -> mt ()
in
prec (Lib.contents ())
(** Command [Print Section] *)
let read_sec_context qid =
let dir =
try Nametab.locate_section qid
with Not_found ->
user_err ?loc:qid.loc (str "Unknown section.") in
let rec get_cxt in_cxt = function
| (Lib.OpenedSection ({Nametab.obj_dir;_},_), _ as hd)::rest ->
if DirPath.equal dir obj_dir then (hd::in_cxt) else get_cxt (hd::in_cxt) rest
| [] -> []
| hd::rest -> get_cxt (hd::in_cxt) rest
in
let cxt = Lib.contents () in
List.rev (get_cxt [] cxt)
let print_sec_context access env sigma sec =
print_context env sigma ~with_values:(Some access) None (read_sec_context sec)
let print_sec_context_typ env sigma sec =
print_context env sigma ~with_values:None None (read_sec_context sec)
(** Command [Print] *)
type 'a locatable_info = {
locate : qualid -> 'a option;
locate_all : qualid -> 'a list;
shortest_qualid : 'a -> qualid;
name : 'a -> Pp.t;
print : 'a -> Pp.t;
about : 'a -> Pp.t;
}
type logical_name =
| Term of GlobRef.t
| Dir of Nametab.GlobDirRef.t
| Abbreviation of abbreviation
| Module of ModPath.t
| ModuleType of ModPath.t
| Other : 'a * 'a locatable_info -> logical_name
| Undefined of qualid
type locatable = Locatable : 'a locatable_info -> locatable
(** Generic table for objects that are accessible through a name. *)
let locatable_map : locatable String.Map.t ref = ref String.Map.empty
let register_locatable name f =
locatable_map := String.Map.add name (Locatable f) !locatable_map
exception ObjFound of logical_name
let locate_any_name qid =
try Term (Nametab.locate qid)
with Not_found ->
try Abbreviation (Nametab.locate_abbreviation qid)
with Not_found ->
try Dir (Nametab.locate_dir qid)
with Not_found ->
try Module (Nametab.locate_module qid)
with Not_found ->
try ModuleType (Nametab.locate_modtype qid)
with Not_found ->
let iter _ (Locatable info) = match info.locate qid with
| None -> ()
| Some ans -> raise (ObjFound (Other (ans, info)))
in
try String.Map.iter iter !locatable_map; Undefined qid
with ObjFound obj -> obj
let canonical_info env ref =
let cref = QGlobRef.canonize env ref in
if GlobRef.UserOrd.equal ref cref then mt ()
else match Nametab.path_of_global cref with
| path -> spc() ++ str "(syntactically equal to" ++ spc() ++ pr_path path ++ str ")"
| exception Not_found -> spc() ++ str "(missing canonical, bug?)"
let pr_located_qualid env = function
| Term ref ->
let ref_str = let open GlobRef in match ref with
ConstRef _ -> "Constant"
| IndRef _ -> "Inductive"
| ConstructRef _ -> "Constructor"
| VarRef _ -> "Variable"
in
let = canonical_info env ref in
str ref_str ++ spc () ++ pr_path (Nametab.path_of_global ref) ++ extra
| Abbreviation kn ->
str "Notation" ++ spc () ++ pr_path (Nametab.path_of_abbreviation kn)
| Dir dir ->
let s,mp =
let open Nametab in
let open GlobDirRef in match dir with
| DirOpenModule mp -> "Open Module", ModPath.print mp
| DirOpenModtype mp -> "Open Module Type", ModPath.print mp
| DirOpenSection dir -> "Open Section", DirPath.print dir
in
str s ++ spc () ++ mp
| Module mp ->
str "Module" ++ spc () ++ DirPath.print (Nametab.dirpath_of_module mp)
| ModuleType mp ->
str "Module Type" ++ spc () ++ pr_path (Nametab.path_of_modtype mp)
| Other (obj, info) -> info.name obj
| Undefined qid ->
pr_qualid qid ++ spc () ++ str "not a defined object."
let maybe_error_reject_univ_decl na udecl =
let open GlobRef in
match na, udecl with
| _, None | Term (ConstRef _ | IndRef _ | ConstructRef _), Some _ -> ()
| (Term (VarRef _) | Abbreviation _ | Dir _ | Module _ | ModuleType _ | Other _ | Undefined _), Some udecl ->
user_err (str "This object does not support universe names.")
let print_any_name access env sigma na udecl =
maybe_error_reject_univ_decl na udecl;
match na with
| Term gref -> print_global_reference access env sigma gref udecl
| Abbreviation kn -> print_abbreviation access env sigma kn
| Module mp -> print_module mp
| Dir _ -> mt ()
| ModuleType mp -> print_modtype mp
| Other (obj, info) -> info.print obj
| Undefined qid ->
try
let dir,str = repr_qualid qid in
if not (DirPath.is_empty dir) then raise Not_found;
print_named_decl env sigma true str
with Not_found -> user_err ?loc:qid.loc (pr_qualid qid ++ spc () ++ str "not a defined object.")
let print_notation_interpretation env sigma (entry,ntn) df sc c =
let filter = Notation.{
notation_entry_pattern = [entry];
interp_rule_key_pattern = Some (Inl ntn);
use_pattern = OnlyPrinting;
scope_pattern = sc;
interpretation_pattern = Some c;
} in
Vernacstate.System.protect (fun () ->
Notation.toggle_notations ~on:false ~all:false ~verbose:false (pr_glob_constr_env env sigma) filter;
hov 0 (str "Notation" ++ spc () ++ Notation_ops.pr_notation_info (pr_glob_constr_env env sigma) df (snd c))) ()
let print_name access env sigma na udecl =
match na with
| {loc; v=Constrexpr.ByNotation (ntn,sc)} ->
let ntn, df, sc, c, ref = Notation.interp_notation_as_global_reference_expanded ?loc ~head:false (fun _ -> true) ntn sc in
print_notation_interpretation env sigma ntn df (Some sc) c ++ fnl () ++ fnl () ++
print_any_name access env sigma (Term ref) udecl
| {loc; v=Constrexpr.AN ref} ->
print_any_name access env sigma (locate_any_name ref) udecl
(** Command [Print Notation] *)
let print_notation_grammar env sigma ntn =
let ng = List.hd (Notgram_ops.grammar_of_notation ntn) in
let assoc = ng.Notation_gram.notgram_assoc in
let prdf () = Pp.str "no associativity" in
Pp.(pr_opt_no_spc_default prdf Gramlib.Gramext.pr_assoc assoc)
exception PrintNotationNotFound of Constrexpr.notation_entry * string
let () = CErrors.register_handler @@ function
| PrintNotationNotFound (entry, ntn_str) ->
let entry_string = match entry with
| Constrexpr.InConstrEntry -> "."
| Constrexpr.InCustomEntry e -> " in " ^ e ^ " entry."
in
Some Pp.(str "\"" ++ str ntn_str ++ str "\"" ++ spc ()
++ str "cannot be interpreted as a known notation" ++ str entry_string ++ spc ()
++ strbrk "Make sure that symbols are surrounded by spaces and that holes are explicitly denoted by \"_\".")
| _ -> None
let error_print_notation_not_found e s =
raise @@ PrintNotationNotFound (e, s)
let print_notation env sigma entry raw_ntn =
let () =
match entry with
| Constrexpr.InConstrEntry -> ()
| Constrexpr.InCustomEntry e -> Metasyntax.check_custom_entry e
in
let interp_ntn = Notation.interpret_notation_string raw_ntn in
let ntn = (entry, interp_ntn) in
try
let lvl = Notation.level_of_notation ntn in
let args = Notgram_ops.non_terminals_of_notation ntn in
let pplvl = Metasyntax.pr_level ntn lvl args in
Pp.(str "Notation \"" ++ str interp_ntn ++ str "\"" ++ spc ()
++ pplvl ++ pr_comma () ++ print_notation_grammar env sigma ntn
++ str ".")
with Not_found -> error_print_notation_not_found entry raw_ntn
(** Command [About] *)
let print_about_global_reference ?loc env ref udecl =
pr_infos_list
(print_ref env false ref udecl :: blankline ::
print_polymorphism env ref @
print_name_infos env ref @
print_reduction_behaviour ref @
print_opacity env ref @
print_bidi_hints ref @
[hov 0 (str "Expands to: " ++ pr_located_qualid env (Term ref))])
let print_about_abbreviation env sigma kn =
let (vars,c) = glob_constr_of_abbreviation kn in
let pp = match DAst.get c with
| GRef (gref,_udecl) -> [print_about_global_reference env gref None]
| _ -> [] in
print_abbreviation_body env kn (vars,c) ++ fnl () ++
hov 0 (str "Expands to: " ++ pr_located_qualid env (Abbreviation kn)) ++
with_line_skip pp
let print_about_any ?loc env sigma k udecl =
maybe_error_reject_univ_decl k udecl;
match k with
| Term ref -> Dumpglob.add_glob ?loc ref; print_about_global_reference env ref udecl
| Abbreviation kn -> v 0 (print_about_abbreviation env sigma kn)
| Dir _ | Module _ | ModuleType _ | Undefined _ -> hov 0 (pr_located_qualid env k)
| Other (obj, info) -> hov 0 (info.about obj)
let print_about env sigma na udecl =
match na with
| {loc;v=Constrexpr.ByNotation (ntn,sc)} ->
let ntn, df, sc, c, ref = Notation.interp_notation_as_global_reference_expanded ?loc ~head:false (fun _ -> true) ntn sc in
print_notation_interpretation env sigma ntn df (Some sc) c ++ fnl () ++ fnl () ++
print_about_any ?loc env sigma (Term ref) udecl
| {loc;v=Constrexpr.AN ref} ->
print_about_any ?loc env sigma (locate_any_name ref) udecl
let inspect env sigma depth =
print_context env sigma ~with_values:None (Some depth) (Lib.contents ())
(** Command [Print Classes] *)
open Coercionops
let print_coercion_value v = Printer.pr_global v.coe_value
let print_path ((i,j),p) =
hov 2 (
str"[" ++ hov 0 (prlist_with_sep pr_semicolon print_coercion_value p) ++
str"] : ") ++
pr_class i ++ str" >-> " ++ pr_class j ++
str (if path_is_reversible p then " (reversible)" else "")
let _ = Coercionops.install_path_printer print_path
let print_graph () =
prlist_with_sep fnl print_path (inheritance_graph())
(** Command [Print Classes] *)
let print_classes () =
pr_sequence pr_class (classes())
(** Command [Print Coercions] *)
let print_coercions () =
pr_sequence print_coercion_value (coercions())
(** Command [Print Coercion Paths] *)
let print_coercion_paths cls clt =
let p =
try
lookup_path_between_class (cls, clt)
with Not_found ->
user_err
(str"No path between " ++ pr_class cls ++ str" and " ++ pr_class clt
++ str ".")
in
print_path ((cls, clt), p)
(** Command [Print Canonical Projections] *)
let print_canonical_projections env sigma grefs =
let open Structures in
let match_proj_gref { CSTable.projection; value; solution } gr =
QGlobRef.equal env projection gr ||
begin match value with
| ValuePattern.Const_cs y -> QGlobRef.equal env y gr
| _ -> false
end ||
QGlobRef.equal env solution gr
in
let projs =
List.filter (fun p -> List.for_all (match_proj_gref p) grefs)
(CSTable.entries ())
in
prlist_with_sep fnl
(fun { CSTable.projection; value; solution } ->
ValuePattern.print value ++
str " <- " ++
pr_global projection ++ str " ( " ++ pr_global solution ++ str " )")
projs
(** Pretty-printing functions for type classes *)
(** Command [Print Typeclasses] *)
open Typeclasses
let pr_typeclass env t =
print_ref env false t.cl_impl None
let print_typeclasses () =
let env = Global.env () in
prlist_with_sep fnl (pr_typeclass env) (typeclasses ())
(** Command [Print Instances] *)
let pr_instance env i =
print_ref env false (instance_impl i) None ++
begin match hint_priority i with
| None -> mt ()
| Some i -> spc () ++ str "|" ++ spc () ++ int i
end
let print_all_instances () =
let env = Global.env () in
let inst = all_instances () in
prlist_with_sep fnl (pr_instance env) inst
let print_instances r =
let env = Global.env () in
let inst = instances_exn env (Evd.from_env env) r in
prlist_with_sep fnl (pr_instance env) inst
let canonize_ref = let open GlobRef in function
| ConstRef c ->
let kn = Constant.canonical c in
if KerName.equal (Constant.user c) kn then None
else Some (ConstRef (Constant.make1 kn))
| IndRef (ind,i) ->
let kn = MutInd.canonical ind in
if KerName.equal (MutInd.user ind) kn then None
else Some (IndRef (MutInd.make1 kn, i))
| ConstructRef ((ind,i),j) ->
let kn = MutInd.canonical ind in
if KerName.equal (MutInd.user ind) kn then None
else Some (ConstructRef ((MutInd.make1 kn, i),j))
| VarRef _ -> None
let display_alias = function
| Term r ->
begin match canonize_ref r with
| None -> mt ()
| Some r' ->
let q' = Nametab.shortest_qualid_of_global Id.Set.empty r' in
spc () ++ str "(alias of " ++ pr_qualid q' ++ str ")"
end
| _ -> mt ()
let locate_term qid =
let expand = function
| TrueGlobal ref ->
Term ref, Nametab.shortest_qualid_of_global Id.Set.empty ref
| Abbrev kn ->
Abbreviation kn, Nametab.shortest_qualid_of_abbreviation Id.Set.empty kn
in
List.map expand (Nametab.locate_extended_all qid)
let locate_module qid =
let all = Nametab.locate_extended_all_module qid in
let map mp = Module mp, Nametab.shortest_qualid_of_module mp in
let mods = List.map map all in
let all = Nametab.locate_extended_all_dir qid in
let map dir = let open Nametab.GlobDirRef in match dir with
| DirOpenModule _ -> Some (Dir dir, qid)
| _ -> None
in
mods @ List.map_filter map all
let locate_modtype qid =
let all = Nametab.locate_extended_all_modtype qid in
let map mp = ModuleType mp, Nametab.shortest_qualid_of_modtype mp in
let modtypes = List.map map all in
let all = Nametab.locate_extended_all_dir qid in
let map dir = let open Nametab.GlobDirRef in match dir with
| DirOpenModtype _ -> Some (Dir dir, qid)
| _ -> None
in
modtypes @ List.map_filter map all
let locate_other s qid =
let Locatable info = String.Map.find s !locatable_map in
let ans = info.locate_all qid in
let map obj = (Other (obj, info), info.shortest_qualid obj) in
List.map map ans
type locatable_kind =
| LocTerm
| LocModule
| LocOther of string
| LocAny
let print_located_qualid env name flags qid =
let located = match flags with
| LocTerm -> locate_term qid
| LocModule -> locate_modtype qid @ locate_module qid
| LocOther s -> locate_other s qid
| LocAny ->
locate_term qid @
locate_modtype qid @
locate_module qid @
String.Map.fold (fun s _ accu -> locate_other s qid @ accu) !locatable_map []
in
match located with
| [] ->
let (dir,id) = repr_qualid qid in
if DirPath.is_empty dir then
str "No " ++ str name ++ str " of basename" ++ spc () ++ Id.print id
else
str "No " ++ str name ++ str " of suffix" ++ spc () ++ pr_qualid qid
| l ->
prlist_with_sep fnl
(fun (o,oqid) ->
hov 2 (pr_located_qualid env o ++
(if not (qualid_eq oqid qid) then
spc() ++ str "(shorter name to refer to it in current context is "
++ pr_qualid oqid ++ str")"
else mt ()) ++
display_alias o)) l
let print_located_term env ref = print_located_qualid env "term" LocTerm ref
let print_located_other env s ref = print_located_qualid env s (LocOther s) ref
let print_located_module env ref = print_located_qualid env "module" LocModule ref
let print_located_qualid env ref = print_located_qualid env "object" LocAny ref