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_uri ?(capitalize=false) uri =
92 String.capitalize (UriManager.name_of_uri uri)
94 ppid (UriManager.name_of_uri uri) in
96 let suri = UriManager.buri_of_uri uri in
97 let s = String.sub suri 5 (String.length suri - 5) in
98 let s = Pcre.replace ~pat:"/" ~templ:"_" s in
99 String.uncapitalize s in
100 if current_module_uri = UriManager.buri_of_uri uri then
103 String.capitalize filename ^ "." ^ name
107 (* pretty-prints a term t of cic in an environment l where l is a list of *)
108 (* identifier names used to resolve DeBrujin indexes. The head of l is the *)
109 (* name associated to the greatest DeBrujin index in t *)
110 let pp current_module_uri ?metasenv =
111 let rec pp t context =
112 let module C = Cic in
117 (match get_nth context n with
118 Some (C.Name s,_) -> ppid s
119 | Some (C.Anonymous,_) -> "__" ^ string_of_int n
120 | None -> "_hidden_" ^ string_of_int n
123 NotEnoughElements -> string_of_int (List.length context - n)
125 | C.Var (uri,exp_named_subst) ->
126 qualified_name_of_uri current_module_uri uri ^
127 pp_exp_named_subst exp_named_subst context
131 "?" ^ (string_of_int n) ^ "[" ^
133 (List.rev_map (function None -> "_" | Some t -> pp t context) l1) ^
137 let _,context,_ = CicUtil.lookup_meta n metasenv in
138 "?" ^ (string_of_int n) ^ "[" ^
146 | Some _, Some t -> pp t context
150 CicUtil.Meta_not_found _
151 | Invalid_argument _ ->
152 "???" ^ (string_of_int n) ^ "[" ^
154 (List.rev_map (function None -> "_" | Some t -> pp t context) l1) ^
162 (*| C.Type u -> ("Type" ^ CicUniv.string_of_universe u)*)
165 | C.Implicit (Some `Hole) -> "%"
166 | C.Implicit _ -> "?"
170 let n = "'" ^ String.uncapitalize n in
171 "(" ^ pp s context ^ " -> " ^ pp t ((Some (Cic.Name n,Cic.Decl s))::context) ^ ")"
172 | C.Anonymous -> "(" ^ pp s context ^ " -> " ^ pp t ((Some (b,Cic.Decl s))::context) ^ ")"
174 | C.Cast (v,t) -> pp v context
175 | C.Lambda (b,s,t) ->
176 (match analyze_type context s with
178 | `Statement -> pp t ((Some (b,Cic.Decl s))::context)
179 | `Type -> "(function " ^ ppname b ^ " -> " ^ pp t ((Some (b,Cic.Decl s))::context) ^ ")")
181 let ty,_ = CicTypeChecker.type_of_aux' [] context t CicUniv.oblivion_ugraph in
182 "(let " ^ ppname b ^ " = " ^ pp s context ^ " in " ^ pp t ((Some (b,Cic.Def (s,Some ty)))::context) ^ ")"
183 | C.Appl (C.MutInd _ as he::tl) ->
184 let hes = pp he context in
185 let stl = String.concat "," (clean_args_for_ty context tl) in
186 (if stl = "" then "" else "(" ^ stl ^ ") ") ^ hes
187 | C.Appl (C.MutConstruct (uri,n,_,_) as he::tl) ->
189 match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
190 C.InductiveDefinition (_,_,nparams,_) -> nparams
191 | _ -> assert false in
192 let hes = pp he context in
193 let stl = String.concat "," (clean_args nparams context tl) in
194 "(" ^ hes ^ (if stl = "" then "" else "(" ^ stl ^ ")") ^ ")"
196 "(" ^ String.concat " " (clean_args 0 context li) ^ ")"
197 | C.Const (uri,exp_named_subst) ->
198 qualified_name_of_uri current_module_uri uri ^
199 pp_exp_named_subst exp_named_subst context
200 | C.MutInd (uri,n,exp_named_subst) ->
202 match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
203 C.InductiveDefinition (dl,_,_,_) ->
204 let (name,_,_,_) = get_nth dl (n+1) in
205 qualified_name_of_uri current_module_uri
206 (UriManager.uri_of_string
207 (UriManager.buri_of_uri uri ^ "/" ^ name ^ ".con")) ^
208 pp_exp_named_subst exp_named_subst context
209 | _ -> raise CicExportationInternalError
211 Sys.Break as exn -> raise exn
212 | _ -> UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n + 1)
214 | C.MutConstruct (uri,n1,n2,exp_named_subst) ->
216 match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
217 C.InductiveDefinition (dl,_,_,_) ->
218 let _,_,_,cons = get_nth dl (n1+1) in
219 let id,_ = get_nth cons n2 in
220 qualified_name_of_uri current_module_uri ~capitalize:true
221 (UriManager.uri_of_string
222 (UriManager.buri_of_uri uri ^ "/" ^ id ^ ".con")) ^
223 pp_exp_named_subst exp_named_subst context
224 | _ -> raise CicExportationInternalError
226 Sys.Break as exn -> raise exn
228 UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n1 + 1) ^ "/" ^
231 | C.MutCase (uri,n1,ty,te,patterns) ->
233 match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
234 C.InductiveDefinition (_,_,paramsno,_) -> paramsno
235 | _ -> assert false in
236 (match analyze_term context te with
237 `Type -> assert false
240 [] -> "assert false" (* empty type elimination *)
241 | [he] -> pp he context (* singleton elimination *)
244 if patterns = [] then "assert false"
246 (let connames_and_argsno =
247 (match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
248 C.InductiveDefinition (dl,_,paramsno,_) ->
249 let (_,_,_,cons) = get_nth dl (n1+1) in
252 (* this is just an approximation since we do not have
254 let rec count_prods toskip =
256 C.Prod (_,_,bo) when toskip > 0 ->
257 count_prods (toskip - 1) bo
258 | C.Prod (_,_,bo) -> 1 + count_prods 0 bo
261 qualified_name_of_uri current_module_uri
263 (UriManager.uri_of_string
264 (UriManager.buri_of_uri uri ^ "/" ^ id ^ ".con")),
265 count_prods paramsno ty
267 | _ -> raise CicExportationInternalError
270 let connames_and_argsno_and_patterns =
274 | (x,no)::tlx,y::tly -> (x,no,y)::(combine (tlx,tly))
275 | _,_ -> assert false
277 combine (connames_and_argsno,patterns)
279 "\n(match " ^ pp te context ^ " with \n " ^
280 (String.concat "\n | "
283 let rec aux argsno context =
285 Cic.Lambda (name,ty,bo) when argsno > 0 ->
287 aux (argsno - 1) (Some (name,Cic.Decl ty)::context)
290 (match analyze_type context ty with
292 | `Sort _ -> args,res
296 | C.Name s -> s)::args,res)
297 | t when argsno = 0 -> [],pp t context
299 ["{" ^ string_of_int argsno ^ " args missing}"],
303 if argsno = 0 then x,pp y context
305 let args,body = aux argsno context y in
306 let sargs = String.concat "," args in
307 x ^ (if sargs = "" then "" else "(" ^ sargs^ ")"),
310 pattern ^ " -> " ^ body
311 ) connames_and_argsno_and_patterns)) ^
313 | C.Fix (no, funs) ->
317 (function (name,_,ty,_) ->
318 Some (C.Name name,Cic.Decl ty)) funs)
322 (fun (name,ind,ty,bo) i -> name ^ " = \n" ^
323 pp bo (names@context) ^ i)
326 (match get_nth names (no + 1) with
327 Some (Cic.Name n,_) -> n
329 | C.CoFix (no,funs) ->
333 (function (name,ty,_) ->
334 Some (C.Name name,Cic.Decl ty)) funs)
338 (fun (name,ty,bo) i -> "\n" ^ name ^
339 " : " ^ pp ty context ^ " := \n" ^
340 pp bo (names@context) ^ i)
343 and pp_exp_named_subst exp_named_subst context =
344 if exp_named_subst = [] then "" else
346 String.concat " ; " (
348 (function (uri,t) -> UriManager.name_of_uri uri ^ " \\Assign " ^ pp t context)
351 and clean_args nparams context =
352 let nparams = ref nparams in
356 match analyze_term context t with
357 `Term when !nparams < 0 -> Some pp t context
361 and clean_args_for_ty context =
364 match analyze_term context t with
365 `Type -> Some pp t context
372 let ppty current_module_uri =
373 (* nparams is the number of left arguments
374 left arguments should either become parameters or be skipped altogether *)
375 let rec args nparams context =
380 Cic.Anonymous -> Cic.Anonymous
381 | Cic.Name n -> Cic.Name (String.uncapitalize n)
383 (match analyze_type context s with
386 args (nparams - 1) ((Some (n,Cic.Decl s))::context) t
387 | `Type when nparams > 0 ->
388 args (nparams - 1) ((Some (n,Cic.Decl s))::context) t
391 args (nparams - 1) ((Some (n,Cic.Decl s))::context) t in
392 abstr,pp current_module_uri s context::args
396 Cic.Anonymous -> Cic.Anonymous
397 | Cic.Name name -> Cic.Name ("'" ^ name) in
399 args (nparams - 1) ((Some (n,Cic.Decl s))::context) t
402 Cic.Anonymous -> abstr
403 | Cic.Name name -> name::abstr),
410 (* ppinductiveType (typename, inductive, arity, cons) *)
411 (* pretty-prints a single inductive definition *)
412 (* (typename, inductive, arity, cons) *)
413 let ppinductiveType current_module_uri nparams (typename, inductive, arity, cons)
415 match analyze_type [] arity with
418 | `Type -> assert false
421 "type " ^ String.uncapitalize typename ^ " = unit (* empty type *)\n"
425 (fun (id,ty) (_abstr,i) -> (* we should verify _abstr = abstr' *)
426 let abstr',sargs = ppty current_module_uri nparams [] ty in
427 let sargs = String.concat " * " sargs in
429 String.capitalize id ^
430 (if sargs = "" then "" else " of " ^ sargs) ^
431 (if i = "" then "" else "\n | ") ^ i)
435 let s = String.concat "," abstr in
436 if s = "" then "" else "(" ^ s ^ ") "
438 "type " ^ abstr ^ String.uncapitalize typename ^ " =\n" ^ scons ^ "\n")
441 let ppobj current_module_uri obj =
442 let module C = Cic in
443 let module U = UriManager in
444 let pp = pp current_module_uri in
446 C.Constant (name, Some t1, t2, params, _) ->
447 (match analyze_type [] t2 with
450 | `Type -> "let " ^ ppid name ^ " =\n" ^ pp t1 [] ^ "\n"
452 match analyze_type [] t1 with
455 let abstr,args = ppty current_module_uri 0 [] t1 in
457 let s = String.concat "," abstr in
458 if s = "" then "" else "(" ^ s ^ ") "
460 "type " ^ abstr ^ ppid name ^ " = " ^ String.concat "->" args ^
462 | C.Constant (name, None, ty, params, _) ->
463 (match analyze_type [] ty with
466 | `Sort _ -> "type " ^ ppid name ^ "\n"
467 | `Type -> "let " ^ ppid name ^ " = assert false\n")
468 | C.Variable (name, bo, ty, params, _) ->
470 "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
473 (match bo with None -> "" | Some bo -> ":= " ^ pp bo [])
474 | C.CurrentProof (name, conjectures, value, ty, params, _) ->
475 "Current Proof of " ^ name ^
476 "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
478 let separate s = if s = "" then "" else s ^ " ; " in
480 (fun (n, context, t) i ->
481 let conjectures',name_context =
483 (fun context_entry (i,name_context) ->
484 (match context_entry with
485 Some (n,C.Decl at) ->
488 pp ~metasenv:conjectures at name_context ^ " ",
489 context_entry::name_context
490 | Some (n,C.Def (at,None)) ->
492 ppname n ^ ":= " ^ pp ~metasenv:conjectures
493 at name_context ^ " ",
494 context_entry::name_context
496 (separate i) ^ "_ :? _ ", context_entry::name_context
500 conjectures' ^ " |- " ^ "?" ^ (string_of_int n) ^ ": " ^
501 pp ~metasenv:conjectures t name_context ^ "\n" ^ i
503 "\n" ^ pp ~metasenv:conjectures value [] ^ " : " ^
504 pp ~metasenv:conjectures ty []
505 | C.InductiveDefinition (l, params, nparams, _) ->
507 (fun x i -> ppinductiveType current_module_uri nparams x ^ i) l ""
510 let ppobj current_module_uri obj =
511 let res = ppobj current_module_uri obj in
512 if res = "" then "" else res ^ ";;\n\n"