]> 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 pp_binder = function
32   | `Lambda -> "lambda"
33   | `Pi -> "Pi"
34   | `Exists -> "exists"
35   | `Forall -> "forall"
36
37 let pp_literal l =
38   sprintf "literal(%s)"
39     (match l with
40     | `Symbol s
41     | `Keyword s
42     | `Number s -> s)
43
44 let rec pp_term = function
45   | AttributedTerm (_, term) -> pp_term term
46
47   | Appl terms ->
48       sprintf "(%s)" (String.concat " " (List.map pp_term terms))
49   | Binder (`Forall, (Ident ("_", None), typ), body)
50   | Binder (`Pi, (Ident ("_", None), typ), body) ->
51       sprintf "(%s \\to %s)"
52         (match typ with None -> "?" | Some typ -> pp_term typ)
53         (pp_term body)
54   | Binder (kind, var, body) ->
55       sprintf "\\%s %s.%s" (pp_binder kind) (pp_capture_variable var)
56         (pp_term body)
57   | Case (term, indtype, typ, patterns) ->
58       sprintf "%smatch %s with %s"
59         (match typ with None -> "" | Some t -> sprintf "<%s>" (pp_term t))
60         (pp_term term) (pp_patterns patterns)
61   | LetIn (var, t1, t2) ->
62       sprintf "let %s = %s in %s" (pp_capture_variable var) (pp_term t1)
63         (pp_term t2)
64   | LetRec (kind, definitions, term) ->
65       sprintf "let %s %s in %s"
66         (match kind with `Inductive -> "rec" | `CoInductive -> "corec")
67         (String.concat " and "
68           (List.map
69             (fun (var, body, _) ->
70               sprintf "%s = %s" (pp_capture_variable var) (pp_term body))
71             definitions))
72         (pp_term term)
73   | Ident (name, Some []) | Ident (name, None)
74   | Uri (name, Some []) | Uri (name, None) ->
75       name
76   | Ident (name, Some substs)
77   | Uri (name, Some substs) ->
78       sprintf "%s \\subst [%s]" name (pp_substs substs)
79   | Implicit -> "?"
80   | Meta (index, substs) ->
81       sprintf "%d[%s]" index
82         (String.concat "; "
83           (List.map (function None -> "_" | Some term -> pp_term term) substs))
84   | Num (num, _) -> num
85   | Sort `Set -> "Set"
86   | Sort `Prop -> "Prop"
87   | Sort `Type -> "Type"
88   | Sort `CProp -> "CProp"
89   | Symbol (name, _) -> name
90
91   | UserInput -> ""
92
93   | Literal l -> pp_literal l
94   | Layout l -> pp_layout l
95   | Magic m -> pp_magic m
96   | Variable v -> pp_variable v
97
98 and pp_subst (name, term) = sprintf "%s \\Assign %s" name (pp_term term)
99 and pp_substs substs = String.concat "; " (List.map pp_subst substs)
100
101 and pp_pattern ((head, vars), term) =
102   sprintf "%s \\Rightarrow %s"
103     (match vars with
104     | [] -> head
105     | _ ->
106         sprintf "(%s %s)" head
107           (String.concat " " (List.map pp_capture_variable vars)))
108     (pp_term term)
109
110 and pp_patterns patterns =
111   sprintf "[%s]" (String.concat " | " (List.map pp_pattern patterns))
112
113 and pp_capture_variable = function
114   | term, None -> pp_term term
115   | term, Some typ -> "(" ^ pp_term term ^ ": " ^ pp_term typ ^ ")"
116
117 and pp_layout = function
118   | Sub (t1, t2) -> sprintf "%s \\SUB %s" (pp_term t1) (pp_term t2)
119   | Sup (t1, t2) -> sprintf "%s \\SUP %s" (pp_term t1) (pp_term t2)
120   | Below (t1, t2) -> sprintf "%s \\BELOW %s" (pp_term t1) (pp_term t2)
121   | Above (t1, t2) -> sprintf "%s \\ABOVE %s" (pp_term t1) (pp_term t2)
122   | Over (t1, t2) -> sprintf "[%s \\OVER %s]" (pp_term t1) (pp_term t2)
123   | Atop (t1, t2) -> sprintf "[%s \\ATOP %s]" (pp_term t1) (pp_term t2)
124   | Frac (t1, t2) -> sprintf "\\FRAC %s %s" (pp_term t1) (pp_term t2)
125   | Sqrt t -> sprintf "\\SQRT %s" (pp_term t)
126   | Root (arg, index) ->
127       sprintf "\\ROOT %s \\OF %s" (pp_term index) (pp_term arg)
128   | Break -> "\\BREAK"
129   | Box (H, terms) ->
130       sprintf "\\HBOX [%s]" (String.concat " " (List.map pp_term terms))
131   | Box (V, terms) ->
132       sprintf "\\VBOX [%s]" (String.concat " " (List.map pp_term terms))
133
134 and pp_magic = function
135   | List0 (t, sep_opt) -> sprintf "\\LIST0 %s%s" (pp_term t) (pp_sep_opt sep_opt)
136   | List1 (t, sep_opt) -> sprintf "\\LIST1 %s%s" (pp_term t) (pp_sep_opt sep_opt)
137   | Opt t -> sprintf "\\OPT %s" (pp_term t)
138   | _ -> failwith "magic not implemented"
139
140 and pp_sep_opt = function
141   | None -> ""
142   | Some sep -> sprintf "\\SEP %s" (pp_literal sep)
143
144 and pp_variable = function
145   | NumVar s -> "\\NUM " ^ s
146   | IdentVar s -> "\\IDENT " ^ s
147   | TermVar s -> "\\TERM " ^ s
148   | Ascription (t, n) -> sprintf "[%s \\AS %s]" (pp_term t) n
149   | FreshVar n -> "\\FRESH " ^ n
150
151 let rec pp_value = function
152   | TermValue t -> sprintf "$%s$" (pp_term t)
153   | StringValue s -> sprintf "\"%s\"" s
154   | NumValue n -> n
155   | OptValue (Some v) -> "Some " ^ pp_value v
156   | OptValue None -> "None"
157   | ListValue l -> sprintf "[%s]" (String.concat "; " (List.map pp_value l))
158
159 let rec pp_value_type =
160   function
161   | TermType -> "Term"
162   | StringType -> "String"
163   | NumType -> "Number"
164   | OptType t -> "Maybe " ^ pp_value_type t
165   | ListType l -> "List " ^ pp_value_type l
166
167 let pp_env env =
168   String.concat "; "
169     (List.map
170       (fun (name, (ty, value)) ->
171         sprintf "%s : %s = %s" name (pp_value_type ty) (pp_value value))
172       env)
173