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