]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_proof_checking/cicPp.ml
added Pp of Cast
[helm.git] / helm / ocaml / cic_proof_checking / cicPp.ml
1 (* Copyright (C) 2000, 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://cs.unibo.it/helm/.
24  *)
25
26 (******************************************************************************)
27 (*                                                                            *)
28 (*                               PROJECT HELM                                 *)
29 (*                                                                            *)
30 (*                Claudio Sacerdoti Coen <sacerdot@cs.unibo.it>               *)
31 (*                                 24/01/2000                                 *)
32 (*                                                                            *)
33 (* This module implements a very simple Coq-like pretty printer that, given   *)
34 (* an object of cic (internal representation) returns a string describing the *)
35 (* object in a syntax similar to that of coq                                  *)
36 (*                                                                            *)
37 (******************************************************************************)
38
39 exception CicPpInternalError;;
40 exception NotEnoughElements;;
41
42 (* Utility functions *)
43
44 let ppname =
45  function
46     Cic.Name s     -> s
47   | Cic.Anonymous  -> "_"
48 ;;
49
50 (* get_nth l n   returns the nth element of the list l if it exists or *)
51 (* raises NotEnoughElements if l has less than n elements             *)
52 let rec get_nth l n =
53  match (n,l) with
54     (1, he::_) -> he
55   | (n, he::tail) when n > 1 -> get_nth tail (n-1)
56   | (_,_) -> raise NotEnoughElements
57 ;;
58
59 (* pp t l                                                                  *)
60 (* pretty-prints a term t of cic in an environment l where l is a list of  *)
61 (* identifier names used to resolve DeBrujin indexes. The head of l is the *)
62 (* name associated to the greatest DeBrujin index in t                     *)
63 let rec pp t l =
64  let module C = Cic in
65    match t with
66       C.Rel n ->
67        begin
68         try
69          (match get_nth l n with
70              Some (C.Name s) -> s
71            | Some C.Anonymous -> "__" ^ string_of_int n
72            | None -> "_hidden_" ^ string_of_int n
73          )
74         with
75          NotEnoughElements -> string_of_int (List.length l - n)
76        end
77     | C.Var (uri,exp_named_subst) ->
78        UriManager.string_of_uri (*UriManager.name_of_uri*) uri ^ pp_exp_named_subst exp_named_subst l
79     | C.Meta (n,l1) ->
80        "?" ^ (string_of_int n) ^ "[" ^ 
81         String.concat " ; "
82          (List.rev_map (function None -> "_" | Some t -> pp t l) l1) ^
83         "]"
84     | C.Sort s ->
85        (match s with
86            C.Prop  -> "Prop"
87          | C.Set   -> "Set"
88          | C.Type _ -> "Type"
89          (*| C.Type u -> ("Type" ^ CicUniv.string_of_universe u)*)
90          | C.CProp -> "CProp" 
91        )
92     | C.Implicit _ -> "?"
93     | C.Prod (b,s,t) ->
94        (match b with
95           C.Name n -> "(" ^ n ^ ":" ^ pp s l ^ ")" ^ pp t ((Some b)::l)
96         | C.Anonymous -> "(" ^ pp s l ^ "->" ^ pp t ((Some b)::l) ^ ")"
97        )
98     | C.Cast (v,t) -> "(" ^ pp v l ^ ":" ^ pp t l ^ ")"
99     | C.Lambda (b,s,t) ->
100        "[" ^ ppname b ^ ":" ^ pp s l ^ "]" ^ pp t ((Some b)::l)
101     | C.LetIn (b,s,t) ->
102        "[" ^ ppname b ^ ":=" ^ pp s l ^ "]" ^ pp t ((Some b)::l)
103     | C.Appl li ->
104        "(" ^
105        (List.fold_right
106         (fun x i -> pp x l ^ (match i with "" -> "" | _ -> " ") ^ i)
107         li ""
108        ) ^ ")"
109     | C.Const (uri,exp_named_subst) ->
110        UriManager.name_of_uri uri ^ pp_exp_named_subst exp_named_subst l
111     | C.MutInd (uri,n,exp_named_subst) ->
112        (try
113          match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
114             C.InductiveDefinition (dl,_,_,_) ->
115              let (name,_,_,_) = get_nth dl (n+1) in
116               name ^ pp_exp_named_subst exp_named_subst l
117           | _ -> raise CicPpInternalError
118         with
119          _ -> UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n + 1)
120        )
121     | C.MutConstruct (uri,n1,n2,exp_named_subst) ->
122        (try
123          match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
124             C.InductiveDefinition (dl,_,_,_) ->
125              let (_,_,_,cons) = get_nth dl (n1+1) in
126               let (id,_) = get_nth cons n2 in
127                id ^ pp_exp_named_subst exp_named_subst l
128           | _ -> raise CicPpInternalError
129         with
130          _ ->
131           UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n1 + 1) ^ "/" ^
132            string_of_int n2
133        )
134     | C.MutCase (uri,n1,ty,te,patterns) ->
135        let connames =
136         (match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
137             C.InductiveDefinition (dl,_,_,_) ->
138              let (_,_,_,cons) = get_nth dl (n1+1) in
139               List.map (fun (id,_) -> id) cons
140           | _ -> raise CicPpInternalError
141         )
142        in
143         "\n<" ^ pp ty l ^ ">Cases " ^ pp te l ^ " of " ^
144           List.fold_right (fun (x,y) i -> "\n " ^ x ^ " => " ^ pp y l ^ i)
145            (List.combine connames patterns) "" ^
146           "\nend"
147     | C.Fix (no, funs) ->
148        let snames = List.map (fun (name,_,_,_) -> name) funs in
149         let names =
150          List.rev (List.map (function name -> Some (C.Name name)) snames)
151         in
152          "\nFix " ^ get_nth snames (no + 1) ^ " {" ^
153          List.fold_right
154           (fun (name,ind,ty,bo) i -> "\n" ^ name ^ " / " ^ string_of_int ind ^
155             " : " ^ pp ty l ^ " := \n" ^
156             pp bo (names@l) ^ i)
157           funs "" ^
158          "}\n"
159     | C.CoFix (no,funs) ->
160        let snames = List.map (fun (name,_,_) -> name) funs in
161         let names =
162          List.rev (List.map (function name -> Some (C.Name name)) snames)
163         in
164          "\nCoFix " ^ get_nth snames (no + 1) ^ " {" ^
165          List.fold_right
166           (fun (name,ty,bo) i -> "\n" ^ name ^ 
167             " : " ^ pp ty l ^ " := \n" ^
168             pp bo (names@l) ^ i)
169           funs "" ^
170          "}\n"
171 and pp_exp_named_subst exp_named_subst l =
172  if exp_named_subst = [] then "" else
173   "{" ^
174    String.concat " ; " (
175     List.map
176      (function (uri,t) -> UriManager.name_of_uri uri ^ ":=" ^ pp t l)
177      exp_named_subst
178    ) ^ "}"
179 ;;
180
181 let ppterm t =
182  pp t []
183 ;;
184
185 (* ppinductiveType (typename, inductive, arity, cons)                       *)
186 (* pretty-prints a single inductive definition                              *)
187 (* (typename, inductive, arity, cons)                                       *)
188 let ppinductiveType (typename, inductive, arity, cons) =
189   (if inductive then "\nInductive " else "\nCoInductive ") ^ typename ^ ": " ^
190   pp arity [] ^ " =\n   " ^
191   List.fold_right
192    (fun (id,ty) i -> id ^ " : " ^ pp ty [] ^ 
193     (if i = "" then "\n" else "\n | ") ^ i)
194    cons ""
195 ;;
196
197 let ppcontext ?(sep = "\n") context =
198  let separate s = if s = "" then "" else s ^ sep in
199  fst (List.fold_right 
200    (fun context_entry (i,name_context) ->
201      match context_entry with
202         Some (n,Cic.Decl t) ->
203          Printf.sprintf "%s%s : %s" (separate i) (ppname n)
204           (pp t name_context), (Some n)::name_context
205       | Some (n,Cic.Def (bo,ty)) ->
206          Printf.sprintf "%s%s : %s := %s" (separate i) (ppname n)
207           (match ty with
208               None -> "_"
209             | Some ty -> pp ty name_context)
210           (pp bo name_context), (Some n)::name_context
211        | None ->
212           Printf.sprintf "%s_ :? _" (separate i), None::name_context
213     ) context ("",[]))
214
215 (* ppobj obj  returns a string with describing the cic object obj in a syntax *)
216 (* similar to the one used by Coq                                             *)
217 let ppobj obj =
218  let module C = Cic in
219  let module U = UriManager in
220   match obj with
221     C.Constant (name, Some t1, t2, params, _) ->
222       "Definition of " ^ name ^
223        "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
224        ")" ^ ":\n" ^ pp t1 [] ^ " : " ^ pp t2 []
225    | C.Constant (name, None, ty, params, _) ->
226       "Axiom " ^ name ^
227        "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
228        "):\n" ^ pp ty []
229    | C.Variable (name, bo, ty, params, _) ->
230       "Variable " ^ name ^
231        "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
232        ")" ^ ":\n" ^
233        pp ty [] ^ "\n" ^
234        (match bo with None -> "" | Some bo -> ":= " ^ pp bo [])
235    | C.CurrentProof (name, conjectures, value, ty, params, _) ->
236       "Current Proof of " ^ name ^
237        "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
238        ")" ^ ":\n" ^
239       let separate s = if s = "" then "" else s ^ " ; " in
240        List.fold_right
241         (fun (n, context, t) i -> 
242           let conjectures',name_context =
243                  List.fold_right 
244                   (fun context_entry (i,name_context) ->
245                     (match context_entry with
246                         Some (n,C.Decl at) ->
247                    (separate i) ^
248                      ppname n ^ ":" ^ pp at name_context ^ " ",
249                       (Some n)::name_context
250                       | Some (n,C.Def (at,None)) ->
251                    (separate i) ^
252                      ppname n ^ ":= " ^ pp at name_context ^ " ",
253                       (Some n)::name_context
254                 | None ->
255                    (separate i) ^ "_ :? _ ", None::name_context
256                 | _ -> assert false)
257             ) context ("",[])
258           in
259            conjectures' ^ " |- " ^ "?" ^ (string_of_int n) ^ ": " ^
260             pp t name_context ^ "\n" ^ i
261         ) conjectures "" ^
262         "\n" ^ pp value [] ^ " : " ^ pp ty [] 
263    | C.InductiveDefinition (l, params, nparams, _) ->
264       "Parameters = " ^
265        String.concat ";" (List.map UriManager.string_of_uri params) ^ "\n" ^
266        "NParams = " ^ string_of_int nparams ^ "\n" ^
267         List.fold_right (fun x i -> ppinductiveType x ^ i) l ""
268 ;;
269
270 let ppsort = function
271   | Cic.Prop -> "Prop"
272   | Cic.Set -> "Set"
273   | Cic.Type _ -> "Type"
274   | Cic.CProp -> "CProp"
275