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