2 ||M|| This file is part of HELM, an Hypertextual, Electronic
3 ||A|| Library of Mathematics, developed at the Computer Science
4 ||T|| Department, University of Bologna, Italy.
6 ||T|| HELM is free software; you can redistribute it and/or
7 ||A|| modify it under the terms of the GNU General Public License
8 \ / version 2 or (at your option) any later version.
9 \ / This software is distributed as is, NO WARRANTY.
10 V_______________________________________________________________ *)
15 module Ref = NReference
16 module E = NCicEnvironment
18 exception AssertFailure of string Lazy.t;;
20 let debug = ref false;;
21 let pp m = if !debug then prerr_endline (Lazy.force m) else ();;
23 module type Strategy = sig
26 type config = int * env_term list * C.term * stack_term list
28 reduce: (delta:int -> config -> config * bool) ->
29 unwind: (config -> C.term) ->
31 val from_stack : delta:int -> stack_term -> config
32 val from_stack_list_for_unwind :
33 unwind: (config -> C.term) -> stack_term list -> C.term list
34 val from_env : delta:int -> env_term -> config
35 val from_env_for_unwind :
36 unwind: (config -> C.term) -> env_term -> C.term
38 reduce: (delta:int -> config -> config * bool) ->
39 unwind: (config -> C.term) ->
40 stack_term -> env_term
42 reduce: (delta:int -> config -> config * bool) ->
43 unwind: (config -> C.term) ->
44 int -> env_term list -> C.term -> env_term
45 val compute_to_stack :
46 reduce: (delta:int -> config -> config * bool) ->
47 unwind: (config -> C.term) ->
52 module CallByValueByNameForUnwind' : Strategy = struct
53 type config = int * env_term list * C.term * stack_term list
55 config Lazy.t * (int -> config) * C.term Lazy.t
57 config Lazy.t (* cbneed ~delta:0 *)
58 * (int -> config) (* cbvalue ~delta *)
59 * C.term Lazy.t (* cbname ~delta:max_int *)
60 let to_env ~reduce ~unwind c =
61 lazy (fst (reduce ~delta:0 c)),
62 (fun delta -> fst (reduce ~delta c)),
64 let from_stack ~delta (c0,c,_) = if delta = 0 then Lazy.force c0 else c delta
65 let from_stack_list_for_unwind ~unwind:_ l =
66 List.map (fun (_,_,c) -> Lazy.force c) l
67 let from_env ~delta (c0,c,_) = if delta = 0 then Lazy.force c0 else c delta
68 let from_env_for_unwind ~unwind:_ (_,_,c) = Lazy.force c
69 let stack_to_env ~reduce:_ ~unwind:_ config = config
70 let compute_to_env ~reduce ~unwind k e t =
71 lazy (fst (reduce ~delta:0 (k,e,t,[]))),
72 (fun delta -> fst (reduce ~delta (k,e,t,[]))),
73 lazy (unwind (k,e,t,[]))
74 let compute_to_stack ~reduce ~unwind config =
75 lazy (fst (reduce ~delta:0 config)),
76 (fun delta -> fst (reduce ~delta config)),
81 module Reduction(RS : Strategy) = struct
82 type env = RS.env_term list
83 type stack = RS.stack_term list
84 type config = int * env * C.term * stack
86 let rec unwind (k,e,t,s) =
90 NCicSubstitution.psubst ~avoid_beta_redexes:true
91 (RS.from_env_for_unwind ~unwind) e t
94 else C.Appl(t::(RS.from_stack_list_for_unwind ~unwind s))
97 let list_nth l n = try List.nth l n with Failure _ -> assert false;;
98 let rec replace i s t =
101 | n,he::tl -> he::(replace (n - 1) tl t)
102 | _,_ -> assert false
105 let rec reduce ~delta ?(subst = []) context : config -> config * bool =
106 let rec aux = function
107 | k, e, C.Rel n, s when n <= k ->
108 let k',e',t',s' = RS.from_env ~delta (list_nth e (n-1)) in
110 | k, _, C.Rel n, s as config (* when n > k *) ->
111 let x= try Some (List.nth context (n - 1 - k)) with Failure _ -> None in
113 | Some(_,C.Def(x,_)) -> aux (0,[],NCicSubstitution.lift (n - k) x,s)
115 | (k, e, C.Meta (n,l), s) as config ->
117 let _,_, term,_ = NCicUtils.lookup_subst n subst in
118 aux (k, e, NCicSubstitution.subst_meta l term,s)
119 with NCicUtils.Subst_not_found _ -> config, true)
120 | (_, _, C.Implicit _, _) -> assert false
121 | (_, _, C.Sort _, _)
122 | (_, _, C.Prod _, _)
123 | (_, _, C.Lambda _, []) as config -> config, true
124 | (k, e, C.Lambda (_,_,t), p::s) ->
125 aux (k+1, (RS.stack_to_env ~reduce:(reduce ~subst context) ~unwind p)::e, t,s)
126 | (k, e, C.LetIn (_,_,m,t), s) ->
127 let m' = RS.compute_to_env ~reduce:(reduce ~subst context) ~unwind k e m in
128 aux (k+1, m'::e, t, s)
129 | (_, _, C.Appl ([]|[_]), _) -> assert false
130 | (k, e, C.Appl (he::tl), s) ->
132 List.map (fun t->RS.compute_to_stack
133 ~reduce:(reduce ~subst context) ~unwind (k,e,t,[])) tl
135 aux (k, e, he, tl' @ s)
137 (Ref.Ref (_,Ref.Def height) as refer), s) as config ->
138 if delta >= height then
141 let _,_,body,_,_,_ = NCicEnvironment.get_checked_def refer in
143 | (_, _, C.Const (Ref.Ref (_,
144 (Ref.Decl|Ref.Ind _|Ref.Con _|Ref.CoFix _))), _) as config ->
146 | (_, _, (C.Const (Ref.Ref
147 (_,Ref.Fix (fixno,recindex,height)) as refer)),s) as config ->
148 (let arg = try Some (List.nth s recindex) with Failure _ -> None in
152 let fixes,(_,_,pragma),_ =
153 NCicEnvironment.get_checked_fixes_or_cofixes refer in
154 if delta >= height then
157 (match RS.from_stack ~delta:max_int arg with
158 | _,_,C.Const(Ref.Ref(_,Ref.Con _)),_::_ ->
159 let _,_,_,_,body = List.nth fixes fixno in
164 match RS.from_stack ~delta:0 arg with
165 | (_,_,C.Const (Ref.Ref (_,Ref.Con _)), _) as c ->
168 (RS.compute_to_stack ~reduce:(reduce ~subst context)
170 let _,_,_,_,body = List.nth fixes fixno in
171 aux (0, [], body, new_s)
173 | (k, e, C.Match (_,_,term,pl),s) as config ->
174 let decofix = function
175 | (_,_,C.Const(Ref.Ref(_,Ref.CoFix c)as refer),s)->
177 NCicEnvironment.get_checked_fixes_or_cofixes refer in
178 let _,_,_,_,body = List.nth cofixes c in
179 let c,_ = reduce ~delta:0 ~subst context (0,[],body,s) in
183 let match_head = k,e,term,[] in
184 let reduced,_ = reduce ~delta:0 ~subst context match_head in
185 (match decofix reduced with
186 | (_, _, C.Const (Ref.Ref (_,Ref.Con (_,j,_))),[]) ->
187 aux (k, e, List.nth pl (j-1), s)
188 | (_, _, C.Const (Ref.Ref (_,Ref.Con (_,j,lno))), s')->
189 let _,params = HExtlib.split_nth lno s' in
190 aux (k, e, List.nth pl (j-1), params@s)
196 let whd ?(delta=0) ~subst context t =
197 unwind (fst (reduce ~delta ~subst context (0, [], t, [])))
204 module RS = CallByValueByNameForUnwind';;
205 module R = Reduction(RS);;
209 let (===) x y = Pervasives.compare x y = 0 ;;
211 let get_relevance = ref (fun ~metasenv:_ ~subst:_ _ _ -> assert false);;
213 let set_get_relevance f = get_relevance := f;;
215 let alpha_eq ~test_lambda_source aux test_eq_only metasenv subst context t1 t2 =
220 | C.Sort s1, C.Sort s2 ->
221 NCicEnvironment.are_sorts_convertible ~test_eq_only s1 s2
223 | (C.Prod (name1,s1,t1), C.Prod(_,s2,t2)) ->
224 aux true context s1 s2 &&
225 aux test_eq_only ((name1, C.Decl s1)::context) t1 t2
226 | (C.Lambda (name1,s1,t1), C.Lambda(_,_,t2)) ->
227 if test_lambda_source then
228 aux test_eq_only context t1 t2
230 (* thanks to inversion of well typedness, the source
231 * of these lambdas must be already convertible *)
232 aux test_eq_only ((name1, C.Decl s1)::context) t1 t2
233 | (C.LetIn (name1,ty1,s1,t1), C.LetIn(_,ty2,s2,t2)) ->
234 aux test_eq_only context ty1 ty2 &&
235 aux test_eq_only context s1 s2 &&
236 aux test_eq_only ((name1, C.Def (s1,ty1))::context) t1 t2
238 | (C.Meta (n1,(s1, C.Irl _)), C.Meta (n2,(s2, C.Irl _)))
239 when n1 = n2 && s1 = s2 -> true
240 | (C.Meta (n1,(s1, l1)), C.Meta (n2,(s2, l2))) when n1 = n2 &&
241 let l1 = NCicUtils.expand_local_context l1 in
242 let l2 = NCicUtils.expand_local_context l2 in
244 (fun t1 t2 -> aux test_eq_only context
245 (NCicSubstitution.lift s1 t1)
246 (NCicSubstitution.lift s2 t2))
248 with Invalid_argument "List.for_all2" ->
249 prerr_endline ("Meta " ^ string_of_int n1 ^
250 " occurrs with local contexts of different lenght\n"^
251 NCicPp.ppterm ~metasenv ~subst ~context t1 ^ " === " ^
252 NCicPp.ppterm ~metasenv ~subst ~context t2);
253 assert false) -> true
255 | C.Meta (n1,l1), _ ->
257 let _,_,term,_ = NCicUtils.lookup_subst n1 subst in
258 let term = NCicSubstitution.subst_meta l1 term in
259 aux test_eq_only context term t2
260 with NCicUtils.Subst_not_found _ -> false)
261 | _, C.Meta (n2,l2) ->
263 let _,_,term,_ = NCicUtils.lookup_subst n2 subst in
264 let term = NCicSubstitution.subst_meta l2 term in
265 aux test_eq_only context t1 term
266 with NCicUtils.Subst_not_found _ -> false)
268 | (C.Appl ((C.Const r1) as hd1::tl1), C.Appl (C.Const r2::tl2))
269 when (Ref.eq r1 r2 &&
270 List.length (E.get_relevance r1) >= List.length tl1) ->
271 let relevance = E.get_relevance r1 in
272 (* if the types were convertible the following optimization is sound
273 let relevance = match r1 with
274 | Ref.Ref (_,Ref.Con (_,_,lno)) ->
275 let _,relevance = HExtlib.split_nth lno relevance in
276 HExtlib.mk_list false lno @ relevance
281 HExtlib.list_forall_default3_var
282 (fun t1 t2 b -> not b || aux true context t1 t2 )
283 tl1 tl2 true relevance
284 with Invalid_argument _ -> false
285 | HExtlib.FailureAt fail ->
287 !get_relevance ~metasenv ~subst context hd1 tl1 in
288 let _,relevance = HExtlib.split_nth fail relevance in
289 let b,relevance = (match relevance with
293 let _,tl1 = HExtlib.split_nth (fail+1) tl1 in
294 let _,tl2 = HExtlib.split_nth (fail+1) tl2 in
296 HExtlib.list_forall_default3
297 (fun t1 t2 b -> not b || aux true context t1 t2)
298 tl1 tl2 true relevance
299 with Invalid_argument _ -> false
302 | (C.Appl (hd1::tl1), C.Appl (hd2::tl2)) ->
303 aux test_eq_only context hd1 hd2 &&
304 let relevance = !get_relevance ~metasenv ~subst context hd1 tl1 in
306 HExtlib.list_forall_default3
307 (fun t1 t2 b -> not b || aux true context t1 t2)
308 tl1 tl2 true relevance
309 with Invalid_argument _ -> false)
311 | (C.Match (Ref.Ref (_,Ref.Ind (_,tyno,_)) as ref1,outtype1,term1,pl1),
312 C.Match (ref2,outtype2,term2,pl2)) ->
313 let _,_,itl,_,_ = E.get_checked_indtys ref1 in
314 let _,_,ty,_ = List.nth itl tyno in
315 let rec remove_prods ~subst context ty =
316 let ty = whd ~subst context ty in
319 | C.Prod (name,so,ta) -> remove_prods ~subst ((name,(C.Decl so))::context) ta
323 match remove_prods ~subst [] ty with
324 | C.Sort C.Prop -> true
328 aux test_eq_only context outtype1 outtype2 &&
329 (is_prop || aux test_eq_only context term1 term2) &&
330 (try List.for_all2 (aux test_eq_only context) pl1 pl2
331 with Invalid_argument _ -> false)
332 | (C.Implicit _, _) | (_, C.Implicit _) -> assert false
336 (* t1, t2 must be well-typed *)
337 let are_convertible ~metasenv ~subst =
338 let rec aux test_eq_only context t1 t2 =
339 let alpha_eq test_eq_only =
340 alpha_eq ~test_lambda_source:false aux test_eq_only metasenv subst context
342 if alpha_eq test_eq_only t1 t2 then
345 let height_of = function
346 | C.Const (Ref.Ref (_,Ref.Def h))
347 | C.Const (Ref.Ref (_,Ref.Fix (_,_,h)))
348 | C.Appl(C.Const(Ref.Ref(_,Ref.Def h))::_)
349 | C.Appl(C.Const(Ref.Ref(_,Ref.Fix (_,_,h)))::_) -> h
352 let put_in_whd m1 m2 =
353 R.reduce ~delta:max_int ~subst context m1,
354 R.reduce ~delta:max_int ~subst context m2
357 ((_,_,t1,_ as m1), norm1 as x1) ((_,_,t2,_ as m2), norm2 as x2)
359 assert(not (norm1 && norm2));
361 x1, R.reduce ~delta:(height_of t2 -1) ~subst context m2
363 R.reduce ~delta:(height_of t1 -1) ~subst context m1, x2
365 let h1 = height_of t1 in
366 let h2 = height_of t2 in
367 let delta = if h1 = h2 then max 0 (h1 -1) else min h1 h2 in
368 R.reduce ~delta ~subst context m1,
369 R.reduce ~delta ~subst context m2
371 let rec convert_machines test_eq_only
372 ((k1,e1,t1,s1),norm1 as m1),((k2,e2,t2,s2), norm2 as m2)
374 (alpha_eq test_eq_only
375 (R.unwind (k1,e1,t1,[])) (R.unwind (k2,e2,t2,[])) &&
378 C.Const r -> NCicEnvironment.get_relevance r
381 HExtlib.list_forall_default3
384 let t1 = RS.from_stack ~delta:max_int t1 in
385 let t2 = RS.from_stack ~delta:max_int t2 in
386 convert_machines true (put_in_whd t1 t2)) s1 s2 true relevance
387 with Invalid_argument _ -> false) ||
388 (not (norm1 && norm2) && convert_machines test_eq_only (small_delta_step m1 m2))
390 convert_machines test_eq_only (put_in_whd (0,[],t1,[]) (0,[],t2,[]))
395 let alpha_eq metasenv subst =
396 let rec aux test_lambda_source context t1 t2 =
397 alpha_eq ~test_lambda_source aux true metasenv subst context t1 t2
402 let rec head_beta_reduce ~delta ~upto ~subst t l =
403 match upto, t, l with
404 | 0, C.Appl l1, _ -> C.Appl (l1 @ l)
406 | 0, t, _ -> C.Appl (t::l)
407 | _, C.Meta (n,ctx), _ ->
409 let _,_, term,_ = NCicUtils.lookup_subst n subst in
410 head_beta_reduce ~delta ~upto ~subst
411 (NCicSubstitution.subst_meta ctx term) l
412 with NCicUtils.Subst_not_found _ -> if l = [] then t else C.Appl (t::l))
413 | _, C.Appl (hd::tl), _ -> head_beta_reduce ~delta ~upto ~subst hd (tl @ l)
414 | _, C.Lambda(_,_,bo), arg::tl ->
415 let bo = NCicSubstitution.subst arg bo in
416 head_beta_reduce ~delta ~upto:(upto - 1) ~subst bo tl
417 | _, C.Const (Ref.Ref (_, Ref.Def height) as re), _
418 when delta <= height ->
419 let _, _, bo, _, _, _ = NCicEnvironment.get_checked_def re in
420 head_beta_reduce ~upto ~delta ~subst bo l
422 | _, t, _ -> C.Appl (t::l)
425 let head_beta_reduce ?(delta=max_int) ?(upto= -1) ?(subst=[]) t =
426 head_beta_reduce ~delta ~upto ~subst t []
429 type stack_item = RS.stack_term
430 type environment_item = RS.env_term
432 type machine = int * environment_item list * NCic.term * stack_item list
434 let reduce_machine = R.reduce
435 let from_stack = RS.from_stack
436 let unwind = R.unwind
439 NCicUtils.set_head_beta_reduce (fun ~upto t -> head_beta_reduce ~upto t);
440 NCicPp.set_head_beta_reduce (fun ~upto t -> head_beta_reduce ~upto t);
443 (* if n < 0, then splits all prods from an arity, returning a sort *)
444 let rec split_prods ~subst context n te =
445 match (n, R.whd ~subst context te) with
446 | (0, _) -> context,te
447 | (n, C.Sort _) when n <= 0 -> context,te
448 | (n, C.Prod (name,so,ta)) ->
449 split_prods ~subst ((name,(C.Decl so))::context) (n - 1) ta
450 | (_, _) -> raise (AssertFailure (lazy "split_prods"))
453 (* vim:set foldmethod=marker: *)