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
71 let n = String.uncapitalize n in
72 if List.mem n reserved then n ^ "_" else n
78 | Cic.Anonymous -> "_"
81 (* get_nth l n returns the nth element of the list l if it exists or *)
82 (* raises NotEnoughElements if l has less than n elements *)
86 | (n, he::tail) when n > 1 -> get_nth tail (n-1)
87 | (_,_) -> raise NotEnoughElements
90 let qualified_name_of_uri current_module_uri ?(capitalize=false) uri =
93 String.capitalize (UriManager.name_of_uri uri)
95 ppid (UriManager.name_of_uri uri) in
97 let suri = UriManager.buri_of_uri uri in
98 let s = String.sub suri 5 (String.length suri - 5) in
99 let s = Pcre.replace ~pat:"/" ~templ:"_" s in
100 String.uncapitalize s in
101 if current_module_uri = UriManager.buri_of_uri uri then
104 String.capitalize filename ^ "." ^ name
107 let pp current_module_uri ?metasenv ~in_type =
108 let rec pp ~in_type 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_uri uri ^
124 pp_exp_named_subst exp_named_subst context
128 "?" ^ (string_of_int n) ^ "[" ^
133 | Some t -> pp ~in_type:false t context) l1) ^
137 let _,context,_ = CicUtil.lookup_meta n metasenv in
138 "?" ^ (string_of_int n) ^ "[" ^
146 | Some _, Some t -> pp ~in_type:false t context
150 CicUtil.Meta_not_found _
151 | Invalid_argument _ ->
152 "???" ^ (string_of_int n) ^ "[" ^
154 (List.rev_map (function None -> "_" | Some t ->
155 pp ~in_type:false t context) l1) ^
163 (*| C.Type u -> ("Type" ^ CicUniv.string_of_universe u)*)
166 | C.Implicit (Some `Hole) -> "%"
167 | C.Implicit _ -> "?"
171 let n = "'" ^ String.uncapitalize n in
172 "(" ^ pp ~in_type:true s context ^ " -> " ^
173 pp ~in_type:true t ((Some (Cic.Name n,Cic.Decl s))::context) ^ ")"
175 "(" ^ pp ~in_type:true s context ^ " -> " ^
176 pp ~in_type:true t ((Some (b,Cic.Decl s))::context) ^ ")")
177 | C.Cast (v,t) -> pp ~in_type v context
178 | C.Lambda (b,s,t) ->
179 (match analyze_type context s with
181 | `Statement -> pp ~in_type t ((Some (b,Cic.Decl s))::context)
183 "(function " ^ ppname b ^ " -> " ^
184 pp ~in_type t ((Some (b,Cic.Decl s))::context) ^ ")")
186 let ty,_ = CicTypeChecker.type_of_aux' [] context s CicUniv.oblivion_ugraph in
187 "(let " ^ ppname b ^ " = " ^ pp ~in_type:false s context ^ " in " ^
188 pp ~in_type t ((Some (b,Cic.Def (s,Some ty)))::context) ^ ")"
189 | C.Appl (he::tl) when in_type ->
190 let hes = pp ~in_type he context in
191 let stl = String.concat "," (clean_args_for_ty context tl) in
192 (if stl = "" then "" else "(" ^ stl ^ ") ") ^ hes
193 | C.Appl (C.MutInd _ as he::tl) ->
194 let hes = pp ~in_type he context in
195 let stl = String.concat "," (clean_args_for_ty context tl) in
196 (if stl = "" then "" else "(" ^ stl ^ ") ") ^ hes
197 | C.Appl (C.MutConstruct (uri,n,_,_) as he::tl) ->
199 match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
200 C.InductiveDefinition (_,_,nparams,_) -> nparams
201 | _ -> assert false in
202 let hes = pp ~in_type he context in
203 let stl = String.concat "," (clean_args nparams context tl) in
204 "(" ^ hes ^ (if stl = "" then "" else "(" ^ stl ^ ")") ^ ")"
206 "(" ^ String.concat " " (clean_args 0 context li) ^ ")"
207 | C.Const (uri,exp_named_subst) ->
208 qualified_name_of_uri current_module_uri uri ^
209 pp_exp_named_subst exp_named_subst context
210 | C.MutInd (uri,n,exp_named_subst) ->
212 match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
213 C.InductiveDefinition (dl,_,_,_) ->
214 let (name,_,_,_) = get_nth dl (n+1) in
215 qualified_name_of_uri current_module_uri
216 (UriManager.uri_of_string
217 (UriManager.buri_of_uri uri ^ "/" ^ name ^ ".con")) ^
218 pp_exp_named_subst exp_named_subst context
219 | _ -> raise CicExportationInternalError
221 Sys.Break as exn -> raise exn
222 | _ -> UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n + 1)
224 | C.MutConstruct (uri,n1,n2,exp_named_subst) ->
226 match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
227 C.InductiveDefinition (dl,_,_,_) ->
228 let _,_,_,cons = get_nth dl (n1+1) in
229 let id,_ = get_nth cons n2 in
230 qualified_name_of_uri current_module_uri ~capitalize:true
231 (UriManager.uri_of_string
232 (UriManager.buri_of_uri uri ^ "/" ^ id ^ ".con")) ^
233 pp_exp_named_subst exp_named_subst context
234 | _ -> raise CicExportationInternalError
236 Sys.Break as exn -> raise exn
238 UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n1 + 1) ^ "/" ^
241 | C.MutCase (uri,n1,ty,te,patterns) ->
243 "unit (* TOO POLYMORPHIC TYPE *)"
245 let needs_obj_magic =
246 (* BUG HERE: we should consider also the right parameters *)
247 match CicReduction.whd context ty with
248 Cic.Lambda (_,_,t) -> not (DoubleTypeInference.does_not_occur 1 t)
249 | _ -> false (* it can be a Rel, e.g. in *_rec *)
251 (match analyze_term context te with
252 `Type -> assert false
255 [] -> "assert false" (* empty type elimination *)
257 pp ~in_type:false he context (* singleton elimination *)
260 if patterns = [] then "assert false"
262 (let connames_and_argsno =
263 (match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
264 C.InductiveDefinition (dl,_,paramsno,_) ->
265 let (_,_,_,cons) = get_nth dl (n1+1) in
268 (* this is just an approximation since we do not have
270 let rec count_prods toskip =
272 C.Prod (_,_,bo) when toskip > 0 ->
273 count_prods (toskip - 1) bo
274 | C.Prod (_,_,bo) -> 1 + count_prods 0 bo
277 qualified_name_of_uri current_module_uri
279 (UriManager.uri_of_string
280 (UriManager.buri_of_uri uri ^ "/" ^ id ^ ".con")),
281 count_prods paramsno ty
283 | _ -> raise CicExportationInternalError
286 let connames_and_argsno_and_patterns =
290 | (x,no)::tlx,y::tly -> (x,no,y)::(combine (tlx,tly))
291 | _,_ -> assert false
293 combine (connames_and_argsno,patterns)
295 "\n(match " ^ pp ~in_type:false te context ^ " with \n " ^
296 (String.concat "\n | "
299 let rec aux argsno context =
301 Cic.Lambda (name,ty,bo) when argsno > 0 ->
304 Cic.Anonymous -> Cic.Anonymous
305 | Cic.Name n -> Cic.Name (ppid n) in
307 aux (argsno - 1) (Some (name,Cic.Decl ty)::context)
310 (match analyze_type context ty with
312 | `Sort _ -> args,res
316 | C.Name s -> s)::args,res)
317 | t when argsno = 0 -> [],pp ~in_type:false t context
319 ["{" ^ string_of_int argsno ^ " args missing}"],
320 pp ~in_type:false t context
323 if argsno = 0 then x,pp ~in_type:false y context
325 let args,body = aux argsno context y in
326 let sargs = String.concat "," args in
327 x ^ (if sargs = "" then "" else "(" ^ sargs^ ")"),
331 if needs_obj_magic then
332 "Obj.magic (" ^ body ^ ")"
335 ) connames_and_argsno_and_patterns)) ^
337 | C.Fix (no, funs) ->
341 (function (name,_,ty,_) ->
342 Some (C.Name name,Cic.Decl ty)) funs)
346 (fun (name,ind,ty,bo) i -> name ^ " = \n" ^
347 pp ~in_type:false bo (names@context) ^ i)
350 (match get_nth names (no + 1) with
351 Some (Cic.Name n,_) -> n
353 | C.CoFix (no,funs) ->
357 (function (name,ty,_) ->
358 Some (C.Name name,Cic.Decl ty)) funs)
362 (fun (name,ty,bo) i -> "\n" ^ name ^
363 " : " ^ pp ~in_type:true ty context ^ " := \n" ^
364 pp ~in_type:false bo (names@context) ^ i)
367 and pp_exp_named_subst exp_named_subst context =
368 if exp_named_subst = [] then "" else
370 String.concat " ; " (
372 (function (uri,t) -> UriManager.name_of_uri uri ^ " \\Assign " ^ pp ~in_type:false t context)
375 and clean_args nparams context =
376 let nparams = ref nparams in
380 match analyze_term context t with
381 `Term when !nparams < 0 -> Some (pp ~in_type:false t context)
385 and clean_args_for_ty context =
388 match analyze_term context t with
389 `Type -> Some (pp ~in_type:true t context)
396 let ppty current_module_uri =
397 (* nparams is the number of left arguments
398 left arguments should either become parameters or be skipped altogether *)
399 let rec args nparams context =
404 Cic.Anonymous -> Cic.Anonymous
405 | Cic.Name n -> Cic.Name (String.uncapitalize n)
407 (match analyze_type context s with
410 args (nparams - 1) ((Some (n,Cic.Decl s))::context) t
411 | `Type when nparams > 0 ->
412 args (nparams - 1) ((Some (n,Cic.Decl s))::context) t
415 args (nparams - 1) ((Some (n,Cic.Decl s))::context) t in
416 abstr,pp ~in_type:true current_module_uri s context::args
417 | `Sort _ when nparams <= 0 ->
418 let n = Cic.Name "unit (* EXISTENTIAL TYPE *)" in
419 args (nparams - 1) ((Some (n,Cic.Decl s))::context) t
423 Cic.Anonymous -> Cic.Anonymous
424 | Cic.Name name -> Cic.Name ("'" ^ name) in
426 args (nparams - 1) ((Some (n,Cic.Decl s))::context) t
429 Cic.Anonymous -> abstr
430 | Cic.Name name -> name::abstr),
437 exception DoNotExtract;;
439 let pp_abstracted_ty current_module_uri =
440 let rec args context =
442 Cic.Lambda (n,s,t) ->
445 Cic.Anonymous -> Cic.Anonymous
446 | Cic.Name n -> Cic.Name (String.uncapitalize n)
448 (match analyze_type context s with
452 args ((Some (n,Cic.Decl s))::context) t
456 Cic.Anonymous -> Cic.Anonymous
457 | Cic.Name name -> Cic.Name ("'" ^ name) in
459 args ((Some (n,Cic.Decl s))::context) t
462 Cic.Anonymous -> abstr
463 | Cic.Name name -> name::abstr),
466 match analyze_type context ty with
468 | `Statement -> raise DoNotExtract
470 (* BUG HERE: this can be a real System F type *)
471 let head = pp ~in_type:true current_module_uri ty context in
478 (* ppinductiveType (typename, inductive, arity, cons) *)
479 (* pretty-prints a single inductive definition *)
480 (* (typename, inductive, arity, cons) *)
481 let ppinductiveType current_module_uri nparams (typename, inductive, arity, cons)
483 match analyze_type [] arity with
486 | `Type -> assert false
489 "type " ^ String.uncapitalize typename ^ " = unit (* empty type *)\n"
493 (fun (id,ty) (_abstr,i) -> (* we should verify _abstr = abstr' *)
494 let abstr',sargs = ppty current_module_uri nparams [] ty in
495 let sargs = String.concat " * " sargs in
497 String.capitalize id ^
498 (if sargs = "" then "" else " of " ^ sargs) ^
499 (if i = "" then "" else "\n | ") ^ i)
503 let s = String.concat "," abstr in
504 if s = "" then "" else "(" ^ s ^ ") "
506 "type " ^ abstr ^ String.uncapitalize typename ^ " =\n" ^ scons ^ "\n")
509 let ppobj current_module_uri obj =
510 let module C = Cic in
511 let module U = UriManager in
512 let pp ~in_type = pp ~in_type current_module_uri in
514 C.Constant (name, Some t1, t2, params, _) ->
515 (match analyze_type [] t2 with
518 | `Type -> "let " ^ ppid name ^ " =\n" ^ pp ~in_type:false t1 [] ^ "\n"
520 match analyze_type [] t1 with
524 let abstr,res = pp_abstracted_ty current_module_uri [] t1 in
526 let s = String.concat "," abstr in
527 if s = "" then "" else "(" ^ s ^ ") "
529 "type " ^ abstr ^ ppid name ^ " = " ^ res ^ "\n"
532 | C.Constant (name, None, ty, params, _) ->
533 (match analyze_type [] ty with
536 | `Sort _ -> "type " ^ ppid name ^ "\n"
537 | `Type -> "let " ^ ppid name ^ " = assert false\n")
538 | C.Variable (name, bo, ty, params, _) ->
540 "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
542 pp ~in_type:true ty [] ^ "\n" ^
543 (match bo with None -> "" | Some bo -> ":= " ^ pp ~in_type:false bo [])
544 | C.CurrentProof (name, conjectures, value, ty, params, _) ->
545 "Current Proof of " ^ name ^
546 "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
548 let separate s = if s = "" then "" else s ^ " ; " in
550 (fun (n, context, t) i ->
551 let conjectures',name_context =
553 (fun context_entry (i,name_context) ->
554 (match context_entry with
555 Some (n,C.Decl at) ->
558 pp ~in_type:true ~metasenv:conjectures
559 at name_context ^ " ",
560 context_entry::name_context
561 | Some (n,C.Def (at,None)) ->
563 ppname n ^ ":= " ^ pp ~in_type:false
564 ~metasenv:conjectures at name_context ^ " ",
565 context_entry::name_context
567 (separate i) ^ "_ :? _ ", context_entry::name_context
571 conjectures' ^ " |- " ^ "?" ^ (string_of_int n) ^ ": " ^
572 pp ~in_type:true ~metasenv:conjectures t name_context ^ "\n" ^ i
574 "\n" ^ pp ~in_type:false ~metasenv:conjectures value [] ^ " : " ^
575 pp ~in_type:true ~metasenv:conjectures ty []
576 | C.InductiveDefinition (l, params, nparams, _) ->
578 (fun x i -> ppinductiveType current_module_uri nparams x ^ i) l ""
581 let ppobj current_module_uri obj =
582 let res = ppobj current_module_uri obj in
583 if res = "" then "" else res ^ ";;\n\n"