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 ~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
59 | Some t -> Printf.sprintf "\\vdash (%s)" (term_pp t) in
60 Printf.sprintf "%sin %s%s" what_text hyp_text goal_text
62 let pp_intros_specs s = function
64 | Some num, [] -> Printf.sprintf " %s%i" s num
65 | None, idents -> Printf.sprintf " %s%s" s (pp_idents idents)
66 | Some num, idents -> Printf.sprintf " %s%i %s" s num (pp_idents idents)
68 let terms_pp ~term_pp terms = String.concat ", " (List.map term_pp terms)
70 let opt_string_pp = function
72 | Some what -> what ^ " "
74 let rec pp_tactic ~term_pp ~lazy_term_pp =
75 let pp_reduction_kind = pp_reduction_kind ~term_pp in
76 let pp_tactic_pattern = pp_tactic_pattern ~lazy_term_pp ~term_pp in
78 (* Higher order tactics *)
79 | Do (_, count, tac) ->
80 Printf.sprintf "do %d %s" count (pp_tactic ~term_pp ~lazy_term_pp tac)
81 | Repeat (_, tac) -> "repeat " ^ pp_tactic ~term_pp ~lazy_term_pp tac
82 | Seq (_, tacs) -> pp_tactics ~term_pp ~lazy_term_pp ~sep:"; " tacs
83 | Then (_, tac, tacs) ->
84 Printf.sprintf "%s; [%s]" (pp_tactic ~term_pp ~lazy_term_pp tac)
85 (pp_tactics ~term_pp ~lazy_term_pp ~sep:" | " tacs)
87 Printf.sprintf "tries [%s]" (pp_tactics ~term_pp ~lazy_term_pp ~sep:" | " tacs)
88 | Try (_, tac) -> "try " ^ pp_tactic ~term_pp ~lazy_term_pp tac
90 Printf.sprintf "solve [%s]" (pp_tactics ~term_pp ~lazy_term_pp ~sep:" | " tac)
91 | Progress (_, tac) -> "progress " ^ pp_tactic ~term_pp ~lazy_term_pp tac
92 (* First order tactics *)
93 | Absurd (_, term) -> "absurd" ^ term_pp term
94 | Apply (_, term) -> "apply " ^ term_pp term
95 | ApplyS (_, term, params) ->
96 "applyS " ^ term_pp term ^
98 (List.map (fun (k,v) -> if v <> "" then k ^ "=" ^ v else k) params)
99 | Auto (_,params) -> "auto " ^
101 (List.map (fun (k,v) -> if v <> "" then k ^ "=" ^ v else k) params)
102 | Assumption _ -> "assumption"
103 | Cases (_, term, specs) -> Printf.sprintf "cases " ^ term_pp term ^
104 pp_intros_specs "names " specs
105 | Change (_, where, with_what) ->
106 Printf.sprintf "change %s with %s" (pp_tactic_pattern where) (lazy_term_pp with_what)
107 | Clear (_,ids) -> Printf.sprintf "clear %s" (pp_hyps ids)
108 | ClearBody (_,id) -> Printf.sprintf "clearbody %s" (pp_hyps [id])
109 | Constructor (_,n) -> "constructor " ^ string_of_int n
110 | Contradiction _ -> "contradiction"
111 | Cut (_, ident, term) ->
112 "cut " ^ term_pp term ^
113 (match ident with None -> "" | Some id -> " as " ^ id)
114 | Decompose (_, names) ->
115 Printf.sprintf "decompose%s"
116 (pp_intros_specs "names " (None, names))
117 | Demodulate _ -> "demodulate"
118 | Destruct (_, term) -> "destruct " ^ term_pp term
119 | Elim (_, what, using, pattern, specs) ->
120 Printf.sprintf "elim %s%s %s%s"
122 (match using with None -> "" | Some term -> " using " ^ term_pp term)
123 (pp_tactic_pattern pattern)
124 (pp_intros_specs "names " specs)
125 | ElimType (_, term, using, specs) ->
126 Printf.sprintf "elim type %s%s%s"
128 (match using with None -> "" | Some term -> " using " ^ term_pp term)
129 (pp_intros_specs "names " specs)
130 | Exact (_, term) -> "exact " ^ term_pp term
131 | Exists _ -> "exists"
132 | Fold (_, kind, term, pattern) ->
133 Printf.sprintf "fold %s %s %s" (pp_reduction_kind kind)
134 (lazy_term_pp term) (pp_tactic_pattern pattern)
135 | FwdSimpl (_, hyp, names) ->
136 Printf.sprintf "fwd %s%s" hyp (pp_intros_specs "names " (None, names))
137 | Generalize (_, pattern, ident) ->
138 Printf.sprintf "generalize %s%s" (pp_tactic_pattern pattern)
139 (match ident with None -> "" | Some id -> " as " ^ id)
141 | Fourier _ -> "fourier"
143 | Intros (_, specs) -> Printf.sprintf "intros%s" (pp_intros_specs "" specs)
144 | Inversion (_, term) -> "inversion " ^ term_pp term
145 | LApply (_, linear, level_opt, terms, term, ident_opt) ->
146 Printf.sprintf "lapply %s%s%s%s%s"
147 (if linear then " linear " else "")
148 (match level_opt with None -> "" | Some i -> " depth = " ^ string_of_int i ^ " ")
150 (match terms with [] -> "" | _ -> " to " ^ terms_pp ~term_pp terms)
151 (match ident_opt with None -> "" | Some ident -> " as " ^ ident)
153 | LetIn (_, term, ident) ->
154 Printf.sprintf "letin %s \\def %s" ident (term_pp term)
155 | Reduce (_, kind, pat) ->
156 Printf.sprintf "%s %s" (pp_reduction_kind kind) (pp_tactic_pattern pat)
157 | Reflexivity _ -> "reflexivity"
158 | Replace (_, pattern, t) ->
159 Printf.sprintf "replace %s with %s" (pp_tactic_pattern pattern) (lazy_term_pp t)
160 | Rewrite (_, pos, t, pattern, names) ->
161 Printf.sprintf "rewrite %s %s %s%s"
162 (if pos = `LeftToRight then ">" else "<")
164 (pp_tactic_pattern pattern)
165 (if names = [] then "" else " as " ^ pp_idents names)
170 | Symmetry _ -> "symmetry"
171 | Transitivity (_, term) -> "transitivity " ^ term_pp term
172 (* Tattiche Aggiunte *)
173 | Assume (_, ident , term) -> "assume" ^ ident ^ ":" ^ term_pp term
174 | Suppose (_, term, ident,term1) -> "suppose" ^ term_pp term ^ "(" ^ ident ^ ")" ^ (match term1 with None -> " " | Some term1 -> term_pp term1)
175 | Bydone (_, term) -> "by" ^ (match term with None -> "_" | Some term -> term_pp term) ^ "done"
176 | By_term_we_proved (_, term, term1, ident, term2) -> "by" ^ (match term with None -> "_" | Some term -> term_pp term) ^ "we proved" ^ term_pp term1 ^ (match ident with None -> "" | Some ident -> "(" ^ident^ ")") ^
177 (match term2 with None -> " " | Some term2 -> term_pp term2)
178 | 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)
179 | We_proceed_by_cases_on (_, term, term1) -> "we proceed by cases on" ^ term_pp term ^ "to prove" ^ term_pp term1
180 | We_proceed_by_induction_on (_, term, term1) -> "we proceed by induction on" ^ term_pp term ^ "to prove" ^ term_pp term1
181 | Byinduction (_, term, ident) -> "by induction hypothesis we know" ^ term_pp term ^ "(" ^ ident ^ ")"
182 | Thesisbecomes (_, term) -> "the thesis becomes " ^ term_pp term
183 | ExistsElim (_, term0, ident, term, ident1, term1) -> "by " ^ (match term0 with None -> "_" | Some term -> term_pp term) ^ "let " ^ ident ^ ":" ^ term_pp term ^ "such that " ^ lazy_term_pp term1 ^ "(" ^ ident1 ^ ")"
184 | AndElim (_, term, ident1, term1, ident2, term2) -> "by " ^ term_pp term ^ "we have " ^ term_pp term1 ^ " (" ^ ident1 ^ ") " ^ "and " ^ term_pp term2 ^ " (" ^ ident2 ^ ")"
185 | RewritingStep (_, term, term1, term2, cont) -> (match term with None -> " " | Some (None,term) -> "conclude " ^ term_pp term | Some (Some name,term) -> "obtain (" ^ name ^ ") " ^ term_pp term) ^ "=" ^ term_pp term1 ^ (match term2 with `Auto params -> "_" ^ String.concat " " (List.map (fun (k,v) -> if v <> "" then k ^ "=" ^ v else k) params) | `Term term2 -> term_pp term2) ^ (if cont then " done" else "")
186 | Case (_, id, args) ->
189 (List.map (function (id,term) -> "(" ^ id ^ ": " ^ term_pp term ^ ")")
192 and pp_tactics ~term_pp ~lazy_term_pp ~sep tacs =
193 String.concat sep (List.map (pp_tactic ~lazy_term_pp ~term_pp) tacs)
195 let pp_search_kind = function
196 | `Locate -> "locate"
200 | `Instance -> "instance"
202 let pp_arg ~term_pp arg =
203 let s = term_pp arg in
204 if s = "" || (s.[0] = '(' && s.[String.length s - 1] = ')') then
205 (* _nice_ heuristic *)
210 let pp_macro ~term_pp =
211 let term_pp = pp_arg ~term_pp in
212 let style_pp = function
214 | Procedural None -> "procedural "
215 | Procedural (Some i) -> Printf.sprintf "procedural %u " i
217 let prefix_pp prefix =
218 if prefix = "" then "" else Printf.sprintf " \"%s\"" prefix
222 | WInstance (_, term) -> "whelp instance " ^ term_pp term
223 | WHint (_, t) -> "whelp hint " ^ term_pp t
224 | WLocate (_, s) -> "whelp locate \"" ^ s ^ "\""
225 | WElim (_, t) -> "whelp elim " ^ term_pp t
226 | WMatch (_, term) -> "whelp match " ^ term_pp term
228 | Check (_, term) -> Printf.sprintf "check %s" (term_pp term)
230 | Inline (_, style, suri, prefix) ->
231 Printf.sprintf "inline %s\"%s\"%s" (style_pp style) suri (prefix_pp prefix)
233 let pp_associativity = function
234 | Gramext.LeftA -> "left associative"
235 | Gramext.RightA -> "right associative"
236 | Gramext.NonA -> "non associative"
238 let pp_precedence i = Printf.sprintf "with precedence %d" i
240 let pp_dir_opt = function
242 | Some `LeftToRight -> "> "
243 | Some `RightToLeft -> "< "
245 let pp_default what uris =
246 Printf.sprintf "default \"%s\" %s" what
247 (String.concat " " (List.map UriManager.string_of_uri uris))
249 let pp_coercion uri do_composites arity =
250 Printf.sprintf "coercion %s %d (* %s *)" (UriManager.string_of_uri uri) arity
251 (if do_composites then "compounds" else "no compounds")
253 let pp_command ~term_pp ~obj_pp = function
254 | Index (_,_,uri) -> "Indexing " ^ UriManager.string_of_uri uri
255 | Coercion (_, uri, do_composites, i) -> pp_coercion uri do_composites i
256 | Default (_,what,uris) -> pp_default what uris
258 | Include (_,path) -> "include \"" ^ path ^ "\""
259 | Obj (_,obj) -> obj_pp obj
261 | Relation (_,id,a,aeq,refl,sym,trans) ->
262 "relation " ^ term_pp aeq ^ " on " ^ term_pp a ^
264 Some r -> " reflexivity proved by " ^ term_pp r
267 Some r -> " symmetry proved by " ^ term_pp r
270 Some r -> " transitivity proved by " ^ term_pp r
272 | Print (_,s) -> "print " ^ s
273 | Set (_, name, value) -> Printf.sprintf "set \"%s\" \"%s\"" name value
275 let pp_punctuation_tactical ~term_pp ~lazy_term_pp =
281 | Pos (_, i) -> Printf.sprintf "%s:" (String.concat "," (List.map string_of_int i))
285 let pp_non_punctuation_tactical ~term_pp ~lazy_term_pp =
287 | Focus (_, goals) ->
288 Printf.sprintf "focus %s" (String.concat " " (List.map string_of_int goals))
289 | Unfocus _ -> "unfocus"
292 let pp_executable ~term_pp ~lazy_term_pp ~obj_pp =
294 | Macro (_, macro) -> pp_macro ~term_pp macro ^ "."
295 | Tactic (_, Some tac, punct) ->
296 pp_tactic ~lazy_term_pp ~term_pp tac
297 ^ pp_punctuation_tactical ~lazy_term_pp ~term_pp punct
298 | Tactic (_, None, punct) ->
299 pp_punctuation_tactical ~lazy_term_pp ~term_pp punct
300 | NonPunctuationTactical (_, tac, punct) ->
301 pp_non_punctuation_tactical ~lazy_term_pp ~term_pp tac
302 ^ pp_punctuation_tactical ~lazy_term_pp ~term_pp punct
303 | Command (_, cmd) -> pp_command ~term_pp ~obj_pp cmd ^ "."
305 let pp_comment ~term_pp ~lazy_term_pp ~obj_pp =
307 | Note (_,"") -> Printf.sprintf "\n"
308 | Note (_,str) -> Printf.sprintf "\n(* %s *)" str
310 Printf.sprintf "\n(** %s. **)" (pp_executable ~term_pp ~lazy_term_pp ~obj_pp code)
312 let pp_statement ~term_pp ~lazy_term_pp ~obj_pp =
314 | Executable (_, ex) -> pp_executable ~lazy_term_pp ~term_pp ~obj_pp ex
315 | Comment (_, c) -> pp_comment ~term_pp ~lazy_term_pp ~obj_pp c