1 (* Copyright (C) 2003-2005, 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/.
30 module TC = CicTypeChecker
31 module PEH = ProofEngineHelpers
32 module E = CicEnvironment
33 module UM = UriManager
35 module PER = ProofEngineReduction
38 (* fresh name generator *****************************************************)
42 if i <= 0 then assert false else
43 let c = name.[pred i] in
44 if c >= '0' && c <= '9' then aux (pred i)
45 else Str.string_before name i, Str.string_after name i
47 let before, after = aux (String.length name) in
48 let i = if after = "" then -1 else int_of_string after in
52 C.Name (if i < 0 then s else s ^ string_of_int i)
54 let mk_fresh_name context (name, k) =
55 let rec aux i = function
57 | Some (C.Name s, _) :: entries ->
59 if m = name && j >= i then aux (succ j) entries else aux i entries
60 | _ :: entries -> aux i entries
64 let mk_fresh_name context = function
65 | C.Anonymous -> C.Anonymous
66 | C.Name s -> mk_fresh_name context (split s)
68 (* helper functions *********************************************************)
70 let rec list_fold_right_cps g map l a =
74 let h a = map g hd a in
75 list_fold_right_cps h map tl a
77 let rec list_fold_left_cps g map a = function
80 let h a = list_fold_left_cps g map a tl in
83 let rec list_map_cps g map = function
87 let g tl = g (hd :: tl) in
94 let compose f g x = f (g x)
96 let fst3 (x, _, _) = x
100 Printf.eprintf "Ref: context: %s\n" (Pp.ppcontext c);
101 Printf.eprintf "Ref: term : %s\n" (Pp.ppterm t);
104 try let t, _, _, _ = Rf.type_of_aux' [] c t Un.default_ugraph in t with
105 | Rf.RefineFailure s as e ->
106 Printf.eprintf "REFINE FAILURE: %s\n" (Lazy.force s);
109 Printf.eprintf "REFINE ERROR: %s\n" (Printexc.to_string e);
112 let get_type msg c t =
114 prerr_endline ("TC: " ^ s);
115 prerr_endline ("TC: context: " ^ Pp.ppcontext c);
116 prerr_string "TC: term : "; Ut.pp_term prerr_string [] c t;
117 prerr_newline (); prerr_endline ("TC: location: " ^ msg)
119 try let ty, _ = TC.type_of_aux' [] c t Un.default_ugraph in ty with
120 | TC.TypeCheckerFailure s as e ->
121 log ("failure: " ^ Lazy.force s); raise e
122 | TC.AssertFailure s as e ->
123 log ("assert : " ^ Lazy.force s); raise e
126 match PEH.split_with_whd (c, t) with
127 | (_, hd) :: _, _ -> hd
131 match get_tail c (get_type "is_prop" c t) with
132 | C.Sort C.Prop -> true
137 is_prop c (get_type "is_prop" c t)
139 let is_sort = function
143 let is_unsafe h (c, t) = true
145 let is_not_atomic = function
151 | C.MutConstruct _ -> false
154 let is_atomic t = not (is_not_atomic t)
156 let get_ind_type uri tyno =
157 match E.get_obj Un.default_ugraph uri with
158 | C.InductiveDefinition (tys, _, lpsno, _), _ -> lpsno, List.nth tys tyno
161 let get_ind_names uri tno =
163 let ts = match E.get_obj Un.default_ugraph uri with
164 | C.InductiveDefinition (ts, _, _, _), _ -> ts
167 match List.nth ts tno with
168 | (_, _, _, cs) -> List.map fst cs
169 with Invalid_argument _ -> failwith "get_ind_names"
171 let get_default_eliminator context uri tyno ty =
172 let _, (name, _, _, _) = get_ind_type uri tyno in
173 let ext = match get_tail context (get_type "get_def_elim" context ty) with
174 | C.Sort C.Prop -> "_ind"
175 | C.Sort C.Set -> "_rec"
176 | C.Sort (C.CProp _) -> "_rect"
177 | C.Sort (C.Type _) -> "_rect"
179 Printf.eprintf "CicPPP get_default_eliminator: %s\n" (Pp.ppterm t);
182 let buri = UM.buri_of_uri uri in
183 let uri = UM.uri_of_string (buri ^ "/" ^ name ^ ext ^ ".con") in
186 let get_ind_parameters c t =
187 let ty = get_type "get_ind_pars 1" c t in
188 let ps = match get_tail c ty with
190 | C.Appl (C.MutInd _ :: args) -> args
193 let disp = match get_tail c (get_type "get_ind_pars 2" c ty) with
200 let cic = D.deannotate_term
202 let occurs c ~what ~where =
203 let result = ref false in
204 let equality c t1 t2 =
205 let r = Ut.alpha_equivalence t1 t2 in
206 result := !result || r; r
208 let context, what, with_what = c, [what], [C.Rel 0] in
209 let _ = PER.replace_lifting ~equality ~context ~what ~with_what ~where in
212 let name_of_uri uri tyno cno =
213 let get_ind_type tys tyno =
214 let s, _, _, cs = List.nth tys tyno in s, cs
216 match (fst (E.get_obj Un.default_ugraph uri)), tyno, cno with
217 | C.Variable (s, _, _, _, _), _, _ -> s
218 | C.Constant (s, _, _, _, _), _, _ -> s
219 | C.InductiveDefinition (tys, _, _, _), Some i, None ->
220 let s, _ = get_ind_type tys i in s
221 | C.InductiveDefinition (tys, _, _, _), Some i, Some j ->
222 let _, cs = get_ind_type tys i in
223 let s, _ = List.nth cs (pred j) in s
226 (* Ensuring Barendregt convenction ******************************************)
228 let rec add_entries map c = function
231 let sname, w = map hd in
232 let entry = Some (Cic.Name sname, C.Decl w) in
233 add_entries map (entry :: c) tl
236 try match List.nth c (pred i) with
237 | Some (Cic.Name sname, _) -> sname
240 | Failure _ -> assert false
241 | Invalid_argument _ -> assert false
244 let get_fix_decl (sname, i, w, v) = sname, w in
245 let get_cofix_decl (sname, w, v) = sname, w in
246 let rec bc c = function
247 | C.LetIn (name, v, ty, t) ->
248 let name = mk_fresh_name c name in
249 let entry = Some (name, C.Def (v, ty)) in
250 let v, ty, t = bc c v, bc c ty, bc (entry :: c) t in
251 C.LetIn (name, v, ty, t)
252 | C.Lambda (name, w, t) ->
253 let name = mk_fresh_name c name in
254 let entry = Some (name, C.Decl w) in
255 let w, t = bc c w, bc (entry :: c) t in
256 C.Lambda (name, w, t)
257 | C.Prod (name, w, t) ->
258 let name = mk_fresh_name c name in
259 let entry = Some (name, C.Decl w) in
260 let w, t = bc c w, bc (entry :: c) t in
263 let vs = List.map (bc c) vs in
265 | C.MutCase (uri, tyno, u, v, ts) ->
266 let u, v, ts = bc c u, bc c v, List.map (bc c) ts in
267 C.MutCase (uri, tyno, u, v, ts)
269 let t, u = bc c t, bc c u in
271 | C.Fix (i, fixes) ->
272 let d = add_entries get_fix_decl c fixes in
273 let bc_fix (sname, i, w, v) = (sname, i, bc c w, bc d v) in
274 let fixes = List.map bc_fix fixes in
276 | C.CoFix (i, cofixes) ->
277 let d = add_entries get_cofix_decl c cofixes in
278 let bc_cofix (sname, w, v) = (sname, bc c w, bc d v) in
279 let cofixes = List.map bc_cofix cofixes in
286 let get_fix_decl (id, sname, i, w, v) = sname, cic w in
287 let get_cofix_decl (id, sname, w, v) = sname, cic w in
288 let rec bc c = function
289 | C.ALetIn (id, name, v, ty, t) ->
290 let name = mk_fresh_name c name in
291 let entry = Some (name, C.Def (cic v, cic ty)) in
292 let v, ty, t = bc c v, bc c ty, bc (entry :: c) t in
293 C.ALetIn (id, name, v, ty, t)
294 | C.ALambda (id, name, w, t) ->
295 let name = mk_fresh_name c name in
296 let entry = Some (name, C.Decl (cic w)) in
297 let w, t = bc c w, bc (entry :: c) t in
298 C.ALambda (id, name, w, t)
299 | C.AProd (id, name, w, t) ->
300 let name = mk_fresh_name c name in
301 let entry = Some (name, C.Decl (cic w)) in
302 let w, t = bc c w, bc (entry :: c) t in
303 C.AProd (id, name, w, t)
304 | C.AAppl (id, vs) ->
305 let vs = List.map (bc c) vs in
307 | C.AMutCase (id, uri, tyno, u, v, ts) ->
308 let u, v, ts = bc c u, bc c v, List.map (bc c) ts in
309 C.AMutCase (id, uri, tyno, u, v, ts)
310 | C.ACast (id, t, u) ->
311 let t, u = bc c t, bc c u in
313 | C.AFix (id, i, fixes) ->
314 let d = add_entries get_fix_decl c fixes in
315 let bc_fix (id, sname, i, w, v) = (id, sname, i, bc c w, bc d v) in
316 let fixes = List.map bc_fix fixes in
317 C.AFix (id, i, fixes)
318 | C.ACoFix (id, i, cofixes) ->
319 let d = add_entries get_cofix_decl c cofixes in
320 let bc_cofix (id, sname, w, v) = (id, sname, bc c w, bc d v) in
321 let cofixes = List.map bc_cofix cofixes in
322 C.ACoFix (id, i, cofixes)
323 | C.ARel (id1, id2, i, sname) ->
324 let sname = get_sname c i in
325 C.ARel (id1, id2, i, sname)