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/.
26 module UM = UriManager
31 module E = CicEnvironment
32 module S = CicSubstitution
33 module TC = CicTypeChecker
35 module DTI = DoubleTypeInference
37 module PEH = ProofEngineHelpers
39 module Cl = ProceduralClassify
41 (* helper functions *********************************************************)
43 let rec list_map_cps g map = function
47 let g tl = g (hd :: tl) in
54 let comp f g x = f (g x)
57 try let t, _, _, _ = Rf.type_of_aux' [] c t Un.empty_ugraph in t
59 Printf.eprintf "REFINE EROR: %s\n" (Printexc.to_string e);
60 Printf.eprintf "Ref: context: %s\n" (Pp.ppcontext c);
61 Printf.eprintf "Ref: term : %s\n" (Pp.ppterm t);
65 try let ty, _ = TC.type_of_aux' [] c t Un.empty_ugraph in ty
67 Printf.eprintf "TC: context: %s\n" (Pp.ppcontext c);
68 Printf.eprintf "TC: term : %s\n" (Pp.ppterm t);
72 match PEH.split_with_whd (c, t) with
73 | (_, hd) :: _, _ -> hd
77 match get_tail c (get_type c (get_type c t)) with
78 | C.Sort C.Prop -> true
82 let is_not_atomic = function
88 | C.MutConstruct _ -> false
91 let get_ind_type uri tyno =
92 match E.get_obj Un.empty_ugraph uri with
93 | C.InductiveDefinition (tys, _, lpsno, _), _ -> lpsno, List.nth tys tyno
96 let get_default_eliminator context uri tyno ty =
97 let _, (name, _, _, _) = get_ind_type uri tyno in
98 let ext = match get_tail context (get_type context ty) with
99 | C.Sort C.Prop -> "_ind"
100 | C.Sort C.Set -> "_rec"
101 | C.Sort C.CProp -> "_rec"
102 | C.Sort (C.Type _) -> "_rect"
104 Printf.eprintf "CicPPP get_default_eliminator: %s\n" (Pp.ppterm t);
107 let buri = UM.buri_of_uri uri in
108 let uri = UM.uri_of_string (buri ^ "/" ^ name ^ ext ^ ".con") in
111 let get_ind_parameters c t =
112 let ty = get_type c t in
113 let ps = match get_tail c ty with
115 | C.Appl (C.MutInd _ :: args) -> args
118 let disp = match get_tail c (get_type c ty) with
125 (* term preprocessing: optomization 1 ***************************************)
127 let defined_premise = "DEFINED"
130 let name = C.Name defined_premise in
131 C.LetIn (name, v, C.Rel 1)
134 let rec aux k n = function
135 | C.Lambda (s, v, t) when k > 0 ->
136 C.Lambda (s, v, aux (pred k) n t)
137 | C.Lambda (_, _, t) when n > 0 ->
138 aux 0 (pred n) (S.lift (-1) t)
140 Printf.eprintf "CicPPP clear_absts: %u %s\n" n (Pp.ppterm t);
146 let rec add_abst k = function
147 | C.Lambda (s, v, t) when k > 0 -> C.Lambda (s, v, add_abst (pred k) t)
148 | t when k > 0 -> assert false
149 | t -> C.Lambda (C.Anonymous, C.Implicit None, S.lift 1 t)
151 let rec opt1_letin g es c name v t =
152 let entry = Some (name, C.Def (v, None)) in
154 if DTI.does_not_occur 1 t then begin
155 HLog.warn "Optimizer: remove 1"; g (S.lift (-1) t)
158 | C.LetIn (nname, vv, tt) when is_proof c v ->
159 let x = C.LetIn (nname, vv, C.LetIn (name, tt, S.lift_from 2 1 t)) in
160 HLog.warn "Optimizer: swap 1"; opt1_proof g false c x
162 g (C.LetIn (name, v, t))
164 if es then opt1_term g es c v else g v
166 if es then opt1_proof g es (entry :: c) t else g t
168 and opt1_lambda g es c name w t =
169 let entry = Some (name, C.Decl w) in
171 let name = if DTI.does_not_occur 1 t then C.Anonymous else name in
172 g (C.Lambda (name, w, t))
174 if es then opt1_proof g es (entry :: c) t else g t
176 and opt1_appl g es c t vs =
179 | C.LetIn (mame, vv, tt) ->
180 let vs = List.map (S.lift 1) vs in
181 let x = C.LetIn (mame, vv, C.Appl (tt :: vs)) in
182 HLog.warn "Optimizer: swap 2"; opt1_proof g false c x
183 | C.Lambda (name, ww, tt) ->
184 let v, vs = List.hd vs, List.tl vs in
185 let x = C.Appl (C.LetIn (name, v, tt) :: vs) in
186 HLog.warn "Optimizer: remove 2"; opt1_proof g false c x
188 let x = C.Appl (vvs @ vs) in
189 HLog.warn "Optimizer: nested application"; opt1_proof g false c x
191 let rec aux d rvs = function
193 let x = C.Appl (t :: List.rev rvs) in
194 if d then opt1_proof g false c x else g x
195 | v :: vs, (c, b) :: cs ->
196 if is_not_atomic v && I.S.mem 0 c && b then begin
197 HLog.warn "Optimizer: anticipate 1";
198 aux true (define c v :: rvs) (vs, cs)
200 aux d (v :: rvs) (vs, cs)
201 | _, [] -> assert false
204 let classes, conclusion = Cl.classify c (get_type c t) in
205 let csno, vsno = List.length classes, List.length vs in
206 if csno < vsno && csno > 0 then
207 let vvs, vs = HEL.split_nth csno vs in
208 let x = C.Appl (define c (C.Appl (t :: vvs)) :: vs) in
209 HLog.warn "Optimizer: anticipate 2"; opt1_proof g false c x
210 else match conclusion, List.rev vs with
211 | Some _, rv :: rvs when csno = vsno && is_not_atomic rv ->
212 let x = C.Appl (t :: List.rev rvs @ [define c rv]) in
213 HLog.warn "Optimizer: anticipate 3"; opt1_proof g false c x
217 if csno > 0 then aux false [] (vs, classes)
218 else g (C.Appl (t :: vs))
220 let rec aux h prev = function
221 | C.LetIn (name, vv, tt) :: vs ->
222 let t = S.lift 1 t in
223 let prev = List.map (S.lift 1) prev in
224 let vs = List.map (S.lift 1) vs in
225 let y = C.Appl (t :: List.rev prev @ tt :: vs) in
226 let x = C.LetIn (name, vv, y) in
227 HLog.warn "Optimizer: swap 3"; opt1_proof g false c x
228 | v :: vs -> aux h (v :: prev) vs
233 if es then opt1_proof g es c t else g t
235 if es then list_map_cps g (fun h -> opt1_term h es c) vs else g vs
237 and opt1_mutcase g es c uri tyno outty arg cases =
238 let eliminator = get_default_eliminator c uri tyno outty in
239 let lpsno, (_, _, _, constructors) = get_ind_type uri tyno in
240 let ps, sort_disp = get_ind_parameters c arg in
241 let lps, rps = HEL.split_nth lpsno ps in
242 let rpsno = List.length rps in
243 let predicate = clear_absts rpsno (1 - sort_disp) outty in
245 I.S.mem tyno (I.get_mutinds_of_uri uri t)
247 let map2 case (_, cty) =
248 let map (h, case, k) (_, premise) =
249 if h > 0 then pred h, case, k else
250 if is_recursive premise then
251 0, add_abst k case, k + 2
255 let premises, _ = PEH.split_with_whd (c, cty) in
256 let _, lifted_case, _ =
257 List.fold_left map (lpsno, case, 1) (List.rev (List.tl premises))
261 let lifted_cases = List.map2 map2 cases constructors in
262 let args = eliminator :: lps @ predicate :: lifted_cases @ rps @ [arg] in
263 let x = refine c (C.Appl args) in
264 HLog.warn "Optimizer: remove 3"; opt1_proof g es c x
266 and opt1_cast g es c t w =
267 let g t = HLog.warn "Optimizer: remove 4"; g t in
268 if es then opt1_proof g es c t else g t
270 and opt1_other g es c t = g t
272 and opt1_proof g es c = function
273 | C.LetIn (name, v, t) -> opt1_letin g es c name v t
274 | C.Lambda (name, w, t) -> opt1_lambda g es c name w t
275 | C.Appl (t :: v :: vs) -> opt1_appl g es c t (v :: vs)
276 | C.Appl [t] -> opt1_proof g es c t
277 | C.MutCase (u, n, t, v, ws) -> opt1_mutcase g es c u n t v ws
278 | C.Cast (t, w) -> opt1_cast g es c t w
279 | t -> opt1_other g es c t
281 and opt1_term g es c t =
282 if is_proof c t then opt1_proof g es c t else g t
284 (* term preprocessing: optomization 2 ***************************************)
286 let expanded_premise = "EXPANDED"
288 let eta_expand g tys t =
290 let name i = Printf.sprintf "%s%u" expanded_premise i in
291 let lambda i ty t = C.Lambda (C.Name (name i), ty, t) in
292 let arg i = C.Rel (succ i) in
293 let rec aux i f a = function
295 | (_, ty) :: tl -> aux (succ i) (comp f (lambda i ty)) (arg i :: a) tl
297 let n = List.length tys in
298 let absts, args = aux 0 identity [] tys in
299 let t = match S.lift n t with
300 | C.Appl ts -> C.Appl (ts @ args)
301 | t -> C.Appl (t :: args)
305 let rec opt2_letin g c name v t =
306 let entry = Some (name, C.Def (v, None)) in
308 let g v = g (C.LetIn (name, v, t)) in
311 opt2_proof g (entry :: c) t
313 and opt2_lambda g c name w t =
314 let entry = Some (name, C.Decl w) in
315 let g t = g (C.Lambda (name, w, t)) in
316 opt2_proof g (entry :: c) t
318 and opt2_appl g c t vs =
320 let x = C.Appl (t :: vs) in
321 let vsno = List.length vs in
322 let _, csno = PEH.split_with_whd (c, get_type c t) in
324 let tys, _ = PEH.split_with_whd (c, get_type c x) in
325 let tys = List.rev (List.tl tys) in
326 let tys, _ = HEL.split_nth (csno - vsno) tys in
327 HLog.warn "Optimizer: eta 1"; eta_expand g tys x
330 list_map_cps g (fun h -> opt2_term h c) vs
332 and opt2_other g c t =
333 let tys, csno = PEH.split_with_whd (c, get_type c t) in
334 if csno > 0 then begin
335 let tys = List.rev (List.tl tys) in
336 HLog.warn "Optimizer: eta 2"; eta_expand g tys t
339 and opt2_proof g c = function
340 | C.LetIn (name, v, t) -> opt2_letin g c name v t
341 | C.Lambda (name, w, t) -> opt2_lambda g c name w t
342 | C.Appl (t :: vs) -> opt2_appl g c t vs
343 | t -> opt2_other g c t
345 and opt2_term g c t =
346 if is_proof c t then opt2_proof g c t else g t
348 (* object preprocessing *****************************************************)
350 let pp_obj = function
351 | C.Constant (name, Some bo, ty, pars, attrs) ->
352 let g bo = C.Constant (name, Some bo, ty, pars, attrs) in
353 Printf.eprintf "BEGIN: %s\n" name;
354 begin try opt1_term (opt2_term g []) true [] bo
355 with e -> failwith ("PPP: " ^ Printexc.to_string e) end