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