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 =
49 | Cic.Lambda (_,_,t) -> aux t
56 fst(CicTypeChecker.type_of_aux' [] context t CicUniv.oblivion_ugraph)
58 Cic.Sort Cic.Prop -> `Statement
70 let n = String.uncapitalize n in
71 if List.mem n reserved then n ^ "_" else n
77 | Cic.Anonymous -> "_"
80 (* get_nth l n returns the nth element of the list l if it exists or *)
81 (* raises NotEnoughElements if l has less than n elements *)
85 | (n, he::tail) when n > 1 -> get_nth tail (n-1)
86 | (_,_) -> raise NotEnoughElements
89 let qualified_name_of_uri current_module_name ?(capitalize=false) uri =
92 String.capitalize (UriManager.name_of_uri uri)
94 ppid (UriManager.name_of_uri uri) in
95 let buri = UriManager.buri_of_uri uri in
96 let index = String.rindex buri '/' in
97 let filename = String.sub buri (index + 1) (String.length buri - index - 1) in
98 if current_module_name = filename then
101 String.capitalize filename ^ "." ^ name
105 (* pretty-prints a term t of cic in an environment l where l is a list of *)
106 (* identifier names used to resolve DeBrujin indexes. The head of l is the *)
107 (* name associated to the greatest DeBrujin index in t *)
108 let pp current_module_name ?metasenv =
109 let rec pp t context =
110 let module C = Cic in
115 (match get_nth context n with
116 Some (C.Name s,_) -> ppid s
117 | Some (C.Anonymous,_) -> "__" ^ string_of_int n
118 | None -> "_hidden_" ^ string_of_int n
121 NotEnoughElements -> string_of_int (List.length context - n)
123 | C.Var (uri,exp_named_subst) ->
124 qualified_name_of_uri current_module_name uri ^
125 pp_exp_named_subst exp_named_subst context
129 "?" ^ (string_of_int n) ^ "[" ^
131 (List.rev_map (function None -> "_" | Some t -> pp t context) l1) ^
135 let _,context,_ = CicUtil.lookup_meta n metasenv in
136 "?" ^ (string_of_int n) ^ "[" ^
144 | Some _, Some t -> pp t context
148 CicUtil.Meta_not_found _
149 | Invalid_argument _ ->
150 "???" ^ (string_of_int n) ^ "[" ^
152 (List.rev_map (function None -> "_" | Some t -> pp t context) l1) ^
160 (*| C.Type u -> ("Type" ^ CicUniv.string_of_universe u)*)
163 | C.Implicit (Some `Hole) -> "%"
164 | C.Implicit _ -> "?"
168 let n = "'" ^ String.uncapitalize n in
169 "(" ^ pp s context ^ " -> " ^ pp t ((Some (Cic.Name n,Cic.Decl s))::context) ^ ")"
170 | C.Anonymous -> "(" ^ pp s context ^ " -> " ^ pp t ((Some (b,Cic.Decl s))::context) ^ ")"
172 | C.Cast (v,t) -> pp v context
173 | C.Lambda (b,s,t) ->
174 (match analyze_type context s with
176 | `Statement -> pp t ((Some (b,Cic.Decl s))::context)
177 | `Type -> "(function " ^ ppname b ^ " -> " ^ pp t ((Some (b,Cic.Decl s))::context) ^ ")")
179 let ty,_ = CicTypeChecker.type_of_aux' [] context t CicUniv.oblivion_ugraph in
180 "(let " ^ ppname b ^ " = " ^ pp s context ^ " in " ^ pp t ((Some (b,Cic.Def (s,Some ty)))::context) ^ ")"
181 | C.Appl (C.MutInd _ as he::tl) ->
182 let hes = pp he context in
183 let stl = String.concat "," (clean_args_for_ty context tl) in
184 (if stl = "" then "" else "(" ^ stl ^ ") ") ^ hes
185 | C.Appl (C.MutConstruct _ as he::tl) ->
186 let hes = pp he context in
187 let stl = String.concat "," (clean_args context tl) in
188 "(" ^ hes ^ (if stl = "" then "" else "(" ^ stl ^ ")") ^ ")"
190 "(" ^ String.concat " " (clean_args context li) ^ ")"
191 | C.Const (uri,exp_named_subst) ->
192 qualified_name_of_uri current_module_name uri ^
193 pp_exp_named_subst exp_named_subst context
194 | C.MutInd (uri,n,exp_named_subst) ->
196 match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
197 C.InductiveDefinition (dl,_,_,_) ->
198 let (name,_,_,_) = get_nth dl (n+1) in
199 qualified_name_of_uri current_module_name
200 (UriManager.uri_of_string
201 (UriManager.buri_of_uri uri ^ "/" ^ name ^ ".con")) ^
202 pp_exp_named_subst exp_named_subst context
203 | _ -> raise CicExportationInternalError
205 Sys.Break as exn -> raise exn
206 | _ -> UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n + 1)
208 | C.MutConstruct (uri,n1,n2,exp_named_subst) ->
210 match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
211 C.InductiveDefinition (dl,_,_,_) ->
212 let _,_,_,cons = get_nth dl (n1+1) in
213 let id,_ = get_nth cons n2 in
214 qualified_name_of_uri current_module_name ~capitalize:true
215 (UriManager.uri_of_string
216 (UriManager.buri_of_uri uri ^ "/" ^ id ^ ".con")) ^
217 pp_exp_named_subst exp_named_subst context
218 | _ -> raise CicExportationInternalError
220 Sys.Break as exn -> raise exn
222 UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n1 + 1) ^ "/" ^
225 | C.MutCase (uri,n1,ty,te,patterns) ->
226 (match analyze_term context te with
227 `Type -> assert false
230 [] -> "assert false" (* empty type elimination *)
231 | [he] -> pp he context (* singleton elimination *)
234 if patterns = [] then "assert false"
236 (let connames_and_argsno =
237 (match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
238 C.InductiveDefinition (dl,_,paramsno,_) ->
239 let (_,_,_,cons) = get_nth dl (n1+1) in
242 (* this is just an approximation since we do not have
244 let rec count_prods toskip =
246 C.Prod (_,_,bo) when toskip > 0 ->
247 count_prods (toskip - 1) bo
248 | C.Prod (_,_,bo) -> 1 + count_prods 0 bo
251 qualified_name_of_uri current_module_name
253 (UriManager.uri_of_string
254 (UriManager.buri_of_uri uri ^ "/" ^ id ^ ".con")),
255 count_prods paramsno ty
257 | _ -> raise CicExportationInternalError
260 let connames_and_argsno_and_patterns =
264 | [],l -> List.map (fun x -> "???",0,Some x) l
265 | l,[] -> List.map (fun (x,no) -> x,no,None) l
266 | (x,no)::tlx,y::tly -> (x,no,Some y)::(combine (tlx,tly))
268 combine (connames_and_argsno,patterns)
270 "\n(match " ^ pp te context ^ " with \n" ^
271 (String.concat "\n | "
274 let rec aux argsno context =
276 Cic.Lambda (name,ty,bo) when argsno > 0 ->
278 aux (argsno - 1) (Some (name,Cic.Decl ty)::context)
281 (match name with C.Anonymous -> "_" | C.Name s -> s)
284 | t when argsno = 0 -> [],pp t context
286 ["{" ^ string_of_int argsno ^ " args missing}"],
292 | Some y when argsno = 0 -> x,pp y context
294 let args,body = aux argsno context y in
295 let sargs = String.concat "," args in
296 x ^ (if sargs = "" then "" else "(" ^ sargs^ ")"),
299 pattern ^ " -> " ^ body
300 ) connames_and_argsno_and_patterns)) ^
302 | C.Fix (no, funs) ->
306 (function (name,_,ty,_) ->
307 Some (C.Name name,Cic.Decl ty)) funs)
311 (fun (name,ind,ty,bo) i -> name ^ " = \n" ^
312 pp bo (names@context) ^ i)
315 (match get_nth names (no + 1) with
316 Some (Cic.Name n,_) -> n
317 | _ -> assert false) ^ "\n"
318 | C.CoFix (no,funs) ->
322 (function (name,ty,_) ->
323 Some (C.Name name,Cic.Decl ty)) funs)
327 (fun (name,ty,bo) i -> "\n" ^ name ^
328 " : " ^ pp ty context ^ " := \n" ^
329 pp bo (names@context) ^ i)
332 and pp_exp_named_subst exp_named_subst context =
333 if exp_named_subst = [] then "" else
335 String.concat " ; " (
337 (function (uri,t) -> UriManager.name_of_uri uri ^ " \\Assign " ^ pp t context)
340 and clean_args context =
343 match analyze_term context t with
346 | `Term -> Some pp t context)
347 and clean_args_for_ty context =
350 match analyze_term context t with
351 `Type -> Some pp t context
358 let ppty current_module_name =
359 let rec args context =
364 Cic.Anonymous -> Cic.Anonymous
365 | Cic.Name n -> Cic.Name (String.uncapitalize n)
367 (match analyze_type context s with
368 `Sort Cic.Prop -> args ((Some (n,Cic.Decl s))::context) t
373 Cic.Anonymous -> Cic.Anonymous
374 | Cic.Name name -> Cic.Name ("'" ^ name) in
375 let abstr,args = args ((Some (n,Cic.Decl s))::context) t in
377 Cic.Anonymous -> abstr
378 | Cic.Name name -> name::abstr),
381 let abstr,args = args ((Some (n,Cic.Decl s))::context) t in
382 abstr,pp current_module_name s context::args)
388 (* ppinductiveType (typename, inductive, arity, cons) *)
389 (* pretty-prints a single inductive definition *)
390 (* (typename, inductive, arity, cons) *)
391 let ppinductiveType current_module_name nparams (typename, inductive, arity, cons)
393 match analyze_type [] arity with
396 | `Type -> assert false
399 "type " ^ String.uncapitalize typename ^ " = unit (* empty type *)\n"
403 (fun (id,ty) (_abstr,i) -> (* we should verify _abstr = abstr' *)
404 let abstr',sargs = ppty current_module_name [] ty in
405 let sargs = String.concat " * " sargs in
407 String.capitalize id ^
408 (if sargs = "" then "" else " of " ^ sargs) ^
409 (if i = "" then "" else "\n | ") ^ i)
413 let s = String.concat "," abstr in
414 if s = "" then "" else "(" ^ s ^ ") "
416 "type " ^ abstr ^ String.uncapitalize typename ^ " =\n" ^ scons ^ "\n")
419 let ppobj current_module_name obj =
420 let module C = Cic in
421 let module U = UriManager in
422 let pp = pp current_module_name in
424 C.Constant (name, Some t1, t2, params, _) ->
425 (match analyze_type [] t2 with
428 | `Type -> "let " ^ ppid name ^ " =\n" ^ pp t1 [] ^ "\n"
430 match analyze_type [] t1 with
433 let abstr,args = ppty current_module_name [] t1 in
435 let s = String.concat "," abstr in
436 if s = "" then "" else "(" ^ s ^ ") "
438 "type " ^ abstr ^ ppid name ^ " = " ^ String.concat "->" args ^
440 | C.Constant (name, None, ty, params, _) ->
441 (match analyze_type [] ty with
444 | `Sort _ -> "type " ^ ppid name ^ "\n"
445 | `Type -> "let " ^ ppid name ^ " = assert false\n")
446 | C.Variable (name, bo, ty, params, _) ->
448 "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
451 (match bo with None -> "" | Some bo -> ":= " ^ pp bo [])
452 | C.CurrentProof (name, conjectures, value, ty, params, _) ->
453 "Current Proof of " ^ name ^
454 "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
456 let separate s = if s = "" then "" else s ^ " ; " in
458 (fun (n, context, t) i ->
459 let conjectures',name_context =
461 (fun context_entry (i,name_context) ->
462 (match context_entry with
463 Some (n,C.Decl at) ->
466 pp ~metasenv:conjectures at name_context ^ " ",
467 context_entry::name_context
468 | Some (n,C.Def (at,None)) ->
470 ppname n ^ ":= " ^ pp ~metasenv:conjectures
471 at name_context ^ " ",
472 context_entry::name_context
474 (separate i) ^ "_ :? _ ", context_entry::name_context
478 conjectures' ^ " |- " ^ "?" ^ (string_of_int n) ^ ": " ^
479 pp ~metasenv:conjectures t name_context ^ "\n" ^ i
481 "\n" ^ pp ~metasenv:conjectures value [] ^ " : " ^
482 pp ~metasenv:conjectures ty []
483 | C.InductiveDefinition (l, params, nparams, _) ->
485 (fun x i -> ppinductiveType current_module_name nparams x ^ i) l ""
488 let ppobj current_module_name obj =
489 let res = ppobj current_module_name obj in
490 if res = "" then "" else res ^ ";;\n\n"