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
30 module E = CicEnvironment
31 module S = CicSubstitution
32 module DTI = DoubleTypeInference
34 module PEH = ProofEngineHelpers
35 module TC = CicTypeChecker
40 module H = ProceduralHelpers
41 module Cl = ProceduralClassify
43 (* debugging ****************************************************************)
47 (* term optimization ********************************************************)
54 let info st str = {st with info = st.info ^ str ^ "\n"}
56 let defined_premise = "LOCAL"
59 let name = C.Name defined_premise in
60 let ty = H.get_type "define" c v in
61 C.LetIn (name, v, ty, C.Rel 1)
64 let rec aux k n = function
65 | C.Lambda (s, v, t) when k > 0 ->
66 C.Lambda (s, v, aux (pred k) n t)
67 | C.Lambda (_, _, t) when n > 0 ->
68 aux 0 (pred n) (S.lift (-1) t)
70 Printf.eprintf "PO.clear_absts: %u %s\n" n (Pp.ppterm t);
76 let rec add_abst k = function
77 | C.Lambda (s, v, t) when k > 0 -> C.Lambda (s, v, add_abst (pred k) t)
78 | t when k > 0 -> assert false
79 | t -> C.Lambda (C.Anonymous, C.Implicit None, S.lift 1 t)
81 let rec opt_letin g st es c name v w t =
82 let name = H.mk_fresh_name c name in
83 let entry = Some (name, C.Def (v, w)) in
85 if DTI.does_not_occur 1 t then
86 let x = S.lift (-1) t in
87 opt_proof g (info st "Optimizer: remove 1") true c x
90 | C.LetIn (nname, vv, ww, tt) when H.is_proof c v ->
91 let eentry = Some (nname, C.Def (vv, ww)) in
92 let ttw = H.get_type "opt_letin 1" (eentry :: c) tt in
93 let x = C.LetIn (nname, vv, ww,
94 C.LetIn (name, tt, ttw, S.lift_from 2 1 t))
96 opt_proof g (info st "Optimizer: swap 1") true c x
97 | v when H.is_proof c v && H.is_atomic v ->
98 let x = S.subst v t in
99 opt_proof g (info st "Optimizer: remove 5") true c x
101 g st (C.LetIn (name, v, w, t))
103 if es then opt_term g st es c v else g st v
105 if es then opt_proof g st es (entry :: c) t else g st t
107 and opt_lambda g st es c name w t =
108 let name = H.mk_fresh_name c name in
109 let entry = Some (name, C.Decl w) in
110 let g st t = g st (C.Lambda (name, w, t)) in
111 if es then opt_proof g st es (entry :: c) t else g st t
113 and opt_appl g st es c t vs =
116 | C.LetIn (mame, vv, tyty, tt) ->
117 let vs = List.map (S.lift 1) vs in
118 let x = C.LetIn (mame, vv, tyty, C.Appl (tt :: vs)) in
119 opt_proof g (info st "Optimizer: swap 2") true c x
120 | C.Lambda (name, ww, tt) ->
121 let v, vs = List.hd vs, List.tl vs in
122 let w = H.get_type "opt_appl 1" c v in
123 let x = C.Appl (C.LetIn (name, v, w, tt) :: vs) in
124 opt_proof g (info st "Optimizer: remove 2") true c x
126 let x = C.Appl (vvs @ vs) in
127 opt_proof g (info st "Optimizer: nested application") true c x
130 let rec aux st d rvs = function
132 let x = C.Appl (t :: List.rev rvs) in
133 if d then opt_proof g st true c x else g st x
134 | v :: vs, (cc, bb) :: cs ->
135 if H.is_not_atomic v && I.S.mem 0 cc && bb then
136 aux (st info "Optimizer: anticipate 1") true
137 (define c v :: rvs) (vs, cs)
139 aux st d (v :: rvs) (vs, cs)
140 | _, [] -> assert false
144 let classes, conclusion = Cl.classify c (H.get_type "opt_appl 3" c t) in
145 let csno, vsno = List.length classes, List.length vs in
147 let vvs, vs = HEL.split_nth csno vs in
148 let x = C.Appl (define c (C.Appl (t :: vvs)) :: vs) in
149 opt_proof g (info st "Optimizer: anticipate 2") true c x
150 else match conclusion, List.rev vs with
151 | Some _, rv :: rvs when csno = vsno && H.is_not_atomic rv ->
152 let x = C.Appl (t :: List.rev rvs @ [define c rv]) in
153 opt_proof g (info st "Optimizer: anticipate 3";) true c x
154 | _ (* Some _, _ *) ->
155 g st (C.Appl (t :: vs))
157 aux false [] (vs, classes)
159 let rec aux h st prev = function
160 | C.LetIn (name, vv, tyty, tt) :: vs ->
161 let t = S.lift 1 t in
162 let prev = List.map (S.lift 1) prev in
163 let vs = List.map (S.lift 1) vs in
164 let y = C.Appl (t :: List.rev prev @ tt :: vs) in
165 let ww = H.get_type "opt_appl 2" c vv in
166 let x = C.LetIn (name, vv, ww, y) in
167 opt_proof g (info st "Optimizer: swap 3") true c x
168 | v :: vs -> aux h st (v :: prev) vs
173 if es then opt_proof g st es c t else g st t
175 let map h v (st, vs) =
176 let h st vv = h (st, vv :: vs) in opt_term h st es c v
178 if es then H.list_fold_right_cps g map vs (st, []) else g (st, vs)
180 and opt_mutcase g st es c uri tyno outty arg cases =
181 let eliminator = H.get_default_eliminator c uri tyno outty in
182 let lpsno, (_, _, _, constructors) = H.get_ind_type uri tyno in
183 let ps, sort_disp = H.get_ind_parameters c arg in
184 let lps, rps = HEL.split_nth lpsno ps in
185 let rpsno = List.length rps in
186 let predicate = clear_absts rpsno (1 - sort_disp) outty in
188 I.S.mem tyno (I.get_mutinds_of_uri uri t)
190 let map2 case (_, cty) =
191 let map (h, case, k) (_, premise) =
192 if h > 0 then pred h, case, k else
193 if is_recursive premise then
194 0, add_abst k case, k + 2
198 let premises, _ = PEH.split_with_whd (c, cty) in
199 let _, lifted_case, _ =
200 List.fold_left map (lpsno, case, 1) (List.rev (List.tl premises))
204 let lifted_cases = List.map2 map2 cases constructors in
205 let args = eliminator :: lps @ predicate :: lifted_cases @ rps @ [arg] in
206 let x = H.refine c (C.Appl args) in
207 opt_proof g (info st "Optimizer: remove 3") es c x
209 and opt_cast g st es c t w =
210 let g st t = g (info st "Optimizer: remove 4") t in
211 if es then opt_proof g st es c t else g st t
213 and opt_other g st es c t = g st t
215 and opt_proof g st es c = function
216 | C.LetIn (name, v, ty, t) -> opt_letin g st es c name v ty t
217 | C.Lambda (name, w, t) -> opt_lambda g st es c name w t
218 | C.Appl (t :: v :: vs) -> opt_appl g st es c t (v :: vs)
219 | C.Appl [t] -> opt_proof g st es c t
220 | C.MutCase (u, n, t, v, ws) -> opt_mutcase g st es c u n t v ws
221 | C.Cast (t, w) -> opt_cast g st es c t w
222 | t -> opt_other g st es c t
224 and opt_term g st es c t =
225 if H.is_proof c t then opt_proof g st es c t else g st t
227 (* object optimization ******************************************************)
230 try opt_term g st true c bo
232 | E.Object_not_found uri ->
233 let msg = "optimize_obj: object not found: " ^ UM.string_of_uri uri in
236 let msg = "optimize_obj: " ^ Printexc.to_string e in
239 let optimize_obj = function
240 | C.Constant (name, Some bo, ty, pars, attrs) ->
241 let st, c = {info = ""; dummy = ()}, [] in
242 let bo, ty = H.cic_bc c bo, H.cic_bc c ty in
245 Printf.eprintf "Optimized : %s\n" (Pp.ppterm bo);
246 prerr_string "Ut.pp_term : ";
247 Ut.pp_term prerr_string [] c bo; prerr_newline ()
249 (* let _ = H.get_type "opt" [] (C.Cast (bo, ty)) in *)
250 let nodes = Printf.sprintf "Optimized nodes: %u" (I.count_nodes 0 bo) in
251 let st = info st nodes in
252 L.time_stamp ("PO: DONE " ^ name);
253 C.Constant (name, Some bo, ty, pars, attrs), st.info
255 L.time_stamp ("PO: OPTIMIZING " ^ name);
256 if !debug then Printf.eprintf "BEGIN: %s\n" name;
257 let nodes = Printf.sprintf "Initial nodes: %u" (I.count_nodes 0 bo) in
258 wrap g (info st nodes) c bo
261 let optimize_term c bo =
262 let st = {info = ""; dummy = ()} in
263 let bo = H.cic_bc c bo in
264 let g st bo = bo, st.info in