]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_proof_checking/cicPp.ml
05c964370b7b711941fc1a9a0930851058f60a37
[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 string_of_name =
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            | _ -> raise CicPpInternalError
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.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        )
90     | C.Implicit -> "?"
91     | C.Prod (b,s,t) ->
92        (match b with
93           C.Name n -> "(" ^ n ^ ":" ^ pp s l ^ ")" ^ pp t ((Some b)::l)
94         | C.Anonymous -> "(" ^ pp s l ^ "->" ^ pp t ((Some b)::l) ^ ")"
95        )
96     | C.Cast (v,t) -> pp v l
97     | C.Lambda (b,s,t) ->
98        "[" ^ string_of_name b ^ ":" ^ pp s l ^ "]" ^ pp t ((Some b)::l)
99     | C.LetIn (b,s,t) ->
100        "[" ^ string_of_name b ^ ":=" ^ pp s l ^ "]" ^ pp t ((Some b)::l)
101     | C.Appl li ->
102        "(" ^
103        (List.fold_right
104         (fun x i -> pp x l ^ (match i with "" -> "" | _ -> " ") ^ i)
105         li ""
106        ) ^ ")"
107     | C.Const (uri,exp_named_subst) ->
108        UriManager.name_of_uri uri ^ pp_exp_named_subst exp_named_subst l
109     | C.MutInd (uri,n,exp_named_subst) ->
110        (match CicEnvironment.get_obj uri with
111            C.InductiveDefinition (dl,_,_) ->
112             let (name,_,_,_) = get_nth dl (n+1) in
113              name ^ pp_exp_named_subst exp_named_subst l
114          | _ -> raise CicPpInternalError
115        )
116     | C.MutConstruct (uri,n1,n2,exp_named_subst) ->
117        (match CicEnvironment.get_obj uri with
118            C.InductiveDefinition (dl,_,_) ->
119             let (_,_,_,cons) = get_nth dl (n1+1) in
120              let (id,_) = get_nth cons n2 in
121               id ^ pp_exp_named_subst exp_named_subst l
122          | _ -> raise CicPpInternalError
123        )
124     | C.MutCase (uri,n1,ty,te,patterns) ->
125        let connames =
126         (match CicEnvironment.get_obj uri with
127             C.InductiveDefinition (dl,_,_) ->
128              let (_,_,_,cons) = get_nth dl (n1+1) in
129               List.map (fun (id,_) -> id) cons
130           | _ -> raise CicPpInternalError
131         )
132        in
133         "\n<" ^ pp ty l ^ ">Cases " ^ pp te l ^ " of " ^
134           List.fold_right (fun (x,y) i -> "\n " ^ x ^ " => " ^ pp y l ^ i)
135            (List.combine connames patterns) "" ^
136           "\nend"
137     | C.Fix (no, funs) ->
138        let snames = List.map (fun (name,_,_,_) -> name) funs in
139         let names =
140          List.rev (List.map (function name -> Some (C.Name name)) snames)
141         in
142          "\nFix " ^ get_nth snames (no + 1) ^ " {" ^
143          List.fold_right
144           (fun (name,ind,ty,bo) i -> "\n" ^ name ^ " / " ^ string_of_int ind ^
145             " : " ^ pp ty l ^ " := \n" ^
146             pp bo (names@l) ^ i)
147           funs "" ^
148          "}\n"
149     | C.CoFix (no,funs) ->
150        let snames = List.map (fun (name,_,_) -> name) funs in
151         let names =
152          List.rev (List.map (function name -> Some (C.Name name)) snames)
153         in
154          "\nCoFix " ^ get_nth snames (no + 1) ^ " {" ^
155          List.fold_right
156           (fun (name,ty,bo) i -> "\n" ^ name ^ 
157             " : " ^ pp ty l ^ " := \n" ^
158             pp bo (names@l) ^ i)
159           funs "" ^
160          "}\n"
161 and pp_exp_named_subst exp_named_subst l =
162  if exp_named_subst = [] then "" else
163   "{" ^
164    String.concat " ; " (
165     List.map
166      (function (uri,t) -> UriManager.name_of_uri uri ^ ":=" ^ pp t l)
167      exp_named_subst
168    ) ^ "}"
169 ;;
170
171 let ppterm t =
172  pp t []
173 ;;
174
175 (* ppinductiveType (typename, inductive, arity, cons) names                 *)
176 (* pretty-prints a single inductive definition (typename, inductive, arity, *)
177 (*  cons) where the cic terms in the inductive definition need to be        *)
178 (*  evaluated in the environment names that is the list of typenames of the *)
179 (*  mutual inductive definitions defined in the block of mutual inductive   *)
180 (*  definitions to which this one belongs to                                *)
181 let ppinductiveType (typename, inductive, arity, cons) names =
182   (if inductive then "\nInductive " else "\nCoInductive ") ^ typename ^ ": " ^
183   (*CSC: bug found: was pp arity names ^ " =\n   " ^*)
184   pp arity [] ^ " =\n   " ^
185   List.fold_right
186    (fun (id,ty) i -> id ^ " : " ^ pp ty names ^ 
187     (if i = "" then "\n" else "\n | ") ^ i)
188    cons ""
189 ;;
190
191 (* ppobj obj  returns a string with describing the cic object obj in a syntax *)
192 (* similar to the one used by Coq                                             *)
193 let ppobj obj =
194  let module C = Cic in
195  let module U = UriManager in
196   match obj with
197     C.Constant (name, Some t1, t2, params) ->
198       "Definition of " ^ name ^
199        "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
200        ")" ^ ":\n" ^ pp t1 [] ^ " : " ^ pp t2 []
201    | C.Constant (name, None, ty, params) ->
202       "Axiom " ^ name ^
203        "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
204        "):\n" ^ pp ty []
205    | C.Variable (name, bo, ty, params) ->
206       "Variable " ^ name ^
207        "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
208        ")" ^ ":\n" ^
209        pp ty [] ^ "\n" ^
210        (match bo with None -> "" | Some bo -> ":= " ^ pp bo [])
211    | C.CurrentProof (name, conjectures, value, ty, params) ->
212       "Current Proof of " ^ name ^
213        "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
214        ")" ^ ":\n" ^
215       let separate s = if s = "" then "" else s ^ " ; " in
216        List.fold_right
217         (fun (n, context, t) i -> 
218           let conjectures',name_context =
219            List.fold_right 
220             (fun context_entry (i,name_context) ->
221               (match context_entry with
222                   Some (n,C.Decl at) ->
223                    (separate i) ^
224                     string_of_name n ^ ":" ^ pp at name_context ^ " ",
225                    (Some n)::name_context
226                 | Some (n,C.Def at) ->
227                    (separate i) ^
228                     string_of_name n ^ ":= " ^ pp at name_context ^ " ",
229                    (Some n)::name_context
230                 | None ->
231                    (separate i) ^ "_ :? _ ", None::name_context)
232              ) context ("",[])
233           in
234            conjectures' ^ " |- " ^ "?" ^ (string_of_int n) ^ ": " ^
235             pp t name_context ^ "\n" ^ i
236         ) conjectures "" ^
237         "\n" ^ pp value [] ^ " : " ^ pp ty [] 
238    | C.InductiveDefinition (l, params, nparams) ->
239       "Parameters = " ^
240        String.concat ";" (List.map UriManager.string_of_uri params) ^ "\n" ^
241        "NParams = " ^ string_of_int nparams ^ "\n" ^
242        let names = List.rev (List.map (fun (n,_,_,_) -> Some (C.Name n)) l) in
243         List.fold_right (fun x i -> ppinductiveType x names ^ i) l ""
244 ;;