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
68 let n = String.uncapitalize n in
69 if List.mem n reserved then n ^ "_" else n
75 | Cic.Anonymous -> "_"
78 (* get_nth l n returns the nth element of the list l if it exists or *)
79 (* raises NotEnoughElements if l has less than n elements *)
83 | (n, he::tail) when n > 1 -> get_nth tail (n-1)
84 | (_,_) -> raise NotEnoughElements
88 (* pretty-prints a term t of cic in an environment l where l is a list of *)
89 (* identifier names used to resolve DeBrujin indexes. The head of l is the *)
90 (* name associated to the greatest DeBrujin index in t *)
92 let rec pp t context =
98 (match get_nth context n with
99 Some (C.Name s,_) -> ppid s
100 | Some (C.Anonymous,_) -> "__" ^ string_of_int n
101 | None -> "_hidden_" ^ string_of_int n
104 NotEnoughElements -> string_of_int (List.length context - n)
106 | C.Var (uri,exp_named_subst) ->
107 UriManager.name_of_uri uri ^ pp_exp_named_subst exp_named_subst context
111 "?" ^ (string_of_int n) ^ "[" ^
113 (List.rev_map (function None -> "_" | Some t -> pp t context) l1) ^
117 let _,context,_ = CicUtil.lookup_meta n metasenv in
118 "?" ^ (string_of_int n) ^ "[" ^
126 | Some _, Some t -> pp t context
130 CicUtil.Meta_not_found _
131 | Invalid_argument _ ->
132 "???" ^ (string_of_int n) ^ "[" ^
134 (List.rev_map (function None -> "_" | Some t -> pp t context) l1) ^
142 (*| C.Type u -> ("Type" ^ CicUniv.string_of_universe u)*)
145 | C.Implicit (Some `Hole) -> "%"
146 | C.Implicit _ -> "?"
149 C.Name n -> "(\\forall " ^ n ^ ":" ^ pp s context ^ "." ^ pp t ((Some (b,Cic.Decl s))::context) ^ ")"
150 | C.Anonymous -> "(" ^ pp s context ^ "\\to " ^ pp t ((Some (b,Cic.Decl s))::context) ^ ")"
152 | C.Cast (v,t) -> pp v context
153 | C.Lambda (b,s,t) ->
154 (match analyze_type context s with
156 | `Statement -> pp t ((Some (b,Cic.Decl s))::context)
157 | `Type -> "(function " ^ ppname b ^ " -> " ^ pp t ((Some (b,Cic.Decl s))::context) ^ ")")
159 let ty,_ = CicTypeChecker.type_of_aux' [] context t CicUniv.oblivion_ugraph in
160 "(let " ^ ppname b ^ " = " ^ pp s context ^ " in " ^ pp t ((Some (b,Cic.Def (s,Some ty)))::context) ^ ")"
161 | C.Appl (C.MutConstruct _ as he::tl) ->
162 let hes = pp he context in
163 let stl = String.concat "," (clean_args context tl) in
164 "(" ^ hes ^ (if stl = "" then "" else "(" ^ stl ^ ")") ^ ")"
166 "(" ^ String.concat " " (clean_args context li) ^ ")"
167 | C.Const (uri,exp_named_subst) ->
168 ppid (UriManager.name_of_uri uri) ^ pp_exp_named_subst exp_named_subst context
169 | C.MutInd (uri,n,exp_named_subst) ->
171 match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
172 C.InductiveDefinition (dl,_,_,_) ->
173 let (name,_,_,_) = get_nth dl (n+1) in
174 ppid name ^ pp_exp_named_subst exp_named_subst context
175 | _ -> raise CicExportationInternalError
177 Sys.Break as exn -> raise exn
178 | _ -> UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n + 1)
180 | C.MutConstruct (uri,n1,n2,exp_named_subst) ->
182 match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
183 C.InductiveDefinition (dl,_,_,_) ->
184 let (_,_,_,cons) = get_nth dl (n1+1) in
185 let (id,_) = get_nth cons n2 in
186 String.capitalize id ^ pp_exp_named_subst exp_named_subst context
187 | _ -> raise CicExportationInternalError
189 Sys.Break as exn -> raise exn
191 UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n1 + 1) ^ "/" ^
194 | C.MutCase (uri,n1,ty,te,patterns) ->
195 let connames_and_argsno =
196 (match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
197 C.InductiveDefinition (dl,_,paramsno,_) ->
198 let (_,_,_,cons) = get_nth dl (n1+1) in
201 (* this is just an approximation since we do not have
203 let rec count_prods toskip =
205 C.Prod (_,_,bo) when toskip > 0 ->
206 count_prods (toskip - 1) bo
207 | C.Prod (_,_,bo) -> 1 + count_prods 0 bo
210 String.capitalize id, count_prods paramsno ty
212 | _ -> raise CicExportationInternalError
215 let connames_and_argsno_and_patterns =
219 | [],l -> List.map (fun x -> "???",0,Some x) l
220 | l,[] -> List.map (fun (x,no) -> x,no,None) l
221 | (x,no)::tlx,y::tly -> (x,no,Some y)::(combine (tlx,tly))
223 combine (connames_and_argsno,patterns)
225 "\n(match " ^ pp te context ^ " with \n" ^
226 (String.concat "\n | "
229 let rec aux argsno context =
231 Cic.Lambda (name,ty,bo) when argsno > 0 ->
232 let args,res = aux (argsno - 1) (Some (name,Cic.Decl ty)::context) bo in
233 (match name with C.Anonymous -> "_" | C.Name s -> s)::args,
235 | t when argsno = 0 -> [],pp t context
236 | t -> ["{" ^ string_of_int argsno ^ " args missing}"],pp t context
241 | Some y when argsno = 0 -> x,pp y context
243 let args,body = aux argsno context y in
244 let sargs = String.concat "," args in
245 x ^ (if sargs = "" then "" else "(" ^ sargs^ ")"),body
247 pattern ^ " -> " ^ body
248 ) connames_and_argsno_and_patterns)) ^
250 | C.Fix (no, funs) ->
254 (function (name,_,ty,_) ->
255 Some (C.Name name,Cic.Decl ty)) funs)
259 (fun (name,ind,ty,bo) i -> name ^ " = \n" ^
260 pp bo (names@context) ^ i)
263 (match get_nth names (no + 1) with
264 Some (Cic.Name n,_) -> n
265 | _ -> assert false) ^ "\n"
266 | C.CoFix (no,funs) ->
270 (function (name,ty,_) ->
271 Some (C.Name name,Cic.Decl ty)) funs)
275 (fun (name,ty,bo) i -> "\n" ^ name ^
276 " : " ^ pp ty context ^ " := \n" ^
277 pp bo (names@context) ^ i)
280 and pp_exp_named_subst exp_named_subst context =
281 if exp_named_subst = [] then "" else
283 String.concat " ; " (
285 (function (uri,t) -> UriManager.name_of_uri uri ^ " \\Assign " ^ pp t context)
288 and clean_args context =
291 match analyze_term context t with
294 | `Term -> Some pp t context)
299 (* ppinductiveType (typename, inductive, arity, cons) *)
300 (* pretty-prints a single inductive definition *)
301 (* (typename, inductive, arity, cons) *)
302 let ppinductiveType (typename, inductive, arity, cons) =
305 (fun (id,ty) (abstr,i) ->
306 let rec args context =
309 (match analyze_type context s with
314 Cic.Anonymous -> Cic.Anonymous
315 | Cic.Name name -> Cic.Name ("'" ^ name) in
316 let abstr,args = args ((Some (n,Cic.Decl s))::context) t in
318 Cic.Anonymous -> abstr
319 | Cic.Name name -> name::abstr),
322 let abstr,args = args ((Some (n,Cic.Decl s))::context) t in
323 abstr,pp s context::args)
326 let abstr',sargs = args [] ty in
327 let sargs = String.concat " * " sargs in
329 String.capitalize id ^
330 (if sargs = "" then "" else " of " ^ sargs) ^
331 (if i = "" then "\n" else "\n | ") ^ i)
335 let s = String.concat "," abstr in
336 if s = "" then "" else "(" ^ s ^ ") "
338 "type " ^ abstr ^ typename ^ " =\n" ^ scons
341 (* ppobj obj returns a string with describing the cic object obj in a syntax *)
342 (* similar to the one used by Coq *)
344 let module C = Cic in
345 let module U = UriManager in
347 C.Constant (name, Some t1, t2, params, _) ->
348 (match analyze_type [] t2 with
351 | `Sort -> "let " ^ ppid name ^ " =\n" ^ pp t1 [] ^ "\n")
352 | C.Constant (name, None, ty, params, _) ->
353 (match analyze_type [] ty with
355 | `Sort -> "type " ^ ppid name ^ "\n"
356 | `Type -> "let " ^ ppid name ^ " = assert false\n")
357 | C.Variable (name, bo, ty, params, _) ->
359 "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
362 (match bo with None -> "" | Some bo -> ":= " ^ pp bo [])
363 | C.CurrentProof (name, conjectures, value, ty, params, _) ->
364 "Current Proof of " ^ name ^
365 "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
367 let separate s = if s = "" then "" else s ^ " ; " in
369 (fun (n, context, t) i ->
370 let conjectures',name_context =
372 (fun context_entry (i,name_context) ->
373 (match context_entry with
374 Some (n,C.Decl at) ->
377 pp ~metasenv:conjectures at name_context ^ " ",
378 context_entry::name_context
379 | Some (n,C.Def (at,None)) ->
381 ppname n ^ ":= " ^ pp ~metasenv:conjectures
382 at name_context ^ " ",
383 context_entry::name_context
385 (separate i) ^ "_ :? _ ", context_entry::name_context
389 conjectures' ^ " |- " ^ "?" ^ (string_of_int n) ^ ": " ^
390 pp ~metasenv:conjectures t name_context ^ "\n" ^ i
392 "\n" ^ pp ~metasenv:conjectures value [] ^ " : " ^
393 pp ~metasenv:conjectures ty []
394 | C.InductiveDefinition (l, params, nparams, _) ->
395 List.fold_right (fun x i -> ppinductiveType x ^ i) l "\n"
399 let res = ppobj obj in
400 if res = "" then "" else res ^ ";;\n"