1 (* Copyright (C) 2000, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
26 exception ReferenceToVariable;;
27 exception RferenceToCurrentProof;;
28 exception ReferenceToInductiveDefinition;;
31 let rec fix_lambdas_wrt_type ty te =
33 let module S = CicSubstitution in
34 (* prerr_endline ("entering fix_lambdas: type=" ^ CicPp.ppterm ty ^ "term=" ^ CicPp.ppterm te); *)
37 (match CicReduction.whd [] te with
39 C.Lambda (n,s,fix_lambdas_wrt_type ty' te')
43 C.Prod (_,s,ty) -> s::(get_sources ty)
45 let sources = get_sources ty in
46 let no_sources = List.length sources in
47 let rec mk_rels n shift =
49 else (C.Rel (n + shift))::(mk_rels (n - 1) shift) in
50 let t' = S.lift no_sources t in
55 (C.Name "w",t',C.Appl ((C.Rel 1)::(mk_rels no_sources 1)))
57 C.Appl (t'::(mk_rels no_sources 0)) in
59 (fun source t -> C.Lambda (C.Name "y",source,t))
64 let rec fix_lambdas_wrt_type ty te =
66 let module S = CicSubstitution in
67 (* prerr_endline ("entering fix_lambdas: type=" ^ CicPp.ppterm ty ^ "term=" ^ CicPp.ppterm te); *)
69 C.Prod (_,_,ty'), C.Lambda (n,s,te') ->
70 C.Lambda (n,s,fix_lambdas_wrt_type ty' te')
71 | C.Prod (_,s,ty'), t ->
74 C.Prod (_,s,ty) -> s::(get_sources ty)
76 let sources = get_sources ty in
77 let no_sources = List.length sources in
78 let rec mk_rels n shift =
80 else (C.Rel (n + shift))::(mk_rels (n - 1) shift) in
81 let t' = S.lift no_sources t in
85 C.LetIn (C.Name "w",t',C.Appl ((C.Rel 1)::(mk_rels no_sources 1)))
86 | _ -> C.Appl (t'::(mk_rels no_sources 0)) in
88 (fun source t -> C.Lambda (C.Name "y",CicReduction.whd [] source,t)) sources t2
93 let rec fix_lambdas_wrt_type ty te =
95 let module S = CicSubstitution in
96 (* prerr_endline ("entering fix_lambdas: type=" ^ CicPp.ppterm ty ^ "term=" ^ CicPp.ppterm te); *)
98 C.Prod (_,_,ty'), C.Lambda (n,s,te') ->
99 C.Lambda (n,s,fix_lambdas_wrt_type ty' te')
100 | C.Prod (_,s,ty'), ((C.Appl (C.Const _ ::_)) as t) ->
101 (* const have a fixed arity *)
102 (* prerr_endline ("******** fl - eta expansion 0: type=" ^ CicPp.ppterm ty ^ "term=" ^ CicPp.ppterm te); *)
103 let t' = S.lift 1 t in
104 C.Lambda (C.Name "x",s,
106 (C.Name "H", fix_lambdas_wrt_type ty' t',
107 C.Appl [C.Rel 1;C.Rel 2]))
108 | C.Prod (_,s,ty'), C.Appl l ->
109 (* prerr_endline ("******** fl - eta expansion 1: type=" ^ CicPp.ppterm ty ^ "term=" ^ CicPp.ppterm te); *)
110 let l' = List.map (S.lift 1) l in
111 C.Lambda (C.Name "x",s,
112 fix_lambdas_wrt_type ty' (C.Appl (l'@[C.Rel 1])))
113 | C.Prod (_,s,ty'), _ ->
114 (* prerr_endline ("******** fl - eta expansion 2: type=" ^ CicPp.ppterm ty ^ "term=" ^ CicPp.ppterm te); *)
116 let te' = S.lift 1 te in
117 C.Lambda (C.Name "x",s,
118 fix_lambdas_wrt_type ty' (C.Appl [te';C.Rel 1]))
122 let fix_according_to_type ty hd tl =
123 let module C = Cic in
124 let module S = CicSubstitution in
125 let rec count_prods =
127 C.Prod (_,_,t) -> 1 + (count_prods t)
129 let expected_arity = count_prods ty in
130 let rec aux n ty tl res =
137 | [a] -> C.Appl (a::tl)
139 (* prerr_endline ("******* too many args: type=" ^ CicPp.ppterm ty ^ "term=" ^ CicPp.ppterm (C.Appl res)); *)
142 C.Appl res, C.Appl (C.Rel 1::(List.map (S.lift 1) tl))))
144 let name,source,target =
146 C.Prod (C.Name _ as n,s,t) -> n,s,t
147 | C.Prod (C.Anonymous, s,t) -> C.Name "z",s,t
148 | _ -> (* prods number may only increase for substitution *)
152 (* prerr_endline ("******* too few args: type=" ^ CicPp.ppterm ty ^ "term=" ^ CicPp.ppterm (C.Appl res)); *)
153 let res' = List.map (S.lift 1) res in
155 (name, source, aux (n-1) target [] (res'@[C.Rel 1]))
157 let hd' = fix_lambdas_wrt_type source hd in
158 (* (prerr_endline ("++++++prima :" ^(CicPp.ppterm hd));
159 prerr_endline ("++++++dopo :" ^(CicPp.ppterm hd'))); *)
160 aux (n-1) (S.subst hd' target) tl' (res@[hd']) in
161 aux expected_arity ty tl [hd]
164 let eta_fix metasenv t =
166 (* prerr_endline ("entering aux with: term=" ^ CicPp.ppterm t);
168 let module C = Cic in
171 | C.Var (uri,exp_named_subst) ->
172 let exp_named_subst' =
174 (function i,t -> i, (eta_fix' t)) exp_named_subst
176 C.Var (uri,exp_named_subst')
178 let (_,canonical_context,_) =
179 List.find (function (m,_,_) -> n = m) metasenv
186 | _, Some t -> Some (eta_fix' t)
187 | Some _, None -> assert false (* due to typing rules *))
191 | C.Sort s -> C.Sort s
192 | C.Implicit -> C.Implicit
193 | C.Cast (v,t) -> C.Cast (eta_fix' v, eta_fix' t)
194 | C.Prod (n,s,t) -> C.Prod (n, eta_fix' s, eta_fix' t)
195 | C.Lambda (n,s,t) -> C.Lambda (n, eta_fix' s, eta_fix' t)
196 | C.LetIn (n,s,t) -> C.LetIn (n, eta_fix' s, eta_fix' t)
197 | C.Appl l as appl ->
198 let l' = List.map eta_fix' l
201 C.Const(uri,exp_named_subst)::l'' ->
203 (match CicEnvironment.get_obj uri with
204 C.Constant (_,_,ty,_) -> ty
205 | C.Variable _ -> raise ReferenceToVariable
206 | C.CurrentProof (_,_,_,_,params) -> raise RferenceToCurrentProof
207 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
210 let result = fix_according_to_type constant_type (C.Const(uri,exp_named_subst)) l'' in
211 if not (CicReduction.are_convertible [] appl result) then
212 (prerr_endline ("prima :" ^(CicPp.ppterm appl));
213 prerr_endline ("dopo :" ^(CicPp.ppterm result)));
216 | C.Const (uri,exp_named_subst) ->
217 let exp_named_subst' =
219 (function i,t -> i, (eta_fix' t)) exp_named_subst
221 C.Const (uri,exp_named_subst')
222 | C.MutInd (uri,tyno,exp_named_subst) ->
223 let exp_named_subst' =
225 (function i,t -> i, (eta_fix' t)) exp_named_subst
227 C.MutInd (uri, tyno, exp_named_subst')
228 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
229 let exp_named_subst' =
231 (function i,t -> i, (eta_fix' t)) exp_named_subst
233 C.MutConstruct (uri, tyno, consno, exp_named_subst')
234 | C.MutCase (uri, tyno, outty, term, patterns) ->
235 C.MutCase (uri, tyno, eta_fix' outty,
236 eta_fix' term, List.map eta_fix' patterns)
237 | C.Fix (funno, funs) ->
240 (fun (name, no, ty, bo) ->
241 (name, no, eta_fix' ty, eta_fix' bo)) funs)
242 | C.CoFix (funno, funs) ->
245 (fun (name, ty, bo) ->
246 (name, eta_fix' ty, eta_fix' bo)) funs)