]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_omdoc/eta_fixing.ml
patch
[helm.git] / helm / ocaml / cic_omdoc / eta_fixing.ml
1 (* Copyright (C) 2000, 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 exception ReferenceToVariable;;
27 exception RferenceToCurrentProof;;
28 exception ReferenceToInductiveDefinition;;
29
30 let prerr_endline _ = ();;
31
32 (* 
33 let rec fix_lambdas_wrt_type ty te =
34  let module C = Cic in
35  let module S = CicSubstitution in
36 (*  prerr_endline ("entering fix_lambdas: type=" ^ CicPp.ppterm ty ^ "term=" ^ CicPp.ppterm te); *)
37    match ty with
38      C.Prod (_,_,ty') ->
39        (match CicReduction.whd [] te with
40           C.Lambda (n,s,te') ->
41             C.Lambda (n,s,fix_lambdas_wrt_type ty' te')
42         | t ->
43             let rec get_sources =
44               function 
45                 C.Prod (_,s,ty) -> s::(get_sources ty)
46               | _ -> [] in
47             let sources = get_sources ty in
48             let no_sources = List.length sources in
49             let rec mk_rels n shift =
50               if n = 0 then []
51             else (C.Rel (n + shift))::(mk_rels (n - 1) shift) in
52             let t' = S.lift no_sources t in
53             let t2 = 
54               match t' with
55                 C.Appl l -> 
56                   C.LetIn 
57                      (C.Name "w",t',C.Appl ((C.Rel 1)::(mk_rels no_sources 1)))
58               | _ -> 
59                   C.Appl (t'::(mk_rels no_sources 0)) in
60                    List.fold_right
61                      (fun source t -> C.Lambda (C.Name "y",source,t)) 
62                       sources t2)
63    | _ -> te
64 ;; *)
65
66 let rec fix_lambdas_wrt_type ty te =
67  let module C = Cic in
68  let module S = CicSubstitution in
69 (*  prerr_endline ("entering fix_lambdas: type=" ^ CicPp.ppterm ty ^ "term=" ^ CicPp.ppterm te); *)
70    match ty,te with
71      C.Prod (_,_,ty'), C.Lambda (n,s,te') ->
72        C.Lambda (n,s,fix_lambdas_wrt_type ty' te')
73    | C.Prod (_,s,ty'), t -> 
74       let rec get_sources =
75         function 
76             C.Prod (_,s,ty) -> s::(get_sources ty)
77           | _ -> [] in
78       let sources = get_sources ty in
79       let no_sources = List.length sources in
80       let rec mk_rels n shift =
81         if n = 0 then []
82         else (C.Rel (n + shift))::(mk_rels (n - 1) shift) in
83       let t' = S.lift no_sources t in
84       let t2 = 
85          match t' with
86            C.Appl l -> 
87              C.LetIn (C.Name "w",t',C.Appl ((C.Rel 1)::(mk_rels no_sources 1)))
88          | _ -> C.Appl (t'::(mk_rels no_sources 0)) in
89       List.fold_right
90         (fun source t -> C.Lambda (C.Name "y",CicReduction.whd [] source,t)) sources t2
91    | _, _ -> te
92 ;;
93
94 (*
95 let rec fix_lambdas_wrt_type ty te =
96  let module C = Cic in
97  let module S = CicSubstitution in
98 (*  prerr_endline ("entering fix_lambdas: type=" ^ CicPp.ppterm ty ^ "term=" ^ CicPp.ppterm te); *)
99    match ty,te with
100      C.Prod (_,_,ty'), C.Lambda (n,s,te') ->
101        C.Lambda (n,s,fix_lambdas_wrt_type ty' te')
102    | C.Prod (_,s,ty'), ((C.Appl (C.Const _ ::_)) as t) -> 
103       (* const have a fixed arity *)
104       (* prerr_endline ("******** fl - eta expansion 0: type=" ^ CicPp.ppterm ty ^ "term=" ^ CicPp.ppterm te); *)
105        let t' = S.lift 1 t in
106        C.Lambda (C.Name "x",s,
107          C.LetIn 
108           (C.Name "H", fix_lambdas_wrt_type ty' t', 
109             C.Appl [C.Rel 1;C.Rel 2])) 
110    | C.Prod (_,s,ty'), C.Appl l ->
111        (* prerr_endline ("******** fl - eta expansion 1: type=" ^ CicPp.ppterm ty ^ "term=" ^ CicPp.ppterm te); *)
112        let l' = List.map (S.lift 1) l in
113         C.Lambda (C.Name "x",s,
114          fix_lambdas_wrt_type ty' (C.Appl (l'@[C.Rel 1])))
115    | C.Prod (_,s,ty'), _ ->
116        (* prerr_endline ("******** fl - eta expansion 2: type=" ^ CicPp.ppterm ty ^ "term=" ^ CicPp.ppterm te); *)
117        flush stderr ;
118        let te' = S.lift 1 te in
119         C.Lambda (C.Name "x",s,
120           fix_lambdas_wrt_type ty' (C.Appl [te';C.Rel 1]))
121    | _, _ -> te
122 ;;*) 
123
124 let fix_according_to_type ty hd tl =
125  let module C = Cic in
126  let module S = CicSubstitution in
127    let rec count_prods =
128      function
129        C.Prod (_,_,t) -> 1 + (count_prods t)
130        | _ -> 0 in
131   let expected_arity = count_prods ty in
132   let rec aux n ty tl res =
133     if n = 0 then
134       (match tl with 
135          [] -> C.Appl res
136        | _ -> 
137           match res with
138             [] -> assert false
139           | [a] -> C.Appl (a::tl)
140           | _ ->
141               (* prerr_endline ("******* too many args: type=" ^ CicPp.ppterm ty ^ "term=" ^ CicPp.ppterm (C.Appl res)); *)
142               C.LetIn 
143                 (C.Name "H", 
144                   C.Appl res, C.Appl (C.Rel 1::(List.map (S.lift 1) tl))))
145     else 
146       let name,source,target =
147         (match ty with
148            C.Prod (C.Name _ as n,s,t) -> n,s,t
149          | C.Prod (C.Anonymous, s,t) -> C.Name "z",s,t
150          | _ -> (* prods number may only increase for substitution *) 
151            assert false) in
152       match tl with 
153          [] ->
154            (* prerr_endline ("******* too few args: type=" ^ CicPp.ppterm ty ^ "term=" ^ CicPp.ppterm (C.Appl res)); *)
155            let res' = List.map (S.lift 1) res in 
156            C.Lambda 
157             (name, source, aux (n-1) target [] (res'@[C.Rel 1]))
158         | hd::tl' -> 
159            let hd' = fix_lambdas_wrt_type source hd in
160             (*  (prerr_endline ("++++++prima :" ^(CicPp.ppterm hd)); 
161               prerr_endline ("++++++dopo :" ^(CicPp.ppterm hd'))); *)
162            aux (n-1) (S.subst hd' target) tl' (res@[hd']) in
163   aux expected_arity ty tl [hd]
164 ;;
165
166 let eta_fix metasenv t =
167  let rec eta_fix' context t = 
168   (* prerr_endline ("entering aux with: term=" ^ CicPp.ppterm t); 
169   flush stderr ; *)
170   let module C = Cic in
171   let module S = CicSubstitution in
172   match t with
173      C.Rel n -> C.Rel n 
174    | C.Var (uri,exp_named_subst) ->
175       let exp_named_subst' =
176        List.map
177         (function i,t -> i, (eta_fix' context t)) exp_named_subst
178       in
179       C.Var (uri,exp_named_subst')
180    | C.Meta (n,l) ->
181       let (_,canonical_context,_) =
182        List.find (function (m,_,_) -> n = m) metasenv
183        in
184        let l' =
185         List.map2
186          (fun ct t ->
187           match (ct, t) with
188             None, _ -> None
189           | _, Some t -> Some (eta_fix' context t)
190           | Some _, None -> assert false (* due to typing rules *))
191         canonical_context l
192        in
193        C.Meta (n,l')
194    | C.Sort s -> C.Sort s
195    | C.Implicit -> C.Implicit
196    | C.Cast (v,t) -> C.Cast (eta_fix' context v, eta_fix' context t)
197    | C.Prod (n,s,t) -> 
198        C.Prod 
199         (n, eta_fix' context s, eta_fix' ((Some (n,(C.Decl s)))::context) t)
200    | C.Lambda (n,s,t) -> 
201        C.Lambda 
202         (n, eta_fix' context s, eta_fix' ((Some (n,(C.Decl s)))::context) t)
203    | C.LetIn (n,s,t) -> 
204        C.LetIn 
205         (n,eta_fix' context s,eta_fix' ((Some (n,(C.Def (s,None))))::context) t)
206    | C.Appl l as appl -> 
207        let l' =  List.map (eta_fix' context) l 
208        in 
209        (match l' with
210          C.Const(uri,exp_named_subst)::l'' ->
211            let constant_type =
212              (match CicEnvironment.get_obj uri with
213                C.Constant (_,_,ty,_) -> ty
214              | C.Variable _ -> raise ReferenceToVariable
215              | C.CurrentProof (_,_,_,_,params) -> raise RferenceToCurrentProof
216              | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
217              )
218            in
219             fix_according_to_type constant_type (C.Const(uri,exp_named_subst)) l''
220         | _ -> C.Appl l' )
221    | C.Const (uri,exp_named_subst) ->
222        let exp_named_subst' =
223        List.map
224         (function i,t -> i, (eta_fix' context t)) exp_named_subst
225        in
226        C.Const (uri,exp_named_subst')
227    | C.MutInd (uri,tyno,exp_named_subst) ->
228        let exp_named_subst' =
229        List.map
230         (function i,t -> i, (eta_fix' context t)) exp_named_subst
231        in
232         C.MutInd (uri, tyno, exp_named_subst')
233    | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
234        let exp_named_subst' =
235        List.map
236         (function i,t -> i, (eta_fix' context t)) exp_named_subst
237        in
238         C.MutConstruct (uri, tyno, consno, exp_named_subst')
239    | C.MutCase (uri, tyno, outty, term, patterns) as prima ->
240        let outty' =  eta_fix' context outty in
241        let term' = eta_fix' context term in
242        let patterns' = List.map (eta_fix' context) patterns in
243        let inductive_types,noparams =
244            (match CicEnvironment.get_obj uri with
245                Cic.Constant _ -> assert false
246              | Cic.Variable _ -> assert false
247              | Cic.CurrentProof _ -> assert false
248              | Cic.InductiveDefinition (l,_,n) -> l,n 
249            ) in
250        let (_,_,_,constructors) = List.nth inductive_types tyno in
251         prerr_endline ("QUI"); 
252        let constructor_types = 
253          let rec clean_up t =
254            function 
255                [] -> t
256              | a::tl -> 
257                  (match t with
258                    Cic.Prod (_,_,t') -> clean_up (S.subst a t') tl
259                   | _ -> assert false) in
260           if noparams = 0 then 
261             List.map (fun (_,t) -> t) constructors 
262           else 
263                  let term_type = 
264             CicTypeChecker.type_of_aux' metasenv context term
265            in
266             (match term_type with
267                C.Appl (hd::params) -> 
268                  List.map (fun (_,t) -> clean_up t params) constructors
269              | _ -> prerr_endline ("QUA"); assert false) in 
270        let patterns2 = 
271          List.map2 fix_lambdas_wrt_type
272            constructor_types patterns in 
273          C.MutCase (uri, tyno, outty',term',patterns2)
274    | C.Fix (funno, funs) ->
275        let fun_types = 
276          List.map (fun (n,_,ty,_) -> Some (C.Name n,(Cic.Decl ty))) funs in
277        C.Fix (funno,
278         List.map
279          (fun (name, no, ty, bo) ->
280            (name, no, eta_fix' context ty, eta_fix' (fun_types@context) bo)) 
281         funs)
282    | C.CoFix (funno, funs) ->
283        let fun_types = 
284          List.map (fun (n,ty,_) -> Some (C.Name n,(Cic.Decl ty))) funs in
285        C.CoFix (funno,
286         List.map
287          (fun (name, ty, bo) ->
288            (name, eta_fix' context ty, eta_fix' (fun_types@context) bo)) funs)
289    in
290    eta_fix' [] t
291 ;;
292
293
294
295
296