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
69 let n = String.uncapitalize n in
70 if List.mem n reserved then n ^ "_" else n
76 | Cic.Anonymous -> "_"
79 (* get_nth l n returns the nth element of the list l if it exists or *)
80 (* raises NotEnoughElements if l has less than n elements *)
84 | (n, he::tail) when n > 1 -> get_nth tail (n-1)
85 | (_,_) -> raise NotEnoughElements
88 let qualified_name_of_uri current_module_name ?(capitalize=false) uri =
91 String.capitalize (UriManager.name_of_uri uri)
93 ppid (UriManager.name_of_uri uri) in
94 let buri = UriManager.buri_of_uri uri in
95 let index = String.rindex buri '/' in
96 let filename = String.sub buri (index + 1) (String.length buri - index - 1) in
97 if current_module_name = filename then
100 String.capitalize filename ^ "." ^ name
104 (* pretty-prints a term t of cic in an environment l where l is a list of *)
105 (* identifier names used to resolve DeBrujin indexes. The head of l is the *)
106 (* name associated to the greatest DeBrujin index in t *)
107 let pp current_module_name ?metasenv =
108 let rec pp t context =
109 let module C = Cic in
114 (match get_nth context n with
115 Some (C.Name s,_) -> ppid s
116 | Some (C.Anonymous,_) -> "__" ^ string_of_int n
117 | None -> "_hidden_" ^ string_of_int n
120 NotEnoughElements -> string_of_int (List.length context - n)
122 | C.Var (uri,exp_named_subst) ->
123 qualified_name_of_uri current_module_name uri ^
124 pp_exp_named_subst exp_named_subst context
128 "?" ^ (string_of_int n) ^ "[" ^
130 (List.rev_map (function None -> "_" | Some t -> pp t context) l1) ^
134 let _,context,_ = CicUtil.lookup_meta n metasenv in
135 "?" ^ (string_of_int n) ^ "[" ^
143 | Some _, Some t -> pp t context
147 CicUtil.Meta_not_found _
148 | Invalid_argument _ ->
149 "???" ^ (string_of_int n) ^ "[" ^
151 (List.rev_map (function None -> "_" | Some t -> pp t context) l1) ^
159 (*| C.Type u -> ("Type" ^ CicUniv.string_of_universe u)*)
162 | C.Implicit (Some `Hole) -> "%"
163 | C.Implicit _ -> "?"
166 C.Name n -> "(\\forall " ^ n ^ ":" ^ pp s context ^ "." ^ pp t ((Some (b,Cic.Decl s))::context) ^ ")"
167 | C.Anonymous -> "(" ^ pp s context ^ "\\to " ^ pp t ((Some (b,Cic.Decl s))::context) ^ ")"
169 | C.Cast (v,t) -> pp v context
170 | C.Lambda (b,s,t) ->
171 (match analyze_type context s with
173 | `Statement -> pp t ((Some (b,Cic.Decl s))::context)
174 | `Type -> "(function " ^ ppname b ^ " -> " ^ pp t ((Some (b,Cic.Decl s))::context) ^ ")")
176 let ty,_ = CicTypeChecker.type_of_aux' [] context t CicUniv.oblivion_ugraph in
177 "(let " ^ ppname b ^ " = " ^ pp s context ^ " in " ^ pp t ((Some (b,Cic.Def (s,Some ty)))::context) ^ ")"
178 | C.Appl (C.MutConstruct _ as he::tl) ->
179 let hes = pp he context in
180 let stl = String.concat "," (clean_args context tl) in
181 "(" ^ hes ^ (if stl = "" then "" else "(" ^ stl ^ ")") ^ ")"
183 "(" ^ String.concat " " (clean_args context li) ^ ")"
184 | C.Const (uri,exp_named_subst) ->
185 qualified_name_of_uri current_module_name uri ^
186 pp_exp_named_subst exp_named_subst context
187 | C.MutInd (uri,n,exp_named_subst) ->
189 match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
190 C.InductiveDefinition (dl,_,_,_) ->
191 let (name,_,_,_) = get_nth dl (n+1) in
192 qualified_name_of_uri current_module_name
193 (UriManager.uri_of_string
194 (UriManager.buri_of_uri uri ^ "/" ^ name ^ ".con")) ^
195 pp_exp_named_subst exp_named_subst context
196 | _ -> raise CicExportationInternalError
198 Sys.Break as exn -> raise exn
199 | _ -> UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n + 1)
201 | C.MutConstruct (uri,n1,n2,exp_named_subst) ->
203 match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
204 C.InductiveDefinition (dl,_,_,_) ->
205 let _,_,_,cons = get_nth dl (n1+1) in
206 let id,_ = get_nth cons n2 in
207 qualified_name_of_uri current_module_name ~capitalize:true
208 (UriManager.uri_of_string
209 (UriManager.buri_of_uri uri ^ "/" ^ id ^ ".con")) ^
210 pp_exp_named_subst exp_named_subst context
211 | _ -> raise CicExportationInternalError
213 Sys.Break as exn -> raise exn
215 UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n1 + 1) ^ "/" ^
218 | C.MutCase (uri,n1,ty,te,patterns) ->
219 (match analyze_term context te with
220 `Type -> assert false
223 [] -> "assert false" (* empty type elimination *)
224 | [he] -> pp he context (* singleton elimination *)
227 if patterns = [] then "assert false"
229 (let connames_and_argsno =
230 (match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
231 C.InductiveDefinition (dl,_,paramsno,_) ->
232 let (_,_,_,cons) = get_nth dl (n1+1) in
235 (* this is just an approximation since we do not have
237 let rec count_prods toskip =
239 C.Prod (_,_,bo) when toskip > 0 ->
240 count_prods (toskip - 1) bo
241 | C.Prod (_,_,bo) -> 1 + count_prods 0 bo
244 qualified_name_of_uri current_module_name
246 (UriManager.uri_of_string
247 (UriManager.buri_of_uri uri ^ "/" ^ id ^ ".con")),
248 count_prods paramsno ty
250 | _ -> raise CicExportationInternalError
253 let connames_and_argsno_and_patterns =
257 | [],l -> List.map (fun x -> "???",0,Some x) l
258 | l,[] -> List.map (fun (x,no) -> x,no,None) l
259 | (x,no)::tlx,y::tly -> (x,no,Some y)::(combine (tlx,tly))
261 combine (connames_and_argsno,patterns)
263 "\n(match " ^ pp te context ^ " with \n" ^
264 (String.concat "\n | "
267 let rec aux argsno context =
269 Cic.Lambda (name,ty,bo) when argsno > 0 ->
271 aux (argsno - 1) (Some (name,Cic.Decl ty)::context)
274 (match name with C.Anonymous -> "_" | C.Name s -> s)
277 | t when argsno = 0 -> [],pp t context
279 ["{" ^ string_of_int argsno ^ " args missing}"],
285 | Some y when argsno = 0 -> x,pp y context
287 let args,body = aux argsno context y in
288 let sargs = String.concat "," args in
289 x ^ (if sargs = "" then "" else "(" ^ sargs^ ")"),
292 pattern ^ " -> " ^ body
293 ) connames_and_argsno_and_patterns)) ^
295 | C.Fix (no, funs) ->
299 (function (name,_,ty,_) ->
300 Some (C.Name name,Cic.Decl ty)) funs)
304 (fun (name,ind,ty,bo) i -> name ^ " = \n" ^
305 pp bo (names@context) ^ i)
308 (match get_nth names (no + 1) with
309 Some (Cic.Name n,_) -> n
310 | _ -> assert false) ^ "\n"
311 | C.CoFix (no,funs) ->
315 (function (name,ty,_) ->
316 Some (C.Name name,Cic.Decl ty)) funs)
320 (fun (name,ty,bo) i -> "\n" ^ name ^
321 " : " ^ pp ty context ^ " := \n" ^
322 pp bo (names@context) ^ i)
325 and pp_exp_named_subst exp_named_subst context =
326 if exp_named_subst = [] then "" else
328 String.concat " ; " (
330 (function (uri,t) -> UriManager.name_of_uri uri ^ " \\Assign " ^ pp t context)
333 and clean_args context =
336 match analyze_term context t with
339 | `Term -> Some pp t context)
344 let ppty current_module_name =
345 let rec args context =
350 Cic.Anonymous -> Cic.Anonymous
351 | Cic.Name n -> Cic.Name (String.uncapitalize n)
353 (match analyze_type context s with
354 `Sort Cic.Prop -> args ((Some (n,Cic.Decl s))::context) t
359 Cic.Anonymous -> Cic.Anonymous
360 | Cic.Name name -> Cic.Name ("'" ^ name) in
361 let abstr,args = args ((Some (n,Cic.Decl s))::context) t in
363 Cic.Anonymous -> abstr
364 | Cic.Name name -> name::abstr),
367 let abstr,args = args ((Some (n,Cic.Decl s))::context) t in
368 abstr,pp current_module_name s context::args)
374 (* ppinductiveType (typename, inductive, arity, cons) *)
375 (* pretty-prints a single inductive definition *)
376 (* (typename, inductive, arity, cons) *)
377 let ppinductiveType current_module_name (typename, inductive, arity, cons) =
378 match analyze_type [] arity with
381 | `Type -> assert false
384 "type " ^ String.uncapitalize typename ^ " = unit (* empty type *)\n"
388 (fun (id,ty) (abstr,i) ->
389 let abstr',sargs = ppty current_module_name [] ty in
390 let sargs = String.concat " * " sargs in
392 String.capitalize id ^
393 (if sargs = "" then "" else " of " ^ sargs) ^
394 (if i = "" then "" else "\n | ") ^ i)
398 let s = String.concat "," abstr in
399 if s = "" then "" else "(" ^ s ^ ") "
401 "type " ^ abstr ^ String.uncapitalize typename ^ " =\n" ^ scons ^ "\n")
404 let ppobj current_module_name obj =
405 let module C = Cic in
406 let module U = UriManager in
407 let pp = pp current_module_name in
409 C.Constant (name, Some t1, t2, params, _) ->
410 (match analyze_type [] t2 with
413 | `Type -> "let " ^ ppid name ^ " =\n" ^ pp t1 [] ^ "\n"
415 match analyze_type [] t1 with
418 let abstr,args = ppty current_module_name [] t1 in
420 let s = String.concat "," abstr in
421 if s = "" then "" else "(" ^ s ^ ") "
423 "type " ^ abstr ^ ppid name ^ " = " ^ String.concat "->" args ^
425 | C.Constant (name, None, ty, params, _) ->
426 (match analyze_type [] ty with
429 | `Sort _ -> "type " ^ ppid name ^ "\n"
430 | `Type -> "let " ^ ppid name ^ " = assert false\n")
431 | C.Variable (name, bo, ty, params, _) ->
433 "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
436 (match bo with None -> "" | Some bo -> ":= " ^ pp bo [])
437 | C.CurrentProof (name, conjectures, value, ty, params, _) ->
438 "Current Proof of " ^ name ^
439 "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
441 let separate s = if s = "" then "" else s ^ " ; " in
443 (fun (n, context, t) i ->
444 let conjectures',name_context =
446 (fun context_entry (i,name_context) ->
447 (match context_entry with
448 Some (n,C.Decl at) ->
451 pp ~metasenv:conjectures at name_context ^ " ",
452 context_entry::name_context
453 | Some (n,C.Def (at,None)) ->
455 ppname n ^ ":= " ^ pp ~metasenv:conjectures
456 at name_context ^ " ",
457 context_entry::name_context
459 (separate i) ^ "_ :? _ ", context_entry::name_context
463 conjectures' ^ " |- " ^ "?" ^ (string_of_int n) ^ ": " ^
464 pp ~metasenv:conjectures t name_context ^ "\n" ^ i
466 "\n" ^ pp ~metasenv:conjectures value [] ^ " : " ^
467 pp ~metasenv:conjectures ty []
468 | C.InductiveDefinition (l, params, nparams, _) ->
470 (fun x i -> ppinductiveType current_module_name x ^ i) l ""
473 let ppobj current_module_name obj =
474 let res = ppobj current_module_name obj in
475 if res = "" then "" else res ^ ";;\n\n"