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 module type Strategy = sig
21 type config = int * env_term list * C.term * stack_term list
23 reduce: (config -> config * bool) -> unwind: (config -> C.term) ->
25 val from_stack : stack_term -> config
26 val from_stack_list_for_unwind :
27 unwind: (config -> C.term) -> stack_term list -> C.term list
28 val from_env : env_term -> config
29 val from_env_for_unwind :
30 unwind: (config -> C.term) -> env_term -> C.term
32 reduce: (config -> config * bool) -> unwind: (config -> C.term) ->
33 stack_term -> env_term
35 reduce: (config -> config * bool) -> unwind: (config -> C.term) ->
36 int -> env_term list -> C.term -> env_term
37 val compute_to_stack :
38 reduce: (config -> config * bool) -> unwind: (config -> C.term) ->
43 module CallByValueByNameForUnwind' = struct
44 type config = int * env_term list * C.term * stack_term list
45 and stack_term = config lazy_t * C.term lazy_t (* cbv, cbn *)
46 and env_term = config lazy_t * C.term lazy_t (* cbv, cbn *)
47 let to_env ~reduce ~unwind c = lazy (fst (reduce c)),lazy (unwind c)
48 let from_stack (c,_) = Lazy.force c
49 let from_stack_list_for_unwind ~unwind:_ l =
50 List.map (function (_,c) -> Lazy.force c) l
51 let from_env (c,_) = Lazy.force c
52 let from_env_for_unwind ~unwind:_ (_,c) = Lazy.force c
53 let stack_to_env ~reduce:_ ~unwind:_ config = config
54 let compute_to_env ~reduce ~unwind k e t =
55 lazy (fst (reduce (k,e,t,[]))), lazy (unwind (k,e,t,[]))
56 let compute_to_stack ~reduce ~unwind config =
57 lazy (fst (reduce config)), lazy (unwind config)
61 module Reduction(RS : Strategy) = struct
62 type env = RS.env_term list
63 type stack = RS.stack_term list
64 type config = int * env * C.term * stack
66 let rec unwind (k,e,t,s) =
70 NCicSubstitution.psubst ~avoid_beta_redexes:true
71 (RS.from_env_for_unwind ~unwind) e t
74 else C.Appl(t::(RS.from_stack_list_for_unwind ~unwind s))
77 let list_nth l n = try List.nth l n with Failure _ -> assert false;;
78 let rec replace i s t =
81 | n,he::tl -> he::(replace (n - 1) tl t)
85 let rec reduce ~delta ?(subst = []) context : config -> config * bool =
86 let rec aux = function
87 | k, e, C.Rel n, s when n <= k ->
88 let k',e',t',s' = RS.from_env (list_nth e (n-1)) in
90 | k, _, C.Rel n, s as config (* when n > k *) ->
91 let x= try Some (List.nth context (n - 1 - k)) with Failure _ -> None in
93 | Some(_,C.Def(x,_)) -> aux (0,[],NCicSubstitution.lift (n - k) x,s)
95 | (k, e, C.Meta (n,l), s) as config ->
97 let _,_, term,_ = NCicUtils.lookup_subst n subst in
98 aux (k, e, NCicSubstitution.subst_meta l term,s)
99 with NCicUtils.Subst_not_found _ -> config, true)
100 | (_, _, C.Implicit _, _) -> assert false
101 | (_, _, C.Sort _, _)
102 | (_, _, C.Prod _, _)
103 | (_, _, C.Lambda _, []) as config -> config, true
104 | (k, e, C.Lambda (_,_,t), p::s) ->
105 aux (k+1, (RS.stack_to_env ~reduce:aux ~unwind p)::e, t,s)
106 | (k, e, C.LetIn (_,_,m,t), s) ->
107 let m' = RS.compute_to_env ~reduce:aux ~unwind k e m in
108 aux (k+1, m'::e, t, s)
109 | (_, _, C.Appl ([]|[_]), _) -> assert false
110 | (k, e, C.Appl (he::tl), s) ->
112 List.map (fun t->RS.compute_to_stack ~reduce:aux ~unwind (k,e,t,[])) tl
114 aux (k, e, he, tl' @ s)
116 (Ref.Ref (_,Ref.Def height) as refer), s) as config ->
117 if delta >= height then
120 let _,_,body,_,_,_ = NCicEnvironment.get_checked_def refer in
122 | (_, _, C.Const (Ref.Ref (_,
123 (Ref.Decl|Ref.Ind _|Ref.Con _|Ref.CoFix _))), _) as config ->
125 | (_, _, (C.Const (Ref.Ref
126 (_,Ref.Fix (fixno,recindex,height)) as refer) as head),s) as config ->
127 (* if delta >= height then config else *)
129 try Some (RS.from_stack (List.nth s recindex))
130 with Failure _ -> None
132 | None -> config, true
134 let fixes,_,_ = NCicEnvironment.get_checked_fixes_or_cofixes refer in
135 match reduce ~delta:0 ~subst context recparam with
136 | (_,_,C.Const (Ref.Ref (_,Ref.Con _)), _) as c, _
137 when delta >= height ->
139 replace recindex s (RS.compute_to_stack ~reduce:aux ~unwind c)
141 (0, [], head, new_s), false
142 | (_,_,C.Const (Ref.Ref (_,Ref.Con _)), _) as c, _ ->
144 replace recindex s (RS.compute_to_stack ~reduce:aux ~unwind c)
146 let _,_,_,_,body = List.nth fixes fixno in
147 aux (0, [], body, new_s)
149 | (k, e, C.Match (_,_,term,pl),s) as config ->
150 let decofix = function
151 | (_,_,C.Const(Ref.Ref(_,Ref.CoFix c)as refer),s)->
153 NCicEnvironment.get_checked_fixes_or_cofixes refer in
154 let _,_,_,_,body = List.nth cofixes c in
155 let c,_ = reduce ~delta:0 ~subst context (0,[],body,s) in
159 let match_head = k,e,term,[] in
160 let reduced,_ = reduce ~delta:0 ~subst context match_head in
161 (match decofix reduced with
162 | (_, _, C.Const (Ref.Ref (_,Ref.Con (_,j,_))),[]) ->
163 aux (k, e, List.nth pl (j-1), s)
164 | (_, _, C.Const (Ref.Ref (_,Ref.Con (_,j,lno))), s')->
165 let _,params = HExtlib.split_nth lno s' in
166 aux (k, e, List.nth pl (j-1), params@s)
172 let whd ?(delta=0) ~subst context t =
173 unwind (fst (reduce ~delta ~subst context (0, [], t, [])))
180 module RS = CallByValueByNameForUnwind';;
181 module R = Reduction(RS);;
185 let (===) x y = Pervasives.compare x y = 0 ;;
187 let get_relevance = ref (fun ~metasenv:_ ~subst:_ _ _ -> assert false);;
189 let set_get_relevance f = get_relevance := f;;
191 (* t1, t2 must be well-typed *)
192 let are_convertible ~metasenv ~subst =
193 let rec aux test_eq_only context t1 t2 =
194 let alpha_eq test_eq_only t1 t2 =
199 | (C.Sort (C.Type a), C.Sort (C.Type b)) when not test_eq_only ->
200 NCicEnvironment.universe_leq a b
201 | (C.Sort (C.Type a), C.Sort (C.Type b)) ->
202 NCicEnvironment.universe_eq a b
203 | (C.Sort C.Prop,C.Sort (C.Type _)) -> (not test_eq_only)
204 | (C.Sort C.Prop, C.Sort C.Prop) -> true
206 | (C.Prod (name1,s1,t1), C.Prod(_,s2,t2)) ->
207 aux true context s1 s2 &&
208 aux test_eq_only ((name1, C.Decl s1)::context) t1 t2
209 | (C.Lambda (name1,s1,t1), C.Lambda(_,_,t2)) ->
210 (* thanks to inversion of well typedness, the source
211 * of these lambdas must be already convertible *)
212 aux test_eq_only ((name1, C.Decl s1)::context) t1 t2
213 | (C.LetIn (name1,ty1,s1,t1), C.LetIn(_,ty2,s2,t2)) ->
214 aux test_eq_only context ty1 ty2 &&
215 aux test_eq_only context s1 s2 &&
216 aux test_eq_only ((name1, C.Def (s1,ty1))::context) t1 t2
218 | (C.Meta (n1,(s1, C.Irl _)), C.Meta (n2,(s2, C.Irl _)))
219 when n1 = n2 && s1 = s2 -> true
220 | (C.Meta (n1,(s1, l1)), C.Meta (n2,(s2, l2))) when n1 = n2 &&
221 let l1 = NCicUtils.expand_local_context l1 in
222 let l2 = NCicUtils.expand_local_context l2 in
224 (fun t1 t2 -> aux test_eq_only context
225 (NCicSubstitution.lift s1 t1)
226 (NCicSubstitution.lift s2 t2))
228 with Invalid_argument "List.for_all2" ->
229 prerr_endline ("Meta " ^ string_of_int n1 ^
230 " occurrs with local contexts of different lenght\n"^
231 NCicPp.ppterm ~metasenv ~subst ~context t1 ^ " === " ^
232 NCicPp.ppterm ~metasenv ~subst ~context t2);
233 assert false) -> true
235 | C.Meta (n1,l1), _ ->
237 let _,_,term,_ = NCicUtils.lookup_subst n1 subst in
238 let term = NCicSubstitution.subst_meta l1 term in
239 aux test_eq_only context term t2
240 with NCicUtils.Subst_not_found _ -> false)
241 | _, C.Meta (n2,l2) ->
243 let _,_,term,_ = NCicUtils.lookup_subst n2 subst in
244 let term = NCicSubstitution.subst_meta l2 term in
245 aux test_eq_only context t1 term
246 with NCicUtils.Subst_not_found _ -> false)
248 | (C.Appl ((C.Const r1) as hd1::tl1), C.Appl (C.Const r2::tl2))
249 when (Ref.eq r1 r2 &&
250 List.length (E.get_relevance r1) >= List.length tl1) ->
251 let relevance = E.get_relevance r1 in
252 let relevance = match r1 with
253 | Ref.Ref (_,Ref.Con (_,_,lno)) ->
254 let _,relevance = HExtlib.split_nth lno relevance in
255 HExtlib.mk_list false lno @ relevance
259 HExtlib.list_forall_default3_var
260 (fun t1 t2 b -> not b || aux test_eq_only context t1 t2 )
261 tl1 tl2 true relevance
262 with Invalid_argument _ -> false
263 | HExtlib.FailureAt fail ->
265 !get_relevance ~metasenv ~subst context hd1 tl1 in
266 let _,relevance = HExtlib.split_nth fail relevance in
267 let b,relevance = (match relevance with
271 let _,tl1 = HExtlib.split_nth (fail+1) tl1 in
272 let _,tl2 = HExtlib.split_nth (fail+1) tl2 in
274 HExtlib.list_forall_default3
275 (fun t1 t2 b -> not b || aux test_eq_only context t1 t2)
276 tl1 tl2 true relevance
277 with Invalid_argument _ -> false
280 | (C.Appl (hd1::tl1), C.Appl (hd2::tl2)) ->
281 aux test_eq_only context hd1 hd2 &&
282 let relevance = !get_relevance ~metasenv ~subst context hd1 tl1 in
284 HExtlib.list_forall_default3
285 (fun t1 t2 b -> not b || aux test_eq_only context t1 t2)
286 tl1 tl2 true relevance
287 with Invalid_argument _ -> false)
289 | (C.Match (Ref.Ref (_,Ref.Ind (_,tyno,_)) as ref1,outtype1,term1,pl1),
290 C.Match (ref2,outtype2,term2,pl2)) ->
291 let _,_,itl,_,_ = E.get_checked_indtys ref1 in
292 let _,_,ty,_ = List.nth itl tyno in
293 let rec remove_prods ~subst context ty =
294 let ty = whd ~subst context ty in
297 | C.Prod (name,so,ta) -> remove_prods ~subst ((name,(C.Decl so))::context) ta
301 match remove_prods ~subst [] ty with
302 | C.Sort C.Prop -> true
306 aux test_eq_only context outtype1 outtype2 &&
307 (is_prop || aux test_eq_only context term1 term2) &&
308 (try List.for_all2 (aux test_eq_only context) pl1 pl2
309 with Invalid_argument _ -> false)
310 | (C.Implicit _, _) | (_, C.Implicit _) -> assert false
313 if alpha_eq test_eq_only t1 t2 then
316 let height_of = function
317 | C.Const (Ref.Ref (_,Ref.Def h))
318 | C.Const (Ref.Ref (_,Ref.Fix (_,_,h)))
319 | C.Appl(C.Const(Ref.Ref(_,Ref.Def h))::_)
320 | C.Appl(C.Const(Ref.Ref(_,Ref.Fix (_,_,h)))::_) -> h
323 let put_in_whd m1 m2 =
324 R.reduce ~delta:max_int ~subst context m1,
325 R.reduce ~delta:max_int ~subst context m2
328 ((_,_,t1,_ as m1), norm1 as x1) ((_,_,t2,_ as m2), norm2 as x2)
330 assert(not (norm1 && norm2));
332 x1, R.reduce ~delta:(height_of t2 -1) ~subst context m2
334 R.reduce ~delta:(height_of t1 -1) ~subst context m1, x2
336 let h1 = height_of t1 in
337 let h2 = height_of t2 in
338 let delta = if h1 = h2 then max 0 (h1 -1) else min h1 h2 in
339 R.reduce ~delta ~subst context m1,
340 R.reduce ~delta ~subst context m2
342 let rec convert_machines
343 ((k1,e1,t1,s1),norm1 as m1),((k2,e2,t2,s2), norm2 as m2)
345 (alpha_eq test_eq_only
346 (R.unwind (k1,e1,t1,[])) (R.unwind (k2,e2,t2,[])) &&
349 C.Const r -> NCicEnvironment.get_relevance r
352 HExtlib.list_forall_default3
355 let t1 = RS.from_stack t1 in
356 let t2 = RS.from_stack t2 in
357 convert_machines (put_in_whd t1 t2)) s1 s2 true relevance
358 with Invalid_argument _ -> false) ||
359 (not (norm1 && norm2) && convert_machines (small_delta_step m1 m2))
361 convert_machines (put_in_whd (0,[],t1,[]) (0,[],t2,[]))
366 let rec head_beta_reduce ?(delta=max_int) ?(upto=(-1)) t l =
367 match upto, t, l with
368 | 0, C.Appl l1, _ -> C.Appl (l1 @ l)
370 | 0, t, _ -> C.Appl (t::l)
371 | _, C.Appl (hd::tl), _ -> head_beta_reduce ~delta ~upto hd (tl @ l)
372 | _, C.Lambda(_,_,bo), arg::tl ->
373 let bo = NCicSubstitution.subst arg bo in
374 head_beta_reduce ~delta ~upto:(upto - 1) bo tl
375 | _, C.Const (Ref.Ref (_, Ref.Def height) as re), _
376 when delta <= height ->
377 let _, _, bo, _, _, _ = NCicEnvironment.get_checked_def re in
378 head_beta_reduce ~upto ~delta bo l
380 | _, t, _ -> C.Appl (t::l)
383 let head_beta_reduce ?delta ?upto t = head_beta_reduce ?delta ?upto t [];;
385 type stack_item = RS.stack_term
386 type environment_item = RS.env_term
388 type machine = int * environment_item list * NCic.term * stack_item list
390 let reduce_machine = R.reduce
391 let from_stack = RS.from_stack
392 let unwind = R.unwind
394 (* vim:set foldmethod=marker: *)