]> matita.cs.unibo.it Git - helm.git/blob - matita/components/grafite/grafiteAstPp.ml
a4a5788d9fbb4c1a637cb95259f5f4618137c5dc
[helm.git] / matita / 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 open Printf
30
31 let tactical_terminator = ""
32 let tactic_terminator = tactical_terminator
33 let command_terminator = tactical_terminator
34
35 let pp_tactic_pattern status ~map_unicode_to_tex (what, hyp, goal) = 
36   if what = None && hyp = [] && goal = None then "" else 
37   let what_text =
38     match what with
39     | None -> ""
40     | Some t -> Printf.sprintf "in match (%s) " (NotationPp.pp_term status t) in
41   let hyp_text =
42     String.concat " "
43       (List.map (fun (name, p) -> Printf.sprintf "%s:(%s)" name
44        (NotationPp.pp_term status p)) hyp) in
45   let goal_text =
46     match goal with
47     | None -> ""
48     | Some t ->
49        let vdash = if map_unicode_to_tex then "\\vdash" else "⊢" in
50         Printf.sprintf "%s (%s)" vdash (NotationPp.pp_term status t)
51   in
52    Printf.sprintf "%sin %s%s" what_text hyp_text goal_text
53
54 let pp_auto_params params status =
55     match params with
56     | (None,flags) -> String.concat " " (List.map (fun a,b -> a ^ "=" ^ b) flags)
57     | (Some l,flags) -> (String.concat "," (List.map (NotationPp.pp_term status) l)) ^
58     String.concat " " (List.map (fun a,b -> a ^ "=" ^ b) flags)
59 ;;
60
61 let pp_just status just =
62  match just with
63     `Term term -> "exact " ^ NotationPp.pp_term status term
64   | `Auto params -> pp_auto_params params status 
65 ;;
66
67 let rec pp_ntactic status ~map_unicode_to_tex =
68  let pp_tactic_pattern = pp_tactic_pattern ~map_unicode_to_tex in
69  function
70   | NApply (_,t) -> "@" ^ NotationPp.pp_term status t
71   | NSmartApply (_,t) -> "fixme"
72   | NAuto (_,(None,flgs)) ->
73       "nautobatch" ^
74         String.concat " " (List.map (fun a,b -> a ^ "=" ^ b) flgs)
75   | NAuto (_,(Some l,flgs)) ->
76       "nautobatch" ^ " by " ^
77          (String.concat "," (List.map (NotationPp.pp_term status) l)) ^
78         String.concat " " (List.map (fun a,b -> a ^ "=" ^ b) flgs)
79   | NCases (_,what,where) -> "ncases " ^ NotationPp.pp_term status what ^
80       "...to be implemented..." ^ " " ^ "...to be implemented..."
81   | NConstructor (_,None,l) -> "@ " ^
82       String.concat " " (List.map (NotationPp.pp_term status) l)
83   | NConstructor (_,Some x,l) -> "@" ^ string_of_int x ^ " " ^
84       String.concat " " (List.map (NotationPp.pp_term status) l)
85   | NCase1 (_,n) -> "*" ^ n ^ ":"
86   | NChange (_,what,wwhat) -> "nchange " ^ "...to be implemented..." ^ 
87       " with " ^ NotationPp.pp_term status wwhat
88   | NCut (_,t) -> "ncut " ^ NotationPp.pp_term status t
89 (*| NDiscriminate (_,t) -> "ndiscriminate " ^ NotationPp.pp_term status t
90   | NSubst (_,t) -> "nsubst " ^ NotationPp.pp_term status t *)
91   | NClear (_,l) -> "nclear " ^ String.concat " " l
92   | NDestruct (_,dom,skip) -> "ndestruct ..." 
93   | NElim (_,what,where) -> "nelim " ^ NotationPp.pp_term status what ^
94       "...to be implemented..." ^ " " ^ "...to be implemented..."
95   | NId _ -> "nid"
96   | NIntro (_,n) -> "#" ^ n
97   | NIntros (_,l) -> "#" ^ String.concat " " l
98   | NInversion (_,what,where) -> "ninversion " ^ NotationPp.pp_term status what ^
99       "...to be implemented..." ^ " " ^ "...to be implemented..."
100   | NLApply (_,t) -> "lapply " ^ NotationPp.pp_term status t
101   | NRewrite (_,dir,n,where) -> "nrewrite " ^
102      (match dir with `LeftToRight -> ">" | `RightToLeft -> "<") ^
103      " " ^ NotationPp.pp_term status n ^ " " ^ pp_tactic_pattern status where
104   | NReduce _ | NGeneralize _ | NLetIn _ | NAssert _ -> "TO BE IMPLEMENTED"
105   | NDot _ -> "."
106   | NSemicolon _ -> ";"
107   | NBranch _ -> "["
108   | NShift _ -> "|"
109   | NPos (_, l) -> String.concat "," (List.map string_of_int l)^ ":"
110   | NPosbyname (_, s) -> s ^ ":"
111   | NWildcard _ -> "*:"
112   | NMerge _ -> "]"
113   | NFocus (_,l) -> 
114       Printf.sprintf "focus %s" 
115         (String.concat " " (List.map string_of_int l))
116   | NUnfocus _ -> "unfocus"
117   | NSkip _ -> "skip"
118   | NTry (_,tac) -> "ntry " ^ pp_ntactic status ~map_unicode_to_tex tac
119   | NAssumption _ -> "nassumption"
120   | NBlock (_,l) -> 
121      "(" ^ String.concat " " (List.map (pp_ntactic status ~map_unicode_to_tex) l)^ ")"
122   | NRepeat (_,t) -> "nrepeat " ^ pp_ntactic status ~map_unicode_to_tex t
123   | Assume (_, ident, term, term1) -> "assume" ^ ident ^ ":" ^ NotationPp.pp_term status term ^
124   (match term1 with None -> " " | Some t1 -> " that is eqivalent to " ^ NotationPp.pp_term status t1)
125   | Suppose (_,term,ident,term1) -> "suppose" ^ NotationPp.pp_term status term ^ "(" ^ ident ^ ")" ^ (match
126   term1 with None -> " " | Some t -> " that is equivalent to " ^ NotationPp.pp_term status t)
127   | By_just_we_proved (_, just, term1, ident, term2) -> pp_just status just  ^ "we proved" ^
128   NotationPp.pp_term status term1 ^ (match ident with None -> "" | Some ident -> "(" ^ident^ ")") ^ (match
129   term2 with  None -> " " | Some term2 -> " that is equivalent to " ^ NotationPp.pp_term status term2)
130   | We_need_to_prove (_,term,ident,term1) -> "we need to prove" ^ NotationPp.pp_term status term ^
131   (match ident with None -> " " | Some id -> "(" ^ id ^ ")") ^ (match term1 with None -> " " | Some t1
132   -> "or equivalently" ^ NotationPp.pp_term status t1)
133   | Bydone (_, just) -> pp_just status just ^ "done"
134   | ExistsElim (_, just, ident, term, term1, ident1) -> pp_just status just ^ "let " ^ ident ^ ":"
135   ^ NotationPp.pp_term status term ^ "such that " ^ NotationPp.pp_term status term1 ^ "(" ^ ident1 ^ ")"
136   | AndElim (_, just, term1, ident1, term2, ident2) -> pp_just status just ^ "we have " ^
137   NotationPp.pp_term status term1 ^ " (" ^ ident1 ^ ") " ^ "and " ^ NotationPp.pp_term status term2
138   ^ " (" ^ ident2 ^ ")" 
139   | Thesisbecomes (_, term1, term2) -> "the thesis becomes " ^ NotationPp.pp_term status term1 ^ (match
140   term2 with None -> " " | Some term2 -> NotationPp.pp_term status term2)
141   | RewritingStep (_, rhs, just, cont) -> 
142       "=" ^
143       NotationPp.pp_term status rhs ^ 
144       (match just with 
145       | `Auto params -> pp_auto_params params status
146       | `Term t -> " exact " ^ NotationPp.pp_term status t
147       | `Proof -> " proof"
148       | `SolveWith t -> " using " ^ NotationPp.pp_term status t)
149       ^ (if cont then " done" else "")
150 (*
151   | RewritingStep (_, term, term1, term2, cont) -> 
152       (match term with 
153       | None -> " " 
154       | Some (None,term) -> "conclude " ^ NotationPp.pp_term status term 
155       | Some (Some name,term) -> 
156           "obtain (" ^ name ^ ") " ^ NotationPp.pp_term status term) 
157       ^ "=" ^
158       NotationPp.pp_term status term1 ^ 
159       (match term2 with 
160       | `Auto params -> pp_auto_params params status
161       | `Term term2 -> " exact " ^ NotationPp.pp_term status term2 
162       | `Proof -> " proof"
163       | `SolveWith term -> " using " ^ NotationPp.pp_term status term)
164       ^ (if cont then " done" else "")
165 *)
166   | Obtain (_,id,t1) -> "obtain (" ^ id ^ ")" ^ NotationPp.pp_term status t1
167   | Conclude (_,t1) -> "conclude " ^ NotationPp.pp_term status t1
168   | We_proceed_by_cases_on (_, term, term1) -> "we proceed by cases on" ^ NotationPp.pp_term status term ^ "to prove" ^ NotationPp.pp_term status  term1
169   | We_proceed_by_induction_on (_, term, term1) -> "we proceed by induction on" ^ NotationPp.pp_term status  term ^ "to prove" ^ NotationPp.pp_term status  term1
170   | Byinduction (_, term, ident) -> "by induction hypothesis we know" ^ NotationPp.pp_term status  term ^ "(" ^ ident ^ ")"
171   | Case (_, id, args) ->
172      "case" ^ id ^
173        String.concat " "
174         (List.map (function (id,term) -> "(" ^ id ^ ": " ^ NotationPp.pp_term status  term ^  ")")
175           args)
176 ;;
177
178 let pp_nmacro status = function
179   | NCheck (_, term) -> Printf.sprintf "ncheck %s" (NotationPp.pp_term status term)
180   | Screenshot (_, name) -> Printf.sprintf "screenshot \"%s\"" name
181 ;;
182
183 let pp_l1_pattern = NotationPp.pp_term
184 let pp_l2_pattern = NotationPp.pp_term
185
186 let pp_alias = function
187   | Ident_alias (id, uri) -> sprintf "alias id \"%s\" = \"%s\"." id uri
188   | Symbol_alias (symb, instance, desc) ->
189       sprintf "alias symbol \"%s\" %s= \"%s\"."
190         symb
191         (if instance=0 then "" else "(instance "^ string_of_int instance ^ ") ")
192         desc
193   | Number_alias (instance,desc) ->
194       sprintf "alias num (instance %d) = \"%s\"." instance desc
195   
196 let pp_associativity = function
197   | Gramext.LeftA -> "left associative"
198   | Gramext.RightA -> "right associative"
199   | Gramext.NonA -> "non associative"
200
201 let pp_precedence i = sprintf "with precedence %d" i
202
203 let pp_argument_pattern = function
204   | NotationPt.IdentArg (eta_depth, name) ->
205       let eta_buf = Buffer.create 5 in
206       for i = 1 to eta_depth do
207         Buffer.add_string eta_buf "\\eta."
208       done;
209       sprintf "%s%s" (Buffer.contents eta_buf) name
210
211 let pp_interpretation dsc symbol arg_patterns cic_appl_pattern = 
212   sprintf "interpretation \"%s\" '%s %s = %s."
213     dsc symbol
214     (String.concat " " (List.map pp_argument_pattern arg_patterns))
215     (NotationPp.pp_cic_appl_pattern cic_appl_pattern)
216  
217 let pp_dir_opt = function
218   | None -> ""
219   | Some `LeftToRight -> "> "
220   | Some `RightToLeft -> "< "
221
222 let pp_notation status dir_opt l1_pattern assoc prec l2_pattern = 
223   sprintf "notation %s\"%s\" %s %s for %s."
224     (pp_dir_opt dir_opt)
225     (pp_l1_pattern status l1_pattern)
226     (pp_associativity assoc)
227     (pp_precedence prec)
228     (pp_l2_pattern status l2_pattern)
229
230 let pp_ncommand status = function
231   | UnificationHint (_,t, n) -> 
232       "unification hint " ^ string_of_int n ^ " " ^ NotationPp.pp_term status t
233   | NDiscriminator (_,_)
234   | NInverter (_,_,_,_,_)
235   | NUnivConstraint (_) -> "not supported"
236   | NCoercion (_) -> "not supported"
237   | NObj (_,obj,index) -> 
238       (if not index then "-" else "") ^ 
239         NotationPp.pp_obj (NotationPp.pp_term status) obj
240   | NQed (_,true) -> "qed"
241   | NQed (_,false) -> "qed-"
242   | NCopy (_,name,uri,map) -> 
243       "copy " ^ name ^ " from " ^ NUri.string_of_uri uri ^ " with " ^ 
244         String.concat " and " 
245           (List.map 
246             (fun (a,b) -> NUri.string_of_uri a ^ " ↦ " ^ NUri.string_of_uri b) 
247             map)
248   | Include (_,mode,path) -> (* not precise, since path is absolute *)
249       if mode = WithPreferences then
250         "include \"" ^ path ^ "\""
251       else
252         "include' \"" ^ path ^ "\""
253   | Alias (_,s) -> pp_alias s
254   | Interpretation (_, dsc, (symbol, arg_patterns), cic_appl_pattern) ->
255       pp_interpretation dsc symbol arg_patterns cic_appl_pattern
256   | Notation (_, dir_opt, l1_pattern, assoc, prec, l2_pattern) ->
257       pp_notation status dir_opt l1_pattern assoc prec l2_pattern
258 ;;
259     
260 let pp_executable status ~map_unicode_to_tex =
261   function
262   | NMacro (_, macro) -> pp_nmacro status macro ^ "."
263   | NTactic (_,tacl) ->
264       String.concat " " (List.map (pp_ntactic status ~map_unicode_to_tex) tacl)
265   | NCommand (_, cmd) -> pp_ncommand status cmd ^ "."
266                       
267 let pp_comment status ~map_unicode_to_tex =
268   function
269   | Note (_,"") -> Printf.sprintf "\n"
270   | Note (_,str) -> Printf.sprintf "\n(* %s *)" str
271   | Code (_,code) ->
272       Printf.sprintf "\n(** %s. **)" (pp_executable status ~map_unicode_to_tex code)
273
274 let pp_statement status =
275   function
276   | Executable (_, ex) -> pp_executable status ex 
277   | Comment (_, c) -> pp_comment status c