]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/grafite/grafiteAstPp.ml
test branch
[helm.git] / helm / ocaml / 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 Printf
29
30 open GrafiteAst
31
32 let tactical_terminator = ""
33 let tactic_terminator = tactical_terminator
34 let command_terminator = tactical_terminator
35
36 let pp_idents idents = "[" ^ String.concat "; " idents ^ "]"
37
38 let pp_reduction_kind ~term_pp = function
39   | `Normalize -> "normalize"
40   | `Reduce -> "reduce"
41   | `Simpl -> "simplify"
42   | `Unfold (Some t) -> "unfold " ^ term_pp t
43   | `Unfold None -> "unfold"
44   | `Whd -> "whd"
45  
46 let pp_tactic_pattern ~term_pp ~lazy_term_pp (what, hyp, goal) = 
47   let what_text =
48     match what with
49     | None -> ""
50     | Some t -> sprintf "in match (%s) " (lazy_term_pp t) in
51   let hyp_text =
52     String.concat " "
53       (List.map (fun (name, p) -> sprintf "%s:(%s)" name (term_pp p)) hyp) in
54   let goal_text =
55     match goal with
56     | None -> ""
57     | Some t -> sprintf "\\vdash (%s)" (term_pp t) in
58   sprintf "%sin %s%s" what_text hyp_text goal_text
59
60 let pp_intros_specs = function
61    | None, []         -> ""
62    | Some num, []     -> Printf.sprintf " names %i" num
63    | None, idents     -> Printf.sprintf " names %s" (pp_idents idents)
64    | Some num, idents -> Printf.sprintf " names %i %s" num (pp_idents idents)
65
66 let terms_pp ~term_pp terms = String.concat ", " (List.map term_pp terms)
67
68 let rec pp_tactic ~term_pp ~lazy_term_pp =
69   let pp_reduction_kind = pp_reduction_kind ~term_pp in
70   let pp_tactic_pattern = pp_tactic_pattern ~lazy_term_pp ~term_pp in
71   function
72   | Absurd (_, term) -> "absurd" ^ term_pp term
73   | Apply (_, term) -> "apply " ^ term_pp term
74   | Auto _ -> "auto"
75   | Assumption _ -> "assumption"
76   | Change (_, where, with_what) ->
77       sprintf "change %s with %s" (pp_tactic_pattern where) (lazy_term_pp with_what)
78   | Clear (_,id) -> sprintf "clear %s" id
79   | ClearBody (_,id) -> sprintf "clearbody %s" id
80   | Compare (_,term) -> "compare " ^ term_pp term
81   | Constructor (_,n) -> "constructor " ^ string_of_int n
82   | Contradiction _ -> "contradiction"
83   | Cut (_, ident, term) ->
84      "cut " ^ term_pp term ^
85       (match ident with None -> "" | Some id -> " as " ^ id)
86   | DecideEquality _ -> "decide equality"
87   | Decompose (_, [], what, names) ->
88       sprintf "decompose %s%s" what (pp_intros_specs (None, names)) 
89   | Decompose (_, types, what, names) ->
90       let to_ident = function
91          | Ident id -> id
92          | Type _   -> assert false 
93       in
94       let types = List.rev_map to_ident types in
95       sprintf "decompose %s %s%s" (pp_idents types) what (pp_intros_specs (None, names)) 
96   | Discriminate (_, term) -> "discriminate " ^ term_pp term
97   | Elim (_, term, using, num, idents) ->
98       sprintf "elim " ^ term_pp term ^
99       (match using with None -> "" | Some term -> " using " ^ term_pp term)
100       ^ pp_intros_specs (num, idents) 
101   | ElimType (_, term, using, num, idents) ->
102       sprintf "elim type " ^ term_pp term ^
103       (match using with None -> "" | Some term -> " using " ^ term_pp term)
104       ^ pp_intros_specs (num, idents)
105   | Exact (_, term) -> "exact " ^ term_pp term
106   | Exists _ -> "exists"
107   | Fold (_, kind, term, pattern) ->
108       sprintf "fold %s %s %s" (pp_reduction_kind kind)
109        (lazy_term_pp term) (pp_tactic_pattern pattern)
110   | FwdSimpl (_, hyp, idents) -> 
111       sprintf "fwd %s%s" hyp 
112         (match idents with [] -> "" | idents -> " " ^ pp_idents idents)
113   | Generalize (_, pattern, ident) ->
114      sprintf "generalize %s%s" (pp_tactic_pattern pattern)
115       (match ident with None -> "" | Some id -> " as " ^ id)
116   | Goal (_, n) -> "goal " ^ string_of_int n
117   | Fail _ -> "fail"
118   | Fourier _ -> "fourier"
119   | IdTac _ -> "id"
120   | Injection (_, term) -> "injection " ^ term_pp term
121   | Intros (_, None, []) -> "intro"
122   | Inversion (_, term) -> "inversion " ^ term_pp term
123   | Intros (_, num, idents) ->
124       sprintf "intros%s%s"
125         (match num with None -> "" | Some num -> " " ^ string_of_int num)
126         (match idents with [] -> "" | idents -> " " ^ pp_idents idents)
127   | LApply (_, level_opt, terms, term, ident_opt) -> 
128       sprintf "lapply %s%s%s%s" 
129         (match level_opt with None -> "" | Some i -> " depth = " ^ string_of_int i ^ " ")  
130         (term_pp term) 
131         (match terms with [] -> "" | _ -> " to " ^ terms_pp ~term_pp terms)
132         (match ident_opt with None -> "" | Some ident -> " using " ^ ident)
133   | Left _ -> "left"
134   | LetIn (_, term, ident) -> sprintf "let %s in %s" (term_pp term) ident
135   | Reduce (_, kind, pat) ->
136       sprintf "%s %s" (pp_reduction_kind kind) (pp_tactic_pattern pat)
137   | Reflexivity _ -> "reflexivity"
138   | Replace (_, pattern, t) ->
139       sprintf "replace %s with %s" (pp_tactic_pattern pattern) (lazy_term_pp t)
140   | Rewrite (_, pos, t, pattern) -> 
141       sprintf "rewrite %s %s %s" 
142         (if pos = `LeftToRight then ">" else "<")
143         (term_pp t)
144         (pp_tactic_pattern pattern)
145   | Right _ -> "right"
146   | Ring _ -> "ring"
147   | Split _ -> "split"
148   | Symmetry _ -> "symmetry"
149   | Transitivity (_, term) -> "transitivity " ^ term_pp term
150
151 let pp_search_kind = function
152   | `Locate -> "locate"
153   | `Hint -> "hint"
154   | `Match -> "match"
155   | `Elim -> "elim"
156   | `Instance -> "instance"
157
158 let pp_macro ~term_pp = function 
159   (* Whelp *)
160   | WInstance (_, term) -> "whelp instance " ^ term_pp term
161   | WHint (_, t) -> "whelp hint " ^ term_pp t
162   | WLocate (_, s) -> "whelp locate " ^ s
163   | WElim (_, t) -> "whelp elim " ^ term_pp t
164   | WMatch (_, term) -> "whelp match " ^ term_pp term
165   (* real macros *)
166   | Check (_, term) -> sprintf "Check %s" (term_pp term)
167   | Hint _ -> "hint"
168   | Search_pat (_, kind, pat) ->
169       sprintf "search %s \"%s\"" (pp_search_kind kind) pat
170   | Search_term (_, kind, term) ->
171       sprintf "search %s %s" (pp_search_kind kind) (term_pp term)
172   | Print (_, name) -> sprintf "Print \"%s\"" name
173   | Quit _ -> "Quit"
174
175 let pp_associativity = function
176   | Gramext.LeftA -> "left associative"
177   | Gramext.RightA -> "right associative"
178   | Gramext.NonA -> "non associative"
179
180 let pp_precedence i = sprintf "with precedence %d" i
181
182 let pp_dir_opt = function
183   | None -> ""
184   | Some `LeftToRight -> "> "
185   | Some `RightToLeft -> "< "
186
187 let pp_default what uris = 
188   sprintf "default \"%s\" %s" what
189     (String.concat " " (List.map UriManager.string_of_uri uris))
190
191 let pp_coercion uri do_composites =
192    sprintf "coercion %s (* %s *)" (UriManager.string_of_uri uri)
193      (if do_composites then "compounds" else "no compounds")
194     
195 let pp_command ~obj_pp = function
196   | Include (_,path) -> "include " ^ path
197   | Qed _ -> "qed"
198   | Drop _ -> "drop"
199   | Set (_, name, value) -> sprintf "set \"%s\" \"%s\"" name value
200   | Coercion (_, uri, do_composites) -> pp_coercion uri do_composites
201   | Obj (_,obj) -> obj_pp obj
202   | Default (_,what,uris) ->
203       pp_default what uris
204
205 let rec pp_tactical ~term_pp ~lazy_term_pp =
206   let pp_tactic = pp_tactic ~lazy_term_pp ~term_pp in
207   let pp_tacticals = pp_tacticals ~lazy_term_pp ~term_pp in
208   function
209   | Tactic (_, tac) -> pp_tactic tac
210   | Do (_, count, tac) ->
211       sprintf "do %d %s" count (pp_tactical ~term_pp ~lazy_term_pp tac)
212   | Repeat (_, tac) -> "repeat " ^ pp_tactical ~term_pp ~lazy_term_pp tac
213   | Seq (_, tacs) -> pp_tacticals ~sep:"; " tacs
214   | Then (_, tac, tacs) ->
215       sprintf "%s; [%s]" (pp_tactical ~term_pp ~lazy_term_pp tac)
216         (pp_tacticals ~sep:" | " tacs)
217   | First (_, tacs) -> sprintf "tries [%s]" (pp_tacticals ~sep:" | " tacs)
218   | Try (_, tac) -> "try " ^ pp_tactical ~term_pp ~lazy_term_pp tac
219   | Solve (_, tac) -> sprintf "solve [%s]" (pp_tacticals ~sep:" | " tac)
220
221   | Dot _ -> "."
222   | Semicolon _ -> ";"
223   | Branch _ -> "["
224   | Shift _ -> "|"
225   | Pos (_, i) -> sprintf "%d:" i
226   | Merge _ -> "]"
227   | Focus (_, goals) ->
228       sprintf "focus %s" (String.concat " " (List.map string_of_int goals))
229   | Unfocus _ -> "unfocus"
230   | Skip _ -> "skip"
231
232 and pp_tacticals ~term_pp ~lazy_term_pp ~sep tacs =
233   String.concat sep (List.map (pp_tactical~lazy_term_pp ~term_pp) tacs)
234
235 let pp_executable ~term_pp ~lazy_term_pp ~obj_pp =
236   function
237   | Macro (_, macro) -> pp_macro ~term_pp macro
238   | Tactical (_, tac, Some punct) ->
239       pp_tactical ~lazy_term_pp ~term_pp tac
240       ^ pp_tactical ~lazy_term_pp ~term_pp punct
241   | Tactical (_, tac, None) -> pp_tactical ~lazy_term_pp ~term_pp tac
242   | Command (_, cmd) -> pp_command ~obj_pp cmd
243                       
244 let pp_comment ~term_pp ~lazy_term_pp ~obj_pp =
245   function
246   | Note (_,str) -> sprintf "(* %s *)" str
247   | Code (_,code) ->
248       sprintf "(** %s. **)" (pp_executable ~term_pp ~lazy_term_pp ~obj_pp code)
249
250 let pp_statement ~term_pp ~lazy_term_pp ~obj_pp =
251   function
252   | Executable (_, ex) -> pp_executable ~lazy_term_pp ~term_pp ~obj_pp ex
253   | Comment (_, c) -> pp_comment ~term_pp ~lazy_term_pp ~obj_pp c