PageRenderTime 54ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/parsing/ppvernac.ml

http://github.com/mzp/coq-ruby
OCaml | 951 lines | 814 code | 99 blank | 38 comment | 74 complexity | 5e20e6be8285e82b36e9ae11c3dddb85 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-2.0
  1. (************************************************************************)
  2. (* v * The Coq Proof Assistant / The Coq Development Team *)
  3. (* <O___,, * CNRS-Ecole Polytechnique-INRIA Futurs-Universite Paris Sud *)
  4. (* \VV/ **************************************************************)
  5. (* // * This file is distributed under the terms of the *)
  6. (* * GNU Lesser General Public License Version 2.1 *)
  7. (************************************************************************)
  8. (* $Id: ppvernac.ml 12187 2009-06-13 19:36:59Z msozeau $ *)
  9. open Pp
  10. open Names
  11. open Nameops
  12. open Nametab
  13. open Util
  14. open Extend
  15. open Vernacexpr
  16. open Ppconstr
  17. open Pptactic
  18. open Rawterm
  19. open Genarg
  20. open Pcoq
  21. open Libnames
  22. open Ppextend
  23. open Topconstr
  24. open Decl_kinds
  25. open Tacinterp
  26. let pr_spc_lconstr = pr_sep_com spc pr_lconstr_expr
  27. let pr_lident (loc,id) =
  28. if loc <> dummy_loc then
  29. let (b,_) = unloc loc in
  30. pr_located pr_id (make_loc (b,b+String.length(string_of_id id)),id)
  31. else pr_id id
  32. let string_of_fqid fqid =
  33. String.concat "." (List.map string_of_id fqid)
  34. let pr_fqid fqid = str (string_of_fqid fqid)
  35. let pr_lfqid (loc,fqid) =
  36. if loc <> dummy_loc then
  37. let (b,_) = unloc loc in
  38. pr_located pr_fqid (make_loc (b,b+String.length(string_of_fqid fqid)),fqid)
  39. else
  40. pr_fqid fqid
  41. let pr_lname = function
  42. (loc,Name id) -> pr_lident (loc,id)
  43. | lna -> pr_located pr_name lna
  44. let pr_ltac_id = Libnames.pr_reference
  45. let pr_module = Libnames.pr_reference
  46. let pr_import_module = Libnames.pr_reference
  47. let sep_end () = str"."
  48. (* Warning: [pr_raw_tactic] globalises and fails if globalisation fails *)
  49. let pr_raw_tactic_env l env t =
  50. pr_glob_tactic env (Tacinterp.glob_tactic_env l env t)
  51. let pr_gen env t =
  52. pr_raw_generic
  53. pr_constr_expr
  54. pr_lconstr_expr
  55. (pr_raw_tactic_level env) pr_reference t
  56. let pr_raw_tactic tac = pr_raw_tactic (Global.env()) tac
  57. let rec extract_signature = function
  58. | [] -> []
  59. | Egrammar.TacNonTerm (_,(_,t),_) :: l -> t :: extract_signature l
  60. | _::l -> extract_signature l
  61. let rec match_vernac_rule tys = function
  62. [] -> raise Not_found
  63. | pargs::rls ->
  64. if extract_signature pargs = tys then pargs
  65. else match_vernac_rule tys rls
  66. let sep = fun _ -> spc()
  67. let sep_p = fun _ -> str"."
  68. let sep_v = fun _ -> str","
  69. let sep_v2 = fun _ -> str"," ++ spc()
  70. let sep_pp = fun _ -> str":"
  71. let pr_ne_sep sep pr = function
  72. [] -> mt()
  73. | l -> sep() ++ pr l
  74. let pr_entry_prec = function
  75. | Some Gramext.LeftA -> str"LEFTA "
  76. | Some Gramext.RightA -> str"RIGHTA "
  77. | Some Gramext.NonA -> str"NONA "
  78. | None -> mt()
  79. let pr_prec = function
  80. | Some Gramext.LeftA -> str", left associativity"
  81. | Some Gramext.RightA -> str", right associativity"
  82. | Some Gramext.NonA -> str", no associativity"
  83. | None -> mt()
  84. let pr_set_entry_type = function
  85. | ETIdent -> str"ident"
  86. | ETReference -> str"global"
  87. | ETPattern -> str"pattern"
  88. | ETConstr _ -> str"constr"
  89. | ETOther (_,e) -> str e
  90. | ETBigint -> str "bigint"
  91. | ETConstrList _ -> failwith "Internal entry type"
  92. let strip_meta id =
  93. let s = string_of_id id in
  94. if s.[0]='$' then id_of_string (String.sub s 1 (String.length s - 1))
  95. else id
  96. let pr_production_item = function
  97. | VNonTerm (loc,nt,Some p) -> str nt ++ str"(" ++ pr_id (strip_meta p) ++ str")"
  98. | VNonTerm (loc,nt,None) -> str nt
  99. | VTerm s -> qs s
  100. let pr_comment pr_c = function
  101. | CommentConstr c -> pr_c c
  102. | CommentString s -> qs s
  103. | CommentInt n -> int n
  104. let pr_in_out_modules = function
  105. | SearchInside l -> spc() ++ str"inside" ++ spc() ++ prlist_with_sep sep pr_module l
  106. | SearchOutside [] -> mt()
  107. | SearchOutside l -> spc() ++ str"outside" ++ spc() ++ prlist_with_sep sep pr_module l
  108. let pr_search_about (b,c) =
  109. (if b then str "-" else mt()) ++
  110. match c with
  111. | SearchSubPattern p -> pr_constr_pattern_expr p
  112. | SearchString (s,sc) -> qs s ++ pr_opt (fun sc -> str "%" ++ str sc) sc
  113. let pr_search a b pr_p = match a with
  114. | SearchHead qid -> str"Search" ++ spc() ++ pr_reference qid ++ pr_in_out_modules b
  115. | SearchPattern c -> str"SearchPattern" ++ spc() ++ pr_p c ++ pr_in_out_modules b
  116. | SearchRewrite c -> str"SearchRewrite" ++ spc() ++ pr_p c ++ pr_in_out_modules b
  117. | SearchAbout sl -> str"SearchAbout" ++ spc() ++ str "[" ++ prlist_with_sep spc pr_search_about sl ++ str "]" ++ pr_in_out_modules b
  118. let pr_locality local = if local then str "Local " else str ""
  119. let pr_non_locality local = if local then str "" else str "Global "
  120. let pr_explanation (e,b,f) =
  121. let a = match e with
  122. | ExplByPos (n,_) -> anomaly "No more supported"
  123. | ExplByName id -> pr_id id in
  124. let a = if f then str"!" ++ a else a in
  125. if b then str "[" ++ a ++ str "]" else a
  126. let pr_class_rawexpr = function
  127. | FunClass -> str"Funclass"
  128. | SortClass -> str"Sortclass"
  129. | RefClass qid -> pr_reference qid
  130. let pr_option_ref_value = function
  131. | QualidRefValue id -> pr_reference id
  132. | StringRefValue s -> qs s
  133. let pr_printoption a b = match a with
  134. | Goptions.PrimaryTable table -> str table ++ pr_opt (prlist_with_sep sep pr_option_ref_value) b
  135. | Goptions.SecondaryTable (table,field) -> str table ++ spc() ++ str field ++ pr_opt (prlist_with_sep sep pr_option_ref_value) b
  136. | Goptions.TertiaryTable (table,field1,field2) -> str table ++ spc() ++
  137. str field1 ++ spc() ++ str field2 ++
  138. pr_opt (prlist_with_sep sep pr_option_ref_value) b
  139. let pr_set_option a b =
  140. let pr_opt_value = function
  141. | IntValue n -> spc() ++ int n
  142. | StringValue s -> spc() ++ str s
  143. | BoolValue b -> mt()
  144. in pr_printoption a None ++ pr_opt_value b
  145. let pr_topcmd _ = str"(* <Warning> : No printer for toplevel commands *)"
  146. let pr_destruct_location = function
  147. | Tacexpr.ConclLocation () -> str"Conclusion"
  148. | Tacexpr.HypLocation b -> if b then str"Discardable Hypothesis" else str"Hypothesis"
  149. let pr_opt_hintbases l = match l with
  150. | [] -> mt()
  151. | _ as z -> str":" ++ spc() ++ prlist_with_sep sep str z
  152. let pr_hints local db h pr_c pr_pat =
  153. let opth = pr_opt_hintbases db in
  154. let pph =
  155. match h with
  156. | HintsResolve l ->
  157. str "Resolve " ++ prlist_with_sep sep
  158. (fun (pri, _, c) -> pr_c c ++
  159. match pri with Some x -> spc () ++ str"(" ++ int x ++ str")" | None -> mt ())
  160. l
  161. | HintsImmediate l ->
  162. str"Immediate" ++ spc() ++ prlist_with_sep sep pr_c l
  163. | HintsUnfold l ->
  164. str "Unfold " ++ prlist_with_sep sep pr_reference l
  165. | HintsTransparency (l, b) ->
  166. str (if b then "Transparent " else "Opaque ") ++ prlist_with_sep sep
  167. pr_reference l
  168. | HintsConstructors c ->
  169. str"Constructors" ++ spc() ++ prlist_with_sep spc pr_reference c
  170. | HintsExtern (n,c,tac) ->
  171. let pat = match c with None -> mt () | Some pat -> pr_pat pat in
  172. str "Extern" ++ spc() ++ int n ++ spc() ++ pat ++ str" =>" ++
  173. spc() ++ pr_raw_tactic tac
  174. | HintsDestruct(name,i,loc,c,tac) ->
  175. str "Destruct " ++ pr_id name ++ str" :=" ++ spc() ++
  176. hov 0 (int i ++ spc() ++ pr_destruct_location loc ++ spc() ++
  177. pr_c c ++ str " =>") ++ spc() ++
  178. pr_raw_tactic tac in
  179. hov 2 (str"Hint "++pr_locality local ++ pph ++ opth)
  180. let pr_with_declaration pr_c = function
  181. | CWith_Definition (id,c) ->
  182. let p = pr_c c in
  183. str"Definition" ++ spc() ++ pr_lfqid id ++ str" := " ++ p
  184. | CWith_Module (id,qid) ->
  185. str"Module" ++ spc() ++ pr_lfqid id ++ str" := " ++
  186. pr_located pr_qualid qid
  187. let rec pr_module_type pr_c = function
  188. | CMTEident qid -> spc () ++ pr_located pr_qualid qid
  189. | CMTEwith (mty,decl) ->
  190. let m = pr_module_type pr_c mty in
  191. let p = pr_with_declaration pr_c decl in
  192. m ++ spc() ++ str"with" ++ spc() ++ p
  193. | CMTEapply (fexpr,mexpr)->
  194. let f = pr_module_type pr_c fexpr in
  195. let m = pr_module_expr mexpr in
  196. f ++ spc () ++ m
  197. and pr_module_expr = function
  198. | CMEident qid -> pr_located pr_qualid qid
  199. | CMEapply (me1,(CMEident _ as me2)) ->
  200. pr_module_expr me1 ++ spc() ++ pr_module_expr me2
  201. | CMEapply (me1,me2) ->
  202. pr_module_expr me1 ++ spc() ++
  203. hov 1 (str"(" ++ pr_module_expr me2 ++ str")")
  204. let pr_of_module_type prc (mty,b) =
  205. str (if b then ":" else "<:") ++
  206. pr_module_type prc mty
  207. let pr_require_token = function
  208. | Some true -> str "Export "
  209. | Some false -> str "Import "
  210. | None -> mt()
  211. let pr_module_vardecls pr_c (export,idl,mty) =
  212. let m = pr_module_type pr_c mty in
  213. (* Update the Nametab for interpreting the body of module/modtype *)
  214. let lib_dir = Lib.library_dp() in
  215. List.iter (fun (_,id) ->
  216. Declaremods.process_module_bindings [id]
  217. [make_mbid lib_dir (string_of_id id),
  218. Modintern.interp_modtype (Global.env()) mty]) idl;
  219. (* Builds the stream *)
  220. spc() ++
  221. hov 1 (str"(" ++ pr_require_token export ++
  222. prlist_with_sep spc pr_lident idl ++ str":" ++ m ++ str")")
  223. let pr_module_binders l pr_c =
  224. (* Effet de bord complexe pour garantir la declaration des noms des
  225. modules parametres dans la Nametab des l'appel de pr_module_binders
  226. malgre l'aspect paresseux des streams *)
  227. let ml = List.map (pr_module_vardecls pr_c) l in
  228. prlist (fun id -> id) ml
  229. let pr_module_binders_list l pr_c = pr_module_binders l pr_c
  230. let pr_type_option pr_c = function
  231. | CHole (loc, k) -> mt()
  232. | _ as c -> brk(0,2) ++ str":" ++ pr_c c
  233. let pr_decl_notation prc =
  234. pr_opt (fun (ntn,c,scopt) -> fnl () ++
  235. str "where " ++ qs ntn ++ str " := " ++ prc c ++
  236. pr_opt (fun sc -> str ": " ++ str sc) scopt)
  237. let pr_vbinders l =
  238. hv 0 (pr_binders l)
  239. let pr_binders_arg =
  240. pr_ne_sep spc pr_binders
  241. let pr_and_type_binders_arg bl =
  242. pr_binders_arg bl
  243. let pr_onescheme (idop,schem) =
  244. match schem with
  245. | InductionScheme (dep,ind,s) ->
  246. (match idop with
  247. | Some id -> hov 0 (pr_lident id ++ str" :=") ++ spc()
  248. | None -> spc ()
  249. ) ++
  250. hov 0 ((if dep then str"Induction for" else str"Minimality for")
  251. ++ spc() ++ pr_reference ind) ++ spc() ++
  252. hov 0 (str"Sort" ++ spc() ++ pr_rawsort s)
  253. | EqualityScheme ind ->
  254. (match idop with
  255. | Some id -> hov 0 (pr_lident id ++ str" :=") ++ spc()
  256. | None -> spc()
  257. ) ++
  258. hov 0 (str"Equality for")
  259. ++ spc() ++ pr_reference ind
  260. let begin_of_inductive = function
  261. [] -> 0
  262. | (_,((loc,_),_))::_ -> fst (unloc loc)
  263. let pr_class_rawexpr = function
  264. | FunClass -> str"Funclass"
  265. | SortClass -> str"Sortclass"
  266. | RefClass qid -> pr_reference qid
  267. let pr_assumption_token many = function
  268. | (Local,Logical) ->
  269. str (if many then "Hypotheses" else "Hypothesis")
  270. | (Local,Definitional) ->
  271. str (if many then "Variables" else "Variable")
  272. | (Global,Logical) ->
  273. str (if many then "Axioms" else "Axiom")
  274. | (Global,Definitional) ->
  275. str (if many then "Parameters" else "Parameter")
  276. | (Global,Conjectural) -> str"Conjecture"
  277. | (Local,Conjectural) ->
  278. anomaly "Don't know how to beautify a local conjecture"
  279. let pr_params pr_c (xl,(c,t)) =
  280. hov 2 (prlist_with_sep sep pr_lident xl ++ spc() ++
  281. (if c then str":>" else str":" ++
  282. spc() ++ pr_c t))
  283. let rec factorize = function
  284. | [] -> []
  285. | (c,(idl,t))::l ->
  286. match factorize l with
  287. | (xl,t')::l' when t' = (c,t) -> (idl@xl,t')::l'
  288. | l' -> (idl,(c,t))::l'
  289. let pr_ne_params_list pr_c l =
  290. match factorize l with
  291. | [p] -> pr_params pr_c p
  292. | l ->
  293. prlist_with_sep spc
  294. (fun p -> hov 1 (str "(" ++ pr_params pr_c p ++ str ")")) l
  295. (*
  296. prlist_with_sep pr_semicolon (pr_params pr_c)
  297. *)
  298. let pr_thm_token k = str (string_of_theorem_kind k)
  299. let pr_syntax_modifier = function
  300. | SetItemLevel (l,NextLevel) ->
  301. prlist_with_sep sep_v2 str l ++
  302. spc() ++ str"at next level"
  303. | SetItemLevel (l,NumLevel n) ->
  304. prlist_with_sep sep_v2 str l ++
  305. spc() ++ str"at level" ++ spc() ++ int n
  306. | SetLevel n -> str"at level" ++ spc() ++ int n
  307. | SetAssoc Gramext.LeftA -> str"left associativity"
  308. | SetAssoc Gramext.RightA -> str"right associativity"
  309. | SetAssoc Gramext.NonA -> str"no associativity"
  310. | SetEntryType (x,typ) -> str x ++ spc() ++ pr_set_entry_type typ
  311. | SetOnlyParsing -> str"only parsing"
  312. | SetFormat s -> str"format " ++ pr_located qs s
  313. let pr_syntax_modifiers = function
  314. | [] -> mt()
  315. | l -> spc() ++
  316. hov 1 (str"(" ++ prlist_with_sep sep_v2 pr_syntax_modifier l ++ str")")
  317. let print_level n =
  318. if n <> 0 then str " (at level " ++ int n ++ str ")" else mt ()
  319. let pr_grammar_tactic_rule n (_,pil,t) =
  320. hov 2 (str "Tactic Notation" ++ print_level n ++ spc() ++
  321. hov 0 (prlist_with_sep sep pr_production_item pil ++
  322. spc() ++ str":=" ++ spc() ++ pr_raw_tactic t))
  323. let pr_box b = let pr_boxkind = function
  324. | PpHB n -> str"h" ++ spc() ++ int n
  325. | PpVB n -> str"v" ++ spc() ++ int n
  326. | PpHVB n -> str"hv" ++ spc() ++ int n
  327. | PpHOVB n -> str"hov" ++ spc() ++ int n
  328. | PpTB -> str"t"
  329. in str"<" ++ pr_boxkind b ++ str">"
  330. let pr_paren_reln_or_extern = function
  331. | None,L -> str"L"
  332. | None,E -> str"E"
  333. | Some pprim,Any -> qs pprim
  334. | Some pprim,Prec p -> qs pprim ++ spc() ++ str":" ++ spc() ++ int p
  335. | _ -> mt()
  336. (**************************************)
  337. (* Pretty printer for vernac commands *)
  338. (**************************************)
  339. let make_pr_vernac pr_constr pr_lconstr =
  340. let pr_constrarg c = spc () ++ pr_constr c in
  341. let pr_lconstrarg c = spc () ++ pr_lconstr c in
  342. let pr_intarg n = spc () ++ int n in
  343. (* let pr_lident_constr sep (i,c) = pr_lident i ++ sep ++ pr_constrarg c in *)
  344. let pr_record_field (x, ntn) =
  345. let prx = match x with
  346. | (oc,AssumExpr (id,t)) ->
  347. hov 1 (pr_lname id ++
  348. (if oc then str" :>" else str" :") ++ spc() ++
  349. pr_lconstr_expr t)
  350. | (oc,DefExpr(id,b,opt)) -> (match opt with
  351. | Some t ->
  352. hov 1 (pr_lname id ++
  353. (if oc then str" :>" else str" :") ++ spc() ++
  354. pr_lconstr_expr t ++ str" :=" ++ pr_lconstr b)
  355. | None ->
  356. hov 1 (pr_lname id ++ str" :=" ++ spc() ++
  357. pr_lconstr b)) in
  358. prx ++ pr_decl_notation pr_constr ntn
  359. in
  360. let pr_record_decl b c fs =
  361. pr_opt pr_lident c ++ str"{" ++
  362. hv 0 (prlist_with_sep pr_semicolon pr_record_field fs ++ str"}")
  363. in
  364. let rec pr_vernac = function
  365. (* Proof management *)
  366. | VernacAbortAll -> str "Abort All"
  367. | VernacRestart -> str"Restart"
  368. | VernacSuspend -> str"Suspend"
  369. | VernacUnfocus -> str"Unfocus"
  370. | VernacGoal c -> str"Goal" ++ pr_lconstrarg c
  371. | VernacAbort id -> str"Abort" ++ pr_opt pr_lident id
  372. | VernacResume id -> str"Resume" ++ pr_opt pr_lident id
  373. | VernacUndo i -> if i=1 then str"Undo" else str"Undo" ++ pr_intarg i
  374. | VernacUndoTo i -> str"Undo" ++ spc() ++ str"To" ++ pr_intarg i
  375. | VernacBacktrack (i,j,k) ->
  376. str "Backtrack" ++ spc() ++ prlist_with_sep sep int [i;j;k]
  377. | VernacFocus i -> str"Focus" ++ pr_opt int i
  378. | VernacGo g ->
  379. let pr_goable = function
  380. | GoTo i -> int i
  381. | GoTop -> str"top"
  382. | GoNext -> str"next"
  383. | GoPrev -> str"prev"
  384. in str"Go" ++ spc() ++ pr_goable g
  385. | VernacShow s ->
  386. let pr_showable = function
  387. | ShowGoal n -> str"Show" ++ pr_opt int n
  388. | ShowGoalImplicitly n -> str"Show Implicit Arguments" ++ pr_opt int n
  389. | ShowProof -> str"Show Proof"
  390. | ShowNode -> str"Show Node"
  391. | ShowScript -> str"Show Script"
  392. | ShowExistentials -> str"Show Existentials"
  393. | ShowTree -> str"Show Tree"
  394. | ShowProofNames -> str"Show Conjectures"
  395. | ShowIntros b -> str"Show " ++ (if b then str"Intros" else str"Intro")
  396. | ShowMatch id -> str"Show Match " ++ pr_lident id
  397. | ShowThesis -> str "Show Thesis"
  398. | ExplainProof l -> str"Explain Proof" ++ spc() ++ prlist_with_sep sep int l
  399. | ExplainTree l -> str"Explain Proof Tree" ++ spc() ++ prlist_with_sep sep int l
  400. in pr_showable s
  401. | VernacCheckGuard -> str"Guarded"
  402. (* Resetting *)
  403. | VernacRemoveName id -> str"Remove" ++ spc() ++ pr_lident id
  404. | VernacResetName id -> str"Reset" ++ spc() ++ pr_lident id
  405. | VernacResetInitial -> str"Reset Initial"
  406. | VernacBack i -> if i=1 then str"Back" else str"Back" ++ pr_intarg i
  407. | VernacBackTo i -> str"BackTo" ++ pr_intarg i
  408. (* State management *)
  409. | VernacWriteState s -> str"Write State" ++ spc () ++ qs s
  410. | VernacRestoreState s -> str"Restore State" ++ spc() ++ qs s
  411. (* Control *)
  412. | VernacList l ->
  413. hov 2 (str"[" ++ spc() ++
  414. prlist (fun v -> pr_located pr_vernac v ++ sep_end () ++ fnl()) l
  415. ++ spc() ++ str"]")
  416. | VernacLoad (f,s) -> str"Load" ++ if f then (spc() ++ str"Verbose"
  417. ++ spc()) else spc() ++ qs s
  418. | VernacTime v -> str"Time" ++ spc() ++ pr_vernac v
  419. (* Syntax *)
  420. | VernacTacticNotation (n,r,e) -> pr_grammar_tactic_rule n ("",r,e)
  421. | VernacOpenCloseScope (local,opening,sc) ->
  422. str (if opening then "Open " else "Close ") ++ pr_locality local ++
  423. str "Scope" ++ spc() ++ str sc
  424. | VernacDelimiters (sc,key) ->
  425. str"Delimit Scope" ++ spc () ++ str sc ++
  426. spc() ++ str "with " ++ str key
  427. | VernacBindScope (sc,cll) ->
  428. str"Bind Scope" ++ spc () ++ str sc ++
  429. spc() ++ str "with " ++ prlist_with_sep spc pr_class_rawexpr cll
  430. | VernacArgumentsScope (local,q,scl) -> let pr_opt_scope = function
  431. | None -> str"_"
  432. | Some sc -> str sc in
  433. str"Arguments Scope" ++ spc() ++ pr_non_locality local ++ pr_reference q
  434. ++ spc() ++ str"[" ++ prlist_with_sep sep pr_opt_scope scl ++ str"]"
  435. | VernacInfix (local,(s,mv),q,sn) -> (* A Verifier *)
  436. hov 0 (hov 0 (str"Infix " ++ pr_locality local
  437. ++ qs s ++ str " :=" ++ spc() ++ pr_reference q) ++
  438. pr_syntax_modifiers mv ++
  439. (match sn with
  440. | None -> mt()
  441. | Some sc -> spc() ++ str":" ++ spc() ++ str sc))
  442. | VernacNotation (local,c,(s,l),opt) ->
  443. let ps =
  444. let n = String.length s in
  445. if n > 2 & s.[0] = '\'' & s.[n-1] = '\''
  446. then
  447. let s' = String.sub s 1 (n-2) in
  448. if String.contains s' '\'' then qs s else str s'
  449. else qs s in
  450. hov 2( str"Notation" ++ spc() ++ pr_locality local ++ ps ++
  451. str " :=" ++ pr_constrarg c ++ pr_syntax_modifiers l ++
  452. (match opt with
  453. | None -> mt()
  454. | Some sc -> str" :" ++ spc() ++ str sc))
  455. | VernacSyntaxExtension (local,(s,l)) ->
  456. str"Reserved Notation" ++ spc() ++ pr_locality local ++ qs s ++
  457. pr_syntax_modifiers l
  458. (* Gallina *)
  459. | VernacDefinition (d,id,b,f) -> (* A verifier... *)
  460. let pr_def_token dk = str (string_of_definition_kind dk) in
  461. let pr_reduce = function
  462. | None -> mt()
  463. | Some r ->
  464. str"Eval" ++ spc() ++
  465. pr_red_expr (pr_constr, pr_lconstr, pr_or_by_notation pr_reference) r ++
  466. str" in" ++ spc() in
  467. let pr_def_body = function
  468. | DefineBody (bl,red,body,d) ->
  469. let ty = match d with
  470. | None -> mt()
  471. | Some ty -> spc() ++ str":" ++ pr_spc_lconstr ty
  472. in
  473. (pr_binders_arg bl,ty,Some (pr_reduce red ++ pr_lconstr body))
  474. | ProveBody (bl,t) ->
  475. (pr_binders_arg bl, str" :" ++ pr_spc_lconstr t, None) in
  476. let (binds,typ,c) = pr_def_body b in
  477. hov 2 (pr_def_token d ++ spc() ++ pr_lident id ++ binds ++ typ ++
  478. (match c with
  479. | None -> mt()
  480. | Some cc -> str" :=" ++ spc() ++ cc))
  481. | VernacStartTheoremProof (ki,l,_,_) ->
  482. let pr_statement head (id,(bl,c)) =
  483. hov 0
  484. (head ++ pr_opt pr_lident id ++ spc() ++
  485. (match bl with [] -> mt() | _ -> pr_binders bl ++ spc()) ++
  486. str":" ++ pr_spc_lconstr c) in
  487. hov 1 (pr_statement (pr_thm_token ki) (List.hd l) ++
  488. prlist (pr_statement (spc () ++ str "with")) (List.tl l))
  489. | VernacEndProof Admitted -> str"Admitted"
  490. | VernacEndProof (Proved (opac,o)) -> (match o with
  491. | None -> if opac then str"Qed" else str"Defined"
  492. | Some (id,th) -> (match th with
  493. | None -> (if opac then str"Save" else str"Defined") ++ spc() ++ pr_lident id
  494. | Some tok -> str"Save" ++ spc() ++ pr_thm_token tok ++ spc() ++ pr_lident id))
  495. | VernacExactProof c ->
  496. hov 2 (str"Proof" ++ pr_lconstrarg c)
  497. | VernacAssumption (stre,_,l) ->
  498. let n = List.length (List.flatten (List.map fst (List.map snd l))) in
  499. hov 2
  500. (pr_assumption_token (n > 1) stre ++ spc() ++
  501. pr_ne_params_list pr_lconstr_expr l)
  502. | VernacInductive (f,l) ->
  503. let pr_constructor (coe,(id,c)) =
  504. hov 2 (pr_lident id ++ str" " ++
  505. (if coe then str":>" else str":") ++
  506. pr_spc_lconstr c) in
  507. let pr_constructor_list b l = match l with
  508. | Constructors [] -> mt()
  509. | Constructors l ->
  510. pr_com_at (begin_of_inductive l) ++
  511. fnl() ++
  512. str (if List.length l = 1 then " " else " | ") ++
  513. prlist_with_sep (fun _ -> fnl() ++ str" | ") pr_constructor l
  514. | RecordDecl (c,fs) ->
  515. spc() ++
  516. pr_record_decl b c fs in
  517. let pr_oneind key (((coe,id),indpar,s,k,lc),ntn) =
  518. let kw =
  519. str (match k with Record -> "Record" | Structure -> "Structure"
  520. | Inductive_kw -> "Inductive" | CoInductive -> "CoInductive"
  521. | Class b -> if b then "Definitional Class" else "Class")
  522. in
  523. hov 0 (
  524. kw ++ spc() ++
  525. (if coe then str" > " else str" ") ++ pr_lident id ++
  526. pr_and_type_binders_arg indpar ++ spc() ++
  527. Option.cata (fun s -> str":" ++ spc() ++ pr_lconstr_expr s) (mt()) s ++
  528. str" :=") ++ pr_constructor_list k lc ++
  529. pr_decl_notation pr_constr ntn
  530. in
  531. hov 1 (pr_oneind (if (Decl_kinds.recursivity_flag_of_kind f) then "Inductive" else "CoInductive") (List.hd l))
  532. ++
  533. (prlist (fun ind -> fnl() ++ hov 1 (pr_oneind "with" ind)) (List.tl l))
  534. | VernacFixpoint (recs,b) ->
  535. let name_of_binder = function
  536. | LocalRawAssum (nal,_,_) -> nal
  537. | LocalRawDef (_,_) -> [] in
  538. let pr_onerec = function
  539. | ((loc,id),(n,ro),bl,type_,def),ntn ->
  540. let (bl',def,type_) =
  541. if Flags.do_beautify() then extract_def_binders def type_
  542. else ([],def,type_) in
  543. let bl = bl @ bl' in
  544. let ids = List.flatten (List.map name_of_binder bl) in
  545. let annot =
  546. match n with
  547. | None -> mt ()
  548. | Some (loc, id) ->
  549. match (ro : Topconstr.recursion_order_expr) with
  550. CStructRec ->
  551. if List.length ids > 1 then
  552. spc() ++ str "{struct " ++ pr_id id ++ str"}"
  553. else mt()
  554. | CWfRec c ->
  555. spc() ++ str "{wf " ++ pr_lconstr_expr c ++ spc() ++
  556. pr_id id ++ str"}"
  557. | CMeasureRec c ->
  558. spc() ++ str "{measure " ++ pr_lconstr_expr c ++ spc() ++
  559. pr_id id ++ str"}"
  560. in
  561. pr_id id ++ pr_binders_arg bl ++ annot ++ spc()
  562. ++ pr_type_option (fun c -> spc() ++ pr_lconstr_expr c) type_
  563. ++ str" :=" ++ brk(1,1) ++ pr_lconstr def ++
  564. pr_decl_notation pr_constr ntn
  565. in
  566. let start = if b then "Boxed Fixpoint" else "Fixpoint" in
  567. hov 1 (str start ++ spc() ++
  568. prlist_with_sep (fun _ -> fnl() ++ fnl() ++ str"with ") pr_onerec recs)
  569. | VernacCoFixpoint (corecs,b) ->
  570. let pr_onecorec (((loc,id),bl,c,def),ntn) =
  571. let (bl',def,c) =
  572. if Flags.do_beautify() then extract_def_binders def c
  573. else ([],def,c) in
  574. let bl = bl @ bl' in
  575. pr_id id ++ spc() ++ pr_binders bl ++ spc() ++ str":" ++
  576. spc() ++ pr_lconstr_expr c ++
  577. str" :=" ++ brk(1,1) ++ pr_lconstr def ++
  578. pr_decl_notation pr_constr ntn
  579. in
  580. let start = if b then "Boxed CoFixpoint" else "CoFixpoint" in
  581. hov 1 (str start ++ spc() ++
  582. prlist_with_sep (fun _ -> fnl() ++ str"with ") pr_onecorec corecs)
  583. | VernacScheme l ->
  584. hov 2 (str"Scheme" ++ spc() ++
  585. prlist_with_sep (fun _ -> fnl() ++ str"with ") pr_onescheme l)
  586. | VernacCombinedScheme (id, l) ->
  587. hov 2 (str"Combined Scheme" ++ spc() ++
  588. pr_lident id ++ spc() ++ str"from" ++ spc() ++
  589. prlist_with_sep (fun _ -> fnl() ++ str", ") pr_lident l)
  590. (* Gallina extensions *)
  591. | VernacBeginSection id -> hov 2 (str"Section" ++ spc () ++ pr_lident id)
  592. | VernacEndSegment id -> hov 2 (str"End" ++ spc() ++ pr_lident id)
  593. | VernacRequire (exp,spe,l) -> hov 2
  594. (str "Require" ++ spc() ++ pr_require_token exp ++
  595. (match spe with
  596. | None -> mt()
  597. | Some flag ->
  598. (if flag then str"Specification" else str"Implementation") ++
  599. spc ()) ++
  600. prlist_with_sep sep pr_module l)
  601. | VernacImport (f,l) ->
  602. (if f then str"Export" else str"Import") ++ spc() ++
  603. prlist_with_sep sep pr_import_module l
  604. | VernacCanonical q -> str"Canonical Structure" ++ spc() ++ pr_reference q
  605. | VernacCoercion (s,id,c1,c2) ->
  606. hov 1 (
  607. str"Coercion" ++ (match s with | Local -> spc() ++
  608. str"Local" ++ spc() | Global -> spc()) ++
  609. pr_reference id ++ spc() ++ str":" ++ spc() ++ pr_class_rawexpr c1 ++
  610. spc() ++ str">->" ++ spc() ++ pr_class_rawexpr c2)
  611. | VernacIdentityCoercion (s,id,c1,c2) ->
  612. hov 1 (
  613. str"Identity Coercion" ++ (match s with | Local -> spc() ++
  614. str"Local" ++ spc() | Global -> spc()) ++ pr_lident id ++
  615. spc() ++ str":" ++ spc() ++ pr_class_rawexpr c1 ++ spc() ++ str">->" ++
  616. spc() ++ pr_class_rawexpr c2)
  617. | VernacInstance (glob, sup, (instid, bk, cl), props, pri) ->
  618. hov 1 (
  619. pr_non_locality (not glob) ++
  620. str"Instance" ++ spc () ++
  621. pr_and_type_binders_arg sup ++
  622. str"=>" ++ spc () ++
  623. (match snd instid with Name id -> pr_lident (fst instid, id) ++ spc () ++ str":" ++ spc () | Anonymous -> mt ()) ++
  624. pr_constr_expr cl ++ spc () ++
  625. spc () ++ str":=" ++ spc () ++
  626. pr_constr_expr props)
  627. | VernacContext l ->
  628. hov 1 (
  629. str"Context" ++ spc () ++ str"[" ++ spc () ++
  630. pr_and_type_binders_arg l ++ spc () ++ str "]")
  631. | VernacDeclareInstance id ->
  632. hov 1 (str"Instance" ++ spc () ++ pr_lident id)
  633. (* Modules and Module Types *)
  634. | VernacDefineModule (export,m,bl,ty,bd) ->
  635. let b = pr_module_binders_list bl pr_lconstr in
  636. hov 2 (str"Module" ++ spc() ++ pr_require_token export ++
  637. pr_lident m ++ b ++
  638. pr_opt (pr_of_module_type pr_lconstr) ty ++
  639. pr_opt (fun me -> str ":= " ++ pr_module_expr me) bd)
  640. | VernacDeclareModule (export,id,bl,m1) ->
  641. let b = pr_module_binders_list bl pr_lconstr in
  642. hov 2 (str"Declare Module" ++ spc() ++ pr_require_token export ++
  643. pr_lident id ++ b ++
  644. pr_of_module_type pr_lconstr m1)
  645. | VernacDeclareModuleType (id,bl,m) ->
  646. let b = pr_module_binders_list bl pr_lconstr in
  647. hov 2 (str"Module Type " ++ pr_lident id ++ b ++
  648. pr_opt (fun mt -> str ":= " ++ pr_module_type pr_lconstr mt) m)
  649. | VernacInclude (in_ast) ->
  650. begin
  651. match in_ast with
  652. | CIMTE mty ->
  653. hov 2 (str"Include" ++
  654. (fun mt -> str " " ++ pr_module_type pr_lconstr mt) mty)
  655. | CIME mexpr ->
  656. hov 2 (str"Include" ++
  657. (fun me -> str " " ++ pr_module_expr me) mexpr)
  658. end
  659. (* Solving *)
  660. | VernacSolve (i,tac,deftac) ->
  661. (if i = 1 then mt() else int i ++ str ": ") ++
  662. pr_raw_tactic tac
  663. ++ (try if deftac & Pfedit.get_end_tac() <> None then str ".." else mt ()
  664. with UserError _|Stdpp.Exc_located _ -> mt())
  665. | VernacSolveExistential (i,c) ->
  666. str"Existential " ++ int i ++ pr_lconstrarg c
  667. (* MMode *)
  668. | VernacProofInstr instr -> anomaly "Not implemented"
  669. | VernacDeclProof -> str "proof"
  670. | VernacReturn -> str "return"
  671. (* /MMode *)
  672. (* Auxiliary file and library management *)
  673. | VernacRequireFrom (exp,spe,f) -> hov 2
  674. (str"Require" ++ spc() ++ pr_require_token exp ++
  675. (match spe with
  676. | None -> mt()
  677. | Some false -> str"Implementation" ++ spc()
  678. | Some true -> str"Specification" ++ spc ()) ++
  679. qs f)
  680. | VernacAddLoadPath (fl,s,d) -> hov 2
  681. (str"Add" ++
  682. (if fl then str" Rec " else spc()) ++
  683. str"LoadPath" ++ spc() ++ qs s ++
  684. (match d with
  685. | None -> mt()
  686. | Some dir -> spc() ++ str"as" ++ spc() ++ pr_dirpath dir))
  687. | VernacRemoveLoadPath s -> str"Remove LoadPath" ++ qs s
  688. | VernacAddMLPath (fl,s) ->
  689. str"Add" ++ (if fl then str" Rec " else spc()) ++ str"ML Path" ++ qs s
  690. | VernacDeclareMLModule l ->
  691. hov 2 (str"Declare ML Module" ++ spc() ++ prlist_with_sep sep qs l)
  692. | VernacChdir s -> str"Cd" ++ pr_opt qs s
  693. (* Commands *)
  694. | VernacDeclareTacticDefinition (rc,l) ->
  695. let pr_tac_body (id, redef, body) =
  696. let idl, body =
  697. match body with
  698. | Tacexpr.TacFun (idl,b) -> idl,b
  699. | _ -> [], body in
  700. pr_ltac_id id ++
  701. prlist (function None -> str " _"
  702. | Some id -> spc () ++ pr_id id) idl
  703. ++ (if redef then str" ::=" else str" :=") ++ brk(1,1) ++
  704. let idl = List.map Option.get (List.filter (fun x -> not (x=None)) idl)in
  705. pr_raw_tactic_env
  706. (idl @ List.map coerce_global_to_id
  707. (List.map (fun (x, _, _) -> x) (List.filter (fun (_, redef, _) -> not redef) l)))
  708. (Global.env())
  709. body in
  710. hov 1
  711. ((str "Ltac ") ++
  712. prlist_with_sep (fun () -> fnl() ++ str"with ") pr_tac_body l)
  713. | VernacCreateHintDb (local,dbname,b) ->
  714. hov 1 (str "Create " ++ pr_locality local ++ str "HintDb " ++ str dbname ++ (if b then str" discriminated" else mt ()))
  715. | VernacHints (local,dbnames,h) ->
  716. pr_hints local dbnames h pr_constr pr_constr_pattern_expr
  717. | VernacSyntacticDefinition (id,(ids,c),local,onlyparsing) ->
  718. hov 2
  719. (str"Notation " ++ pr_locality local ++ pr_lident id ++
  720. prlist_with_sep spc pr_id ids ++ str" :=" ++ pr_constrarg c ++
  721. pr_syntax_modifiers (if onlyparsing then [SetOnlyParsing] else []))
  722. | VernacDeclareImplicits (local,q,None) ->
  723. hov 2 (str"Implicit Arguments" ++ spc() ++ pr_reference q)
  724. | VernacDeclareImplicits (local,q,Some imps) ->
  725. hov 1 (str"Implicit Arguments " ++ pr_non_locality local ++
  726. spc() ++ pr_reference q ++ spc() ++
  727. str"[" ++ prlist_with_sep sep pr_explanation imps ++ str"]")
  728. | VernacReserve (idl,c) ->
  729. hov 1 (str"Implicit Type" ++
  730. str (if List.length idl > 1 then "s " else " ") ++
  731. prlist_with_sep spc pr_lident idl ++ str " :" ++ spc () ++
  732. pr_lconstr c)
  733. | VernacSetOpacity(b,[k,l]) when k=Conv_oracle.transparent ->
  734. hov 1 (str"Transparent" ++ pr_non_locality b ++
  735. spc() ++ prlist_with_sep sep pr_reference l)
  736. | VernacSetOpacity(b,[Conv_oracle.Opaque,l]) ->
  737. hov 1 (str"Opaque" ++ pr_non_locality b ++
  738. spc() ++ prlist_with_sep sep pr_reference l)
  739. | VernacSetOpacity (local,l) ->
  740. let pr_lev = function
  741. Conv_oracle.Opaque -> str"opaque"
  742. | Conv_oracle.Expand -> str"expand"
  743. | l when l = Conv_oracle.transparent -> str"transparent"
  744. | Conv_oracle.Level n -> int n in
  745. let pr_line (l,q) =
  746. hov 2 (pr_lev l ++ spc() ++
  747. str"[" ++ prlist_with_sep sep pr_reference q ++ str"]") in
  748. hov 1 (pr_locality local ++ str"Strategy" ++ spc() ++
  749. hv 0 (prlist_with_sep sep pr_line l))
  750. | VernacUnsetOption na ->
  751. hov 1 (str"Unset" ++ spc() ++ pr_printoption na None)
  752. | VernacSetOption (na,v) -> hov 2 (str"Set" ++ spc() ++ pr_set_option na v)
  753. | VernacAddOption (na,l) -> hov 2 (str"Add" ++ spc() ++ pr_printoption na (Some l))
  754. | VernacRemoveOption (na,l) -> hov 2 (str"Remove" ++ spc() ++ pr_printoption na (Some l))
  755. | VernacMemOption (na,l) -> hov 2 (str"Test" ++ spc() ++ pr_printoption na (Some l))
  756. | VernacPrintOption na -> hov 2 (str"Test" ++ spc() ++ pr_printoption na None)
  757. | VernacCheckMayEval (r,io,c) ->
  758. let pr_mayeval r c = match r with
  759. | Some r0 ->
  760. hov 2 (str"Eval" ++ spc() ++
  761. pr_red_expr (pr_constr,pr_lconstr,pr_or_by_notation pr_reference) r0 ++
  762. spc() ++ str"in" ++ spc () ++ pr_constr c)
  763. | None -> hov 2 (str"Check" ++ spc() ++ pr_constr c)
  764. in
  765. (if io = None then mt() else int (Option.get io) ++ str ": ") ++
  766. pr_mayeval r c
  767. | VernacGlobalCheck c -> hov 2 (str"Type" ++ pr_constrarg c)
  768. | VernacPrint p ->
  769. let pr_printable = function
  770. | PrintFullContext -> str"Print All"
  771. | PrintSectionContext s ->
  772. str"Print Section" ++ spc() ++ Libnames.pr_reference s
  773. | PrintGrammar ent ->
  774. str"Print Grammar" ++ spc() ++ str ent
  775. | PrintLoadPath dir -> str"Print LoadPath" ++ pr_opt pr_dirpath dir
  776. | PrintModules -> str"Print Modules"
  777. | PrintMLLoadPath -> str"Print ML Path"
  778. | PrintMLModules -> str"Print ML Modules"
  779. | PrintGraph -> str"Print Graph"
  780. | PrintClasses -> str"Print Classes"
  781. | PrintTypeClasses -> str"Print TypeClasses"
  782. | PrintInstances qid -> str"Print Instances" ++ spc () ++ pr_reference qid
  783. | PrintLtac qid -> str"Print Ltac" ++ spc() ++ pr_reference qid
  784. | PrintCoercions -> str"Print Coercions"
  785. | PrintCoercionPaths (s,t) -> str"Print Coercion Paths" ++ spc() ++ pr_class_rawexpr s ++ spc() ++ pr_class_rawexpr t
  786. | PrintCanonicalConversions -> str"Print Canonical Structures"
  787. | PrintTables -> str"Print Tables"
  788. | PrintOpaqueName qid -> str"Print Term" ++ spc() ++ pr_reference qid
  789. | PrintHintGoal -> str"Print Hint"
  790. | PrintHint qid -> str"Print Hint" ++ spc() ++ pr_reference qid
  791. | PrintHintDb -> str"Print Hint *"
  792. | PrintHintDbName s -> str"Print HintDb" ++ spc() ++ str s
  793. | PrintRewriteHintDbName s -> str"Print Rewrite HintDb" ++ spc() ++ str s
  794. | PrintUniverses fopt -> str"Dump Universes" ++ pr_opt str fopt
  795. | PrintName qid -> str"Print" ++ spc() ++ pr_reference qid
  796. | PrintModuleType qid -> str"Print Module Type" ++ spc() ++ pr_reference qid
  797. | PrintModule qid -> str"Print Module" ++ spc() ++ pr_reference qid
  798. | PrintInspect n -> str"Inspect" ++ spc() ++ int n
  799. | PrintScopes -> str"Print Scopes"
  800. | PrintScope s -> str"Print Scope" ++ spc() ++ str s
  801. | PrintVisibility s -> str"Print Visibility" ++ pr_opt str s
  802. | PrintAbout qid -> str"About" ++ spc() ++ pr_reference qid
  803. | PrintImplicit qid -> str"Print Implicit" ++ spc() ++ pr_reference qid
  804. (* spiwack: command printing all the axioms and section variables used in a
  805. term *)
  806. | PrintAssumptions (b,qid) -> (if b then str"Print Assumptions" else str"Print Opaque Dependencies")
  807. ++spc()++pr_reference qid
  808. in pr_printable p
  809. | VernacSearch (sea,sea_r) -> pr_search sea sea_r pr_constr_pattern_expr
  810. | VernacLocate loc ->
  811. let pr_locate =function
  812. | LocateTerm qid -> pr_reference qid
  813. | LocateFile f -> str"File" ++ spc() ++ qs f
  814. | LocateLibrary qid -> str"Library" ++ spc () ++ pr_module qid
  815. | LocateModule qid -> str"Module" ++ spc () ++ pr_module qid
  816. | LocateNotation s -> qs s
  817. in str"Locate" ++ spc() ++ pr_locate loc
  818. | VernacComments l ->
  819. hov 2
  820. (str"Comments" ++ spc() ++ prlist_with_sep sep (pr_comment pr_constr) l)
  821. | VernacNop -> mt()
  822. (* Toplevel control *)
  823. | VernacToplevelControl exn -> pr_topcmd exn
  824. (* For extension *)
  825. | VernacExtend (s,c) -> pr_extend s c
  826. | VernacProof (Tacexpr.TacId _) -> str "Proof"
  827. | VernacProof te -> str "Proof with" ++ spc() ++ pr_raw_tactic te
  828. and pr_extend s cl =
  829. let pr_arg a =
  830. try pr_gen (Global.env()) a
  831. with Failure _ -> str ("<error in "^s^">") in
  832. try
  833. let rls = List.assoc s (Egrammar.get_extend_vernac_grammars()) in
  834. let rl = match_vernac_rule (List.map Genarg.genarg_tag cl) rls in
  835. let start,rl,cl =
  836. match rl with
  837. | Egrammar.TacTerm s :: rl -> str s, rl, cl
  838. | Egrammar.TacNonTerm _ :: rl -> pr_arg (List.hd cl), rl, List.tl cl
  839. | [] -> anomaly "Empty entry" in
  840. let (pp,_) =
  841. List.fold_left
  842. (fun (strm,args) pi ->
  843. let pp,args = match pi with
  844. | Egrammar.TacNonTerm _ -> (pr_arg (List.hd args), List.tl args)
  845. | Egrammar.TacTerm s -> (str s, args) in
  846. (strm ++ spc() ++ pp), args)
  847. (start,cl) rl in
  848. hov 1 pp
  849. with Not_found ->
  850. hov 1 (str ("TODO("^s) ++ prlist_with_sep sep pr_arg cl ++ str ")")
  851. in pr_vernac
  852. let pr_vernac v = make_pr_vernac pr_constr_expr pr_lconstr_expr v ++ sep_end ()