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