]> matita.cs.unibo.it Git - helm.git/blob - components/acic_content/cicNotationPp.ml
matita 0.5.1 tagged
[helm.git] / components / acic_content / cicNotationPp.ml
1 (* Copyright (C) 2004-2005, 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 module Ast = CicNotationPt
31 module Env = CicNotationEnv
32
33   (* when set to true debugging information, not in sync with input syntax, will
34    * be added to the output of pp_term.
35    * set to false if you need, for example, cut and paste from matitac output to
36    * matitatop *)
37 let debug_printing = true
38
39 let pp_binder = function
40   | `Lambda -> "lambda"
41   | `Pi -> "Pi"
42   | `Exists -> "exists"
43   | `Forall -> "forall"
44
45 let pp_literal =
46   if debug_printing then
47     (function (* debugging version *)
48       | `Symbol s -> sprintf "symbol(%s)" s
49       | `Keyword s -> sprintf "keyword(%s)" s
50       | `Number s -> sprintf "number(%s)" s)
51   else
52     (function
53       | `Symbol s
54       | `Keyword s
55       | `Number s -> s)
56
57 let pp_assoc =
58   function
59   | Gramext.NonA -> "NonA"
60   | Gramext.LeftA -> "LeftA"
61   | Gramext.RightA -> "RightA"
62
63 let pp_pos =
64   function
65 (*      `None -> "`None" *)
66     | `Left -> "`Left"
67     | `Right -> "`Right"
68     | `Inner -> "`Inner"
69
70 let pp_attribute =
71   function
72   | `IdRef id -> sprintf "x(%s)" id
73   | `XmlAttrs attrs ->
74       sprintf "X(%s)"
75         (String.concat ";"
76           (List.map (fun (_, n, v) -> sprintf "%s=%s" n v) attrs))
77   | `Level (prec, assoc) -> sprintf "L(%d%s)" prec (pp_assoc assoc)
78   | `Raw _ -> "R"
79   | `Loc _ -> "@"
80   | `ChildPos p -> sprintf "P(%s)" (pp_pos p)
81
82 let pp_capture_variable pp_term = 
83   function
84   | term, None -> pp_term (* ~pp_parens:false *) term
85   | term, Some typ -> "(" ^ pp_term (* ~pp_parens:false *) term ^ ": " ^ pp_term typ ^ ")"
86
87 let rec pp_term ?(pp_parens = true) t =
88   let t_pp =
89     match t with
90     | Ast.AttributedTerm (attr, term) when debug_printing ->
91         sprintf "%s[%s]" (pp_attribute attr) (pp_term ~pp_parens:false term)
92     | Ast.AttributedTerm (`Raw text, _) -> text
93     | Ast.AttributedTerm (_, term) -> pp_term ~pp_parens:false term
94     | Ast.Appl terms ->
95         sprintf "%s" (String.concat " " (List.map pp_term terms))
96     | Ast.Binder (`Forall, (Ast.Ident ("_", None), typ), body)
97     | Ast.Binder (`Pi, (Ast.Ident ("_", None), typ), body) ->
98         sprintf "%s \\to %s"
99           (match typ with None -> "?" | Some typ -> pp_term typ)
100           (pp_term ~pp_parens:true body)
101     | Ast.Binder (kind, var, body) ->
102         sprintf "\\%s %s.%s" (pp_binder kind) (pp_capture_variable pp_term var)
103           (pp_term ~pp_parens:true body)
104     | Ast.Case (term, indtype, typ, patterns) ->
105         sprintf "match %s%s%s with %s"
106           (pp_term term)
107           (match indtype with
108           | None -> ""
109           | Some (ty, href_opt) ->
110               sprintf " in %s%s" ty
111               (match debug_printing, href_opt with
112               | true, Some uri ->
113                   sprintf "(i.e.%s)" (UriManager.string_of_uri uri)
114               | _ -> ""))         
115           (match typ with None -> "" | Some t -> sprintf " return %s" (pp_term t))          
116           (pp_patterns patterns)
117     | Ast.Cast (t1, t2) -> sprintf "(%s: %s)" (pp_term ~pp_parens:true t1) (pp_term ~pp_parens:true t2)
118     | Ast.LetIn ((var,t2), t1, t3) ->
119 (*       let t2 = match t2 with None -> Ast.Implicit | Some t -> t in *)
120         sprintf "let %s \\def %s in %s" (pp_term var)
121 (*           (pp_term ~pp_parens:true t2) *)
122           (pp_term ~pp_parens:true t1)
123           (pp_term ~pp_parens:true t3)
124     | Ast.LetRec (kind, definitions, term) ->
125         let rec get_guard i = function
126            | []                   -> (*assert false*) Ast.Implicit
127            | [term, _] when i = 1 -> term
128            | _ :: tl              -> get_guard (pred i) tl
129         in
130         let map (params, (id,typ), body, i) =
131          let typ =
132           match typ with
133              None -> Ast.Implicit
134            | Some typ -> typ
135          in
136            sprintf "%s %s on %s: %s \\def %s" 
137               (pp_term ~pp_parens:false term)
138               (String.concat " " (List.map (pp_capture_variable pp_term) params))
139               (pp_term ~pp_parens:false (get_guard i params))
140               (pp_term typ) (pp_term body)
141         in
142         sprintf "let %s %s in %s"
143           (match kind with `Inductive -> "rec" | `CoInductive -> "corec")
144           (String.concat " and " (List.map map definitions))
145           (pp_term term)
146     | Ast.Ident (name, Some []) | Ast.Ident (name, None)
147     | Ast.Uri (name, Some []) | Ast.Uri (name, None) ->
148         name
149     | Ast.Ident (name, Some substs)
150     | Ast.Uri (name, Some substs) ->
151         sprintf "%s \\subst [%s]" name (pp_substs substs)
152     | Ast.Implicit -> "?"
153     | Ast.Meta (index, substs) ->
154         sprintf "%d[%s]" index
155           (String.concat "; "
156             (List.map (function None -> "_" | Some t -> pp_term t) substs))
157     | Ast.Num (num, _) -> num
158     | Ast.Sort `Set -> "Set"
159     | Ast.Sort `Prop -> "Prop"
160     | Ast.Sort (`Type _) -> "Type"
161     | Ast.Sort `CProp -> "CProp"
162     | Ast.Symbol (name, _) -> "'" ^ name
163
164     | Ast.UserInput -> ""
165
166     | Ast.Literal l -> pp_literal l
167     | Ast.Layout l -> pp_layout l
168     | Ast.Magic m -> pp_magic m
169     | Ast.Variable v -> pp_variable v
170   in
171   match pp_parens, t with
172     | false, _ 
173     | true, Ast.Implicit
174     | true, Ast.Sort _
175     | true, Ast.Ident (_, Some []) 
176     | true, Ast.Ident (_, None)    -> t_pp
177     | _                            -> sprintf "(%s)" t_pp
178
179 and pp_subst (name, term) = sprintf "%s \\Assign %s" name (pp_term term)
180 and pp_substs substs = String.concat "; " (List.map pp_subst substs)
181
182 and pp_pattern =
183  function
184     Ast.Pattern (head, href, vars), term ->
185      let head_pp =
186        head ^
187        (match debug_printing, href with
188        | true, Some uri -> sprintf "(i.e.%s)" (UriManager.string_of_uri uri)
189        | _ -> "")
190      in
191      sprintf "%s \\Rightarrow %s"
192        (match vars with
193        | [] -> head_pp
194        | _ ->
195            sprintf "(%s %s)" head_pp
196              (String.concat " " (List.map (pp_capture_variable pp_term) vars)))
197        (pp_term term)
198   | Ast.Wildcard, term ->
199      sprintf "_ \\Rightarrow %s" (pp_term term)
200
201 and pp_patterns patterns =
202   sprintf "[%s]" (String.concat " | " (List.map pp_pattern patterns))
203
204 and pp_box_spec (kind, spacing, indent) =
205   let int_of_bool b = if b then 1 else 0 in
206   let kind_string =
207     match kind with
208     Ast.H -> "H" | Ast.V -> "V" | Ast.HV -> "HV" | Ast.HOV -> "HOV"
209   in
210   sprintf "%sBOX%d%d" kind_string (int_of_bool spacing) (int_of_bool indent)
211
212 and pp_layout = function
213   | Ast.Sub (t1, t2) -> sprintf "%s \\SUB %s" (pp_term t1) (pp_term t2)
214   | Ast.Sup (t1, t2) -> sprintf "%s \\SUP %s" (pp_term t1) (pp_term t2)
215   | Ast.Below (t1, t2) -> sprintf "%s \\BELOW %s" (pp_term t1) (pp_term t2)
216   | Ast.Above (t1, t2) -> sprintf "%s \\ABOVE %s" (pp_term t1) (pp_term t2)
217   | Ast.Over (t1, t2) -> sprintf "[%s \\OVER %s]" (pp_term t1) (pp_term t2)
218   | Ast.Atop (t1, t2) -> sprintf "[%s \\ATOP %s]" (pp_term t1) (pp_term t2)
219   | Ast.Frac (t1, t2) -> sprintf "\\FRAC %s %s" (pp_term t1) (pp_term t2)
220   | Ast.Sqrt t -> sprintf "\\SQRT %s" (pp_term t)
221   | Ast.Root (arg, index) ->
222       sprintf "\\ROOT %s \\OF %s" (pp_term index) (pp_term arg)
223   | Ast.Break -> "\\BREAK"
224 (*   | Space -> "\\SPACE" *)
225   | Ast.Box (box_spec, terms) ->
226       sprintf "\\%s [%s]" (pp_box_spec box_spec)
227         (String.concat " " (List.map pp_term terms))
228   | Ast.Group terms ->
229       sprintf "\\GROUP [%s]" (String.concat " " (List.map pp_term terms))
230
231 and pp_magic = function
232   | Ast.List0 (t, sep_opt) ->
233       sprintf "list0 %s%s" (pp_term t) (pp_sep_opt sep_opt)
234   | Ast.List1 (t, sep_opt) ->
235       sprintf "list1 %s%s" (pp_term t) (pp_sep_opt sep_opt)
236   | Ast.Opt t -> sprintf "opt %s" (pp_term t)
237   | Ast.Fold (kind, p_base, names, p_rec) ->
238       let acc = match names with acc :: _ -> acc | _ -> assert false in
239       sprintf "fold %s %s rec %s %s"
240         (pp_fold_kind kind) (pp_term p_base) acc (pp_term p_rec)
241   | Ast.Default (p_some, p_none) ->
242       sprintf "default %s %s" (pp_term p_some) (pp_term p_none)
243   | Ast.If (p_test, p_true, p_false) ->
244       sprintf "if %s then %s else %s"
245         (pp_term p_test) (pp_term p_true) (pp_term p_false)
246   | Ast.Fail -> "fail"
247
248 and pp_fold_kind = function
249   | `Left -> "left"
250   | `Right -> "right"
251
252 and pp_sep_opt = function
253   | None -> ""
254   | Some sep -> sprintf " sep %s" (pp_literal sep)
255
256 and pp_variable = function
257   | Ast.NumVar s -> "number " ^ s
258   | Ast.IdentVar s -> "ident " ^ s
259   | Ast.TermVar s -> "term " ^ s
260   | Ast.Ascription (t, n) -> assert false
261   | Ast.FreshVar n -> "fresh " ^ n
262
263 let _pp_term = ref (pp_term ~pp_parens:false)
264 let pp_term t = !_pp_term t
265 let set_pp_term f = _pp_term := f
266
267 let pp_params pp_term = function
268   | [] -> ""
269   | params -> " " ^ String.concat " " (List.map (pp_capture_variable pp_term) params)
270       
271 let pp_flavour = function
272   | `Definition -> "definition"
273   | `MutualDefinition -> assert false
274   | `Fact -> "fact"
275   | `Goal -> "goal"
276   | `Lemma -> "lemma"
277   | `Remark -> "remark"
278   | `Theorem -> "theorem"
279   | `Variant -> "variant"
280   | `Axiom -> "axiom"
281
282 let pp_fields pp_term fields =
283   (if fields <> [] then "\n" else "") ^ 
284   String.concat ";\n" 
285     (List.map 
286       (fun (name,ty,coercion,arity) -> 
287         " " ^ name ^ 
288         if coercion then (":" ^ 
289           if arity > 0 then string_of_int arity else "" ^ ">") else ": " ^
290         pp_term ty) fields)
291         
292 let pp_obj pp_term = function
293  | Ast.Inductive (params, types) ->
294     let pp_constructors constructors =
295       String.concat "\n"
296         (List.map (fun (name, typ) -> sprintf "| %s: %s" name (pp_term typ))
297           constructors)
298     in
299     let pp_type (name, _, typ, constructors) =
300       sprintf "\nwith %s: %s \\def\n%s" name (pp_term typ)
301         (pp_constructors constructors)
302     in
303     (match types with
304     | [] -> assert false
305     | (name, inductive, typ, constructors) :: tl ->
306         let fst_typ_pp =
307           sprintf "%sinductive %s%s: %s \\def\n%s"
308             (if inductive then "" else "co") name (pp_params pp_term params)
309             (pp_term typ) (pp_constructors constructors)
310         in
311         fst_typ_pp ^ String.concat "" (List.map pp_type tl))
312  | Ast.Theorem (`MutualDefinition, name, typ, body) ->
313     sprintf "<<pretty printing of mutual definitions not implemented yet>>"
314  | Ast.Theorem (flavour, name, typ, body) ->
315     sprintf "%s %s:\n %s\n%s"
316       (pp_flavour flavour)
317       name
318       (pp_term typ)
319       (match body with
320       | None -> ""
321       | Some body -> "\\def\n " ^ pp_term body)
322  | Ast.Record (params,name,ty,fields) ->
323     "record " ^ name ^ " " ^ pp_params pp_term params ^ ": " ^ pp_term ty ^ 
324     " \\def {" ^ pp_fields pp_term fields ^ "\n}"
325
326 let rec pp_value = function
327   | Env.TermValue t -> sprintf "$%s$" (pp_term t)
328   | Env.StringValue s -> sprintf "\"%s\"" s
329   | Env.NumValue n -> n
330   | Env.OptValue (Some v) -> "Some " ^ pp_value v
331   | Env.OptValue None -> "None"
332   | Env.ListValue l -> sprintf "[%s]" (String.concat "; " (List.map pp_value l))
333
334 let rec pp_value_type =
335   function
336   | Env.TermType -> "Term"
337   | Env.StringType -> "String"
338   | Env.NumType -> "Number"
339   | Env.OptType t -> "Maybe " ^ pp_value_type t
340   | Env.ListType l -> "List " ^ pp_value_type l
341
342 let pp_env env =
343   String.concat "; "
344     (List.map
345       (fun (name, (ty, value)) ->
346         sprintf "%s : %s = %s" name (pp_value_type ty) (pp_value value))
347       env)
348
349 let rec pp_cic_appl_pattern = function
350   | Ast.UriPattern uri -> UriManager.string_of_uri uri
351   | Ast.VarPattern name -> name
352   | Ast.ImplicitPattern -> "_"
353   | Ast.ApplPattern aps ->
354       sprintf "(%s)" (String.concat " " (List.map pp_cic_appl_pattern aps))
355