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