]> matita.cs.unibo.it Git - helm.git/blob - components/grafite/grafiteAstPp.ml
procedural: bug fixes
[helm.git] / components / grafite / grafiteAstPp.ml
1 (* Copyright (C) 2004, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 (* $Id$ *)
27
28 open GrafiteAst
29
30 let tactical_terminator = ""
31 let tactic_terminator = tactical_terminator
32 let command_terminator = tactical_terminator
33
34 let pp_idents idents = "(" ^ String.concat " " idents ^ ")"
35
36 let pp_reduction_kind ~term_pp = function
37   | `Normalize -> "normalize"
38   | `Reduce -> "reduce"
39   | `Simpl -> "simplify"
40   | `Unfold (Some t) -> "unfold " ^ term_pp t
41   | `Unfold None -> "unfold"
42   | `Whd -> "whd"
43  
44 let pp_tactic_pattern ~term_pp ~lazy_term_pp (what, hyp, goal) = 
45   if what = None && hyp = [] && goal = None then "" else 
46   let what_text =
47     match what with
48     | None -> ""
49     | Some t -> Printf.sprintf "in match (%s) " (lazy_term_pp t) in
50   let hyp_text =
51     String.concat " "
52       (List.map (fun (name, p) -> Printf.sprintf "%s:(%s)" name (term_pp p)) hyp) in
53   let goal_text =
54     match goal with
55     | None -> ""
56     | Some t -> Printf.sprintf "\\vdash (%s)" (term_pp t) in
57   Printf.sprintf "%sin %s%s" what_text hyp_text goal_text
58
59 let pp_intros_specs = function
60    | None, []         -> ""
61    | Some num, []     -> Printf.sprintf " names %i" num
62    | None, idents     -> Printf.sprintf " names %s" (pp_idents idents)
63    | Some num, idents -> Printf.sprintf " names %i %s" num (pp_idents idents)
64
65 let terms_pp ~term_pp terms = String.concat ", " (List.map term_pp terms)
66
67 let opt_string_pp = function
68    | None -> ""
69    | Some what -> what ^ " "
70
71 let rec pp_tactic ~term_pp ~lazy_term_pp =
72   let pp_reduction_kind = pp_reduction_kind ~term_pp in
73   let pp_tactic_pattern = pp_tactic_pattern ~lazy_term_pp ~term_pp in
74   function
75   (* Higher order tactics *)
76   | Do (_, count, tac) ->
77       Printf.sprintf "do %d %s" count (pp_tactic ~term_pp ~lazy_term_pp tac)
78   | Repeat (_, tac) -> "repeat " ^ pp_tactic ~term_pp ~lazy_term_pp tac
79   | Seq (_, tacs) -> pp_tactics ~term_pp ~lazy_term_pp ~sep:"; " tacs
80   | Then (_, tac, tacs) ->
81       Printf.sprintf "%s; [%s]" (pp_tactic ~term_pp ~lazy_term_pp tac)
82         (pp_tactics ~term_pp ~lazy_term_pp ~sep:" | " tacs)
83   | First (_, tacs) ->
84      Printf.sprintf "tries [%s]" (pp_tactics ~term_pp ~lazy_term_pp ~sep:" | " tacs)
85   | Try (_, tac) -> "try " ^ pp_tactic ~term_pp ~lazy_term_pp tac
86   | Solve (_, tac) ->
87      Printf.sprintf "solve [%s]" (pp_tactics ~term_pp ~lazy_term_pp ~sep:" | " tac)
88   | Progress (_, tac) -> "progress " ^ pp_tactic ~term_pp ~lazy_term_pp tac
89   (* First order tactics *)
90   | Absurd (_, term) -> "absurd" ^ term_pp term
91   | Apply (_, term) -> "apply " ^ term_pp term
92   | ApplyS (_, term, params) ->
93      "applyS " ^ term_pp term ^
94       String.concat " " 
95         (List.map (fun (k,v) -> if v <> "" then k ^ "=" ^ v else k) params)
96   | Auto (_,params) -> "auto " ^ 
97       String.concat " " 
98         (List.map (fun (k,v) -> if v <> "" then k ^ "=" ^ v else k) params)
99   | Assumption _ -> "assumption"
100   | Cases (_, term, idents) -> Printf.sprintf "cases " ^ term_pp term ^
101       pp_intros_specs (None, idents) 
102   | Change (_, where, with_what) ->
103       Printf.sprintf "change %s with %s" (pp_tactic_pattern where) (lazy_term_pp with_what)
104   | Clear (_,ids) -> Printf.sprintf "clear %s" (pp_idents ids)
105   | ClearBody (_,id) -> Printf.sprintf "clearbody %s" id
106   | Constructor (_,n) -> "constructor " ^ string_of_int n
107   | Contradiction _ -> "contradiction"
108   | Cut (_, ident, term) ->
109      "cut " ^ term_pp term ^
110       (match ident with None -> "" | Some id -> " as " ^ id)
111   | Decompose (_, names) ->
112       Printf.sprintf "decompose%s" (pp_intros_specs (None, names)) 
113   | Demodulate _ -> "demodulate"
114   | Destruct (_, term) -> "destruct " ^ term_pp term
115   | Elim (_, what, using, pattern, num, idents) ->
116       Printf.sprintf "elim %s%s %s%s" 
117       (term_pp what)
118       (match using with None -> "" | Some term -> " using " ^ term_pp term)
119       (pp_tactic_pattern pattern)
120       (pp_intros_specs (num, idents)) 
121   | ElimType (_, term, using, num, idents) ->
122       Printf.sprintf "elim type " ^ term_pp term ^
123       (match using with None -> "" | Some term -> " using " ^ term_pp term)
124       ^ pp_intros_specs (num, idents)
125   | Exact (_, term) -> "exact " ^ term_pp term
126   | Exists _ -> "exists"
127   | Fold (_, kind, term, pattern) ->
128       Printf.sprintf "fold %s %s %s" (pp_reduction_kind kind)
129        (lazy_term_pp term) (pp_tactic_pattern pattern)
130   | FwdSimpl (_, hyp, idents) -> 
131       Printf.sprintf "fwd %s%s" hyp 
132         (match idents with [] -> "" | idents -> " as " ^ pp_idents idents)
133   | Generalize (_, pattern, ident) ->
134      Printf.sprintf "generalize %s%s" (pp_tactic_pattern pattern)
135       (match ident with None -> "" | Some id -> " as " ^ id)
136   | Fail _ -> "fail"
137   | Fourier _ -> "fourier"
138   | IdTac _ -> "id"
139   | Intros (_, None, []) -> "intros"
140   | Inversion (_, term) -> "inversion " ^ term_pp term
141   | Intros (_, num, idents) ->
142       Printf.sprintf "intros%s%s"
143         (match num with None -> "" | Some num -> " " ^ string_of_int num)
144         (match idents with [] -> "" | idents -> " " ^ pp_idents idents)
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 ^ " ")  
149         (term_pp term) 
150         (match terms with [] -> "" | _ -> " to " ^ terms_pp ~term_pp terms)
151         (match ident_opt with None -> "" | Some ident -> " as " ^ ident)
152   | Left _ -> "left"
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 "<")
163         (term_pp t)
164         (pp_tactic_pattern pattern)
165         (if names = [] then "" else " as " ^ pp_idents names)
166   | Right _ -> "right"
167   | Ring _ -> "ring"
168   | Split _ -> "split"
169   | Subst _ -> "subst"
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) ->
187      "case" ^ id ^
188        String.concat " "
189         (List.map (function (id,term) -> "(" ^ id ^ ": " ^ term_pp term ^  ")")
190           args)
191
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)
194
195  let pp_search_kind = function
196   | `Locate -> "locate"
197   | `Hint -> "hint"
198   | `Match -> "match"
199   | `Elim -> "elim"
200   | `Instance -> "instance"
201
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 *)
206      s
207    else
208      "(" ^ s ^ ")"
209   
210 let pp_macro ~term_pp = 
211   let term_pp = pp_arg ~term_pp in
212   let style_pp = function
213      | Declarative         -> ""
214      | Procedural None     -> "procedural "
215      | Procedural (Some i) -> Printf.sprintf "procedural %u " i
216   in
217   let prefix_pp prefix = 
218      if prefix = "" then "" else Printf.sprintf " \"%s\"" prefix
219   in
220   function 
221   (* Whelp *)
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
227   (* real macros *)
228   | Check (_, term) -> Printf.sprintf "check %s" (term_pp term)
229   | Hint _ -> "hint"
230   | Inline (_, style, suri, prefix) ->  
231       Printf.sprintf "inline %s\"%s\"%s" (style_pp style) suri (prefix_pp prefix) 
232
233 let pp_associativity = function
234   | Gramext.LeftA -> "left associative"
235   | Gramext.RightA -> "right associative"
236   | Gramext.NonA -> "non associative"
237
238 let pp_precedence i = Printf.sprintf "with precedence %d" i
239
240 let pp_dir_opt = function
241   | None -> ""
242   | Some `LeftToRight -> "> "
243   | Some `RightToLeft -> "< "
244
245 let pp_default what uris = 
246   Printf.sprintf "default \"%s\" %s" what
247     (String.concat " " (List.map UriManager.string_of_uri uris))
248
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")
252     
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
257   | Drop _ -> "drop"
258   | Include (_,path) -> "include \"" ^ path ^ "\""
259   | Obj (_,obj) -> obj_pp obj
260   | Qed _ -> "qed"
261   | Relation (_,id,a,aeq,refl,sym,trans) ->
262      "relation " ^ term_pp aeq ^ " on " ^ term_pp a ^
263      (match refl with
264          Some r -> " reflexivity proved by " ^ term_pp r
265        | None -> "") ^
266      (match sym with
267          Some r -> " symmetry proved by " ^ term_pp r
268        | None -> "") ^
269      (match trans with
270          Some r -> " transitivity proved by " ^ term_pp r
271        | None -> "")
272   | Print (_,s) -> "print " ^ s
273   | Set (_, name, value) -> Printf.sprintf "set \"%s\" \"%s\"" name value
274
275 let pp_punctuation_tactical ~term_pp ~lazy_term_pp =
276   function
277   | Dot _ -> "."
278   | Semicolon _ -> ";"
279   | Branch _ -> "["
280   | Shift _ -> "|"
281   | Pos (_, i) -> Printf.sprintf "%s:" (String.concat "," (List.map string_of_int i))
282   | Wildcard _ -> "*:"
283   | Merge _ -> "]"
284
285 let pp_non_punctuation_tactical ~term_pp ~lazy_term_pp =
286   function
287   | Focus (_, goals) ->
288       Printf.sprintf "focus %s" (String.concat " " (List.map string_of_int goals))
289   | Unfocus _ -> "unfocus"
290   | Skip _ -> "skip"
291
292 let pp_executable ~term_pp ~lazy_term_pp ~obj_pp =
293   function
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 ^ "."
304                       
305 let pp_comment ~term_pp ~lazy_term_pp ~obj_pp =
306   function
307   | Note (_,"") -> Printf.sprintf "\n"
308   | Note (_,str) -> Printf.sprintf "(* %s *)\n" str
309   | Code (_,code) ->
310       Printf.sprintf "(** %s. **)\n" (pp_executable ~term_pp ~lazy_term_pp ~obj_pp code)
311
312 let pp_statement ~term_pp ~lazy_term_pp ~obj_pp =
313   function
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