]> matita.cs.unibo.it Git - helm.git/blob - components/acic_procedural/proceduralPreprocess.ml
d3e655da70ea6ee20f54674c8782bb5f0deed028
[helm.git] / components / acic_procedural / proceduralPreprocess.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 UM   = UriManager
27 module C    = Cic
28 module Pp   = CicPp
29 module Un   = CicUniv
30 module I    = CicInspect
31 module E    = CicEnvironment
32 module S    = CicSubstitution
33 module TC   = CicTypeChecker 
34 module Rf   = CicRefine
35 module DTI  = DoubleTypeInference
36 module HEL  = HExtlib
37 module PEH  = ProofEngineHelpers
38
39 (* helper functions *********************************************************)
40
41 let identity x = x
42
43 let comp f g x = f (g x)
44
45 let get_type c t =
46    try let ty, _ = TC.type_of_aux' [] c t Un.empty_ugraph in ty
47    with e -> 
48       Printf.eprintf "TC: context: %s\n" (Pp.ppcontext c);
49       Printf.eprintf "TC: term   : %s\n" (Pp.ppterm t);
50       raise e
51
52 let refine c t =
53    try let t, _, _, _ = Rf.type_of_aux' [] c t Un.empty_ugraph in t
54    with e -> 
55       Printf.eprintf "REFINE EROR: %s\n" (Printexc.to_string e);
56       Printf.eprintf "Ref: context: %s\n" (Pp.ppcontext c);
57       Printf.eprintf "Ref: term   : %s\n" (Pp.ppterm t);
58       raise e
59
60 let get_tail c t =
61    match PEH.split_with_whd (c, t) with
62       | (_, hd) :: _, _ -> hd
63       | _               -> assert false
64
65 let is_proof c t =
66    match get_tail c (get_type c (get_type c t)) with
67       | C.Sort C.Prop -> true
68       | C.Sort _      -> false
69       | _             -> assert false 
70
71 let is_not_atomic = function
72    | C.Sort _
73    | C.Rel _
74    | C.Const _
75    | C.Var _
76    | C.MutInd _ 
77    | C.MutConstruct _ -> false
78    | _                -> true
79
80 let clear_absts m =
81    let rec aux k n = function
82       | C.Lambda (s, v, t) when k > 0    -> 
83          C.Lambda (s, v, aux (pred k) n t)
84       | C.Lambda (_, _, t) when n > 0    -> 
85          aux 0 (pred n) (S.lift (-1) t)
86       | t                     when n > 0 ->
87          Printf.eprintf "CicPPP clear_absts: %u %s\n" n (Pp.ppterm t);
88          assert false 
89       | t                                 -> t
90    in 
91    aux m
92
93 let rec add_abst k = function 
94    | C.Lambda (s, v, t) when k > 0 -> C.Lambda (s, v, add_abst (pred k) t)
95    | t when k > 0 -> assert false
96    | t -> C.Lambda (C.Anonymous, C.Implicit None, S.lift 1 t)
97
98 let get_ind_type uri tyno =
99    match E.get_obj Un.empty_ugraph uri with
100       | C.InductiveDefinition (tys, _, lpsno, _), _ -> lpsno, List.nth tys tyno
101       | _                                           -> assert false
102
103 let get_ind_parameters c t =
104    let ty = get_type c t in
105    let ps = match get_tail c ty with
106       | C.MutInd _                  -> []
107       | C.Appl (C.MutInd _ :: args) -> args
108       | _                           -> assert false
109    in
110    let disp = match get_tail c (get_type c ty) with
111       | C.Sort C.Prop -> 0
112       | C.Sort _      -> 1
113       | _             -> assert false
114    in
115    ps, disp
116
117 let get_default_eliminator context uri tyno ty =
118    let _, (name, _, _, _) = get_ind_type uri tyno in
119    let ext = match get_tail context (get_type context ty) with
120       | C.Sort C.Prop     -> "_ind"
121       | C.Sort C.Set      -> "_rec"
122       | C.Sort C.CProp    -> "_rec"
123       | C.Sort (C.Type _) -> "_rect"
124       | t                 -> 
125          Printf.eprintf "CicPPP get_default_eliminator: %s\n" (Pp.ppterm t);
126          assert false
127    in
128    let buri = UM.buri_of_uri uri in
129    let uri = UM.uri_of_string (buri ^ "/" ^ name ^ ext ^ ".con") in
130    C.Const (uri, [])
131
132 let add g htbl t proof decurry =
133    if proof then C.CicHash.add htbl t decurry; 
134    g t proof decurry
135
136 let find g htbl t = 
137    try 
138       let decurry = C.CicHash.find htbl t in g t true decurry
139    with Not_found -> g t false 0
140
141 (* term preprocessing *******************************************************)
142
143 let expanded_premise = "EXPANDED"
144
145 let defined_premise = "DEFINED"
146
147 let eta_expand g tys t =
148    assert (tys <> []);
149    let name i = Printf.sprintf "%s%u" expanded_premise i in 
150    let lambda i ty t = C.Lambda (C.Name (name i), ty, t) in
151    let arg i = C.Rel (succ i) in
152    let rec aux i f a = function
153       | []            -> f, a 
154       | (_, ty) :: tl -> aux (succ i) (comp f (lambda i ty)) (arg i :: a) tl
155    in
156    let n = List.length tys in
157    let absts, args = aux 0 identity [] tys in
158    let t = match S.lift n t with
159       | C.Appl ts -> C.Appl (ts @ args)
160       | t         -> C.Appl (t :: args)
161    in
162    g (absts t)
163
164 let get_tys c decurry =
165    let rec aux n = function
166 (*      | C.Appl (hd :: tl) -> aux (n + List.length tl) hd *)
167       | t                 ->
168          let tys, _ = PEH.split_with_whd (c, get_type c t) in
169          let _, tys = HEL.split_nth n (List.rev tys) in
170          let tys, _ = HEL.split_nth decurry tys in
171          tys
172    in
173    aux 0
174
175 let eta_fix c t proof decurry =
176    let rec aux g c = function
177       | C.LetIn (name, v, t) ->
178          let g t = g (C.LetIn (name, v, t)) in
179          let entry = Some (name, C.Def (v, None)) in
180          aux g (entry :: c) t
181       | t                    -> eta_expand g (get_tys c decurry t) t 
182    in 
183    if proof && decurry > 0 then aux identity c t else t
184
185 let rec pp_cast g ht es c t v =
186    if true then pp_proof g ht es c t else find g ht t
187
188 and pp_lambda g ht es c name v t =
189    let name = if DTI.does_not_occur 1 t then C.Anonymous else name in
190    let entry = Some (name, C.Decl v) in
191    let g t _ decurry = 
192       let t = eta_fix (entry :: c) t true decurry in
193       g (C.Lambda (name, v, t)) true 0 in
194    if true then pp_proof g ht es (entry :: c) t else find g ht t
195
196 and pp_letin g ht es c name v t =
197    let entry = Some (name, C.Def (v, None)) in
198    let g t _ decurry =
199       if DTI.does_not_occur 1 t then g (S.lift (-1) t) true decurry else 
200       let g v proof d = match v with
201          | C.LetIn (mame, w, u) when proof ->
202             let x = C.LetIn (mame, w, C.LetIn (name, u, S.lift_from 2 1 t)) in
203             pp_proof g ht false c x 
204          | v                               -> 
205             let v = eta_fix c v proof d in
206             g (C.LetIn (name, v, t)) true decurry
207       in
208       if true then pp_term g ht es c v else find g ht v
209    in
210    if true then pp_proof g ht es (entry :: c) t else find g ht t
211
212 and pp_appl_one g ht es c t v =
213    let g t _ decurry =
214       let g v proof d =
215          match t, v with
216             | t, C.LetIn (mame, w, u) when proof ->
217                let x = C.LetIn (mame, w, C.Appl [S.lift 1 t; u]) in
218                pp_proof g ht false c x 
219             | C.LetIn (mame, w, u), v            ->
220                let x = C.LetIn (mame, w, C.Appl [u; S.lift 1 v]) in
221                pp_proof g ht false c x
222             | C.Appl ts, v when decurry > 0      ->
223                let v = eta_fix c v proof d in
224                g (C.Appl (List.append ts [v])) true (pred decurry)
225             | t, v  when is_not_atomic t         ->
226                let mame = C.Name defined_premise in
227                let x = C.LetIn (mame, t, C.Appl [C.Rel 1; S.lift 1 v]) in
228                pp_proof g ht false c x
229             | t, v                               ->
230                let v = eta_fix c v proof d in
231                g (C.Appl [t; v]) true (pred decurry)
232       in
233       if true then pp_term g ht es c v else find g ht v
234    in
235    if true then pp_proof g ht es c t else find g ht t
236
237 and pp_appl g ht es c t = function
238    | []             -> pp_proof g ht es c t
239    | [v]            -> pp_appl_one g ht es c t v
240    | v1 :: v2 :: vs ->
241       let x = C.Appl (C.Appl [t; v1] :: v2 :: vs) in 
242       pp_proof g ht es c x
243
244 and pp_atomic g ht es c t =
245    let _, premsno = PEH.split_with_whd (c, get_type c t) in
246    g t true premsno
247
248 and pp_mutcase g ht es c uri tyno outty arg cases =
249    let eliminator = get_default_eliminator c uri tyno outty in
250    let lpsno, (_, _, _, constructors) = get_ind_type uri tyno in
251    let ps, sort_disp = get_ind_parameters c arg in
252    let lps, rps = HEL.split_nth lpsno ps in
253    let rpsno = List.length rps in
254    let predicate = clear_absts rpsno (1 - sort_disp) outty in   
255    let is_recursive t =
256       I.S.mem tyno (I.get_mutinds_of_uri uri t) 
257    in
258    let map2 case (_, cty) = 
259       let map (h, case, k) (_, premise) = 
260          if h > 0 then pred h, case, k else
261          if is_recursive premise then 
262             0, add_abst k case, k + 2 
263          else
264             0, case, succ k
265       in
266       let premises, _ = PEH.split_with_whd (c, cty) in
267       let _, lifted_case, _ =
268          List.fold_left map (lpsno, case, 1) (List.rev (List.tl premises))
269       in
270       lifted_case
271    in
272    let lifted_cases = List.map2 map2 cases constructors in
273    let args = eliminator :: lps @ predicate :: lifted_cases @ rps @ [arg] in
274    let x = refine c (C.Appl args) in
275    pp_proof g ht es c x
276
277 and pp_proof g ht es c t = 
278 (*  Printf.eprintf "IN: |- %s\n" (*CicPp.ppcontext c*) (CicPp.ppterm t);
279   let g t proof decurry = 
280      Printf.eprintf "OUT: %b %u |- %s\n" proof decurry (CicPp.ppterm t);
281      g t proof decurry
282   in *)
283 (*   let g t proof decurry = add g ht t proof decurry in *)
284    match t with
285       | C.Cast (t, v)              -> pp_cast g ht es c t v
286       | C.Lambda (name, v, t)      -> pp_lambda g ht es c name v t
287       | C.LetIn (name, v, t)       -> pp_letin g ht es c name v t
288       | C.Appl (t :: vs)           -> pp_appl g ht es c t vs
289       | C.MutCase (u, n, t, v, ws) -> pp_mutcase g ht es c u n t v ws
290       | t                          -> pp_atomic g ht es c t
291
292 and pp_term g ht es c t =
293    if is_proof c t then pp_proof g ht es c t else g t false 0
294
295 (* object preprocessing *****************************************************)
296
297 let pp_obj = function
298    | C.Constant (name, Some bo, ty, pars, attrs) ->
299       let g bo proof decurry = 
300          let bo = eta_fix [] bo proof decurry in
301          C.Constant (name, Some bo, ty, pars, attrs)
302       in
303       let ht = C.CicHash.create 1 in
304       Printf.eprintf "BEGIN: %s\n" name;
305       begin try pp_term g ht true [] bo
306       with e -> failwith ("PPP: " ^ Printexc.to_string e) end
307    | obj                                         -> obj