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