]> matita.cs.unibo.it Git - helm.git/blob - components/acic_procedural/proceduralOptimizer.ml
ab15d70872aeccf94f99493eabc8511a577dbfb1
[helm.git] / components / acic_procedural / proceduralOptimizer.ml
1 (* Copyright (C) 2003-2005, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 module C    = Cic
27 module Pp   = CicPp
28 module I    = CicInspect
29 module S    = CicSubstitution
30 module DTI  = DoubleTypeInference
31 module HEL  = HExtlib
32 module PEH  = ProofEngineHelpers
33
34 module H    = ProceduralHelpers
35 module Cl   = ProceduralClassify
36
37 (* term preprocessing: optomization 1 ***************************************)
38
39 let defined_premise = "DEFINED"
40
41 let define v =
42    let name = C.Name defined_premise in
43    C.LetIn (name, v, C.Rel 1)
44
45 let clear_absts m =
46    let rec aux k n = function
47       | C.Lambda (s, v, t) when k > 0 -> 
48          C.Lambda (s, v, aux (pred k) n t)
49       | C.Lambda (_, _, t) when n > 0 -> 
50          aux 0 (pred n) (S.lift (-1) t)
51       | t                  when n > 0 ->
52          Printf.eprintf "CicPPP clear_absts: %u %s\n" n (Pp.ppterm t);
53          assert false 
54       | t                                 -> t
55    in 
56    aux m
57
58 let rec add_abst k = function 
59    | C.Lambda (s, v, t) when k > 0 -> C.Lambda (s, v, add_abst (pred k) t)
60    | t when k > 0 -> assert false
61    | t -> C.Lambda (C.Anonymous, C.Implicit None, S.lift 1 t)
62
63 let rec opt1_letin g es c name v t =
64    let name = H.mk_fresh_name c name in
65    let entry = Some (name, C.Def (v, None)) in
66    let g t =
67       if DTI.does_not_occur 1 t then begin          
68          let x = S.lift (-1) t in
69          HLog.warn "Optimizer: remove 1"; opt1_proof g true c x 
70       end else 
71       let g = function
72          | C.LetIn (nname, vv, tt) when H.is_proof c v ->
73             let x = C.LetIn (nname, vv, C.LetIn (name, tt, S.lift_from 2 1 t)) in
74             HLog.warn "Optimizer: swap 1"; opt1_proof g true c x 
75          | v                               -> 
76             g (C.LetIn (name, v, t))
77       in
78       if es then opt1_term g es c v else g v
79    in
80    if es then opt1_proof g es (entry :: c) t else g t
81
82 and opt1_lambda g es c name w t =
83    let name = H.mk_fresh_name c name in
84    let entry = Some (name, C.Decl w) in
85    let g t = g (C.Lambda (name, w, t)) in
86    if es then opt1_proof g es (entry :: c) t else g t
87
88 and opt1_appl g es c t vs =
89    let g vs = 
90       let g = function      
91          | C.LetIn (mame, vv, tt) ->
92             let vs = List.map (S.lift 1) vs in
93             let x = C.LetIn (mame, vv, C.Appl (tt :: vs)) in
94             HLog.warn "Optimizer: swap 2"; opt1_proof g true c x
95          | C.Lambda (name, ww, tt) ->
96             let v, vs = List.hd vs, List.tl vs in
97             let x = C.Appl (C.LetIn (name, v, tt) :: vs) in
98             HLog.warn "Optimizer: remove 2"; opt1_proof g true c x
99          | C.Appl vvs              ->
100             let x = C.Appl (vvs @ vs) in
101             HLog.warn "Optimizer: nested application"; opt1_proof g true c x
102          | t                       ->
103             let rec aux d rvs = function
104                | [], _                   -> 
105                   let x = C.Appl (t :: List.rev rvs) in
106                   if d then opt1_proof g true c x else g x
107                | v :: vs, (cc, bb) :: cs ->
108                   if H.is_not_atomic v && I.S.mem 0 cc && bb then begin 
109                      HLog.warn "Optimizer: anticipate 1"; 
110                      aux true (define v :: rvs) (vs, cs)
111                   end else 
112                      aux d (v :: rvs) (vs, cs)
113                | _, []                   -> assert false
114             in
115             let h () =
116                let classes, conclusion = Cl.classify c (H.get_type c t) in
117                let csno, vsno = List.length classes, List.length vs in
118                if csno < vsno then
119                   let vvs, vs = HEL.split_nth csno vs in
120                   let x = C.Appl (define (C.Appl (t :: vvs)) :: vs) in
121                   HLog.warn "Optimizer: anticipate 2"; opt1_proof g true c x
122                else match conclusion, List.rev vs with
123                   | Some _, rv :: rvs when csno = vsno && H.is_not_atomic rv ->
124                      let x = C.Appl (t :: List.rev rvs @ [define rv]) in
125                      HLog.warn "Optimizer: anticipate 3"; opt1_proof g true c x
126                   | _ (* Some _, _ *)                                             ->
127                      g (C.Appl (t :: vs))
128 (*                | None, _                                                ->
129                      aux false [] (vs, classes)
130 *)          in
131             let rec aux h prev = function
132                | C.LetIn (name, vv, tt) :: vs ->
133                   let t = S.lift 1 t in
134                   let prev = List.map (S.lift 1) prev in
135                   let vs = List.map (S.lift 1) vs in
136                   let y = C.Appl (t :: List.rev prev @ tt :: vs) in
137                   let x = C.LetIn (name, vv, y) in  
138                   HLog.warn "Optimizer: swap 3"; opt1_proof g true c x
139                | v :: vs                      -> aux h (v :: prev) vs
140                | []                           -> h ()
141             in 
142             aux h [] vs
143       in
144       if es then opt1_proof g es c t else g t
145    in
146    if es then H.list_map_cps g (fun h -> opt1_term h es c) vs else g vs
147
148 and opt1_mutcase g es c uri tyno outty arg cases =
149    let eliminator = H.get_default_eliminator c uri tyno outty in
150    let lpsno, (_, _, _, constructors) = H.get_ind_type uri tyno in
151    let ps, sort_disp = H.get_ind_parameters c arg in
152    let lps, rps = HEL.split_nth lpsno ps in
153    let rpsno = List.length rps in
154    let predicate = clear_absts rpsno (1 - sort_disp) outty in   
155    let is_recursive t =
156       I.S.mem tyno (I.get_mutinds_of_uri uri t) 
157    in
158    let map2 case (_, cty) = 
159       let map (h, case, k) (_, premise) = 
160          if h > 0 then pred h, case, k else
161          if is_recursive premise then 
162             0, add_abst k case, k + 2 
163          else
164             0, case, succ k
165       in
166       let premises, _ = PEH.split_with_whd (c, cty) in
167       let _, lifted_case, _ =
168          List.fold_left map (lpsno, case, 1) (List.rev (List.tl premises))
169       in
170       lifted_case
171    in
172    let lifted_cases = List.map2 map2 cases constructors in
173    let args = eliminator :: lps @ predicate :: lifted_cases @ rps @ [arg] in
174    let x = H.refine c (C.Appl args) in
175    HLog.warn "Optimizer: remove 3"; opt1_proof g es c x
176
177 and opt1_cast g es c t w =
178    let g t = HLog.warn "Optimizer: remove 4"; g t in
179    if es then  opt1_proof g es c t else g t
180
181 and opt1_other g es c t = g t 
182
183 and opt1_proof g es c = function 
184    | C.LetIn (name, v, t)       -> opt1_letin g es c name v t
185    | C.Lambda (name, w, t)      -> opt1_lambda g es c name w t
186    | C.Appl (t :: v :: vs)      -> opt1_appl g es c t (v :: vs)
187    | C.Appl [t]                 -> opt1_proof g es c t
188    | C.MutCase (u, n, t, v, ws) -> opt1_mutcase g es c u n t v ws
189    | C.Cast (t, w)              -> opt1_cast g es c t w
190    | t                          -> opt1_other g es c t
191
192 and opt1_term g es c t = 
193    if H.is_proof c t then opt1_proof g es c t else g t
194
195 (* term preprocessing: optomization 2 ***************************************)
196
197 let expanded_premise = "EXPANDED"
198
199 let eta_expand g tys t =
200    assert (tys <> []);
201    let name i = Printf.sprintf "%s%u" expanded_premise i in 
202    let lambda i ty t = C.Lambda (C.Name (name i), ty, t) in
203    let arg i = C.Rel (succ i) in
204    let rec aux i f a = function
205       | []            -> f, a 
206       | (_, ty) :: tl -> aux (succ i) (H.compose f (lambda i ty)) (arg i :: a) tl
207    in
208    let n = List.length tys in
209    let absts, args = aux 0 H.identity [] tys in
210    let t = match S.lift n t with
211       | C.Appl ts -> C.Appl (ts @ args)
212       | t         -> C.Appl (t :: args)
213    in
214    g (absts t)
215
216 let rec opt2_letin g c name v t =
217    let entry = Some (name, C.Def (v, None)) in
218    let g t = 
219       let g v = g (C.LetIn (name, v, t)) in
220       opt2_term g c v
221    in
222    opt2_proof g (entry :: c) t
223
224 and opt2_lambda g c name w t =
225    let entry = Some (name, C.Decl w) in
226    let g t = g (C.Lambda (name, w, t)) in
227    opt2_proof g (entry :: c) t
228
229 and opt2_appl g c t vs =
230    let g vs =
231       let x = C.Appl (t :: vs) in
232       let vsno = List.length vs in
233       let _, csno = PEH.split_with_whd (c, H.get_type c t) in
234       if vsno < csno then 
235          let tys, _ = PEH.split_with_whd (c, H.get_type c x) in
236          let tys = List.rev (List.tl tys) in
237          let tys, _ = HEL.split_nth (csno - vsno) tys in
238          HLog.warn "Optimizer: eta 1"; eta_expand g tys x
239       else g x 
240    in
241    H.list_map_cps g (fun h -> opt2_term h c) vs
242
243 and opt2_other g c t =
244    let tys, csno = PEH.split_with_whd (c, H.get_type c t) in
245    if csno > 0 then begin
246       let tys = List.rev (List.tl tys) in      
247       HLog.warn "Optimizer: eta 2"; eta_expand g tys t 
248    end else g t
249
250 and opt2_proof g c = function 
251    | C.LetIn (name, v, t)  -> opt2_letin g c name v t
252    | C.Lambda (name, w, t) -> opt2_lambda g c name w t
253    | C.Appl (t :: vs)      -> opt2_appl g c t vs
254    | t                     -> opt2_other g c t
255
256 and opt2_term g c t = 
257    if H.is_proof c t then opt2_proof g c t else g t
258
259 (* object preprocessing *****************************************************)
260
261 let optimize_obj = function
262    | C.Constant (name, Some bo, ty, pars, attrs) ->
263       let g bo = 
264          Printf.eprintf "Optimized: %s\n" (Pp.ppterm bo);
265          let _ = H.get_type [] (C.Cast (bo, ty)) in
266          C.Constant (name, Some bo, ty, pars, attrs)
267       in
268       Printf.eprintf "BEGIN: %s\n" name;
269       begin try opt1_term g (* (opt2_term g []) *) true [] bo
270       with e -> failwith ("PPP: " ^ Printexc.to_string e) end
271    | obj                                         -> obj