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 ->
61 CicEnvironment.get_cooked_obj ~trust:false CicUniv.empty_ugraph uri
64 C.Constant _ -> raise ReferenceToConstant
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,_,_) ->
69 whdaux l (CicSubstitution.subst_vars exp_named_subst body)
71 | C.Meta _ as t -> if l = [] then t else C.Appl (t::l)
72 | C.Sort _ as t -> t (* l should be empty *)
73 | C.Implicit _ as t -> t
74 | C.Cast (te,ty) -> whdaux l te (*CSC E' GIUSTO BUTTARE IL CAST? *)
75 | C.Prod _ as t -> t (* l should be empty *)
76 | C.Lambda (name,s,t) as t' ->
79 | he::tl -> whdaux tl (S.subst he t)
80 (* when name is Anonimous the substitution should be superfluous *)
82 | C.LetIn (n,s,t) -> whdaux l (S.subst (whdaux [] s) t)
83 | C.Appl (he::tl) -> whdaux (tl@l) he
84 | C.Appl [] -> raise (Impossible 1)
85 | C.Const (uri,exp_named_subst) as t ->
87 CicEnvironment.get_cooked_obj ~trust:false CicUniv.empty_ugraph uri
90 C.Constant (_,Some body,_,_) ->
91 whdaux l (CicSubstitution.subst_vars exp_named_subst body)
92 | C.Constant _ -> if l = [] then t else C.Appl (t::l)
93 | C.Variable _ -> raise ReferenceToVariable
94 | C.CurrentProof (_,_,body,_,_) ->
95 whdaux l (CicSubstitution.subst_vars exp_named_subst body)
96 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
98 | C.MutInd _ as t -> if l = [] then t else C.Appl (t::l)
99 | C.MutConstruct _ as t -> if l = [] then t else C.Appl (t::l)
100 | C.MutCase (mutind,i,_,term,pl) as t->
103 C.CoFix (i,fl) as t ->
104 let (_,_,body) = List.nth fl i in
106 let counter = ref (List.length fl) in
108 (fun _ -> decr counter ; S.subst (C.CoFix (!counter,fl)))
113 | C.Appl (C.CoFix (i,fl) :: tl) ->
114 let (_,_,body) = List.nth fl i in
116 let counter = ref (List.length fl) in
118 (fun _ -> decr counter ; S.subst (C.CoFix (!counter,fl)))
125 (match decofix (whdaux [] term) with
126 C.MutConstruct (_,_,j,_) -> whdaux l (List.nth pl (j-1))
127 | C.Appl (C.MutConstruct (_,_,j,_) :: tl) ->
129 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph mutind in
131 C.InductiveDefinition (tl,ingredients,r) ->
132 let (_,_,arity,_) = List.nth tl i in
134 | _ -> raise WrongUriToInductiveDefinition
140 | (n,he::tl) when n > 0 -> eat_first (n - 1, tl)
141 | _ -> raise (Impossible 5)
145 whdaux (ts@l) (List.nth pl (j-1))
146 | C.Cast _ | C.Implicit _ ->
147 raise (Impossible 2) (* we don't trust our whd ;-) *)
148 | _ -> if l = [] then t else C.Appl (t::l)
150 | C.Fix (i,fl) as t ->
151 let (_,recindex,_,body) = List.nth fl i in
154 Some (List.nth l recindex)
160 (match whdaux [] recparam with
162 | C.Appl ((C.MutConstruct _)::_) ->
164 let counter = ref (List.length fl) in
166 (fun _ -> decr counter ; S.subst (C.Fix (!counter,fl)))
170 (* Possible optimization: substituting whd recparam in l *)
172 | _ -> if l = [] then t else C.Appl (t::l)
174 | None -> if l = [] then t else C.Appl (t::l)
176 | C.CoFix (i,fl) as t ->
177 if l = [] then t else C.Appl (t::l)
181 prerr_endline ("PRIMA WHD" ^ CicPp.ppterm t) ; flush stderr ;
182 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 ;
187 t in prerr_endline "DOPO WHD" ; flush stderr ; res
191 (* t1, t2 must be well-typed *)
192 let are_convertible c t1 t2 ugraph =
193 let module U = UriManager in
194 let rec aux test_equality_only context t1 t2 ugraph =
195 let aux2 test_equality_only t1 t2 ugraph =
196 (* this trivial euristic cuts down the total time of about five times ;-) *)
197 (* this because most of the time t1 and t2 are "sintactically" the same *)
202 let module C = Cic in
204 (C.Rel n1, C.Rel n2) -> (n1 = n2),ugraph
205 | (C.Var (uri1,exp_named_subst1), C.Var (uri2,exp_named_subst2)) ->
206 let b = U.eq uri1 uri2 in
210 (fun (uri1,x) (uri2,y) (b,ugraph) ->
212 let b',ugraph' = aux test_equality_only context x y ugraph in
213 (U.eq uri1 uri2 && b' && b),ugraph'
214 ) exp_named_subst1 exp_named_subst2 (true,ugraph)
216 Invalid_argument _ -> false,ugraph
220 | (C.Meta (n1,l1), C.Meta (n2,l2)) ->
224 (fun (b,ugraph) t1 t2 ->
228 | _,None -> true,ugraph
229 | Some t1',Some t2' ->
230 aux test_equality_only context t1' t2' ugraph
233 ) (true,ugraph) l1 l2
236 (* TASSI: CONSTRAINTS *)
237 | (C.Sort (C.Type t1), C.Sort (C.Type t2)) when test_equality_only ->
238 true,(CicUniv.add_eq t2 t1 ugraph)
239 (* TASSI: CONSTRAINTS *)
240 | (C.Sort (C.Type t1), C.Sort (C.Type t2)) ->
241 true,(CicUniv.add_ge t2 t1 ugraph)
242 (* TASSI: CONSTRAINTS *)
243 | (C.Sort s1, C.Sort (C.Type _)) -> (not test_equality_only),ugraph
244 (* TASSI: CONSTRAINTS *)
245 | (C.Sort s1, C.Sort s2) -> (s1 = s2),ugraph
246 | (C.Prod (name1,s1,t1), C.Prod(_,s2,t2)) ->
247 let b',ugraph' = aux true context s1 s2 ugraph in
249 aux test_equality_only ((Some (name1, (C.Decl s1)))::context)
253 | (C.Lambda (name1,s1,t1), C.Lambda(_,s2,t2)) ->
254 let b',ugraph' = aux test_equality_only context s1 s2 ugraph in
256 aux test_equality_only ((Some (name1, (C.Decl s1)))::context)
260 | (C.LetIn (name1,s1,t1), C.LetIn(_,s2,t2)) ->
261 let b',ugraph' = aux test_equality_only context s1 s2 ugraph in
263 aux test_equality_only
264 ((Some (name1, (C.Def (s1,None))))::context) t1 t2 ugraph'
267 | (C.Appl l1, C.Appl l2) ->
270 (fun x y (b,ugraph) ->
272 aux test_equality_only context x y ugraph
274 false,ugraph) l1 l2 (true,ugraph)
276 Invalid_argument _ -> false,ugraph
278 | (C.Const (uri1,exp_named_subst1), C.Const (uri2,exp_named_subst2)) ->
279 let b' = U.eq uri1 uri2 in
283 (fun (uri1,x) (uri2,y) (b,ugraph) ->
284 if b && U.eq uri1 uri2 then
285 aux test_equality_only context x y ugraph
288 ) exp_named_subst1 exp_named_subst2 (true,ugraph)
290 Invalid_argument _ -> false,ugraph
294 | (C.MutInd (uri1,i1,exp_named_subst1),
295 C.MutInd (uri2,i2,exp_named_subst2)
297 let b' = U.eq uri1 uri2 && i1 = i2 in
301 (fun (uri1,x) (uri2,y) (b,ugraph) ->
302 if b && U.eq uri1 uri2 then
303 aux test_equality_only context x y ugraph
306 ) exp_named_subst1 exp_named_subst2 (true,ugraph)
308 Invalid_argument _ -> false,ugraph
312 | (C.MutConstruct (uri1,i1,j1,exp_named_subst1),
313 C.MutConstruct (uri2,i2,j2,exp_named_subst2)
315 let b' = U.eq uri1 uri2 && i1 = i2 && j1 = j2 in
319 (fun (uri1,x) (uri2,y) (b,ugraph) ->
320 if b && U.eq uri1 uri2 then
321 aux test_equality_only context x y ugraph
324 ) exp_named_subst1 exp_named_subst2 (true,ugraph)
326 Invalid_argument _ -> false,ugraph
330 | (C.MutCase (uri1,i1,outtype1,term1,pl1),
331 C.MutCase (uri2,i2,outtype2,term2,pl2)) ->
332 let b' = U.eq uri1 uri2 && i1 = i2 in
334 let b'',ugraph''=aux test_equality_only context
335 outtype1 outtype2 ugraph in
337 let b''',ugraph'''= aux test_equality_only context
338 term1 term2 ugraph'' in
340 (fun x y (b,ugraph) ->
342 aux test_equality_only context x y ugraph
345 pl1 pl2 (true,ugraph''')
350 | (C.Fix (i1,fl1), C.Fix (i2,fl2)) ->
352 List.map (function (n,_,ty,_) -> Some (C.Name n,(C.Decl ty))) fl1
356 (fun (_,recindex1,ty1,bo1) (_,recindex2,ty2,bo2) (b,ugraph) ->
357 if b && recindex1 = recindex2 then
358 let b',ugraph' = aux test_equality_only context ty1 ty2
361 aux test_equality_only (tys@context) bo1 bo2 ugraph'
366 fl1 fl2 (true,ugraph)
369 | (C.CoFix (i1,fl1), C.CoFix (i2,fl2)) ->
371 List.map (function (n,ty,_) -> Some (C.Name n,(C.Decl ty))) fl1
375 (fun (_,ty1,bo1) (_,ty2,bo2) (b,ugraph) ->
377 let b',ugraph' = aux test_equality_only context ty1 ty2
380 aux test_equality_only (tys@context) bo1 bo2 ugraph'
385 fl1 fl2 (true,ugraph)
388 | (C.Cast _, _) | (_, C.Cast _)
389 | (C.Implicit _, _) | (_, C.Implicit _) ->
391 | (_,_) -> false,ugraph
394 let b,ugraph' = aux2 test_equality_only t1 t2 ugraph in
399 debug t1 [t2] "PREWHD";
400 let t1' = whd context t1 in
401 let t2' = whd context t2 in
402 debug t1' [t2'] "POSTWHD";
403 aux2 test_equality_only t1' t2' ugraph
406 aux false c t1 t2 ugraph