]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/acic_procedural/proceduralConversion.ml
Preparing for 0.5.9 release.
[helm.git] / helm / software / components / acic_procedural / proceduralConversion.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 E    = CicEnvironment
28 module Un   = CicUniv
29 module TC   = CicTypeChecker
30 module UM   = UriManager
31 module Rd   = CicReduction
32 module PEH  = ProofEngineHelpers
33 module PT   = PrimitiveTactics
34 module DTI  = DoubleTypeInference
35
36 module H    = ProceduralHelpers
37
38 (* helpers ******************************************************************)
39
40 let rec list_sub start length = function
41    | _  :: tl when start  > 0 -> list_sub (pred start) length tl
42    | hd :: tl when length > 0 -> hd :: list_sub start (pred length) tl
43    | _                        -> []
44     
45 (* proof construction *******************************************************)
46
47 let iter f k =
48    let rec iter_xns k (uri, t) = uri, iter_term k t
49    and iter_ms k = function
50       | None   -> None
51       | Some t -> Some (iter_term k t)
52    and iter_fix len k (id, name, i, ty, bo) =
53       id, name, i, iter_term k ty, iter_term (k + len) bo
54    and iter_cofix len k (id, name, ty, bo) =
55       id, name, iter_term k ty, iter_term (k + len) bo
56    and iter_term k = function
57       | C.ASort _ as t -> t
58       | C.AImplicit _ as t -> t
59       | C.ARel (id, rid, m, b) as t -> 
60          if m < k then t else f k id rid m b
61       | C.AConst (id, uri, xnss) -> C.AConst (id, uri, List.map (iter_xns k) xnss)
62       | C.AVar (id, uri, xnss) -> C.AVar (id, uri, List.map (iter_xns k) xnss)
63       | C.AMutInd (id, uri, tyno, xnss) -> C.AMutInd (id, uri, tyno, List.map (iter_xns k) xnss)
64       | C.AMutConstruct (id, uri, tyno, consno, xnss) -> C.AMutConstruct (id, uri,tyno,consno, List.map (iter_xns k) xnss)
65       | C.AMeta (id, i, mss) -> C.AMeta(id, i, List.map (iter_ms k) mss)
66       | C.AAppl (id, ts) -> C.AAppl (id, List.map (iter_term k) ts)
67       | C.ACast (id, te, ty) -> C.ACast (id, iter_term k te, iter_term k ty)
68       | C.AMutCase (id, sp, i, outty, t, pl) -> C.AMutCase (id, sp, i, iter_term k outty, iter_term k t, List.map (iter_term k) pl)
69       | C.AProd (id, n, s, t) -> C.AProd (id, n, iter_term k s, iter_term (succ k) t)
70       | C.ALambda (id, n, s, t) -> C.ALambda (id, n, iter_term k s, iter_term (succ k) t)
71       | C.ALetIn (id, n, ty, s, t) -> C.ALetIn (id, n, iter_term k ty, iter_term k s, iter_term (succ k) t)
72       | C.AFix (id, i, fl) -> C.AFix (id, i, List.map (iter_fix (List.length fl) k) fl)
73       | C.ACoFix (id, i, fl) -> C.ACoFix (id, i, List.map (iter_cofix (List.length fl) k) fl)
74    in
75    iter_term k
76
77 let lift k n =
78    let f _ id rid m b =
79       if m + n > 0 then C.ARel (id, rid, m + n, b) else
80       begin 
81          HLog.error (Printf.sprintf "ProceduralConversion.lift: %i %i" m n);
82          assert false
83       end
84    in
85    iter f k
86
87 let subst k v =
88    let f k id rid m b =
89       if m = k then lift 1 (pred k) v else C.ARel (id, rid, pred m, b)
90    in
91    iter f k
92
93 let fake_annotate id c =
94    let get_binder c m =
95       try match List.nth c (pred m) with
96          | Some (C.Name s, _) -> s
97          | _ -> assert false
98       with
99          | Invalid_argument _ -> assert false
100    in
101    let mk_decl n v = Some (n, C.Decl v) in
102    let mk_def n v ty = Some (n, C.Def (v, ty)) in
103    let mk_fix (name, _, ty, bo) = mk_def (C.Name name) bo ty in
104    let mk_cofix (name, ty, bo) = mk_def (C.Name name) bo ty in
105    let rec ann_xns c (uri, t) = uri, ann_term c t
106    and ann_ms c = function
107       | None -> None
108       | Some t -> Some (ann_term c t)
109    and ann_fix newc c (name, i, ty, bo) =
110       id, name, i, ann_term c ty, ann_term (List.rev_append newc c) bo
111    and ann_cofix newc c (name, ty, bo) =
112       id, name, ann_term c ty, ann_term (List.rev_append newc c) bo
113    and ann_term c = function
114       | C.Sort sort -> C.ASort (id, sort)
115       | C.Implicit ann -> C.AImplicit (id, ann)
116       | C.Rel m -> C.ARel (id, id, m, get_binder c m)
117       | C.Const (uri, xnss) -> C.AConst (id, uri, List.map (ann_xns c) xnss)
118       | C.Var (uri, xnss) -> C.AVar (id, uri, List.map (ann_xns c) xnss)
119       | C.MutInd (uri, tyno, xnss) -> C.AMutInd (id, uri, tyno, List.map (ann_xns c) xnss)
120       | C.MutConstruct (uri, tyno, consno, xnss) -> C.AMutConstruct (id, uri,tyno,consno, List.map (ann_xns c) xnss)
121       | C.Meta (i, mss) -> C.AMeta(id, i, List.map (ann_ms c) mss)
122       | C.Appl ts -> C.AAppl (id, List.map (ann_term c) ts)
123       | C.Cast (te, ty) -> C.ACast (id, ann_term c te, ann_term c ty)
124       | C.MutCase (sp, i, outty, t, pl) -> C.AMutCase (id, sp, i, ann_term c outty, ann_term c t, List.map (ann_term c) pl)
125       | C.Prod (n, s, t) -> C.AProd (id, n, ann_term c s, ann_term (mk_decl n s :: c) t)
126       | C.Lambda (n, s, t) -> C.ALambda (id, n, ann_term c s, ann_term (mk_decl n s :: c) t)
127       | C.LetIn (n, s, ty, t) -> C.ALetIn (id, n, ann_term c s, ann_term c ty, ann_term (mk_def n s ty :: c) t)
128       | C.Fix (i, fl) -> C.AFix (id, i, List.map (ann_fix (List.rev_map mk_fix fl) c) fl)
129       | C.CoFix (i, fl) -> C.ACoFix (id, i, List.map (ann_cofix (List.rev_map mk_cofix fl) c) fl)
130    in
131    ann_term c
132
133 let mk_arel k = C.ARel ("", "", k, "")
134
135 let mk_aappl ts = C.AAppl ("", ts)
136
137 let rec clear_absts f n k = function
138    | t when n = 0           -> f k t
139    | C.ALambda (_, _, _, t) -> clear_absts f (pred n) (succ k) t
140    | t                      ->
141       let u = match mk_aappl [lift (succ k) 1 t; mk_arel (succ k)] with
142          | C.AAppl (_, [ C.AAppl (id, ts); t]) -> C.AAppl (id, ts @ [t])
143          | t                                   -> t
144       in
145       clear_absts f (pred n) (succ k) u
146
147 let hole id = C.AImplicit (id, Some `Hole)
148
149 let meta id = C.AImplicit (id, None)
150
151 let anon = C.Anonymous
152
153 let generalize n =
154    let is_meta =
155       let map b = function
156          | C.AImplicit (_, None) when b -> b
157          | _                            -> false
158       in
159       List.fold_left map true
160    in
161    let rec gen_fix len k (id, name, i, ty, bo) =
162       id, name, i, gen_term k ty, gen_term (k + len) bo
163    and gen_cofix len k (id, name, ty, bo) =
164       id, name, gen_term k ty, gen_term (k + len) bo
165    and gen_term k = function
166       | C.ASort (id, _) 
167       | C.AImplicit (id, _)
168       | C.AConst (id, _, _)
169       | C.AVar (id, _, _)
170       | C.AMutInd (id, _, _, _)
171       | C.AMutConstruct (id, _, _, _, _)
172       | C.AMeta (id, _, _) -> meta id
173       | C.ARel (id, _, m, _) -> 
174          if succ (k - n) <= m && m <= k then hole id else meta id
175       | C.AAppl (id, ts) -> 
176          let ts = List.map (gen_term k) ts in
177          if is_meta ts then meta id else C.AAppl (id, ts)
178       | C.ACast (id, te, ty) -> 
179          let te, ty = gen_term k te, gen_term k ty in
180          if is_meta [te; ty] then meta id else C.ACast (id, te, ty)
181       | C.AMutCase (id, sp, i, outty, t, pl) ->         
182          let outty, t, pl = gen_term k outty, gen_term k t, List.map (gen_term k) pl in
183          if is_meta (outty :: t :: pl) then meta id else hole id (* C.AMutCase (id, sp, i, outty, t, pl) *)
184       | C.AProd (id, _, s, t) -> 
185          let s, t = gen_term k s, gen_term (succ k) t in
186          if is_meta [s; t] then meta id else C.AProd (id, anon, s, t)
187       | C.ALambda (id, _, s, t) ->
188          let s, t = gen_term k s, gen_term (succ k) t in
189          if is_meta [s; t] then meta id else C.ALambda (id, anon, s, t)
190       | C.ALetIn (id, _, s, ty, t) -> 
191          let s, ty, t = gen_term k s, gen_term k ty, gen_term (succ k) t in
192          if is_meta [s; t] then meta id else C.ALetIn (id, anon, s, ty, t)
193       | C.AFix (id, i, fl) -> C.AFix (id, i, List.map (gen_fix (List.length fl) k) fl)
194       | C.ACoFix (id, i, fl) -> C.ACoFix (id, i, List.map (gen_cofix (List.length fl) k) fl)
195    in
196    gen_term
197
198 let convert g ity k predicate =
199    let rec aux = function
200       | C.ALambda (_, _, b, ity), C.ALambda (id, n, u, pred) ->
201          C.ALambda (id, n, aux (b, u), aux (ity, pred))
202       | C.AProd (_, _, b, ity), C.AProd (id, n, u, pred) ->
203          C.AProd (id, n, aux (b, u), aux (ity, pred))
204       | C.ALetIn (_, _, a, b, ity), C.ALetIn (id, n, v, u, pred) ->
205          C.ALetIn (id, n, aux (a, v), aux (b, u), aux (ity, pred))
206       | C.AAppl (_, bs), C.AAppl (id, us) when List.length bs = List.length us ->
207          let map b u = aux (b,u) in
208          C.AAppl (id, List.map2 map bs us)
209       | C.ACast (_, ity, b), C.ACast (id, pred, u) ->
210          C.ACast (id, aux (ity, pred), aux (b, u))
211       | ity, C.AAppl (_, C.ALambda (_, _, _, pred) :: v :: []) ->
212          aux (ity, subst 1 v pred)       
213       | ity, C.AAppl (id, C.ALambda (_, _, _, pred) :: v :: vs) ->
214          aux (ity, C.AAppl (id, subst 1 v pred :: vs))
215       | _, pred                                                 -> pred
216    in
217    g k (aux (ity, predicate))
218
219 let mk_pattern psno ity predicate =
220    clear_absts (convert (generalize psno) ity) psno 0 predicate 
221
222 let beta v = function
223    | C.ALambda (_, _, _, t) -> subst 1 v t
224    | _                      -> assert false
225
226 let get_clears c p xtypes = 
227    let meta = C.Implicit None in
228    let rec aux c names p it et = function
229       | []                                                -> 
230          List.rev c, List.rev names         
231       | Some (C.Name name as n, C.Decl v) as hd :: tl     ->
232          let hd, names, v = 
233             if DTI.does_not_occur 1 p && DTI.does_not_occur 1 it && DTI.does_not_occur 1 et then 
234                Some (C.Anonymous, C.Decl v), name :: names, meta 
235             else 
236                hd, names, v
237          in
238          let p = C.Lambda (n, v, p) in
239          let it = C.Prod (n, v, it) in
240          let et = C.Prod (n, v, et) in
241          aux (hd :: c) names p it et tl
242       | Some (C.Name name as n, C.Def (v, x)) as hd :: tl ->
243          let hd, names, v = 
244             if DTI.does_not_occur 1 p && DTI.does_not_occur 1 it && DTI.does_not_occur 1 et then 
245                Some (C.Anonymous, C.Def (v, x)), name :: names, meta
246             else 
247                hd, names, v
248          in
249          let p = C.LetIn (n, v, x, p) in
250          let it = C.LetIn (n, v, x, it) in
251          let et = C.LetIn (n, v, x, et) in
252          aux (hd :: c) names p it et tl
253       | Some (C.Anonymous as n, C.Decl v) as hd :: tl     ->
254          let p = C.Lambda (n, meta, p) in
255          let it = C.Lambda (n, meta, it) in
256          let et = C.Lambda (n, meta, et) in
257          aux (hd :: c) names p it et tl
258       | Some (C.Anonymous as n, C.Def (v, _)) as hd :: tl ->
259          let p = C.LetIn (n, meta, meta, p) in
260          let it = C.LetIn (n, meta, meta, it) in
261          let et = C.LetIn (n, meta, meta, et) in
262          aux (hd :: c) names p it et tl
263       | None :: tl                                        -> assert false
264    in
265    match xtypes with 
266       | Some (it, et) -> aux [] [] p it et c
267       | None          -> c, []
268
269 let clear c hyp =
270    let rec aux c = function
271       | []            -> List.rev c
272       | Some (C.Name name, entry) :: tail when name = hyp ->
273          aux (Some (C.Anonymous, entry) :: c) tail
274       | entry :: tail -> aux (entry :: c) tail
275    in
276    aux [] c
277 (*
278 let elim_inferred_type context goal arg using cpattern =
279    let metasenv, ugraph = [], Un.default_ugraph in
280    let ety = H.get_type "elim_inferred_type" context using in
281    let _splits, args_no = PEH.split_with_whd (context, ety) in
282    let _metasenv, _subst, predicate, _arg, actual_args = 
283      PT.mk_predicate_for_elim 
284      ~context ~metasenv ~subst:[] ~ugraph ~goal ~arg ~using ~cpattern ~args_no
285    in
286    let ty = C.Appl (predicate :: actual_args) in
287    let upto = List.length actual_args in
288    Rd.head_beta_reduce ~delta:false ~upto ty
289 *)
290 let does_not_occur = function
291    | C.AImplicit (_, None) -> true
292    | _                     -> false