]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_notation/cicNotationPp.ml
snapshot
[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 l =
40   sprintf "literal(%s)"
41     (match l with
42     | `Symbol s
43     | `Keyword s
44     | `Number s -> s)
45
46 let rec pp_term = function
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   | LetIn (var, t1, t2) ->
66       sprintf "let %s = %s in %s" (pp_capture_variable var) (pp_term t1)
67         (pp_term t2)
68   | LetRec (kind, definitions, term) ->
69       sprintf "let %s %s in %s"
70         (match kind with `Inductive -> "rec" | `CoInductive -> "corec")
71         (String.concat " and "
72           (List.map
73             (fun (var, body, _) ->
74               sprintf "%s = %s" (pp_capture_variable var) (pp_term body))
75             definitions))
76         (pp_term term)
77   | Ident (name, Some []) | Ident (name, None)
78   | Uri (name, Some []) | Uri (name, None) ->
79       name
80   | Ident (name, Some substs)
81   | Uri (name, Some substs) ->
82       sprintf "%s \\subst [%s]" name (pp_substs substs)
83   | Implicit -> "?"
84   | Meta (index, substs) ->
85       sprintf "%d[%s]" index
86         (String.concat "; "
87           (List.map (function None -> "_" | Some term -> pp_term term) substs))
88   | Num (num, _) -> num
89   | Sort `Set -> "Set"
90   | Sort `Prop -> "Prop"
91   | Sort `Type -> "Type"
92   | Sort `CProp -> "CProp"
93   | Symbol (name, _) -> name
94
95   | UserInput -> ""
96
97   | Literal l -> pp_literal l
98   | Layout l -> pp_layout l
99   | Magic m -> pp_magic m
100   | Variable v -> pp_variable v
101
102 and pp_subst (name, term) = sprintf "%s \\Assign %s" name (pp_term term)
103 and pp_substs substs = String.concat "; " (List.map pp_subst substs)
104
105 and pp_pattern ((head, vars), term) =
106   sprintf "%s \\Rightarrow %s"
107     (match vars with
108     | [] -> head
109     | _ ->
110         sprintf "(%s %s)" head
111           (String.concat " " (List.map pp_capture_variable vars)))
112     (pp_term term)
113
114 and pp_patterns patterns =
115   sprintf "[%s]" (String.concat " | " (List.map pp_pattern patterns))
116
117 and pp_capture_variable = function
118   | term, None -> pp_term term
119   | term, Some typ -> "(" ^ pp_term term ^ ": " ^ pp_term typ ^ ")"
120
121 and pp_layout = function
122   | Sub (t1, t2) -> sprintf "%s \\SUB %s" (pp_term t1) (pp_term t2)
123   | Sup (t1, t2) -> sprintf "%s \\SUP %s" (pp_term t1) (pp_term t2)
124   | Below (t1, t2) -> sprintf "%s \\BELOW %s" (pp_term t1) (pp_term t2)
125   | Above (t1, t2) -> sprintf "%s \\ABOVE %s" (pp_term t1) (pp_term t2)
126   | Over (t1, t2) -> sprintf "[%s \\OVER %s]" (pp_term t1) (pp_term t2)
127   | Atop (t1, t2) -> sprintf "[%s \\ATOP %s]" (pp_term t1) (pp_term t2)
128   | Frac (t1, t2) -> sprintf "\\FRAC %s %s" (pp_term t1) (pp_term t2)
129   | Sqrt t -> sprintf "\\SQRT %s" (pp_term t)
130   | Root (arg, index) ->
131       sprintf "\\ROOT %s \\OF %s" (pp_term index) (pp_term arg)
132 (*   | Break -> "\\BREAK" *)
133   | Box (H, terms) ->
134       sprintf "\\HBOX [%s]" (String.concat " " (List.map pp_term terms))
135   | Box (V, terms) ->
136       sprintf "\\VBOX [%s]" (String.concat " " (List.map pp_term terms))
137   | Box (HV, terms) ->
138       sprintf "\\HVBOX [%s]" (String.concat " " (List.map pp_term terms))
139   | Box (HOV, terms) ->
140       sprintf "\\HOVBOX [%s]" (String.concat " " (List.map pp_term terms))
141
142 and pp_magic = function
143   | List0 (t, sep_opt) ->
144       sprintf "\\LIST0 %s%s" (pp_term t) (pp_sep_opt sep_opt)
145   | List1 (t, sep_opt) ->
146       sprintf "\\LIST1 %s%s" (pp_term t) (pp_sep_opt sep_opt)
147   | Opt t -> sprintf "\\OPT %s" (pp_term t)
148   | Fold (k, p_base, names, p_rec) ->
149       let acc = match names with acc :: _ -> acc | _ -> assert false in
150       sprintf "\\FOLD %s \\[%s\\] \\LAMBDA %s \\[%s\\]"
151         (pp_fold_kind k) (pp_term p_base) acc (pp_term p_rec)
152   | Default (p_some, p_none) ->
153       sprintf "\\DEFAULT \\[%s\\] \\[%s\\]" (pp_term p_some) (pp_term p_none)
154
155 and pp_fold_kind = function
156   | `Left -> "left"
157   | `Right -> "right"
158
159 and pp_sep_opt = function
160   | None -> ""
161   | Some sep -> sprintf "\\SEP %s" (pp_literal sep)
162
163 and pp_variable = function
164   | NumVar s -> "\\NUM " ^ s
165   | IdentVar s -> "\\IDENT " ^ s
166   | TermVar s -> "\\TERM " ^ s
167   | Ascription (t, n) -> sprintf "[%s \\AS %s]" (pp_term t) n
168   | FreshVar n -> "\\FRESH " ^ n
169
170 let rec pp_value = function
171   | TermValue t -> sprintf "$%s$" (pp_term t)
172   | StringValue s -> sprintf "\"%s\"" s
173   | NumValue n -> n
174   | OptValue (Some v) -> "Some " ^ pp_value v
175   | OptValue None -> "None"
176   | ListValue l -> sprintf "[%s]" (String.concat "; " (List.map pp_value l))
177
178 let rec pp_value_type =
179   function
180   | TermType -> "Term"
181   | StringType -> "String"
182   | NumType -> "Number"
183   | OptType t -> "Maybe " ^ pp_value_type t
184   | ListType l -> "List " ^ pp_value_type l
185
186 let pp_env env =
187   String.concat "; "
188     (List.map
189       (fun (name, (ty, value)) ->
190         sprintf "%s : %s = %s" name (pp_value_type ty) (pp_value value))
191       env)
192