]> matita.cs.unibo.it Git - helm.git/blob - helm/interface/cicPp.ml
Initial revision
[helm.git] / helm / interface / 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
41 (* Utility functions *)
42
43 let string_of_name =
44  function
45     Cic.Name s     -> s
46   | Cic.Anonimous  -> "_"
47 ;;
48
49 (* get_nth l n   returns the nth element of the list l if it exists or raise *)
50 (* a CicPpInternalError if l has less than n elements or n < 1               *)
51 let rec get_nth l n =
52  match (n,l) with
53     (1, he::_) -> he
54   | (n, he::tail) when n > 1 -> get_nth tail (n-1)
55   | (_,_) -> raise CicPpInternalError
56 ;;
57
58 (* pp t l                                                                  *)
59 (* pretty-prints a term t of cic in an environment l where l is a list of  *)
60 (* identifier names used to resolve DeBrujin indexes. The head of l is the *)
61 (* name associated to the greatest DeBrujin index in t                     *)
62 let rec pp t l =
63  let module C = Cic in
64    match t with
65       C.Rel n ->
66        (match get_nth l n with
67            C.Name s -> s
68          | _        -> raise CicPpInternalError
69        )
70     | C.Var uri -> UriManager.name_of_uri uri
71     | C.Meta n -> "?" ^ (string_of_int n)
72     | C.Sort s ->
73        (match s with
74            C.Prop -> "Prop"
75          | C.Set  -> "Set"
76          | C.Type -> "Type"
77        )
78     | C.Implicit -> "?"
79     | C.Prod (b,s,t) ->
80        (match b with
81           C.Name n -> "(" ^ n ^ ":" ^ pp s l ^ ")" ^ pp t (b::l)
82         | C.Anonimous -> "(" ^ pp s l ^ "->" ^ pp t (b::l) ^ ")"
83        )
84     | C.Cast (v,t) -> pp v l
85     | C.Lambda (b,s,t) ->
86        "[" ^ string_of_name b ^ ":" ^ pp s l ^ "]" ^ pp t (b::l)
87     | C.LetIn (b,s,t) ->
88        "[" ^ string_of_name b ^ ":=" ^ pp s l ^ "]" ^ pp t (b::l)
89     | C.Appl li ->
90        "(" ^
91        (List.fold_right
92         (fun x i -> pp x l ^ (match i with "" -> "" | _ -> " ") ^ i)
93         li ""
94        ) ^ ")"
95     | C.Const (uri,_) -> UriManager.name_of_uri uri
96     | C.Abst uri -> UriManager.name_of_uri uri
97     | C.MutInd (uri,_,n) ->
98        (match CicCache.get_obj uri with
99            C.InductiveDefinition (dl,_,_) ->
100             let (name,_,_,_) = get_nth dl (n+1) in
101              name
102          | _ -> raise CicPpInternalError
103        )
104     | C.MutConstruct (uri,_,n1,n2) ->
105        (match CicCache.get_obj uri with
106            C.InductiveDefinition (dl,_,_) ->
107             let (_,_,_,cons) = get_nth dl (n1+1) in
108              let (id,_,_) = get_nth cons n2 in
109               id
110          | _ -> raise CicPpInternalError
111        )
112     | C.MutCase (uri,_,n1,ty,te,patterns) ->
113        let connames =
114         (match CicCache.get_obj uri with
115             C.InductiveDefinition (dl,_,_) ->
116              let (_,_,_,cons) = get_nth dl (n1+1) in
117               List.map (fun (id,_,_) -> id) cons
118           | _ -> raise CicPpInternalError
119         )
120        in
121         "\n<" ^ pp ty l ^ ">Cases " ^ pp te l ^ " of " ^
122           List.fold_right (fun (x,y) i -> "\n " ^ x ^ " => " ^ pp y l ^ i)
123            (List.combine connames patterns) "" ^
124           "\nend"
125     | C.Fix (no, funs) ->
126        let snames = List.map (fun (name,_,_,_) -> name) funs in
127         let names = List.rev (List.map (function name -> C.Name name) snames) in
128          "\nFix " ^ get_nth snames (no + 1) ^ " {" ^
129          List.fold_right
130           (fun (name,ind,ty,bo) i -> "\n" ^ name ^ " / " ^ string_of_int ind ^
131             " : " ^ pp ty l ^ " := \n" ^
132             pp bo (names@l) ^ i)
133           funs "" ^
134          "}\n"
135     | C.CoFix (no,funs) ->
136        let snames = List.map (fun (name,_,_) -> name) funs in
137         let names = List.rev (List.map (function name -> C.Name name) snames) in
138          "\nCoFix " ^ get_nth snames (no + 1) ^ " {" ^
139          List.fold_right
140           (fun (name,ty,bo) i -> "\n" ^ name ^ 
141             " : " ^ pp ty l ^ " := \n" ^
142             pp bo (names@l) ^ i)
143           funs "" ^
144          "}\n"
145 ;;
146
147 (* ppinductiveType (typename, inductive, arity, cons) names                 *)
148 (* pretty-prints a single inductive definition (typename, inductive, arity, *)
149 (*  cons) where the cic terms in the inductive definition need to be        *)
150 (*  evaluated in the environment names that is the list of typenames of the *)
151 (*  mutual inductive definitions defined in the block of mutual inductive   *)
152 (*  definitions to which this one belongs to                                *)
153 let ppinductiveType (typename, inductive, arity, cons) names =
154   (if inductive then "\nInductive " else "\nCoInductive ") ^ typename ^ ": " ^
155   (*CSC: bug found: was pp arity names ^ " =\n   " ^*)
156   pp arity [] ^ " =\n   " ^
157   List.fold_right
158    (fun (id,ty,_) i -> id ^ " : " ^ pp ty names ^ 
159     (if i = "" then "\n" else "\n | ") ^ i)
160    cons ""
161 ;;
162
163 (* ppobj obj  returns a string with describing the cic object obj in a syntax *)
164 (* similar to the one used by Coq                                             *)
165 let ppobj obj =
166  let module C = Cic in
167  let module U = UriManager in
168   match obj with
169     C.Definition (id, t1, t2, params) ->
170       "Definition of " ^ id ^
171       "(" ^
172       List.fold_right
173        (fun (_,x) i ->
174          List.fold_right
175           (fun x i ->
176             U.string_of_uri x ^ match i with "" -> "" | i' -> " " ^ i'
177           ) x "" ^ match i with "" -> "" | i' -> " " ^ i'
178        ) params "" ^ ")" ^
179       ":\n" ^ pp t1 [] ^ " : " ^ pp t2 []
180    | C.Axiom (id, ty, params) ->
181       "Axiom " ^ id ^ "(" ^
182       List.fold_right
183        (fun (_,x) i ->
184          List.fold_right
185           (fun x i ->
186             U.string_of_uri x ^ match i with "" -> "" | i' -> " " ^ i'
187           ) x "" ^ match i with "" -> "" | i' -> " " ^ i'
188        ) params "" ^
189       "):\n" ^ pp ty []
190    | C.Variable (name, bo, ty) ->
191       "Variable " ^ name ^ ":\n" ^ pp ty [] ^ "\n" ^
192       (match bo with None -> "" | Some bo -> ":= " ^ pp bo [])
193    | C.CurrentProof (name, conjectures, value, ty) ->
194       "Current Proof:\n" ^
195       List.fold_right
196        (fun (n, t) i -> "?" ^ (string_of_int n) ^ ": " ^ pp t [] ^ "\n" ^ i)
197        conjectures "" ^
198       "\n" ^ pp value [] ^ " : " ^ pp ty [] 
199    | C.InductiveDefinition (l, params, nparams) ->
200       "Parameters = " ^
201       List.fold_right
202        (fun (_,x) i ->
203          List.fold_right
204           (fun x i ->
205             U.string_of_uri x ^ match i with "" -> "" | i' -> " " ^ i'
206           ) x "" ^ match i with "" -> "" | i' -> " " ^ i'
207        ) params "" ^ "\n" ^
208       "NParams = " ^ string_of_int nparams ^ "\n" ^
209       let names = List.rev (List.map (fun (n,_,_,_) -> C.Name n) l) in
210        List.fold_right (fun x i -> ppinductiveType x names ^ i) l ""
211 ;;