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
17 exception Subst_not_found of int
18 exception Meta_not_found of int
20 let head_beta_reduce = ref (fun (_:NCicEnvironment.status) ~upto:_ _ -> assert false);;
21 let set_head_beta_reduce = (:=) head_beta_reduce;;
23 let expand_local_context = function
25 let rec aux acc = function
27 | n -> aux (C.Rel n::acc) (n-1)
33 let lookup_subst n subst =
36 with Not_found -> raise (Subst_not_found n)
39 let lookup_meta n metasenv =
42 with Not_found -> raise (Meta_not_found n)
45 let fold g k f acc = function
46 | C.Meta _ -> assert false
51 | C.Appl [] | C.Appl [_] -> assert false
52 | C.Appl l -> List.fold_left (f k) acc l
54 | C.Lambda (n,s,t) -> f (g (n,C.Decl s) k) (f k acc s) t
55 | C.LetIn (n,ty,t,bo) -> f (g (n,C.Def (t,ty)) k) (f k (f k acc ty) t) bo
56 | C.Match (_,oty,t,pl) -> List.fold_left (f k) (f k (f k acc oty) t) pl
59 let map ?(hbr=true) status g k f = function
60 | C.Meta _ -> assert false
65 | C.Appl [] | C.Appl [_] -> assert false
68 match l with C.Meta _ :: _ -> true, List.length l - 1 | _ -> false, 0
70 let l1 = HExtlib.sharing_map (f k) l in
71 if l == l1 then orig else
74 | C.Appl l :: tl -> C.Appl (l@tl)
77 if fire_beta & hbr then !head_beta_reduce (status :> NCicEnvironment.status) ~upto t
79 | C.Prod (n,s,t) as orig ->
80 let s1 = f k s in let t1 = f (g (n,C.Decl s) k) t in
81 if t1 == t && s1 == s then orig else C.Prod (n,s1,t1)
82 | C.Lambda (n,s,t) as orig ->
83 let s1 = f k s in let t1 = f (g (n,C.Decl s) k) t in
84 if t1 == t && s1 == s then orig else C.Lambda (n,s1,t1)
85 | C.LetIn (n,ty,t,b) as orig ->
86 let ty1 = f k ty in let t1 = f k t in
87 let b1 = f (g (n,C.Def (t,ty)) k) b in
88 if ty1 == ty && t1 == t && b1 == b then orig else C.LetIn (n,ty1,t1,b1)
89 | C.Match (r,oty,t,pl) as orig ->
90 let oty1 = f k oty in let t1 = f k t in let pl1 = HExtlib.sharing_map (f k) pl in
91 if oty1 == oty && t1 == t && pl1 == pl then orig
92 else C.Match(r,oty1,t1,pl1)