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.Anonymous -> "_"
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 | Some C.Anonymous -> "__" ^ string_of_int n
72 | None -> "_hidden_" ^ string_of_int n
75 NotEnoughElements -> string_of_int (List.length l - n)
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
80 "?" ^ (string_of_int n) ^ "[" ^
82 (List.rev_map (function None -> "_" | Some t -> pp t l) l1) ^
88 | C.Type _ -> "Type" (* TASSI: universe is not explicit *)
94 C.Name n -> "(" ^ n ^ ":" ^ pp s l ^ ")" ^ pp t ((Some b)::l)
95 | C.Anonymous -> "(" ^ pp s l ^ "->" ^ pp t ((Some b)::l) ^ ")"
97 | C.Cast (v,t) -> pp v l
99 "[" ^ ppname b ^ ":" ^ pp s l ^ "]" ^ pp t ((Some b)::l)
101 "[" ^ ppname b ^ ":=" ^ pp s l ^ "]" ^ pp t ((Some b)::l)
105 (fun x i -> pp x l ^ (match i with "" -> "" | _ -> " ") ^ i)
108 | C.Const (uri,exp_named_subst) ->
109 UriManager.name_of_uri uri ^ pp_exp_named_subst exp_named_subst l
110 | C.MutInd (uri,n,exp_named_subst) ->
112 match CicEnvironment.get_obj uri with
113 C.InductiveDefinition (dl,_,_) ->
114 let (name,_,_,_) = get_nth dl (n+1) in
115 name ^ pp_exp_named_subst exp_named_subst l
116 | _ -> raise CicPpInternalError
118 _ -> UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n + 1)
120 | C.MutConstruct (uri,n1,n2,exp_named_subst) ->
122 match CicEnvironment.get_obj uri with
123 C.InductiveDefinition (dl,_,_) ->
124 let (_,_,_,cons) = get_nth dl (n1+1) in
125 let (id,_) = get_nth cons n2 in
126 id ^ pp_exp_named_subst exp_named_subst l
127 | _ -> raise CicPpInternalError
130 UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n1 + 1) ^ "/" ^
133 | C.MutCase (uri,n1,ty,te,patterns) ->
135 (match CicEnvironment.get_obj uri with
136 C.InductiveDefinition (dl,_,_) ->
137 let (_,_,_,cons) = get_nth dl (n1+1) in
138 List.map (fun (id,_) -> id) cons
139 | _ -> raise CicPpInternalError
142 "\n<" ^ pp ty l ^ ">Cases " ^ pp te l ^ " of " ^
143 List.fold_right (fun (x,y) i -> "\n " ^ x ^ " => " ^ pp y l ^ i)
144 (List.combine connames patterns) "" ^
146 | C.Fix (no, funs) ->
147 let snames = List.map (fun (name,_,_,_) -> name) funs in
149 List.rev (List.map (function name -> Some (C.Name name)) snames)
151 "\nFix " ^ get_nth snames (no + 1) ^ " {" ^
153 (fun (name,ind,ty,bo) i -> "\n" ^ name ^ " / " ^ string_of_int ind ^
154 " : " ^ pp ty l ^ " := \n" ^
158 | C.CoFix (no,funs) ->
159 let snames = List.map (fun (name,_,_) -> name) funs in
161 List.rev (List.map (function name -> Some (C.Name name)) snames)
163 "\nCoFix " ^ get_nth snames (no + 1) ^ " {" ^
165 (fun (name,ty,bo) i -> "\n" ^ name ^
166 " : " ^ pp ty l ^ " := \n" ^
170 and pp_exp_named_subst exp_named_subst l =
171 if exp_named_subst = [] then "" else
173 String.concat " ; " (
175 (function (uri,t) -> UriManager.name_of_uri uri ^ ":=" ^ pp t l)
184 (* ppinductiveType (typename, inductive, arity, cons) *)
185 (* pretty-prints a single inductive definition *)
186 (* (typename, inductive, arity, cons) *)
187 let ppinductiveType (typename, inductive, arity, cons) =
188 (if inductive then "\nInductive " else "\nCoInductive ") ^ typename ^ ": " ^
189 pp arity [] ^ " =\n " ^
191 (fun (id,ty) i -> id ^ " : " ^ pp ty [] ^
192 (if i = "" then "\n" else "\n | ") ^ i)
196 (* ppobj obj returns a string with describing the cic object obj in a syntax *)
197 (* similar to the one used by Coq *)
199 let module C = Cic in
200 let module U = UriManager in
202 C.Constant (name, Some t1, t2, params) ->
203 "Definition of " ^ name ^
204 "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
205 ")" ^ ":\n" ^ pp t1 [] ^ " : " ^ pp t2 []
206 | C.Constant (name, None, ty, params) ->
208 "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
210 | C.Variable (name, bo, ty, params) ->
212 "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
215 (match bo with None -> "" | Some bo -> ":= " ^ pp bo [])
216 | C.CurrentProof (name, conjectures, value, ty, params) ->
217 "Current Proof of " ^ name ^
218 "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
220 let separate s = if s = "" then "" else s ^ " ; " in
222 (fun (n, context, t) i ->
223 let conjectures',name_context =
225 (fun context_entry (i,name_context) ->
226 (match context_entry with
227 Some (n,C.Decl at) ->
229 ppname n ^ ":" ^ pp at name_context ^ " ",
230 (Some n)::name_context
231 | Some (n,C.Def (at,None)) ->
233 ppname n ^ ":= " ^ pp at name_context ^ " ",
234 (Some n)::name_context
236 (separate i) ^ "_ :? _ ", None::name_context
240 conjectures' ^ " |- " ^ "?" ^ (string_of_int n) ^ ": " ^
241 pp t name_context ^ "\n" ^ i
243 "\n" ^ pp value [] ^ " : " ^ pp ty []
244 | C.InductiveDefinition (l, params, nparams) ->
246 String.concat ";" (List.map UriManager.string_of_uri params) ^ "\n" ^
247 "NParams = " ^ string_of_int nparams ^ "\n" ^
248 List.fold_right (fun x i -> ppinductiveType x ^ i) l ""