]> matita.cs.unibo.it Git - helm.git/blob - components/acic_procedural/proceduralConversion.ml
80aae9e7f3668b83fcee3378d7cb148174b5c31c
[helm.git] / 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 D    = Deannotate
31 module UM   = UriManager
32 module Rd   = CicReduction
33
34 (* helpers ******************************************************************)
35
36 let cic = D.deannotate_term
37
38 let rec list_sub start length = function
39    | _  :: tl when start  > 0 -> list_sub (pred start) length tl
40    | hd :: tl when length > 0 -> hd :: list_sub start (pred length) tl
41    | _                        -> []
42     
43 (* proof construction *******************************************************)
44
45 let lift k n =
46    let rec lift_xns k (uri, t) = uri, lift_term k t
47    and lift_ms k = function
48       | None   -> None
49       | Some t -> Some (lift_term k t)
50    and lift_fix len k (id, name, i, ty, bo) =
51       id, name, i, lift_term k ty, lift_term (k + len) bo
52    and lift_cofix len k (id, name, ty, bo) =
53       id, name, lift_term k ty, lift_term (k + len) bo
54    and lift_term k = function
55       | C.ASort _ as t -> t
56       | C.AImplicit _ as t -> t
57       | C.ARel (id, rid, m, b) as t -> 
58          if m < k then t else 
59          if m + n > 0 then C.ARel (id, rid, m + n, b) else
60          assert false
61       | C.AConst (id, uri, xnss) -> C.AConst (id, uri, List.map (lift_xns k) xnss)
62       | C.AVar (id, uri, xnss) -> C.AVar (id, uri, List.map (lift_xns k) xnss)
63       | C.AMutInd (id, uri, tyno, xnss) -> C.AMutInd (id, uri, tyno, List.map (lift_xns k) xnss)
64       | C.AMutConstruct (id, uri, tyno, consno, xnss) -> C.AMutConstruct (id, uri,tyno,consno, List.map (lift_xns k) xnss)
65       | C.AMeta (id, i, mss) -> C.AMeta(id, i, List.map (lift_ms k) mss)
66       | C.AAppl (id, ts) -> C.AAppl (id, List.map (lift_term k) ts)
67       | C.ACast (id, te, ty) -> C.ACast (id, lift_term k te, lift_term k ty)
68       | C.AMutCase (id, sp, i, outty, t, pl) -> C.AMutCase (id, sp, i, lift_term k outty, lift_term k t, List.map (lift_term k) pl)
69       | C.AProd (id, n, s, t) -> C.AProd (id, n, lift_term k s, lift_term (succ k) t)
70       | C.ALambda (id, n, s, t) -> C.ALambda (id, n, lift_term k s, lift_term (succ k) t)
71       | C.ALetIn (id, n, s, t) -> C.ALetIn (id, n, lift_term k s, lift_term (succ k) t)
72       | C.AFix (id, i, fl) -> C.AFix (id, i, List.map (lift_fix (List.length fl) k) fl)
73       | C.ACoFix (id, i, fl) -> C.ACoFix (id, i, List.map (lift_cofix (List.length fl) k) fl)
74    in
75    lift_term k
76
77 let clear_absts m =
78    let rec aux k n = function
79       | C.AImplicit (_, None) as t         -> t
80       | C.ALambda (id, s, v, t) when k > 0 -> 
81          C.ALambda (id, s, v, aux (pred k) n t)
82       | C.ALambda (_, _, _, t) when n > 0  -> 
83          aux 0 (pred n) (lift 1 (-1) t)
84       | t                      when n > 0  ->
85             Printf.eprintf "CLEAR: %u %s\n" n (CicPp.ppterm (cic t));
86             assert false 
87       | t                                  -> t
88    in 
89    aux m
90
91 let hole id = C.AImplicit (id, Some `Hole)
92
93 let meta id = C.AImplicit (id, None)
94
95 let anon = C.Anonymous
96
97 let generalize n =
98    let is_meta =
99       let map b = function
100          | C.AImplicit (_, None) when b -> b
101          | _                            -> false
102       in
103       List.fold_left map true
104    in
105    let rec gen_fix len k (id, name, i, ty, bo) =
106       id, name, i, gen_term k ty, gen_term (k + len) bo
107    and gen_cofix len k (id, name, ty, bo) =
108       id, name, gen_term k ty, gen_term (k + len) bo
109    and gen_term k = function
110       | C.ASort (id, _) 
111       | C.AImplicit (id, _)
112       | C.AConst (id, _, _)
113       | C.AVar (id, _, _)
114       | C.AMutInd (id, _, _, _)
115       | C.AMutConstruct (id, _, _, _, _)
116       | C.AMeta (id, _, _) -> meta id
117       | C.ARel (id, _, m, _) -> 
118          if succ (k - n) <= m && m <= k then hole id else meta id
119       | C.AAppl (id, ts) -> 
120          let ts = List.map (gen_term k) ts in
121          if is_meta ts then meta id else C.AAppl (id, ts)
122       | C.ACast (id, te, ty) -> 
123          let te, ty = gen_term k te, gen_term k ty in
124          if is_meta [te; ty] then meta id else C.ACast (id, te, ty)
125       | C.AMutCase (id, sp, i, outty, t, pl) ->         
126          let outty, t, pl = gen_term k outty, gen_term k t, List.map (gen_term k) pl in
127          if is_meta (outty :: t :: pl) then meta id else hole id (* C.AMutCase (id, sp, i, outty, t, pl) *)
128       | C.AProd (id, _, s, t) -> 
129          let s, t = gen_term k s, gen_term (succ k) t in
130          if is_meta [s; t] then meta id else C.AProd (id, anon, s, t)
131       | C.ALambda (id, _, s, t) ->
132          let s, t = gen_term k s, gen_term (succ k) t in
133          if is_meta [s; t] then meta id else C.ALambda (id, anon, s, t)
134       | C.ALetIn (id, _, s, t) -> 
135          let s, t = gen_term k s, gen_term (succ k) t in
136          if is_meta [s; t] then meta id else C.ALetIn (id, anon, s, t)
137       | C.AFix (id, i, fl) -> C.AFix (id, i, List.map (gen_fix (List.length fl) k) fl)
138       | C.ACoFix (id, i, fl) -> C.ACoFix (id, i, List.map (gen_cofix (List.length fl) k) fl)
139    in
140    gen_term 0
141
142 let mk_pattern psno predicate =
143    let body = generalize psno predicate in
144    clear_absts 0 psno body