1 (* Copyright (C) 2004, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
30 let tactical_terminator = ""
31 let tactic_terminator = tactical_terminator
32 let command_terminator = tactical_terminator
34 let pp_idents idents =
35 let map = function Some s -> s | None -> "_" in
36 "(" ^ String.concat " " (List.map map idents) ^ ")"
37 let pp_hyps idents = String.concat " " idents
39 let pp_reduction_kind ~term_pp = function
40 | `Normalize -> "normalize"
42 | `Simpl -> "simplify"
43 | `Unfold (Some t) -> "unfold " ^ term_pp t
44 | `Unfold None -> "unfold"
47 let pp_tactic_pattern ~map_unicode_to_tex ~term_pp ~lazy_term_pp (what, hyp, goal) =
48 if what = None && hyp = [] && goal = None then "" else
52 | Some t -> Printf.sprintf "in match (%s) " (lazy_term_pp t) in
55 (List.map (fun (name, p) -> Printf.sprintf "%s:(%s)" name (term_pp p)) hyp) in
60 let vdash = if map_unicode_to_tex then "\\vdash" else "⊢" in
61 Printf.sprintf "%s (%s)" vdash (term_pp t)
63 Printf.sprintf "%sin %s%s" what_text hyp_text goal_text
65 let pp_intros_specs s = function
67 | Some num, [] -> Printf.sprintf " %s%i" s num
68 | None, idents -> Printf.sprintf " %s%s" s (pp_idents idents)
69 | Some num, idents -> Printf.sprintf " %s%i %s" s num (pp_idents idents)
71 let pp_terms ~term_pp terms = String.concat ", " (List.map term_pp terms)
73 let opt_string_pp = function
75 | Some what -> what ^ " "
77 let pp_auto_params ~term_pp (univ, params) =
79 (List.map (fun (k,v) -> if v <> "" then k ^ "=" ^ v else k) params) ^
82 | Some l -> (if params <> [] then " " else "") ^ "by " ^
83 String.concat " " (List.map term_pp l)
86 let pp_just ~term_pp =
88 `Term term -> "exact " ^ term_pp term
89 | `Auto params -> pp_auto_params ~term_pp params
92 let rec pp_ntactic ~map_unicode_to_tex =
93 let term_pp = CicNotationPp.pp_term in
94 let lazy_term_pp = fun _ -> assert false in
95 let pp_tactic_pattern =
96 pp_tactic_pattern ~map_unicode_to_tex ~lazy_term_pp ~term_pp in
98 | NApply (_,t) -> "napply " ^ CicNotationPp.pp_term t
99 | NSmartApply (_,t) -> "fixme"
100 | NAuto (_,(None,flgs)) ->
102 String.concat " " (List.map (fun a,b -> a ^ "=" ^ b) flgs)
103 | NAuto (_,(Some l,flgs)) ->
104 "nautobatch" ^ " by " ^
105 (String.concat "," (List.map CicNotationPp.pp_term l)) ^
106 String.concat " " (List.map (fun a,b -> a ^ "=" ^ b) flgs)
107 | NCases (_,what,where) -> "ncases " ^ CicNotationPp.pp_term what ^
108 assert false ^ " " ^ assert false
109 | NConstructor (_,None,l) -> "@ " ^
110 String.concat " " (List.map CicNotationPp.pp_term l)
111 | NConstructor (_,Some x,l) -> "@" ^ string_of_int x ^ " " ^
112 String.concat " " (List.map CicNotationPp.pp_term l)
113 | NCase1 (_,n) -> "*" ^ n ^ ":"
114 | NChange (_,what,wwhat) -> "nchange " ^ assert false ^
115 " with " ^ CicNotationPp.pp_term wwhat
116 | NCut (_,t) -> "ncut " ^ CicNotationPp.pp_term t
117 (*| NDiscriminate (_,t) -> "ndiscriminate " ^ CicNotationPp.pp_term t
118 | NSubst (_,t) -> "nsubst " ^ CicNotationPp.pp_term t *)
119 | NDestruct (_,dom,skip) -> "ndestruct ..."
120 | NElim (_,what,where) -> "nelim " ^ CicNotationPp.pp_term what ^
121 assert false ^ " " ^ assert false
123 | NIntro (_,n) -> "#" ^ n
124 | NInversion (_,what,where) -> "ninversion " ^ CicNotationPp.pp_term what ^
125 assert false ^ " " ^ assert false
126 | NLApply (_,t) -> "lapply " ^ CicNotationPp.pp_term t
127 | NRewrite (_,dir,n,where) -> "nrewrite " ^
128 (match dir with `LeftToRight -> ">" | `RightToLeft -> "<") ^
129 " " ^ CicNotationPp.pp_term n ^ " " ^ pp_tactic_pattern where
130 | NReduce _ | NGeneralize _ | NLetIn _ | NAssert _ -> "TO BE IMPLEMENTED"
132 | NSemicolon _ -> "##;"
135 | NPos (_, l) -> "##" ^String.concat "," (List.map string_of_int l)^ ":"
136 | NPosbyname (_, s) -> "##" ^ s ^ ":"
137 | NWildcard _ -> "##*:"
140 Printf.sprintf "##focus %s"
141 (String.concat " " (List.map string_of_int l))
142 | NUnfocus _ -> "##unfocus"
143 | NSkip _ -> "##skip"
144 | NTry (_,tac) -> "ntry " ^ pp_ntactic ~map_unicode_to_tex tac
145 | NAssumption _ -> "nassumption"
147 "(" ^ String.concat " " (List.map (pp_ntactic ~map_unicode_to_tex) l)^ ")"
148 | NRepeat (_,t) -> "nrepeat " ^ pp_ntactic ~map_unicode_to_tex t
151 let rec pp_tactic ~map_unicode_to_tex ~term_pp ~lazy_term_pp =
152 let pp_terms = pp_terms ~term_pp in
153 let pp_tactics = pp_tactics ~map_unicode_to_tex ~term_pp ~lazy_term_pp in
154 let pp_reduction_kind = pp_reduction_kind ~term_pp in
155 let pp_tactic_pattern =
156 pp_tactic_pattern ~map_unicode_to_tex ~lazy_term_pp ~term_pp in
159 (* Higher order tactics *)
160 | Do (_, count, tac) ->
161 Printf.sprintf "do %d %s" count (pp_tactic tac)
162 | Repeat (_, tac) -> "repeat " ^ pp_tactic tac
163 | Seq (_, tacs) -> pp_tactics ~sep:"; " tacs
164 | Then (_, tac, tacs) ->
165 Printf.sprintf "%s; [%s]" (pp_tactic tac)
166 (pp_tactics ~sep:" | " tacs)
168 Printf.sprintf "tries [%s]" (pp_tactics ~sep:" | " tacs)
169 | Try (_, tac) -> "try " ^ pp_tactic tac
171 Printf.sprintf "solve [%s]" (pp_tactics ~sep:" | " tac)
172 | Progress (_, tac) -> "progress " ^ pp_tactic tac
173 (* First order tactics *)
174 | Absurd (_, term) -> "absurd" ^ term_pp term
175 | Apply (_, term) -> "apply " ^ term_pp term
176 | ApplyRule (_, term) -> "apply rule " ^ term_pp term
177 | ApplyP (_, term) -> "applyP " ^ term_pp term
178 | ApplyS (_, term, params) ->
179 "applyS " ^ term_pp term ^ pp_auto_params ~term_pp params
180 | AutoBatch (_,params) -> "autobatch " ^
181 pp_auto_params ~term_pp params
182 | Assumption _ -> "assumption"
183 | Cases (_, term, pattern, specs) ->
184 Printf.sprintf "cases %s %s%s"
186 (pp_tactic_pattern pattern)
187 (pp_intros_specs "names " specs)
188 | Change (_, where, with_what) ->
189 Printf.sprintf "change %s with %s" (pp_tactic_pattern where) (lazy_term_pp with_what)
190 | Clear (_,ids) -> Printf.sprintf "clear %s" (pp_hyps ids)
191 | ClearBody (_,id) -> Printf.sprintf "clearbody %s" (pp_hyps [id])
192 | Constructor (_,n) -> "constructor " ^ string_of_int n
193 | Compose (_,t1, t2, times, intro_specs) ->
194 Printf.sprintf "compose %s%s %s%s"
195 (if times > 0 then string_of_int times ^ " " else "")
196 (term_pp t1) (match t2 with None -> "" | Some t2 -> "with "^term_pp t2)
197 (pp_intros_specs " as " intro_specs)
198 | Contradiction _ -> "contradiction"
199 | Cut (_, ident, term) ->
200 "cut " ^ term_pp term ^
201 (match ident with None -> "" | Some id -> " as " ^ id)
202 | Decompose (_, names) ->
203 Printf.sprintf "decompose%s"
204 (pp_intros_specs "names " (None, names))
205 | Demodulate (_, params) -> "demodulate " ^ pp_auto_params ~term_pp params
206 | Destruct (_, None) -> "destruct"
207 | Destruct (_, Some terms) -> "destruct " ^ pp_terms terms
208 | Elim (_, what, using, pattern, specs) ->
209 Printf.sprintf "elim %s%s %s%s"
211 (match using with None -> "" | Some term -> " using " ^ term_pp term)
212 (pp_tactic_pattern pattern)
213 (pp_intros_specs "names " specs)
214 | ElimType (_, term, using, specs) ->
215 Printf.sprintf "elim type %s%s%s"
217 (match using with None -> "" | Some term -> " using " ^ term_pp term)
218 (pp_intros_specs "names " specs)
219 | Exact (_, term) -> "exact " ^ term_pp term
220 | Exists _ -> "exists"
221 | Fold (_, kind, term, pattern) ->
222 Printf.sprintf "fold %s %s %s" (pp_reduction_kind kind)
223 (lazy_term_pp term) (pp_tactic_pattern pattern)
224 | FwdSimpl (_, hyp, names) ->
225 Printf.sprintf "fwd %s%s" hyp (pp_intros_specs "names " (None, names))
226 | Generalize (_, pattern, ident) ->
227 Printf.sprintf "generalize %s%s" (pp_tactic_pattern pattern)
228 (match ident with None -> "" | Some id -> " as " ^ id)
230 | Fourier _ -> "fourier"
232 | Intros (_, specs) -> Printf.sprintf "intros%s" (pp_intros_specs "" specs)
233 | Inversion (_, term) -> "inversion " ^ term_pp term
234 | LApply (_, linear, level_opt, terms, term, ident_opt) ->
235 Printf.sprintf "lapply %s%s%s%s%s"
236 (if linear then " linear " else "")
237 (match level_opt with None -> "" | Some i -> " depth = " ^ string_of_int i ^ " ")
239 (match terms with [] -> "" | _ -> " to " ^ pp_terms terms)
240 (match ident_opt with None -> "" | Some ident -> " as " ^ ident)
242 | LetIn (_, term, ident) ->
243 Printf.sprintf "letin %s \\def %s" ident (term_pp term)
244 | Reduce (_, kind, pat) ->
245 Printf.sprintf "%s %s" (pp_reduction_kind kind) (pp_tactic_pattern pat)
246 | Reflexivity _ -> "reflexivity"
247 | Replace (_, pattern, t) ->
248 Printf.sprintf "replace %s with %s" (pp_tactic_pattern pattern) (lazy_term_pp t)
249 | Rewrite (_, pos, t, pattern, names) ->
250 Printf.sprintf "rewrite %s %s %s%s"
251 (if pos = `LeftToRight then ">" else "<")
253 (pp_tactic_pattern pattern)
254 (if names = [] then "" else " as " ^ pp_idents names)
258 | Symmetry _ -> "symmetry"
259 | Transitivity (_, term) -> "transitivity " ^ term_pp term
260 (* Tattiche Aggiunte *)
261 | Assume (_, ident , term) -> "assume" ^ ident ^ ":" ^ term_pp term
262 | Suppose (_, term, ident,term1) -> "suppose" ^ term_pp term ^ "(" ^ ident ^ ")" ^ (match term1 with None -> " " | Some term1 -> term_pp term1)
263 | Bydone (_, just) -> pp_just ~term_pp just ^ "done"
264 | By_just_we_proved (_, just, term1, ident, term2) -> pp_just ~term_pp just ^ "we proved" ^ term_pp term1 ^ (match ident with None -> "" | Some ident -> "(" ^ident^ ")") ^
265 (match term2 with None -> " " | Some term2 -> term_pp term2)
266 | We_need_to_prove (_, term, ident, term1) -> "we need to prove" ^ term_pp term ^ (match ident with None -> "" | Some ident -> "(" ^ ident ^ ")") ^ (match term1 with None -> " " | Some term1 -> term_pp term1)
267 | We_proceed_by_cases_on (_, term, term1) -> "we proceed by cases on" ^ term_pp term ^ "to prove" ^ term_pp term1
268 | We_proceed_by_induction_on (_, term, term1) -> "we proceed by induction on" ^ term_pp term ^ "to prove" ^ term_pp term1
269 | Byinduction (_, term, ident) -> "by induction hypothesis we know" ^ term_pp term ^ "(" ^ ident ^ ")"
270 | Thesisbecomes (_, term) -> "the thesis becomes " ^ term_pp term
271 | ExistsElim (_, just, ident, term, ident1, term1) -> pp_just ~term_pp just ^ "let " ^ ident ^ ":" ^ term_pp term ^ "such that " ^ lazy_term_pp term1 ^ "(" ^ ident1 ^ ")"
272 | AndElim (_, just, ident1, term1, ident2, term2) -> pp_just ~term_pp just ^ "we have " ^ term_pp term1 ^ " (" ^ ident1 ^ ") " ^ "and " ^ term_pp term2 ^ " (" ^ ident2 ^ ")"
273 | RewritingStep (_, term, term1, term2, cont) ->
276 | Some (None,term) -> "conclude " ^ term_pp term
277 | Some (Some name,term) ->
278 "obtain (" ^ name ^ ") " ^ term_pp term)
282 | `Auto params -> pp_auto_params ~term_pp params
283 | `Term term2 -> " exact " ^ term_pp term2
285 | `SolveWith term -> " using " ^ term_pp term)
286 ^ (if cont then " done" else "")
287 | Case (_, id, args) ->
290 (List.map (function (id,term) -> "(" ^ id ^ ": " ^ term_pp term ^ ")")
294 and pp_tactics ~map_unicode_to_tex ~term_pp ~lazy_term_pp ~sep tacs =
296 (List.map (pp_tactic ~map_unicode_to_tex ~lazy_term_pp ~term_pp) tacs)
298 let pp_search_kind = function
299 | `Locate -> "locate"
303 | `Instance -> "instance"
305 let pp_arg ~term_pp arg =
306 let s = term_pp arg in
307 if s = "" || (s.[0] = '(' && s.[String.length s - 1] = ')') then
308 (* _nice_ heuristic *)
313 let pp_nmacro = function
314 | NCheck (_, term) -> Printf.sprintf "ncheck %s" (CicNotationPp.pp_term term)
315 | Screenshot (_, name) -> Printf.sprintf "screenshot \"%s\"" name
318 let pp_macro ~term_pp ~lazy_term_pp =
319 let term_pp = pp_arg ~term_pp in
320 let flavour_pp = function
321 | `Definition -> "definition"
324 | `Remark -> "remark"
325 | `Theorem -> "theorem"
326 | `Variant -> "variant"
328 | `MutualDefinition -> assert false
330 let pp_inline_params l =
331 let pp_param = function
332 | IPPrefix prefix -> "prefix = \"" ^ prefix ^ "\""
333 | IPAs flavour -> flavour_pp flavour
334 | IPCoercions -> "coercions"
335 | IPDebug debug -> "debug = " ^ string_of_int debug
336 | IPProcedural -> "procedural"
337 | IPNoDefaults -> "nodefaults"
338 | IPDepth depth -> "depth = " ^ string_of_int depth
339 | IPLevel level -> "level = " ^ string_of_int level
340 | IPComments -> "comments"
343 let s = String.concat " " (List.map pp_param l) in
344 if s = "" then s else " " ^ s
346 let pp_reduction_kind = pp_reduction_kind ~term_pp:lazy_term_pp in
349 | WInstance (_, term) -> "whelp instance " ^ term_pp term
350 | WHint (_, t) -> "whelp hint " ^ term_pp t
351 | WLocate (_, s) -> "whelp locate \"" ^ s ^ "\""
352 | WElim (_, t) -> "whelp elim " ^ term_pp t
353 | WMatch (_, term) -> "whelp match " ^ term_pp term
355 | Eval (_, kind, term) ->
356 Printf.sprintf "eval %s on %s" (pp_reduction_kind kind) (term_pp term)
357 | Check (_, term) -> Printf.sprintf "check %s" (term_pp term)
358 | Hint (_, true) -> "hint rewrite"
359 | Hint (_, false) -> "hint"
360 | AutoInteractive (_,params) -> "auto " ^ pp_auto_params ~term_pp params
361 | Inline (_, suri, params) ->
362 Printf.sprintf "inline \"%s\"%s" suri (pp_inline_params params)
364 let pp_associativity = function
365 | Gramext.LeftA -> "left associative"
366 | Gramext.RightA -> "right associative"
367 | Gramext.NonA -> "non associative"
369 let pp_precedence i = Printf.sprintf "with precedence %d" i
371 let pp_default what uris =
372 Printf.sprintf "default \"%s\" %s" what
373 (String.concat " " (List.map UriManager.string_of_uri uris))
375 let pp_coercion ~term_pp t do_composites arity saturations=
376 Printf.sprintf "coercion %s %d %d %s"
377 (term_pp t) arity saturations
378 (if do_composites then "" else "nocomposites")
380 let pp_ncommand ~obj_pp = function
381 | UnificationHint (_,t, n) ->
382 "unification hint " ^ string_of_int n ^ " " ^ CicNotationPp.pp_term t
383 | NDiscriminator (_,_)
384 | NInverter (_,_,_,_,_)
385 | NUnivConstraint (_) -> "not supported"
386 | NCoercion (_) -> "not supported"
387 | NObj (_,obj) -> obj_pp obj
389 | NCopy (_,name,uri,map) ->
390 "copy " ^ name ^ " from " ^ NUri.string_of_uri uri ^ " with " ^
391 String.concat " and "
393 (fun (a,b) -> NUri.string_of_uri a ^ " ↦ " ^ NUri.string_of_uri b)
397 let pp_command ~term_pp ~obj_pp = function
398 | Index (_,_,uri) -> "Indexing " ^ UriManager.string_of_uri uri
399 | Select (_,uri) -> "Selecting " ^ UriManager.string_of_uri uri
400 | Coercion (_, t, do_composites, i, j) ->
401 pp_coercion ~term_pp t do_composites i j
402 | PreferCoercion (_,t) ->
403 "prefer coercion " ^ term_pp t
404 | Inverter (_,n,ty,params) ->
405 "inverter " ^ n ^ " for " ^ term_pp ty ^ " " ^ List.fold_left (fun acc x -> acc ^ (match x with true -> "%" | _ -> "?")) "" params
406 | Default (_,what,uris) -> pp_default what uris
408 | Include (_,true,`OldAndNew,path) -> "include \"" ^ path ^ "\""
409 | Include (_,false,`OldAndNew,path) -> "include source \"" ^ path ^ "\""
410 | Include (_,_,`New,path) -> "RECURSIVELY INCLUDING " ^ path
411 | Obj (_,obj) -> obj_pp obj
413 | Relation (_,id,a,aeq,refl,sym,trans) ->
414 "relation " ^ term_pp aeq ^ " on " ^ term_pp a ^
416 Some r -> " reflexivity proved by " ^ term_pp r
419 Some r -> " symmetry proved by " ^ term_pp r
422 Some r -> " transitivity proved by " ^ term_pp r
424 | Print (_,s) -> "print " ^ s
425 | Set (_, name, value) -> Printf.sprintf "set \"%s\" \"%s\"" name value
426 | Pump (_) -> "not supported"
428 let pp_punctuation_tactical =
434 | Pos (_, i) -> Printf.sprintf "%s:" (String.concat "," (List.map string_of_int i))
438 let pp_non_punctuation_tactical =
440 | Focus (_, goals) ->
441 Printf.sprintf "focus %s" (String.concat " " (List.map string_of_int goals))
442 | Unfocus _ -> "unfocus"
445 let pp_executable ~map_unicode_to_tex ~term_pp ~lazy_term_pp ~obj_pp =
447 | NMacro (_, macro) -> pp_nmacro macro ^ "."
448 | Macro (_, macro) -> pp_macro ~term_pp ~lazy_term_pp macro ^ "."
449 | Tactic (_, Some tac, punct) ->
450 pp_tactic ~map_unicode_to_tex ~term_pp ~lazy_term_pp tac
451 ^ pp_punctuation_tactical punct
452 | Tactic (_, None, punct) ->
453 pp_punctuation_tactical punct
454 | NTactic (_,tacl) ->
455 String.concat " " (List.map (pp_ntactic ~map_unicode_to_tex) tacl)
456 | NonPunctuationTactical (_, tac, punct) ->
457 pp_non_punctuation_tactical tac
458 ^ pp_punctuation_tactical punct
459 | Command (_, cmd) -> pp_command ~term_pp ~obj_pp cmd ^ "."
460 | NCommand (_, cmd) ->
461 let obj_pp = Obj.magic obj_pp in
462 pp_ncommand ~obj_pp cmd ^ "."
464 let pp_comment ~map_unicode_to_tex ~term_pp ~lazy_term_pp ~obj_pp =
466 | Note (_,"") -> Printf.sprintf "\n"
467 | Note (_,str) -> Printf.sprintf "\n(* %s *)" str
469 Printf.sprintf "\n(** %s. **)" (pp_executable ~map_unicode_to_tex ~term_pp ~lazy_term_pp ~obj_pp code)
471 let pp_statement ~term_pp ~lazy_term_pp ~obj_pp =
473 | Executable (_, ex) -> pp_executable ~lazy_term_pp ~term_pp ~obj_pp ex
474 | Comment (_, c) -> pp_comment ~term_pp ~lazy_term_pp ~obj_pp c