1 (* Copyright (C) 2000, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
26 (******************************************************************************)
30 (* Claudio Sacerdoti Coen <sacerdot@cs.unibo.it> *)
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 *)
37 (******************************************************************************)
39 exception CicPpInternalError;;
40 exception NotEnoughElements;;
42 (* Utility functions *)
47 | Cic.Anonimous -> "_"
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 *)
55 | (n, he::tail) when n > 1 -> get_nth tail (n-1)
56 | (_,_) -> raise NotEnoughElements
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 *)
69 (match get_nth l n with
71 | _ -> raise CicPpInternalError
74 NotEnoughElements -> string_of_int (List.length l - n)
76 | C.Var uri -> UriManager.name_of_uri uri
77 | C.Meta n -> "?" ^ (string_of_int n)
87 C.Name n -> "(" ^ n ^ ":" ^ pp s l ^ ")" ^ pp t (b::l)
88 | C.Anonimous -> "(" ^ pp s l ^ "->" ^ pp t (b::l) ^ ")"
90 | C.Cast (v,t) -> pp v l
92 "[" ^ string_of_name b ^ ":" ^ pp s l ^ "]" ^ pp t (b::l)
94 "[" ^ string_of_name b ^ ":=" ^ pp s l ^ "]" ^ pp t (b::l)
98 (fun x i -> pp x l ^ (match i with "" -> "" | _ -> " ") ^ i)
101 | C.Const (uri,_) -> UriManager.name_of_uri uri
102 | C.Abst uri -> UriManager.name_of_uri uri
103 | C.MutInd (uri,_,n) ->
104 (match CicEnvironment.get_obj uri with
105 C.InductiveDefinition (dl,_,_) ->
106 let (name,_,_,_) = get_nth dl (n+1) in
108 | _ -> raise CicPpInternalError
110 | C.MutConstruct (uri,_,n1,n2) ->
111 (match CicEnvironment.get_obj uri with
112 C.InductiveDefinition (dl,_,_) ->
113 let (_,_,_,cons) = get_nth dl (n1+1) in
114 let (id,_,_) = get_nth cons n2 in
116 | _ -> raise CicPpInternalError
118 | C.MutCase (uri,_,n1,ty,te,patterns) ->
120 (match CicEnvironment.get_obj uri with
121 C.InductiveDefinition (dl,_,_) ->
122 let (_,_,_,cons) = get_nth dl (n1+1) in
123 List.map (fun (id,_,_) -> id) cons
124 | _ -> raise CicPpInternalError
127 "\n<" ^ pp ty l ^ ">Cases " ^ pp te l ^ " of " ^
128 List.fold_right (fun (x,y) i -> "\n " ^ x ^ " => " ^ pp y l ^ i)
129 (List.combine connames patterns) "" ^
131 | C.Fix (no, funs) ->
132 let snames = List.map (fun (name,_,_,_) -> name) funs in
133 let names = List.rev (List.map (function name -> C.Name name) snames) in
134 "\nFix " ^ get_nth snames (no + 1) ^ " {" ^
136 (fun (name,ind,ty,bo) i -> "\n" ^ name ^ " / " ^ string_of_int ind ^
137 " : " ^ pp ty l ^ " := \n" ^
141 | C.CoFix (no,funs) ->
142 let snames = List.map (fun (name,_,_) -> name) funs in
143 let names = List.rev (List.map (function name -> C.Name name) snames) in
144 "\nCoFix " ^ get_nth snames (no + 1) ^ " {" ^
146 (fun (name,ty,bo) i -> "\n" ^ name ^
147 " : " ^ pp ty l ^ " := \n" ^
157 (* ppinductiveType (typename, inductive, arity, cons) names *)
158 (* pretty-prints a single inductive definition (typename, inductive, arity, *)
159 (* cons) where the cic terms in the inductive definition need to be *)
160 (* evaluated in the environment names that is the list of typenames of the *)
161 (* mutual inductive definitions defined in the block of mutual inductive *)
162 (* definitions to which this one belongs to *)
163 let ppinductiveType (typename, inductive, arity, cons) names =
164 (if inductive then "\nInductive " else "\nCoInductive ") ^ typename ^ ": " ^
165 (*CSC: bug found: was pp arity names ^ " =\n " ^*)
166 pp arity [] ^ " =\n " ^
168 (fun (id,ty,_) i -> id ^ " : " ^ pp ty names ^
169 (if i = "" then "\n" else "\n | ") ^ i)
173 (* ppobj obj returns a string with describing the cic object obj in a syntax *)
174 (* similar to the one used by Coq *)
176 let module C = Cic in
177 let module U = UriManager in
179 C.Definition (id, t1, t2, params) ->
180 "Definition of " ^ id ^
186 U.string_of_uri x ^ match i with "" -> "" | i' -> " " ^ i'
187 ) x "" ^ match i with "" -> "" | i' -> " " ^ i'
189 ":\n" ^ pp t1 [] ^ " : " ^ pp t2 []
190 | C.Axiom (id, ty, params) ->
191 "Axiom " ^ id ^ "(" ^
196 U.string_of_uri x ^ match i with "" -> "" | i' -> " " ^ i'
197 ) x "" ^ match i with "" -> "" | i' -> " " ^ i'
200 | C.Variable (name, bo, ty) ->
201 "Variable " ^ name ^ ":\n" ^ pp ty [] ^ "\n" ^
202 (match bo with None -> "" | Some bo -> ":= " ^ pp bo [])
203 | C.CurrentProof (name, conjectures, value, ty) ->
206 (fun (n, t) i -> "?" ^ (string_of_int n) ^ ": " ^ pp t [] ^ "\n" ^ i)
208 "\n" ^ pp value [] ^ " : " ^ pp ty []
209 | C.InductiveDefinition (l, params, nparams) ->
215 U.string_of_uri x ^ match i with "" -> "" | i' -> " " ^ i'
216 ) x "" ^ match i with "" -> "" | i' -> " " ^ i'
218 "NParams = " ^ string_of_int nparams ^ "\n" ^
219 let names = List.rev (List.map (fun (n,_,_,_) -> C.Name n) l) in
220 List.fold_right (fun x i -> ppinductiveType x names ^ i) l ""