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
38 print_endline (s ^ "\n" ^ List.fold_right debug_aux (t::env) "") ;
43 exception Impossible of int;;
44 exception ReferenceToDefinition;;
45 exception ReferenceToAxiom;;
46 exception ReferenceToVariable;;
47 exception ReferenceToCurrentProof;;
48 exception ReferenceToInductiveDefinition;;
50 (* takes a well-typed term *)
54 let module S = CicSubstitution in
57 (match List.nth context (n-1) with
58 C.Decl _ -> if l = [] then t else C.Appl (t::l)
59 | C.Def bo -> whdaux l (S.lift n bo)
62 (match CicEnvironment.get_cooked_obj uri 0 with
63 C.Definition _ -> raise ReferenceToDefinition
64 | C.Axiom _ -> raise ReferenceToAxiom
65 | C.CurrentProof _ -> raise ReferenceToCurrentProof
66 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
67 | C.Variable (_,None,_) -> if l = [] then t else C.Appl (t::l)
68 | C.Variable (_,Some body,_) -> whdaux l body
70 | C.Meta _ as t -> if l = [] then t else C.Appl (t::l)
71 | C.Sort _ as t -> t (* l should be empty *)
72 | C.Implicit as t -> t
73 | C.Cast (te,ty) -> whdaux l te (*CSC E' GIUSTO BUTTARE IL CAST? *)
74 | C.Prod _ as t -> t (* l should be empty *)
75 | C.Lambda (name,s,t) as t' ->
78 | he::tl -> whdaux tl (S.subst he t)
79 (* when name is Anonimous the substitution should be superfluous *)
81 | C.LetIn (n,s,t) -> whdaux l (S.subst (whdaux [] s) t)
82 | C.Appl (he::tl) -> whdaux (tl@l) he
83 | C.Appl [] -> raise (Impossible 1)
84 | C.Const (uri,cookingsno) as t ->
85 (match CicEnvironment.get_cooked_obj uri cookingsno with
86 C.Definition (_,body,_,_) -> whdaux l body
87 | C.Axiom _ -> if l = [] then t else C.Appl (t::l)
88 | C.Variable _ -> raise ReferenceToVariable
89 | C.CurrentProof (_,_,body,_) -> whdaux l body
90 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
92 | C.Abst _ as t -> t (*CSC l should be empty ????? *)
93 | C.MutInd (uri,_,_) as t -> if l = [] then t else C.Appl (t::l)
94 | C.MutConstruct (uri,_,_,_) as t -> if l = [] then t else C.Appl (t::l)
95 | C.MutCase (mutind,cookingsno,i,_,term,pl) as t->
98 C.CoFix (i,fl) as t ->
99 let (_,_,body) = List.nth fl i in
101 let counter = ref (List.length fl) in
103 (fun _ -> decr counter ; S.subst (C.CoFix (!counter,fl)))
108 | C.Appl (C.CoFix (i,fl) :: tl) ->
109 let (_,_,body) = List.nth fl i in
111 let counter = ref (List.length fl) in
113 (fun _ -> decr counter ; S.subst (C.CoFix (!counter,fl)))
120 (match decofix (whdaux [] term) with
121 C.MutConstruct (_,_,_,j) -> whdaux l (List.nth pl (j-1))
122 | C.Appl (C.MutConstruct (_,_,_,j) :: tl) ->
123 let (arity, r, num_ingredients) =
124 match CicEnvironment.get_obj mutind with
125 C.InductiveDefinition (tl,ingredients,r) ->
126 let (_,_,arity,_) = List.nth tl i
127 and num_ingredients =
130 if k < cookingsno then i + List.length l else i
133 (arity,r,num_ingredients)
134 | _ -> raise WrongUriToInductiveDefinition
137 let num_to_eat = r + num_ingredients in
141 | (n,he::tl) when n > 0 -> eat_first (n - 1, tl)
142 | _ -> raise (Impossible 5)
144 eat_first (num_to_eat,tl)
146 whdaux (ts@l) (List.nth pl (j-1))
147 | C.Abst _| C.Cast _ | C.Implicit ->
148 raise (Impossible 2) (* we don't trust our whd ;-) *)
149 | _ -> if l = [] then t else C.Appl (t::l)
151 | C.Fix (i,fl) as t ->
152 let (_,recindex,_,body) = List.nth fl i in
155 Some (List.nth l recindex)
161 (match whdaux [] recparam with
163 | C.Appl ((C.MutConstruct _)::_) ->
165 let counter = ref (List.length fl) in
167 (fun _ -> decr counter ; S.subst (C.Fix (!counter,fl)))
171 (* Possible optimization: substituting whd recparam in l *)
173 | _ -> if l = [] then t else C.Appl (t::l)
175 | None -> if l = [] then t else C.Appl (t::l)
177 | C.CoFix (i,fl) as t ->
178 if l = [] then t else C.Appl (t::l)
182 prerr_endline ("PRIMA WHD" ^ CicPp.ppterm t) ; flush stderr ;
183 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 ;
188 t in prerr_endline "DOPO WHD" ; flush stderr ; res
192 (* t1, t2 must be well-typed *)
193 let are_convertible =
194 let module U = UriManager in
195 let rec aux context t1 t2 =
197 (* this trivial euristic cuts down the total time of about five times ;-) *)
198 (* this because most of the time t1 and t2 are "sintactically" the same *)
203 let module C = Cic in
205 (C.Rel n1, C.Rel n2) -> n1 = n2
206 | (C.Var uri1, C.Var uri2) -> U.eq uri1 uri2
207 | (C.Meta n1, C.Meta n2) -> n1 = n2
208 | (C.Sort s1, C.Sort s2) -> true (*CSC da finire con gli universi *)
209 | (C.Prod (_,s1,t1), C.Prod(_,s2,t2)) ->
210 aux context s1 s2 && aux ((C.Decl s1)::context) t1 t2
211 | (C.Lambda (_,s1,t1), C.Lambda(_,s2,t2)) ->
212 aux context s1 s2 && aux ((C.Decl s1)::context) t1 t2
213 | (C.LetIn (_,s1,t1), C.LetIn(_,s2,t2)) ->
214 aux context s1 s2 && aux ((C.Def s1)::context) t1 t2
215 | (C.Appl l1, C.Appl l2) ->
217 List.fold_right2 (fun x y b -> aux context x y && b) l1 l2 true
219 Invalid_argument _ -> false
221 | (C.Const (uri1,_), C.Const (uri2,_)) ->
222 (*CSC: questo commento e' chiaro o delirante? Io lo sto scrivendo *)
223 (*CSC: mentre sono delirante, quindi ... *)
224 (* WARNING: it is really important that the two cookingsno are not*)
225 (* checked for equality. This allows not to cook an object with no*)
226 (* ingredients only to update the cookingsno. E.g: if a term t has*)
227 (* a reference to a term t1 which does not depend on any variable *)
228 (* and t1 depends on a term t2 (that can't depend on any variable *)
229 (* because of t1), then t1 cooked at every level could be the same*)
230 (* as t1 cooked at level 0. Doing so, t2 will be extended in t *)
231 (* with cookingsno 0 and not 2. But this will not cause any *)
232 (* trouble if here we don't check that the two cookingsno are *)
235 | (C.MutInd (uri1,k1,i1), C.MutInd (uri2,k2,i2)) ->
236 (* WARNIG: see the previous warning *)
237 U.eq uri1 uri2 && i1 = i2
238 | (C.MutConstruct (uri1,_,i1,j1), C.MutConstruct (uri2,_,i2,j2)) ->
239 (* WARNIG: see the previous warning *)
240 U.eq uri1 uri2 && i1 = i2 && j1 = j2
241 | (C.MutCase (uri1,_,i1,outtype1,term1,pl1),
242 C.MutCase (uri2,_,i2,outtype2,term2,pl2)) ->
243 (* WARNIG: see the previous warning *)
244 (* aux context outtype1 outtype2 should be true if *)
245 (* aux context pl1 pl2 *)
246 U.eq uri1 uri2 && i1 = i2 && aux context outtype1 outtype2 &&
247 aux context term1 term2 &&
248 List.fold_right2 (fun x y b -> b && aux context x y) pl1 pl2 true
249 | (C.Fix (i1,fl1), C.Fix (i2,fl2)) ->
250 (*CSC: C.Decl e' giusto? *)
251 let tys = List.map (function (_,_,ty,_) -> C.Decl ty) fl1 in
254 (fun (_,recindex1,ty1,bo1) (_,recindex2,ty2,bo2) b ->
255 b && recindex1 = recindex2 && aux context ty1 ty2 &&
256 aux (tys@context) bo1 bo2)
258 | (C.CoFix (i1,fl1), C.CoFix (i2,fl2)) ->
259 (*CSC: C.Decl e' giusto? *)
260 let tys = List.map (function (_,ty,_) -> C.Decl ty) fl1 in
263 (fun (_,ty1,bo1) (_,ty2,bo2) b ->
264 b && aux context ty1 ty2 && aux (tys@context) bo1 bo2)
266 | (C.Abst _, _) | (_, C.Abst _) | (C.Cast _, _) | (_, C.Cast _)
267 | (C.Implicit, _) | (_, C.Implicit) ->
268 raise (Impossible 3) (* we don't trust our whd ;-) *)
272 if aux2 t1 t2 then true
275 debug t1 [t2] "PREWHD";
276 let t1' = whd context t1
277 and t2' = whd context t2 in
278 debug t1' [t2'] "POSTWHD";