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