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 CicReductionInternalError;;
27 exception WrongUriToInductiveDefinition;;
31 let rec debug_aux t i =
33 let module U = UriManager in
34 CicPp.ppobj (C.Variable ("DEBUG", None, t, [])) ^ "\n" ^ i
37 prerr_endline (s ^ "\n" ^ List.fold_right debug_aux (t::env) "")
40 exception Impossible of int;;
41 exception ReferenceToConstant;;
42 exception ReferenceToVariable;;
43 exception ReferenceToCurrentProof;;
44 exception ReferenceToInductiveDefinition;;
45 exception RelToHiddenHypothesis;;
47 (* takes a well-typed term *)
51 let module S = CicSubstitution in
54 (match List.nth context (n-1) with
55 Some (_, C.Decl _) -> if l = [] then t else C.Appl (t::l)
56 | Some (_, C.Def (bo,_)) -> whdaux l (S.lift n bo)
57 | None -> raise RelToHiddenHypothesis
59 | C.Var (uri,exp_named_subst) as t ->
60 (match CicEnvironment.get_cooked_obj ~trust:false uri with
61 C.Constant _ -> raise ReferenceToConstant
62 | C.CurrentProof _ -> raise ReferenceToCurrentProof
63 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
64 | C.Variable (_,None,_,_) -> if l = [] then t else C.Appl (t::l)
65 | C.Variable (_,Some body,_,_) ->
66 whdaux l (CicSubstitution.subst_vars exp_named_subst body)
68 | C.Meta _ as t -> if l = [] then t else C.Appl (t::l)
69 | C.Sort _ as t -> t (* l should be empty *)
70 | C.Implicit as t -> t
71 | C.Cast (te,ty) -> whdaux l te (*CSC E' GIUSTO BUTTARE IL CAST? *)
72 | C.Prod _ as t -> t (* l should be empty *)
73 | C.Lambda (name,s,t) as t' ->
76 | he::tl -> whdaux tl (S.subst he t)
77 (* when name is Anonimous the substitution should be superfluous *)
79 | C.LetIn (n,s,t) -> whdaux l (S.subst (whdaux [] s) t)
80 | C.Appl (he::tl) -> whdaux (tl@l) he
81 | C.Appl [] -> raise (Impossible 1)
82 | C.Const (uri,exp_named_subst) as t ->
83 (match CicEnvironment.get_cooked_obj ~trust:false uri with
84 C.Constant (_,Some body,_,_) ->
85 whdaux l (CicSubstitution.subst_vars exp_named_subst body)
86 | C.Constant _ -> if l = [] then t else C.Appl (t::l)
87 | C.Variable _ -> raise ReferenceToVariable
88 | C.CurrentProof (_,_,body,_,_) ->
89 whdaux l (CicSubstitution.subst_vars exp_named_subst body)
90 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
92 | C.MutInd _ as t -> if l = [] then t else C.Appl (t::l)
93 | C.MutConstruct _ as t -> if l = [] then t else C.Appl (t::l)
94 | C.MutCase (mutind,i,_,term,pl) as t->
97 C.CoFix (i,fl) as t ->
98 let (_,_,body) = List.nth fl i in
100 let counter = ref (List.length fl) in
102 (fun _ -> decr counter ; S.subst (C.CoFix (!counter,fl)))
107 | C.Appl (C.CoFix (i,fl) :: tl) ->
108 let (_,_,body) = List.nth fl i in
110 let counter = ref (List.length fl) in
112 (fun _ -> decr counter ; S.subst (C.CoFix (!counter,fl)))
119 (match decofix (whdaux [] term) with
120 C.MutConstruct (_,_,j,_) -> whdaux l (List.nth pl (j-1))
121 | C.Appl (C.MutConstruct (_,_,j,_) :: tl) ->
123 match CicEnvironment.get_obj mutind with
124 C.InductiveDefinition (tl,ingredients,r) ->
125 let (_,_,arity,_) = List.nth tl i in
127 | _ -> raise WrongUriToInductiveDefinition
133 | (n,he::tl) when n > 0 -> eat_first (n - 1, tl)
134 | _ -> raise (Impossible 5)
138 whdaux (ts@l) (List.nth pl (j-1))
139 | C.Cast _ | C.Implicit ->
140 raise (Impossible 2) (* we don't trust our whd ;-) *)
141 | _ -> if l = [] then t else C.Appl (t::l)
143 | C.Fix (i,fl) as t ->
144 let (_,recindex,_,body) = List.nth fl i in
147 Some (List.nth l recindex)
153 (match whdaux [] recparam with
155 | C.Appl ((C.MutConstruct _)::_) ->
157 let counter = ref (List.length fl) in
159 (fun _ -> decr counter ; S.subst (C.Fix (!counter,fl)))
163 (* Possible optimization: substituting whd recparam in l *)
165 | _ -> if l = [] then t else C.Appl (t::l)
167 | None -> if l = [] then t else C.Appl (t::l)
169 | C.CoFix (i,fl) as t ->
170 if l = [] then t else C.Appl (t::l)
174 prerr_endline ("PRIMA WHD" ^ CicPp.ppterm t) ; flush stderr ;
175 List.iter (function (Cic.Decl t) -> prerr_endline ("Context: " ^ CicPp.ppterm t) | (Cic.Def t) -> prerr_endline ("Context:= " ^ CicPp.ppterm t)) context ; flush stderr ; prerr_endline "<PRIMA WHD" ; flush stderr ;
180 t in prerr_endline "DOPO WHD" ; flush stderr ; res
184 (* t1, t2 must be well-typed *)
185 let are_convertible =
186 let module U = UriManager in
187 let rec aux context t1 t2 =
189 (* this trivial euristic cuts down the total time of about five times ;-) *)
190 (* this because most of the time t1 and t2 are "sintactically" the same *)
195 let module C = Cic in
197 (C.Rel n1, C.Rel n2) -> n1 = n2
198 | (C.Var (uri1,exp_named_subst1), C.Var (uri2,exp_named_subst2)) ->
202 (fun (uri1,x) (uri2,y) b ->
203 U.eq uri1 uri2 && aux context x y && b
204 ) exp_named_subst1 exp_named_subst2 true
206 Invalid_argument _ -> false
208 | (C.Meta (n1,l1), C.Meta (n2,l2)) ->
216 | Some t1',Some t2' -> aux context t1' t2'
218 | (C.Sort s1, C.Sort s2) -> true (*CSC da finire con gli universi *)
219 | (C.Prod (name1,s1,t1), C.Prod(_,s2,t2)) ->
221 aux ((Some (name1, (C.Decl s1)))::context) t1 t2
222 | (C.Lambda (name1,s1,t1), C.Lambda(_,s2,t2)) ->
224 aux ((Some (name1, (C.Decl s1)))::context) t1 t2
225 | (C.LetIn (name1,s1,t1), C.LetIn(_,s2,t2)) ->
227 aux ((Some (name1, (C.Def (s1,None))))::context) t1 t2
228 | (C.Appl l1, C.Appl l2) ->
230 List.fold_right2 (fun x y b -> aux context x y && b) l1 l2 true
232 Invalid_argument _ -> false
234 | (C.Const (uri1,exp_named_subst1), C.Const (uri2,exp_named_subst2)) ->
238 (fun (uri1,x) (uri2,y) b ->
239 U.eq uri1 uri2 && aux context x y && b
240 ) exp_named_subst1 exp_named_subst2 true
242 Invalid_argument _ -> false
244 | (C.MutInd (uri1,i1,exp_named_subst1),
245 C.MutInd (uri2,i2,exp_named_subst2)
247 U.eq uri1 uri2 && i1 = i2 &&
250 (fun (uri1,x) (uri2,y) b ->
251 U.eq uri1 uri2 && aux context x y && b
252 ) exp_named_subst1 exp_named_subst2 true
254 Invalid_argument _ -> false
256 | (C.MutConstruct (uri1,i1,j1,exp_named_subst1),
257 C.MutConstruct (uri2,i2,j2,exp_named_subst2)
259 U.eq uri1 uri2 && i1 = i2 && j1 = j2 &&
262 (fun (uri1,x) (uri2,y) b ->
263 U.eq uri1 uri2 && aux context x y && b
264 ) exp_named_subst1 exp_named_subst2 true
266 Invalid_argument _ -> false
268 | (C.MutCase (uri1,i1,outtype1,term1,pl1),
269 C.MutCase (uri2,i2,outtype2,term2,pl2)) ->
270 U.eq uri1 uri2 && i1 = i2 && aux context outtype1 outtype2 &&
271 aux context term1 term2 &&
272 List.fold_right2 (fun x y b -> b && aux context x y) pl1 pl2 true
273 | (C.Fix (i1,fl1), C.Fix (i2,fl2)) ->
275 List.map (function (n,_,ty,_) -> Some (C.Name n,(C.Decl ty))) fl1
279 (fun (_,recindex1,ty1,bo1) (_,recindex2,ty2,bo2) b ->
280 b && recindex1 = recindex2 && aux context ty1 ty2 &&
281 aux (tys@context) bo1 bo2)
283 | (C.CoFix (i1,fl1), C.CoFix (i2,fl2)) ->
285 List.map (function (n,ty,_) -> Some (C.Name n,(C.Decl ty))) fl1
289 (fun (_,ty1,bo1) (_,ty2,bo2) b ->
290 b && aux context ty1 ty2 && aux (tys@context) bo1 bo2)
292 | (C.Cast _, _) | (_, C.Cast _)
293 | (C.Implicit, _) | (_, C.Implicit) ->
294 raise (Impossible 3) (* we don't trust our whd ;-) *)
298 if aux2 t1 t2 then true
301 debug t1 [t2] "PREWHD";
302 let t1' = whd context t1
303 and t2' = whd context t2 in
304 debug t1' [t2'] "POSTWHD";