]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_notation/cicNotationPp.ml
Better handling of idref propagation, no more Href hack, multiple idrefs are
[helm.git] / helm / ocaml / cic_notation / 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 open Printf
27
28 module Ast = CicNotationPt
29 module Env = CicNotationEnv
30
31   (* when set to true debugging information, not in sync with input syntax, will
32    * be added to the output of pp_term.
33    * set to false if you need, for example, cut and paste from matitac output to
34    * matitatop *)
35 let debug_printing = false
36
37 let pp_binder = function
38   | `Lambda -> "lambda"
39   | `Pi -> "Pi"
40   | `Exists -> "exists"
41   | `Forall -> "forall"
42
43 let pp_literal =
44   if debug_printing then
45     (function (* debugging version *)
46       | `Symbol s -> sprintf "symbol(%s)" s
47       | `Keyword s -> sprintf "keyword(%s)" s
48       | `Number s -> sprintf "number(%s)" s)
49   else
50     (function
51       | `Symbol s
52       | `Keyword s
53       | `Number s -> s)
54
55 let rec pp_term ?(pp_parens = true) t =
56   let t_pp =
57     match t with
58     | Ast.AttributedTerm (`IdRef id, term) when debug_printing ->
59         sprintf "x(%s)[%s]" id (pp_term ~pp_parens:false term)
60     | Ast.AttributedTerm (`XmlAttrs attrs, term) when debug_printing ->
61         sprintf "X(%s)[%s]"
62           (String.concat ";"
63             (List.map (fun (_, n, v) -> sprintf "%s=%s" n v) attrs))
64           (pp_term ~pp_parens:false term)
65     | Ast.AttributedTerm (_, term) when debug_printing ->
66         sprintf "@[%s]" (pp_term ~pp_parens:false term)
67     | Ast.AttributedTerm (`Raw text, _) -> text
68     | Ast.AttributedTerm (_, term) -> pp_term ~pp_parens:false term
69
70     | Ast.Appl terms ->
71         sprintf "%s" (String.concat " " (List.map pp_term terms))
72     | Ast.Binder (`Forall, (Ast.Ident ("_", None), typ), body)
73     | Ast.Binder (`Pi, (Ast.Ident ("_", None), typ), body) ->
74         sprintf "%s \\to %s"
75           (match typ with None -> "?" | Some typ -> pp_term typ)
76           (pp_term body)
77     | Ast.Binder (kind, var, body) ->
78         sprintf "\\%s %s.%s" (pp_binder kind) (pp_capture_variable var)
79           (pp_term body)
80     | Ast.Case (term, indtype, typ, patterns) ->
81         sprintf "%smatch %s%s with %s"
82           (match typ with None -> "" | Some t -> sprintf "[%s]" (pp_term t))
83           (pp_term term)
84           (match indtype with
85           | None -> ""
86           | Some (ty, href_opt) ->
87               sprintf " in %s%s" ty
88               (match debug_printing, href_opt with
89               | true, Some uri ->
90                   sprintf "(i.e.%s)" (UriManager.string_of_uri uri)
91               | _ -> ""))
92           (pp_patterns patterns)
93     | Ast.Cast (t1, t2) -> sprintf "(%s: %s)" (pp_term t1) (pp_term t2)
94     | Ast.LetIn (var, t1, t2) ->
95         sprintf "let %s = %s in %s" (pp_capture_variable var) (pp_term t1)
96           (pp_term t2)
97     | Ast.LetRec (kind, definitions, term) ->
98         sprintf "let %s %s in %s"
99           (match kind with `Inductive -> "rec" | `CoInductive -> "corec")
100           (String.concat " and "
101             (List.map
102               (fun (var, body, _) ->
103                 sprintf "%s = %s" (pp_capture_variable var) (pp_term body))
104               definitions))
105           (pp_term term)
106     | Ast.Ident (name, Some []) | Ast.Ident (name, None)
107     | Ast.Uri (name, Some []) | Ast.Uri (name, None) ->
108         name
109     | Ast.Ident (name, Some substs)
110     | Ast.Uri (name, Some substs) ->
111         sprintf "%s \\subst [%s]" name (pp_substs substs)
112     | Ast.Implicit -> "?"
113     | Ast.Meta (index, substs) ->
114         sprintf "%d[%s]" index
115           (String.concat "; "
116             (List.map (function None -> "_" | Some t -> pp_term t) substs))
117     | Ast.Num (num, _) -> num
118     | Ast.Sort `Set -> "Set"
119     | Ast.Sort `Prop -> "Prop"
120     | Ast.Sort (`Type _) -> "Type"
121     | Ast.Sort `CProp -> "CProp"
122     | Ast.Symbol (name, _) -> "'" ^ name
123
124     | Ast.UserInput -> ""
125
126     | Ast.Literal l -> pp_literal l
127     | Ast.Layout l -> pp_layout l
128     | Ast.Magic m -> pp_magic m
129     | Ast.Variable v -> pp_variable v
130   in
131   if pp_parens then sprintf "(%s)" t_pp
132   else t_pp
133
134 and pp_subst (name, term) = sprintf "%s \\Assign %s" name (pp_term term)
135 and pp_substs substs = String.concat "; " (List.map pp_subst substs)
136
137 and pp_pattern ((head, href, vars), term) =
138   let head_pp =
139     head ^
140     (match debug_printing, href with
141     | true, Some uri -> sprintf "(i.e.%s)" (UriManager.string_of_uri uri)
142     | _ -> "")
143   in
144   sprintf "%s \\Rightarrow %s"
145     (match vars with
146     | [] -> head_pp
147     | _ ->
148         sprintf "(%s %s)" head_pp
149           (String.concat " " (List.map pp_capture_variable vars)))
150     (pp_term term)
151
152 and pp_patterns patterns =
153   sprintf "[%s]" (String.concat " | " (List.map pp_pattern patterns))
154
155 and pp_capture_variable = function
156   | term, None -> pp_term term
157   | term, Some typ -> "(" ^ pp_term term ^ ": " ^ pp_term typ ^ ")"
158
159 and pp_box_spec (kind, spacing, indent) =
160   let int_of_bool b = if b then 1 else 0 in
161   let kind_string =
162     match kind with
163     Ast.H -> "H" | Ast.V -> "V" | Ast.HV -> "HV" | Ast.HOV -> "HOV"
164   in
165   sprintf "%sBOX%d%d" kind_string (int_of_bool spacing) (int_of_bool indent)
166
167 and pp_layout = function
168   | Ast.Sub (t1, t2) -> sprintf "%s \\SUB %s" (pp_term t1) (pp_term t2)
169   | Ast.Sup (t1, t2) -> sprintf "%s \\SUP %s" (pp_term t1) (pp_term t2)
170   | Ast.Below (t1, t2) -> sprintf "%s \\BELOW %s" (pp_term t1) (pp_term t2)
171   | Ast.Above (t1, t2) -> sprintf "%s \\ABOVE %s" (pp_term t1) (pp_term t2)
172   | Ast.Over (t1, t2) -> sprintf "[%s \\OVER %s]" (pp_term t1) (pp_term t2)
173   | Ast.Atop (t1, t2) -> sprintf "[%s \\ATOP %s]" (pp_term t1) (pp_term t2)
174   | Ast.Frac (t1, t2) -> sprintf "\\FRAC %s %s" (pp_term t1) (pp_term t2)
175   | Ast.Sqrt t -> sprintf "\\SQRT %s" (pp_term t)
176   | Ast.Root (arg, index) ->
177       sprintf "\\ROOT %s \\OF %s" (pp_term index) (pp_term arg)
178   | Ast.Break -> "\\BREAK"
179 (*   | Space -> "\\SPACE" *)
180   | Ast.Box (box_spec, terms) ->
181       sprintf "\\%s [%s]" (pp_box_spec box_spec)
182         (String.concat " " (List.map pp_term terms))
183   | Ast.Group terms ->
184       sprintf "\\GROUP [%s]" (String.concat " " (List.map pp_term terms))
185
186 and pp_magic = function
187   | Ast.List0 (t, sep_opt) ->
188       sprintf "list0 %s%s" (pp_term t) (pp_sep_opt sep_opt)
189   | Ast.List1 (t, sep_opt) ->
190       sprintf "list1 %s%s" (pp_term t) (pp_sep_opt sep_opt)
191   | Ast.Opt t -> sprintf "opt %s" (pp_term t)
192   | Ast.Fold (kind, p_base, names, p_rec) ->
193       let acc = match names with acc :: _ -> acc | _ -> assert false in
194       sprintf "fold %s %s rec %s %s"
195         (pp_fold_kind kind) (pp_term p_base) acc (pp_term p_rec)
196   | Ast.Default (p_some, p_none) ->
197       sprintf "default %s %s" (pp_term p_some) (pp_term p_none)
198   | Ast.If (p_test, p_true, p_false) ->
199       sprintf "if %s then %s else %s"
200         (pp_term p_test) (pp_term p_true) (pp_term p_false)
201   | Ast.Fail -> "fail"
202
203 and pp_fold_kind = function
204   | `Left -> "left"
205   | `Right -> "right"
206
207 and pp_sep_opt = function
208   | None -> ""
209   | Some sep -> sprintf " sep %s" (pp_literal sep)
210
211 and pp_variable = function
212   | Ast.NumVar s -> "number " ^ s
213   | Ast.IdentVar s -> "ident " ^ s
214   | Ast.TermVar s -> "term " ^ s
215   | Ast.Ascription (t, n) -> assert false
216   | Ast.FreshVar n -> "fresh " ^ n
217
218 let pp_term t = pp_term ~pp_parens:false t
219
220 let rec pp_value = function
221   | Env.TermValue t -> sprintf "$%s$" (pp_term t)
222   | Env.StringValue s -> sprintf "\"%s\"" s
223   | Env.NumValue n -> n
224   | Env.OptValue (Some v) -> "Some " ^ pp_value v
225   | Env.OptValue None -> "None"
226   | Env.ListValue l -> sprintf "[%s]" (String.concat "; " (List.map pp_value l))
227
228 let rec pp_value_type =
229   function
230   | Env.TermType -> "Term"
231   | Env.StringType -> "String"
232   | Env.NumType -> "Number"
233   | Env.OptType t -> "Maybe " ^ pp_value_type t
234   | Env.ListType l -> "List " ^ pp_value_type l
235
236 let pp_env env =
237   String.concat "; "
238     (List.map
239       (fun (name, (ty, value)) ->
240         sprintf "%s : %s = %s" name (pp_value_type ty) (pp_value value))
241       env)
242