| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Util |
| 12 |
open Constr |
| 13 |
open Context |
| 14 |
open Pp |
| 15 |
open Names |
| 16 |
open Declarations |
| 17 |
open Mod_declarations |
| 18 |
open Libnames |
| 19 |
open Goptions |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
module Tag = |
| 29 |
struct |
| 30 |
|
| 31 |
let definition = "module.definition" |
| 32 |
let keyword = "module.keyword" |
| 33 |
|
| 34 |
end |
| 35 |
|
| 36 |
let tag t s = Pp.tag t s |
| 37 |
let tag_definition s = tag Tag.definition s |
| 38 |
let tag_keyword s = tag Tag.keyword s |
| 39 |
|
| 40 |
type short = OnlyNames | WithContents |
| 41 |
|
| 42 |
let short = ref false |
| 43 |
|
| 44 |
let () = |
| 45 |
declare_bool_option |
| 46 |
{ optstage = Summary.Stage.Interp; |
| 47 |
optdepr = None; |
| 48 |
optkey = ["Short";"Module";"Printing"]; |
| 49 |
optread = (fun () -> !short) ; |
| 50 |
optwrite = ((:=) short) } |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
let mk_fake_top = |
| 56 |
let r = ref 0 in |
| 57 |
fun () -> incr r; Id.of_string ("FAKETOP"^(string_of_int !r)) |
| 58 |
|
| 59 |
let def s = tag_definition (str s) |
| 60 |
let keyword s = tag_keyword (str s) |
| 61 |
|
| 62 |
let get_new_id avoid id = |
| 63 |
Namegen.next_ident_away_from id (fun id -> |
| 64 |
Id.Set.mem id avoid || |
| 65 |
let dir = Libnames.make_path DirPath.empty id in |
| 66 |
Nametab.exists_module dir || Nametab.exists_dir dir) |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
open Reduction |
| 71 |
|
| 72 |
let print_params env sigma params = |
| 73 |
if List.is_empty params then mt () |
| 74 |
else Printer.pr_rel_context env sigma params ++ brk(1,2) |
| 75 |
|
| 76 |
let print_constructors envpar sigma names types = |
| 77 |
let pc = |
| 78 |
prlist_with_sep (fun () -> brk(1,0) ++ str "| ") |
| 79 |
(fun (id,c) -> Id.print id ++ str " : " ++ Printer.pr_lconstr_env envpar sigma c) |
| 80 |
(Array.to_list (Array.map2 (fun n t -> (n,t)) names types)) |
| 81 |
in |
| 82 |
hv 0 (str " " ++ pc) |
| 83 |
|
| 84 |
let build_ind_type mip = |
| 85 |
Inductive.type_of_inductive mip |
| 86 |
|
| 87 |
let get_fields = |
| 88 |
let rec prodec_rec l subst c = |
| 89 |
match kind c with |
| 90 |
| Prod (na,t,c) -> |
| 91 |
let id = match na.binder_name with Name id -> id | Anonymous -> Id.of_string "_" in |
| 92 |
prodec_rec ((id,true,Vars.substl subst t)::l) (mkVar id::subst) c |
| 93 |
| LetIn (na,b,_,c) -> |
| 94 |
let id = match na.binder_name with Name id -> id | Anonymous -> Id.of_string "_" in |
| 95 |
prodec_rec ((id,false,Vars.substl subst b)::l) (mkVar id::subst) c |
| 96 |
| _ -> List.rev l |
| 97 |
in |
| 98 |
prodec_rec [] [] |
| 99 |
|
| 100 |
let print_fields envpar sigma cstrtypes = |
| 101 |
let fields = get_fields cstrtypes.(0) in |
| 102 |
hv 2 (str "{ " ++ |
| 103 |
prlist_with_sep (fun () -> str ";" ++ brk(2,0)) |
| 104 |
(fun (id,b,c) -> |
| 105 |
Id.print id ++ str (if b then " : " else " := ") ++ |
| 106 |
Printer.pr_lconstr_env envpar sigma c) fields) ++ str" }" |
| 107 |
|
| 108 |
let is_canonical_as env ind indname id = |
| 109 |
|
| 110 |
let canonical_id = Record.canonical_inhabitant_id ~isclass:(Typeclasses.is_class env (IndRef ind)) indname in |
| 111 |
Id.equal id canonical_id |
| 112 |
|
| 113 |
let print_as env ind indname = function |
| 114 |
| Anonymous -> mt () |
| 115 |
| Name id -> if is_canonical_as env ind indname id then mt () else str " as " ++ Id.print id |
| 116 |
|
| 117 |
let print_one_inductive env sigma mib ((_,i) as ind) = |
| 118 |
let u = UVars.make_abstract_instance (Declareops.inductive_polymorphic_context mib) in |
| 119 |
let mip = mib.mind_packets.(i) in |
| 120 |
let paramdecls = Inductive.inductive_paramdecls (mib,u) in |
| 121 |
let env_params, params = Namegen.make_all_rel_context_name_different env (Evd.from_env env) (EConstr.of_rel_context paramdecls) in |
| 122 |
let params = EConstr.Unsafe.to_rel_context params in |
| 123 |
let nparamdecls = Context.Rel.length params in |
| 124 |
let args = Context.Rel.instance_list mkRel 0 params in |
| 125 |
let arity = hnf_prod_applist_decls env nparamdecls (build_ind_type ((mib,mip),u)) args in |
| 126 |
let cstrtypes = Inductive.type_of_constructors (ind,u) (mib,mip) in |
| 127 |
let cstrtypes = Array.map (fun c -> snd (Term.decompose_prod_n_decls nparamdecls c)) cstrtypes in |
| 128 |
let isrecord = match mip.mind_record with |
| 129 |
| NotRecord -> None |
| 130 |
| FakeRecord -> Some Anonymous |
| 131 |
| PrimRecord { id; _ } -> Some (Name id) |
| 132 |
in |
| 133 |
if Option.has_some isrecord then assert (Array.length cstrtypes = 1); |
| 134 |
let inst = |
| 135 |
if Declareops.inductive_is_polymorphic mib then |
| 136 |
Printer.pr_universe_instance_binder sigma u Univ.UnivConstraints.empty |
| 137 |
else mt () |
| 138 |
in |
| 139 |
hov 0 ( |
| 140 |
Id.print mip.mind_typename ++ inst ++ brk(1,4) ++ print_params env sigma params ++ |
| 141 |
str ": " ++ Printer.pr_lconstr_env env_params sigma arity ++ str " :=" ++ |
| 142 |
if Option.has_some isrecord then str " " ++ Id.print mip.mind_consnames.(0) else mt()) ++ |
| 143 |
match isrecord with |
| 144 |
| None -> |
| 145 |
brk(0,2) ++ print_constructors env_params sigma mip.mind_consnames cstrtypes |
| 146 |
| Some as_clause -> |
| 147 |
brk(1,2) ++ print_fields env_params sigma cstrtypes ++ print_as env ind mip.mind_typename as_clause |
| 148 |
|
| 149 |
let pr_mutual_inductive_body env mind mib udecl = |
| 150 |
let inds = List.init (Array.length mib.mind_packets) (fun x -> (mind, x)) in |
| 151 |
let keyword = |
| 152 |
let open Declarations in |
| 153 |
match mib.mind_finite with |
| 154 |
| Finite -> "Inductive" |
| 155 |
| CoFinite -> "CoInductive" |
| 156 |
| BiFinite -> |
| 157 |
match mib.mind_packets.(0).mind_record with |
| 158 |
| NotRecord -> "Variant" |
| 159 |
| FakeRecord -> "Record" |
| 160 |
| PrimRecord l -> "Record" |
| 161 |
in |
| 162 |
let udecl = Option.map (fun x -> GlobRef.IndRef (mind,0), x) udecl in |
| 163 |
let bl = Printer.universe_binders_with_opt_names |
| 164 |
(Declareops.inductive_polymorphic_context mib) udecl |
| 165 |
in |
| 166 |
let sigma = Evd.from_ctx (UState.of_names bl) in |
| 167 |
|
| 168 |
hov 0 (def keyword ++ spc () ++ |
| 169 |
prlist_with_sep (fun () -> fnl () ++ str" with ") |
| 170 |
(print_one_inductive env sigma mib) inds ++ str "." ++ |
| 171 |
Printer.pr_universes sigma ?variance:mib.mind_variance mib.mind_universes) |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
let rec print_local_modpath locals = function |
| 176 |
| MPbound mbid -> Id.print (Util.List.assoc_f MBId.equal mbid locals) |
| 177 |
| MPdot(mp,l) -> |
| 178 |
print_local_modpath locals mp ++ str "." ++ Id.print l |
| 179 |
| MPfile _ -> raise Not_found |
| 180 |
|
| 181 |
let print_modpath locals mp = |
| 182 |
try |
| 183 |
let qid = Nametab.shortest_qualid_of_module mp in |
| 184 |
pr_qualid qid |
| 185 |
with |
| 186 |
| Not_found -> print_local_modpath locals mp |
| 187 |
|
| 188 |
let print_kn locals kn = |
| 189 |
try |
| 190 |
let qid = Nametab.shortest_qualid_of_modtype kn in |
| 191 |
pr_qualid qid |
| 192 |
with |
| 193 |
Not_found -> |
| 194 |
try |
| 195 |
print_local_modpath locals kn |
| 196 |
with |
| 197 |
Not_found -> print_modpath locals kn |
| 198 |
|
| 199 |
let nametab_register_dir obj_mp = |
| 200 |
let id = mk_fake_top () in |
| 201 |
let obj_dir = Libnames.make_path DirPath.empty id in |
| 202 |
Nametab.(push_module (Until 1) obj_dir obj_mp) |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
let nametab_register_body mp dir (l,body) = |
| 211 |
let push id ref = |
| 212 |
Nametab.push (Nametab.Until (1+List.length (DirPath.repr dir))) |
| 213 |
(make_path dir id) ref |
| 214 |
in |
| 215 |
match body with |
| 216 |
| SFBmodule _ -> () |
| 217 |
| SFBmodtype _ -> () |
| 218 |
| SFBrules _ -> () |
| 219 |
| SFBconst _ -> |
| 220 |
push l (GlobRef.ConstRef (Constant.make2 mp l)) |
| 221 |
| SFBmind mib -> |
| 222 |
let mind = MutInd.make2 mp l in |
| 223 |
Array.iteri |
| 224 |
(fun i mip -> |
| 225 |
push mip.mind_typename (GlobRef.IndRef (mind,i)); |
| 226 |
Array.iteri (fun j id -> push id (GlobRef.ConstructRef ((mind,i),j+1))) |
| 227 |
mip.mind_consnames) |
| 228 |
mib.mind_packets |
| 229 |
|
| 230 |
|
| 231 |
let import_module = Declaremods.Interp.import_module Libobject.unfiltered |
| 232 |
let process_module_binding = Declaremods.process_module_binding |
| 233 |
|
| 234 |
let nametab_register_module_body mp struc = |
| 235 |
|
| 236 |
try import_module ~export:Lib.Import mp |
| 237 |
with Not_found -> |
| 238 |
|
| 239 |
nametab_register_dir mp; |
| 240 |
List.iter (nametab_register_body mp DirPath.empty) struc |
| 241 |
|
| 242 |
let get_typ_expr_alg mtb = match mod_type_alg mtb with |
| 243 |
| Some (MENoFunctor me) -> me |
| 244 |
| _ -> raise Not_found |
| 245 |
|
| 246 |
let nametab_register_modparam used mbid mtb = |
| 247 |
let id = MBId.to_id mbid in |
| 248 |
match mod_type mtb with |
| 249 |
| MoreFunctor _ -> id |
| 250 |
| NoFunctor struc -> |
| 251 |
|
| 252 |
|
| 253 |
try |
| 254 |
let () = process_module_binding mbid (get_typ_expr_alg mtb) in |
| 255 |
id |
| 256 |
with e when CErrors.noncritical e -> |
| 257 |
|
| 258 |
let mp = MPbound mbid in |
| 259 |
let id = get_new_id used id in |
| 260 |
let dir = DirPath.make [id] in |
| 261 |
nametab_register_dir mp; |
| 262 |
List.iter (nametab_register_body mp dir) struc; |
| 263 |
id |
| 264 |
|
| 265 |
let print_body is_impl extent env mp (l,body) = |
| 266 |
let name = Id.print l in |
| 267 |
hov 2 (match body with |
| 268 |
| SFBmodule _ -> keyword "Module" ++ spc () ++ name |
| 269 |
| SFBmodtype _ -> keyword "Module Type" ++ spc () ++ name |
| 270 |
| SFBrules _ -> keyword "Rewrite Rule" ++ spc () ++ name |
| 271 |
| SFBconst cb -> |
| 272 |
let ctx = Declareops.constant_polymorphic_context cb in |
| 273 |
(match cb.const_body with |
| 274 |
| Def _ -> def "Definition" ++ spc () |
| 275 |
| OpaqueDef _ when is_impl -> def "Theorem" ++ spc () |
| 276 |
| _ -> def "Parameter" ++ spc ()) ++ name ++ |
| 277 |
(match extent with |
| 278 |
| OnlyNames -> mt () |
| 279 |
| WithContents -> |
| 280 |
let bl = Printer.universe_binders_with_opt_names ctx None in |
| 281 |
let sigma = Evd.from_ctx (UState.of_names bl) in |
| 282 |
str " :" ++ spc () ++ |
| 283 |
hov 0 (Printer.pr_ltype_env env sigma cb.const_type) ++ |
| 284 |
(match cb.const_body with |
| 285 |
| Def l when is_impl -> |
| 286 |
spc () ++ |
| 287 |
hov 2 (str ":= " ++ |
| 288 |
Printer.pr_lconstr_env env sigma l) |
| 289 |
| _ -> mt ()) ++ str "." ++ |
| 290 |
Printer.pr_abstract_universe_ctx sigma ctx) |
| 291 |
| SFBmind mib -> |
| 292 |
match extent with |
| 293 |
| WithContents -> |
| 294 |
pr_mutual_inductive_body env (MutInd.make2 mp l) mib None |
| 295 |
| OnlyNames -> |
| 296 |
let keyword = |
| 297 |
let open Declarations in |
| 298 |
match mib.mind_finite with |
| 299 |
| Finite -> def "Inductive" |
| 300 |
| BiFinite -> def "Variant" |
| 301 |
| CoFinite -> def "CoInductive" |
| 302 |
in |
| 303 |
keyword ++ spc () ++ name) |
| 304 |
|
| 305 |
let print_struct is_impl extent env mp struc = |
| 306 |
prlist_with_sep spc (print_body is_impl extent env mp) struc |
| 307 |
|
| 308 |
let print_structure is_type extent env mp locals struc = |
| 309 |
|
| 310 |
let env' = Environ.Internal.overwrite_structure mp struc (Mod_subst.empty_delta_resolver mp) env in |
| 311 |
nametab_register_module_body mp struc; |
| 312 |
let kwd = if is_type then "Sig" else "Struct" in |
| 313 |
hv 2 (keyword kwd ++ spc () ++ print_struct false extent env' mp struc ++ |
| 314 |
brk (1,-2) ++ keyword "End") |
| 315 |
|
| 316 |
let rec flatten_app mexpr l = match mexpr with |
| 317 |
| MEapply (mexpr, arg) -> flatten_app mexpr (arg::l) |
| 318 |
| MEident mp -> mp::l |
| 319 |
| MEwith _ -> assert false |
| 320 |
|
| 321 |
let rec print_typ_expr extent env mp locals mty = |
| 322 |
match mty with |
| 323 |
| MEident kn -> print_kn locals kn |
| 324 |
| MEapply _ -> |
| 325 |
let lapp = flatten_app mty [] in |
| 326 |
let fapp = List.hd lapp in |
| 327 |
let mapp = List.tl lapp in |
| 328 |
hov 3 (str"(" ++ (print_kn locals fapp) ++ spc () ++ |
| 329 |
prlist_with_sep spc (print_modpath locals) mapp ++ str")") |
| 330 |
| MEwith(me,WithDef(idl,(c, _)))-> |
| 331 |
let s = String.concat "." (List.map Id.to_string idl) in |
| 332 |
let body = match extent with |
| 333 |
| WithContents -> |
| 334 |
let sigma = Evd.from_env env in |
| 335 |
spc() ++ str ":=" ++ spc() ++ Printer.pr_lconstr_env env sigma c |
| 336 |
| OnlyNames -> |
| 337 |
mt() in |
| 338 |
hov 2 (print_typ_expr extent env mp locals me ++ spc() ++ str "with" ++ spc() |
| 339 |
++ def "Definition"++ spc() ++ str s ++ body) |
| 340 |
| MEwith(me,WithMod(idl,mp'))-> |
| 341 |
let s = String.concat "." (List.map Id.to_string idl) in |
| 342 |
let body = match extent with |
| 343 |
| WithContents -> |
| 344 |
spc() ++ str ":="++ spc() ++ print_modpath locals mp' |
| 345 |
| OnlyNames -> mt () in |
| 346 |
hov 2 (print_typ_expr extent env mp locals me ++ spc() ++ str "with" ++ spc() ++ |
| 347 |
keyword "Module"++ spc() ++ str s ++ body) |
| 348 |
|
| 349 |
let print_mod_expr env mp locals = function |
| 350 |
| MEident mp -> print_modpath locals mp |
| 351 |
| MEapply _ as me -> |
| 352 |
let lapp = flatten_app me [] in |
| 353 |
hov 3 |
| 354 |
(str"(" ++ prlist_with_sep spc (print_modpath locals) lapp ++ str")") |
| 355 |
| MEwith _ -> assert false |
| 356 |
|
| 357 |
let rec print_functor fty fatom is_type extent env mp used locals = function |
| 358 |
| NoFunctor me -> fatom is_type extent env mp locals me |
| 359 |
| MoreFunctor (mbid,mtb1,me2) -> |
| 360 |
let id = nametab_register_modparam !used mbid mtb1 in |
| 361 |
let () = used := Id.Set.add id !used in |
| 362 |
let mp1 = MPbound mbid in |
| 363 |
let pr_mtb1 = fty extent env mp1 used locals mtb1 in |
| 364 |
let env' = Modops.add_module_parameter mbid mtb1 env in |
| 365 |
let avoid = List.fold_left (fun accu (_, id) -> Id.Set.add id accu) Id.Set.empty locals in |
| 366 |
let locals' = (mbid, get_new_id avoid (MBId.to_id mbid))::locals in |
| 367 |
let kwd = if is_type then "Funsig" else "Functor" in |
| 368 |
hov 2 |
| 369 |
(keyword kwd ++ spc () ++ |
| 370 |
str "(" ++ Id.print id ++ str ":" ++ pr_mtb1 ++ str ")" ++ |
| 371 |
spc() ++ print_functor fty fatom is_type extent env' mp used locals' me2) |
| 372 |
|
| 373 |
let rec print_expression x = |
| 374 |
print_functor |
| 375 |
print_modtype |
| 376 |
(function true -> print_typ_expr | false -> fun _ -> print_mod_expr) x |
| 377 |
|
| 378 |
and print_signature x = |
| 379 |
print_functor print_modtype print_structure x |
| 380 |
|
| 381 |
and print_modtype extent env mp used locals mtb = match mod_type_alg mtb with |
| 382 |
| Some me -> |
| 383 |
let me = Modops.annotate_module_expression me (mod_type mtb) in |
| 384 |
print_expression true extent env mp used locals me |
| 385 |
| None -> print_signature true extent env mp used locals (mod_type mtb) |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
let print_expression' is_type extent env mp me = |
| 391 |
Vernacstate.System.protect |
| 392 |
(fun e -> print_expression is_type extent env mp (ref Id.Set.empty) [] e) me |
| 393 |
|
| 394 |
let print_signature' is_type extent env mp me = |
| 395 |
Vernacstate.System.protect |
| 396 |
(fun e -> print_signature is_type extent env mp (ref Id.Set.empty) [] e) me |
| 397 |
|
| 398 |
let unsafe_print_module extent env mp with_body mb = |
| 399 |
let name = print_modpath [] mp in |
| 400 |
let pr_equals = spc () ++ str ":= " in |
| 401 |
let body = match with_body, Mod_declarations.mod_expr mb with |
| 402 |
| false, _ |
| 403 |
| true, Abstract -> mt() |
| 404 |
| _, Algebraic me -> |
| 405 |
let me = Modops.annotate_module_expression me (mod_type mb) in |
| 406 |
pr_equals ++ print_expression' false extent env mp me |
| 407 |
| _, Struct (_, sign) -> |
| 408 |
let sign = Modops.annotate_struct_body sign (mod_type mb) in |
| 409 |
pr_equals ++ print_signature' false extent env mp sign |
| 410 |
| _, FullStruct -> pr_equals ++ print_signature' false extent env mp (mod_type mb) |
| 411 |
in |
| 412 |
let modtype = match mod_expr mb, mod_type_alg mb with |
| 413 |
| FullStruct, _ -> mt () |
| 414 |
| _, Some ty -> |
| 415 |
let ty = Modops.annotate_module_expression ty (mod_type mb) in |
| 416 |
brk (1,1) ++ str": " ++ print_expression' true extent env mp ty |
| 417 |
| _, _ -> brk (1,1) ++ str": " ++ print_signature' true extent env mp (mod_type mb) |
| 418 |
in |
| 419 |
hv 0 (keyword "Module" ++ spc () ++ name ++ modtype ++ body) |
| 420 |
|
| 421 |
exception ShortPrinting |
| 422 |
|
| 423 |
let print_module ~with_body mp = |
| 424 |
let me = Global.lookup_module mp in |
| 425 |
try |
| 426 |
if !short then raise ShortPrinting; |
| 427 |
unsafe_print_module WithContents |
| 428 |
(Global.env ()) mp with_body me |
| 429 |
with e when CErrors.noncritical e -> |
| 430 |
unsafe_print_module OnlyNames |
| 431 |
(Global.env ()) mp with_body me |
| 432 |
|
| 433 |
let print_modtype kn = |
| 434 |
let mtb = Global.lookup_modtype kn in |
| 435 |
let name = print_kn [] kn in |
| 436 |
hv 1 |
| 437 |
(keyword "Module Type" ++ spc () ++ name ++ str " =" ++ spc () ++ |
| 438 |
try |
| 439 |
if !short then raise ShortPrinting; |
| 440 |
print_signature' true WithContents |
| 441 |
(Global.env ()) kn (mod_type mtb) |
| 442 |
with e when CErrors.noncritical e -> |
| 443 |
print_signature' true OnlyNames |
| 444 |
(Global.env ()) kn (mod_type mtb)) |