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 _ -> "?"
167 C.Name n -> "(\\forall " ^ n ^ ":" ^ pp s context ^ "." ^ pp t ((Some (b,Cic.Decl s))::context) ^ ")"
168 | C.Anonymous -> "(" ^ pp s context ^ "\\to " ^ pp t ((Some (b,Cic.Decl s))::context) ^ ")"
170 | C.Cast (v,t) -> pp v context
171 | C.Lambda (b,s,t) ->
172 (match analyze_type context s with
174 | `Statement -> pp t ((Some (b,Cic.Decl s))::context)
175 | `Type -> "(function " ^ ppname b ^ " -> " ^ pp t ((Some (b,Cic.Decl s))::context) ^ ")")
177 let ty,_ = CicTypeChecker.type_of_aux' [] context t CicUniv.oblivion_ugraph in
178 "(let " ^ ppname b ^ " = " ^ pp s context ^ " in " ^ pp t ((Some (b,Cic.Def (s,Some ty)))::context) ^ ")"
179 | C.Appl (C.MutConstruct _ as he::tl) ->
180 let hes = pp he context in
181 let stl = String.concat "," (clean_args context tl) in
182 "(" ^ hes ^ (if stl = "" then "" else "(" ^ stl ^ ")") ^ ")"
184 "(" ^ String.concat " " (clean_args context li) ^ ")"
185 | C.Const (uri,exp_named_subst) ->
186 qualified_name_of_uri current_module_name uri ^
187 pp_exp_named_subst exp_named_subst context
188 | C.MutInd (uri,n,exp_named_subst) ->
190 match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
191 C.InductiveDefinition (dl,_,_,_) ->
192 let (name,_,_,_) = get_nth dl (n+1) in
193 qualified_name_of_uri current_module_name
194 (UriManager.uri_of_string
195 (UriManager.buri_of_uri uri ^ "/" ^ name ^ ".con")) ^
196 pp_exp_named_subst exp_named_subst context
197 | _ -> raise CicExportationInternalError
199 Sys.Break as exn -> raise exn
200 | _ -> UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n + 1)
202 | C.MutConstruct (uri,n1,n2,exp_named_subst) ->
204 match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
205 C.InductiveDefinition (dl,_,_,_) ->
206 let _,_,_,cons = get_nth dl (n1+1) in
207 let id,_ = get_nth cons n2 in
208 qualified_name_of_uri current_module_name ~capitalize:true
209 (UriManager.uri_of_string
210 (UriManager.buri_of_uri uri ^ "/" ^ id ^ ".con")) ^
211 pp_exp_named_subst exp_named_subst context
212 | _ -> raise CicExportationInternalError
214 Sys.Break as exn -> raise exn
216 UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n1 + 1) ^ "/" ^
219 | C.MutCase (uri,n1,ty,te,patterns) ->
220 (match analyze_term context te with
221 `Type -> assert false
224 [] -> "assert false" (* empty type elimination *)
225 | [he] -> pp he context (* singleton elimination *)
228 if patterns = [] then "assert false"
230 (let connames_and_argsno =
231 (match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
232 C.InductiveDefinition (dl,_,paramsno,_) ->
233 let (_,_,_,cons) = get_nth dl (n1+1) in
236 (* this is just an approximation since we do not have
238 let rec count_prods toskip =
240 C.Prod (_,_,bo) when toskip > 0 ->
241 count_prods (toskip - 1) bo
242 | C.Prod (_,_,bo) -> 1 + count_prods 0 bo
245 qualified_name_of_uri current_module_name
247 (UriManager.uri_of_string
248 (UriManager.buri_of_uri uri ^ "/" ^ id ^ ".con")),
249 count_prods paramsno ty
251 | _ -> raise CicExportationInternalError
254 let connames_and_argsno_and_patterns =
258 | [],l -> List.map (fun x -> "???",0,Some x) l
259 | l,[] -> List.map (fun (x,no) -> x,no,None) l
260 | (x,no)::tlx,y::tly -> (x,no,Some y)::(combine (tlx,tly))
262 combine (connames_and_argsno,patterns)
264 "\n(match " ^ pp te context ^ " with \n" ^
265 (String.concat "\n | "
268 let rec aux argsno context =
270 Cic.Lambda (name,ty,bo) when argsno > 0 ->
272 aux (argsno - 1) (Some (name,Cic.Decl ty)::context)
275 (match name with C.Anonymous -> "_" | C.Name s -> s)
278 | t when argsno = 0 -> [],pp t context
280 ["{" ^ string_of_int argsno ^ " args missing}"],
286 | Some y when argsno = 0 -> x,pp y context
288 let args,body = aux argsno context y in
289 let sargs = String.concat "," args in
290 x ^ (if sargs = "" then "" else "(" ^ sargs^ ")"),
293 pattern ^ " -> " ^ body
294 ) connames_and_argsno_and_patterns)) ^
296 | C.Fix (no, funs) ->
300 (function (name,_,ty,_) ->
301 Some (C.Name name,Cic.Decl ty)) funs)
305 (fun (name,ind,ty,bo) i -> name ^ " = \n" ^
306 pp bo (names@context) ^ i)
309 (match get_nth names (no + 1) with
310 Some (Cic.Name n,_) -> n
311 | _ -> assert false) ^ "\n"
312 | C.CoFix (no,funs) ->
316 (function (name,ty,_) ->
317 Some (C.Name name,Cic.Decl ty)) funs)
321 (fun (name,ty,bo) i -> "\n" ^ name ^
322 " : " ^ pp ty context ^ " := \n" ^
323 pp bo (names@context) ^ i)
326 and pp_exp_named_subst exp_named_subst context =
327 if exp_named_subst = [] then "" else
329 String.concat " ; " (
331 (function (uri,t) -> UriManager.name_of_uri uri ^ " \\Assign " ^ pp t context)
334 and clean_args context =
337 match analyze_term context t with
340 | `Term -> Some pp t context)
345 let ppty current_module_name =
346 let rec args context =
351 Cic.Anonymous -> Cic.Anonymous
352 | Cic.Name n -> Cic.Name (String.uncapitalize n)
354 (match analyze_type context s with
355 `Sort Cic.Prop -> args ((Some (n,Cic.Decl s))::context) t
360 Cic.Anonymous -> Cic.Anonymous
361 | Cic.Name name -> Cic.Name ("'" ^ name) in
362 let abstr,args = args ((Some (n,Cic.Decl s))::context) t in
364 Cic.Anonymous -> abstr
365 | Cic.Name name -> name::abstr),
368 let abstr,args = args ((Some (n,Cic.Decl s))::context) t in
369 abstr,pp current_module_name s context::args)
375 (* ppinductiveType (typename, inductive, arity, cons) *)
376 (* pretty-prints a single inductive definition *)
377 (* (typename, inductive, arity, cons) *)
378 let ppinductiveType current_module_name nparams (typename, inductive, arity, cons)
380 match analyze_type [] arity with
383 | `Type -> assert false
386 "type " ^ String.uncapitalize typename ^ " = unit (* empty type *)\n"
390 (fun (id,ty) (_abstr,i) -> (* we should verify _abstr = abstr' *)
391 let abstr',sargs = ppty current_module_name [] ty in
392 let sargs = String.concat " * " sargs in
394 String.capitalize id ^
395 (if sargs = "" then "" else " of " ^ sargs) ^
396 (if i = "" then "" else "\n | ") ^ i)
400 let s = String.concat "," abstr in
401 if s = "" then "" else "(" ^ s ^ ") "
403 "type " ^ abstr ^ String.uncapitalize typename ^ " =\n" ^ scons ^ "\n")
406 let ppobj current_module_name obj =
407 let module C = Cic in
408 let module U = UriManager in
409 let pp = pp current_module_name in
411 C.Constant (name, Some t1, t2, params, _) ->
412 (match analyze_type [] t2 with
415 | `Type -> "let " ^ ppid name ^ " =\n" ^ pp t1 [] ^ "\n"
417 match analyze_type [] t1 with
420 let abstr,args = ppty current_module_name [] t1 in
422 let s = String.concat "," abstr in
423 if s = "" then "" else "(" ^ s ^ ") "
425 "type " ^ abstr ^ ppid name ^ " = " ^ String.concat "->" args ^
427 | C.Constant (name, None, ty, params, _) ->
428 (match analyze_type [] ty with
431 | `Sort _ -> "type " ^ ppid name ^ "\n"
432 | `Type -> "let " ^ ppid name ^ " = assert false\n")
433 | C.Variable (name, bo, ty, params, _) ->
435 "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
438 (match bo with None -> "" | Some bo -> ":= " ^ pp bo [])
439 | C.CurrentProof (name, conjectures, value, ty, params, _) ->
440 "Current Proof of " ^ name ^
441 "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
443 let separate s = if s = "" then "" else s ^ " ; " in
445 (fun (n, context, t) i ->
446 let conjectures',name_context =
448 (fun context_entry (i,name_context) ->
449 (match context_entry with
450 Some (n,C.Decl at) ->
453 pp ~metasenv:conjectures at name_context ^ " ",
454 context_entry::name_context
455 | Some (n,C.Def (at,None)) ->
457 ppname n ^ ":= " ^ pp ~metasenv:conjectures
458 at name_context ^ " ",
459 context_entry::name_context
461 (separate i) ^ "_ :? _ ", context_entry::name_context
465 conjectures' ^ " |- " ^ "?" ^ (string_of_int n) ^ ": " ^
466 pp ~metasenv:conjectures t name_context ^ "\n" ^ i
468 "\n" ^ pp ~metasenv:conjectures value [] ^ " : " ^
469 pp ~metasenv:conjectures ty []
470 | C.InductiveDefinition (l, params, nparams, _) ->
472 (fun x i -> ppinductiveType current_module_name nparams x ^ i) l ""
475 let ppobj current_module_name obj =
476 let res = ppobj current_module_name obj in
477 if res = "" then "" else res ^ ";;\n\n"