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