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 (*****************************************************************************)
30 (* This module implements a very simple Coq-like pretty printer that, given *)
31 (* an object of cic (internal representation) returns a string describing *)
32 (* the object in a syntax similar to that of coq *)
34 (* It also contains the utility functions to check a name w.r.t the Matita *)
37 (*****************************************************************************)
41 exception CicPpInternalError;;
42 exception NotEnoughElements;;
44 (* Utility functions *)
49 | Cic.Anonymous -> "_"
52 (* get_nth l n returns the nth element of the list l if it exists or *)
53 (* raises NotEnoughElements if l has less than n elements *)
57 | (n, he::tail) when n > 1 -> get_nth tail (n-1)
58 | (_,_) -> raise NotEnoughElements
62 (* pretty-prints a term t of cic in an environment l where l is a list of *)
63 (* identifier names used to resolve DeBrujin indexes. The head of l is the *)
64 (* name associated to the greatest DeBrujin index in t *)
71 (match get_nth l n with
73 | Some C.Anonymous -> "__" ^ string_of_int n
74 | None -> "_hidden_" ^ string_of_int n
77 NotEnoughElements -> string_of_int (List.length l - n)
79 | C.Var (uri,exp_named_subst) ->
80 UriManager.string_of_uri (*UriManager.name_of_uri*) uri ^ pp_exp_named_subst exp_named_subst l
82 "?" ^ (string_of_int n) ^ "[" ^
84 (List.rev_map (function None -> "_" | Some t -> pp t l) l1) ^
91 (*| C.Type u -> ("Type" ^ CicUniv.string_of_universe u)*)
94 | C.Implicit (Some `Hole) -> "%"
98 C.Name n -> "(" ^ n ^ ":" ^ pp s l ^ ")" ^ pp t ((Some b)::l)
99 | C.Anonymous -> "(" ^ pp s l ^ "->" ^ pp t ((Some b)::l) ^ ")"
101 | C.Cast (v,t) -> "(" ^ pp v l ^ ":" ^ pp t l ^ ")"
102 | C.Lambda (b,s,t) ->
103 "(\\lambda " ^ ppname b ^ ":" ^ pp s l ^ "." ^ pp t ((Some b)::l) ^ ")"
105 "[" ^ ppname b ^ ":=" ^ pp s l ^ "]" ^ pp t ((Some b)::l)
109 (fun x i -> pp x l ^ (match i with "" -> "" | _ -> " ") ^ i)
112 | C.Const (uri,exp_named_subst) ->
113 UriManager.name_of_uri uri ^ pp_exp_named_subst exp_named_subst l
114 | C.MutInd (uri,n,exp_named_subst) ->
116 match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
117 C.InductiveDefinition (dl,_,_,_) ->
118 let (name,_,_,_) = get_nth dl (n+1) in
119 name ^ pp_exp_named_subst exp_named_subst l
120 | _ -> raise CicPpInternalError
122 Sys.Break as exn -> raise exn
123 | _ -> UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n + 1)
125 | C.MutConstruct (uri,n1,n2,exp_named_subst) ->
127 match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
128 C.InductiveDefinition (dl,_,_,_) ->
129 let (_,_,_,cons) = get_nth dl (n1+1) in
130 let (id,_) = get_nth cons n2 in
131 id ^ pp_exp_named_subst exp_named_subst l
132 | _ -> raise CicPpInternalError
134 Sys.Break as exn -> raise exn
136 UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n1 + 1) ^ "/" ^
139 | C.MutCase (uri,n1,ty,te,patterns) ->
141 (match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
142 C.InductiveDefinition (dl,_,_,_) ->
143 let (_,_,_,cons) = get_nth dl (n1+1) in
144 List.map (fun (id,_) -> id) cons
145 | _ -> raise CicPpInternalError
148 let connames_and_patterns =
152 | [],l -> List.map (fun x -> "???",Some x) l
153 | l,[] -> List.map (fun x -> x,None) l
154 | x::tlx,y::tly -> (x,Some y)::(combine (tlx,tly))
156 combine (connames,patterns)
158 "\n<" ^ pp ty l ^ ">Cases " ^ pp te l ^ " of " ^
160 (fun (x,y) i -> "\n " ^ x ^ " => " ^
161 (match y with None -> "" | Some y -> pp y l) ^ i)
162 connames_and_patterns "" ^
164 | C.Fix (no, funs) ->
165 let snames = List.map (fun (name,_,_,_) -> name) funs in
167 List.rev (List.map (function name -> Some (C.Name name)) snames)
169 "\nFix " ^ get_nth snames (no + 1) ^ " {" ^
171 (fun (name,ind,ty,bo) i -> "\n" ^ name ^ " / " ^ string_of_int ind ^
172 " : " ^ pp ty l ^ " := \n" ^
176 | C.CoFix (no,funs) ->
177 let snames = List.map (fun (name,_,_) -> name) funs in
179 List.rev (List.map (function name -> Some (C.Name name)) snames)
181 "\nCoFix " ^ get_nth snames (no + 1) ^ " {" ^
183 (fun (name,ty,bo) i -> "\n" ^ name ^
184 " : " ^ pp ty l ^ " := \n" ^
188 and pp_exp_named_subst exp_named_subst l =
189 if exp_named_subst = [] then "" else
191 String.concat " ; " (
193 (function (uri,t) -> UriManager.name_of_uri uri ^ " \\Assign " ^ pp t l)
202 (* ppinductiveType (typename, inductive, arity, cons) *)
203 (* pretty-prints a single inductive definition *)
204 (* (typename, inductive, arity, cons) *)
205 let ppinductiveType (typename, inductive, arity, cons) =
206 (if inductive then "\nInductive " else "\nCoInductive ") ^ typename ^ ": " ^
207 pp arity [] ^ " =\n " ^
209 (fun (id,ty) i -> id ^ " : " ^ pp ty [] ^
210 (if i = "" then "\n" else "\n | ") ^ i)
214 let ppcontext ?(sep = "\n") context =
215 let separate s = if s = "" then "" else s ^ sep in
217 (fun context_entry (i,name_context) ->
218 match context_entry with
219 Some (n,Cic.Decl t) ->
220 Printf.sprintf "%s%s : %s" (separate i) (ppname n)
221 (pp t name_context), (Some n)::name_context
222 | Some (n,Cic.Def (bo,ty)) ->
223 Printf.sprintf "%s%s : %s := %s" (separate i) (ppname n)
226 | Some ty -> pp ty name_context)
227 (pp bo name_context), (Some n)::name_context
229 Printf.sprintf "%s_ :? _" (separate i), None::name_context
232 (* ppobj obj returns a string with describing the cic object obj in a syntax *)
233 (* similar to the one used by Coq *)
235 let module C = Cic in
236 let module U = UriManager in
238 C.Constant (name, Some t1, t2, params, _) ->
239 "Definition of " ^ name ^
240 "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
241 ")" ^ ":\n" ^ pp t1 [] ^ " : " ^ pp t2 []
242 | C.Constant (name, None, ty, params, _) ->
244 "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
246 | C.Variable (name, bo, ty, params, _) ->
248 "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
251 (match bo with None -> "" | Some bo -> ":= " ^ pp bo [])
252 | C.CurrentProof (name, conjectures, value, ty, params, _) ->
253 "Current Proof of " ^ name ^
254 "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
256 let separate s = if s = "" then "" else s ^ " ; " in
258 (fun (n, context, t) i ->
259 let conjectures',name_context =
261 (fun context_entry (i,name_context) ->
262 (match context_entry with
263 Some (n,C.Decl at) ->
265 ppname n ^ ":" ^ pp at name_context ^ " ",
266 (Some n)::name_context
267 | Some (n,C.Def (at,None)) ->
269 ppname n ^ ":= " ^ pp at name_context ^ " ",
270 (Some n)::name_context
272 (separate i) ^ "_ :? _ ", None::name_context
276 conjectures' ^ " |- " ^ "?" ^ (string_of_int n) ^ ": " ^
277 pp t name_context ^ "\n" ^ i
279 "\n" ^ pp value [] ^ " : " ^ pp ty []
280 | C.InductiveDefinition (l, params, nparams, _) ->
282 String.concat ";" (List.map UriManager.string_of_uri params) ^ "\n" ^
283 "NParams = " ^ string_of_int nparams ^ "\n" ^
284 List.fold_right (fun x i -> ppinductiveType x ^ i) l ""
287 let ppsort = function
290 | Cic.Type _ -> "Type"
291 | Cic.CProp -> "CProp"
294 (* MATITA NAMING CONVENTION *)
296 let is_prefix prefix string =
297 let len = String.length prefix in
298 let len1 = String.length string in
301 let head = String.sub string 0 len in
303 (String.compare (String.lowercase head) (String.lowercase prefix)=0) then
305 let diff = len1-len in
306 let tail = String.sub string len diff in
307 if ((diff > 0) && (String.rcontains_from tail 0 '_')) then
308 Some (String.sub tail 1 (diff-1))
315 let remove_prefix prefix (last,string) =
316 if string = "" then (last,string)
318 match is_prefix prefix string with
321 match is_prefix last prefix with
322 None -> (last,string)
324 (match is_prefix prefix (last^string) with
325 None -> (last,string)
326 | Some tail -> (prefix,tail))
328 | Some tail -> (prefix, tail)
330 let legal_suffix string =
331 if string = "" then true else
333 let legal_s = Str.regexp "_?\\([0-9]+\\|r\\|l\\|'\\|\"\\)" in
334 (Str.string_match legal_s string 0) && (Str.matched_string string = string)
337 (** check if a prefix of string_name is legal for term and returns the tail.
338 chec_rec cannot fail: at worst it return string_name.
339 The algorithm is greedy, but last contains the last name matched, providing
341 string_name is here a pair (last,string_name).*)
343 let rec check_rec ctx string_name =
346 (match List.nth ctx (m-1) with
348 remove_prefix name string_name
349 | Cic.Anonymous -> string_name)
350 | Cic.Meta _ -> string_name
351 | Cic.Sort sort -> remove_prefix (ppsort sort) string_name
352 | Cic.Implicit _ -> string_name
353 | Cic.Cast (te,ty) -> check_rec ctx string_name te
354 | Cic.Prod (name,so,dest) ->
355 let l_string_name = check_rec ctx string_name so in
356 check_rec (name::ctx) string_name dest
357 | Cic.Lambda (name,so,dest) ->
360 Cic.Anonymous -> string_name
361 | Cic.Name name -> remove_prefix name string_name in
362 let l_string_name = check_rec ctx string_name so in
363 check_rec (name::ctx) l_string_name dest
364 | Cic.LetIn (name,so,dest) ->
365 let string_name = check_rec ctx string_name so in
366 check_rec (name::ctx) string_name dest
368 List.fold_left (check_rec ctx) string_name l
369 | Cic.Var (uri,exp_named_subst) ->
370 let name = UriManager.name_of_uri uri in
371 remove_prefix name string_name
372 | Cic.Const (uri,exp_named_subst) ->
373 let name = UriManager.name_of_uri uri in
374 remove_prefix name string_name
375 | Cic.MutInd (uri,_,exp_named_subst) ->
376 let name = UriManager.name_of_uri uri in
377 remove_prefix name string_name
378 | Cic.MutConstruct (uri,n,m,exp_named_subst) ->
380 (match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
381 Cic.InductiveDefinition (dl,_,_,_) ->
382 let (_,_,_,cons) = get_nth dl (n+1) in
383 let (id,_) = get_nth cons m in
385 | _ -> assert false) in
386 remove_prefix name string_name
387 | Cic.MutCase (_,_,_,te,pl) ->
388 let strig_name = remove_prefix "match" string_name in
389 let string_name = check_rec ctx string_name te in
390 List.fold_right (fun t s -> check_rec ctx s t) pl string_name
392 let strig_name = remove_prefix "fix" string_name in
393 let names = List.map (fun (name,_,_,_) -> name) fl in
395 List.rev (List.map (function name -> Cic.Name name) names)
398 (fun (_,_,_,bo) s -> check_rec (onames@ctx) s bo) fl string_name
399 | Cic.CoFix (_,fl) ->
400 let strig_name = remove_prefix "cofix" string_name in
401 let names = List.map (fun (name,_,_) -> name) fl in
403 List.rev (List.map (function name -> Cic.Name name) names)
406 (fun (_,_,bo) s -> check_rec (onames@ctx) s bo) fl string_name
408 let check_name ?(allow_suffix=false) ctx name term =
409 let (_,tail) = check_rec ctx ("",name) term in
410 if (not allow_suffix) then (String.length tail = 0)
411 else legal_suffix tail
413 let check_elim ctx conclusion_name =
414 let elim = Str.regexp "_elim\\|_case" in
415 if (Str.string_match elim conclusion_name 0) then
416 let len = String.length conclusion_name in
417 let tail = String.sub conclusion_name 5 (len-5) in
421 let rec check_names ctx hyp_names conclusion_name t =
423 | Cic.Prod (name,s,t) ->
424 (match hyp_names with
425 [] -> check_names (name::ctx) hyp_names conclusion_name t
427 if check_name ctx hd s then
428 check_names (name::ctx) tl conclusion_name t
430 check_names (name::ctx) hyp_names conclusion_name t)
431 | Cic.Appl ((Cic.Rel n)::args) ->
432 (match hyp_names with
434 (check_name ~allow_suffix:true ctx conclusion_name t) ||
435 (check_elim ctx conclusion_name)
437 (* what to elim could be an argument
438 of the predicate: e.g. leb_elim *)
440 List.fold_left (check_rec ctx) ("",what_to_elim) args in
441 (tail = "" && check_elim ctx conclusion_name)
443 | Cic.MutCase (_,_,Cic.Lambda(name,so,ty),te,_) ->
444 (match hyp_names with
446 (match is_prefix "match" conclusion_name with
447 None -> check_name ~allow_suffix:true ctx conclusion_name t
448 | Some tail -> check_name ~allow_suffix:true ctx tail t)
450 (* what to match could be the term te or its type so; in this case the
451 conclusion name should match ty *)
452 check_name ~allow_suffix:true (name::ctx) conclusion_name ty &&
453 (check_name ctx what_to_match te || check_name ctx what_to_match so)
456 hyp_names=[] && check_name ~allow_suffix:true ctx conclusion_name t
458 let check name term =
459 let names = Str.split (Str.regexp_string "_to_") name in
460 let hyp_names,conclusion_name =
461 match List.rev names with
464 let elim = Str.regexp "_elim\\|_case" in
465 let len = String.length hd in
467 let pos = Str.search_backward elim hd len in
468 let hyp = String.sub hd 0 pos in
469 let concl = String.sub hd pos (len-pos) in
470 List.rev (hyp::tl),concl
471 with Not_found -> (List.rev tl),hd in
472 check_names [] hyp_names conclusion_name term