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 (* $Id: cicPp.ml 7413 2007-05-29 15:30:53Z tassi $ *)
28 exception CicExportationInternalError;;
29 exception NotEnoughElements;;
31 (* Utility functions *)
33 let analyze_term context t =
34 match fst(CicTypeChecker.type_of_aux' [] context t CicUniv.oblivion_ugraph)with
38 fst (CicTypeChecker.type_of_aux' [] context ty CicUniv.oblivion_ugraph)
40 Cic.Sort Cic.Prop -> `Proof
44 let analyze_type context t =
48 | Cic.Prod (_,_,t) -> aux t
55 fst(CicTypeChecker.type_of_aux' [] context t CicUniv.oblivion_ugraph)
57 Cic.Sort Cic.Prop -> `Statement
68 let n = String.uncapitalize n in
69 if List.mem n reserved then n ^ "_" else n
75 | Cic.Anonymous -> "_"
78 (* get_nth l n returns the nth element of the list l if it exists or *)
79 (* raises NotEnoughElements if l has less than n elements *)
83 | (n, he::tail) when n > 1 -> get_nth tail (n-1)
84 | (_,_) -> raise NotEnoughElements
87 let qualified_name_of_uri current_module_name ?(capitalize=false) uri =
90 String.capitalize (UriManager.name_of_uri uri)
92 ppid (UriManager.name_of_uri uri) in
93 let buri = UriManager.buri_of_uri uri in
94 let index = String.rindex buri '/' in
95 let filename = String.sub buri (index + 1) (String.length buri - index - 1) in
96 if current_module_name = filename then
99 String.capitalize filename ^ "." ^ name
103 (* pretty-prints a term t of cic in an environment l where l is a list of *)
104 (* identifier names used to resolve DeBrujin indexes. The head of l is the *)
105 (* name associated to the greatest DeBrujin index in t *)
106 let pp current_module_name ?metasenv =
107 let rec pp t context =
108 let module C = Cic in
113 (match get_nth context n with
114 Some (C.Name s,_) -> ppid s
115 | Some (C.Anonymous,_) -> "__" ^ string_of_int n
116 | None -> "_hidden_" ^ string_of_int n
119 NotEnoughElements -> string_of_int (List.length context - n)
121 | C.Var (uri,exp_named_subst) ->
122 qualified_name_of_uri current_module_name uri ^
123 pp_exp_named_subst exp_named_subst context
127 "?" ^ (string_of_int n) ^ "[" ^
129 (List.rev_map (function None -> "_" | Some t -> pp t context) l1) ^
133 let _,context,_ = CicUtil.lookup_meta n metasenv in
134 "?" ^ (string_of_int n) ^ "[" ^
142 | Some _, Some t -> pp t context
146 CicUtil.Meta_not_found _
147 | Invalid_argument _ ->
148 "???" ^ (string_of_int n) ^ "[" ^
150 (List.rev_map (function None -> "_" | Some t -> pp t context) l1) ^
158 (*| C.Type u -> ("Type" ^ CicUniv.string_of_universe u)*)
161 | C.Implicit (Some `Hole) -> "%"
162 | C.Implicit _ -> "?"
165 C.Name n -> "(\\forall " ^ n ^ ":" ^ pp s context ^ "." ^ pp t ((Some (b,Cic.Decl s))::context) ^ ")"
166 | C.Anonymous -> "(" ^ pp s context ^ "\\to " ^ pp t ((Some (b,Cic.Decl s))::context) ^ ")"
168 | C.Cast (v,t) -> pp v context
169 | C.Lambda (b,s,t) ->
170 (match analyze_type context s with
172 | `Statement -> pp t ((Some (b,Cic.Decl s))::context)
173 | `Type -> "(function " ^ ppname b ^ " -> " ^ pp t ((Some (b,Cic.Decl s))::context) ^ ")")
175 let ty,_ = CicTypeChecker.type_of_aux' [] context t CicUniv.oblivion_ugraph in
176 "(let " ^ ppname b ^ " = " ^ pp s context ^ " in " ^ pp t ((Some (b,Cic.Def (s,Some ty)))::context) ^ ")"
177 | C.Appl (C.MutConstruct _ as he::tl) ->
178 let hes = pp he context in
179 let stl = String.concat "," (clean_args context tl) in
180 "(" ^ hes ^ (if stl = "" then "" else "(" ^ stl ^ ")") ^ ")"
182 "(" ^ String.concat " " (clean_args context li) ^ ")"
183 | C.Const (uri,exp_named_subst) ->
184 qualified_name_of_uri current_module_name uri ^
185 pp_exp_named_subst exp_named_subst context
186 | C.MutInd (uri,n,exp_named_subst) ->
188 match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
189 C.InductiveDefinition (dl,_,_,_) ->
190 let (name,_,_,_) = get_nth dl (n+1) in
191 qualified_name_of_uri current_module_name
192 (UriManager.uri_of_string
193 (UriManager.buri_of_uri uri ^ "/" ^ name ^ ".con")) ^
194 pp_exp_named_subst exp_named_subst context
195 | _ -> raise CicExportationInternalError
197 Sys.Break as exn -> raise exn
198 | _ -> UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n + 1)
200 | C.MutConstruct (uri,n1,n2,exp_named_subst) ->
202 match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
203 C.InductiveDefinition (dl,_,_,_) ->
204 let _,_,_,cons = get_nth dl (n1+1) in
205 let id,_ = get_nth cons n2 in
206 qualified_name_of_uri current_module_name ~capitalize:true
207 (UriManager.uri_of_string
208 (UriManager.buri_of_uri uri ^ "/" ^ id ^ ".con")) ^
209 pp_exp_named_subst exp_named_subst context
210 | _ -> raise CicExportationInternalError
212 Sys.Break as exn -> raise exn
214 UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n1 + 1) ^ "/" ^
217 | C.MutCase (uri,n1,ty,te,patterns) ->
218 let connames_and_argsno =
219 (match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
220 C.InductiveDefinition (dl,_,paramsno,_) ->
221 let (_,_,_,cons) = get_nth dl (n1+1) in
224 (* this is just an approximation since we do not have
226 let rec count_prods toskip =
228 C.Prod (_,_,bo) when toskip > 0 ->
229 count_prods (toskip - 1) bo
230 | C.Prod (_,_,bo) -> 1 + count_prods 0 bo
233 qualified_name_of_uri current_module_name ~capitalize:true
234 (UriManager.uri_of_string
235 (UriManager.buri_of_uri uri ^ "/" ^ id ^ ".con")),
236 count_prods paramsno ty
238 | _ -> raise CicExportationInternalError
241 let connames_and_argsno_and_patterns =
245 | [],l -> List.map (fun x -> "???",0,Some x) l
246 | l,[] -> List.map (fun (x,no) -> x,no,None) l
247 | (x,no)::tlx,y::tly -> (x,no,Some y)::(combine (tlx,tly))
249 combine (connames_and_argsno,patterns)
251 "\n(match " ^ pp te context ^ " with \n" ^
252 (String.concat "\n | "
255 let rec aux argsno context =
257 Cic.Lambda (name,ty,bo) when argsno > 0 ->
258 let args,res = aux (argsno - 1) (Some (name,Cic.Decl ty)::context) bo in
259 (match name with C.Anonymous -> "_" | C.Name s -> s)::args,
261 | t when argsno = 0 -> [],pp t context
262 | t -> ["{" ^ string_of_int argsno ^ " args missing}"],pp t context
267 | Some y when argsno = 0 -> x,pp y context
269 let args,body = aux argsno context y in
270 let sargs = String.concat "," args in
271 x ^ (if sargs = "" then "" else "(" ^ sargs^ ")"),body
273 pattern ^ " -> " ^ body
274 ) connames_and_argsno_and_patterns)) ^
276 | C.Fix (no, funs) ->
280 (function (name,_,ty,_) ->
281 Some (C.Name name,Cic.Decl ty)) funs)
285 (fun (name,ind,ty,bo) i -> name ^ " = \n" ^
286 pp bo (names@context) ^ i)
289 (match get_nth names (no + 1) with
290 Some (Cic.Name n,_) -> n
291 | _ -> assert false) ^ "\n"
292 | C.CoFix (no,funs) ->
296 (function (name,ty,_) ->
297 Some (C.Name name,Cic.Decl ty)) funs)
301 (fun (name,ty,bo) i -> "\n" ^ name ^
302 " : " ^ pp ty context ^ " := \n" ^
303 pp bo (names@context) ^ i)
306 and pp_exp_named_subst exp_named_subst context =
307 if exp_named_subst = [] then "" else
309 String.concat " ; " (
311 (function (uri,t) -> UriManager.name_of_uri uri ^ " \\Assign " ^ pp t context)
314 and clean_args context =
317 match analyze_term context t with
320 | `Term -> Some pp t context)
325 (* ppinductiveType (typename, inductive, arity, cons) *)
326 (* pretty-prints a single inductive definition *)
327 (* (typename, inductive, arity, cons) *)
328 let ppinductiveType current_module_name (typename, inductive, arity, cons) =
331 (fun (id,ty) (abstr,i) ->
332 let rec args context =
335 (match analyze_type context s with
340 Cic.Anonymous -> Cic.Anonymous
341 | Cic.Name name -> Cic.Name ("'" ^ name) in
342 let abstr,args = args ((Some (n,Cic.Decl s))::context) t in
344 Cic.Anonymous -> abstr
345 | Cic.Name name -> name::abstr),
348 let abstr,args = args ((Some (n,Cic.Decl s))::context) t in
349 abstr,pp current_module_name s context::args)
352 let abstr',sargs = args [] ty in
353 let sargs = String.concat " * " sargs in
355 String.capitalize id ^
356 (if sargs = "" then "" else " of " ^ sargs) ^
357 (if i = "" then "\n" else "\n | ") ^ i)
361 let s = String.concat "," abstr in
362 if s = "" then "" else "(" ^ s ^ ") "
364 "type " ^ abstr ^ typename ^ " =\n" ^ scons
367 let ppobj current_module_name obj =
368 let module C = Cic in
369 let module U = UriManager in
370 let pp = pp current_module_name in
372 C.Constant (name, Some t1, t2, params, _) ->
373 (match analyze_type [] t2 with
376 | `Sort -> "let " ^ ppid name ^ " =\n" ^ pp t1 [] ^ "\n")
377 | C.Constant (name, None, ty, params, _) ->
378 (match analyze_type [] ty with
380 | `Sort -> "type " ^ ppid name ^ "\n"
381 | `Type -> "let " ^ ppid name ^ " = assert false\n")
382 | C.Variable (name, bo, ty, params, _) ->
384 "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
387 (match bo with None -> "" | Some bo -> ":= " ^ pp bo [])
388 | C.CurrentProof (name, conjectures, value, ty, params, _) ->
389 "Current Proof of " ^ name ^
390 "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
392 let separate s = if s = "" then "" else s ^ " ; " in
394 (fun (n, context, t) i ->
395 let conjectures',name_context =
397 (fun context_entry (i,name_context) ->
398 (match context_entry with
399 Some (n,C.Decl at) ->
402 pp ~metasenv:conjectures at name_context ^ " ",
403 context_entry::name_context
404 | Some (n,C.Def (at,None)) ->
406 ppname n ^ ":= " ^ pp ~metasenv:conjectures
407 at name_context ^ " ",
408 context_entry::name_context
410 (separate i) ^ "_ :? _ ", context_entry::name_context
414 conjectures' ^ " |- " ^ "?" ^ (string_of_int n) ^ ": " ^
415 pp ~metasenv:conjectures t name_context ^ "\n" ^ i
417 "\n" ^ pp ~metasenv:conjectures value [] ^ " : " ^
418 pp ~metasenv:conjectures ty []
419 | C.InductiveDefinition (l, params, nparams, _) ->
421 (fun x i -> ppinductiveType current_module_name x ^ i) l "\n"
424 let ppobj current_module_name obj =
425 let res = ppobj current_module_name obj in
426 if res = "" then "" else res ^ ";;\n"