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