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