X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=helm%2Fsoftware%2Fcomponents%2Fng_kernel%2FnCicUtils.ml;h=a99ae0407f638b65e2932239b6bafac51f13bc2e;hb=806ecbdd749ecf2a1cabff52b41cf748fe022401;hp=87dc48aa58fb5dc3651cfbb4cd6f101019071ea7;hpb=9e09650c5b9a0c1711c6bef7c42618d8def14a54;p=helm.git diff --git a/helm/software/components/ng_kernel/nCicUtils.ml b/helm/software/components/ng_kernel/nCicUtils.ml index 87dc48aa5..a99ae0407 100644 --- a/helm/software/components/ng_kernel/nCicUtils.ml +++ b/helm/software/components/ng_kernel/nCicUtils.ml @@ -9,19 +9,25 @@ \ / This software is distributed as is, NO WARRANTY. V_______________________________________________________________ *) -(* $Id: nCicSubstitution.ml 8135 2008-02-13 15:35:43Z tassi $ *) +(* $Id$ *) + +module C = NCic +module Ref = NReference exception Subst_not_found of int exception Meta_not_found of int +let head_beta_reduce = ref (fun ~upto:_ _ -> assert false);; +let set_head_beta_reduce = (:=) head_beta_reduce;; + let expand_local_context = function - | NCic.Irl len -> + | C.Irl len -> let rec aux acc = function | 0 -> acc - | n -> aux (NCic.Rel n::acc) (n-1) + | n -> aux (C.Rel n::acc) (n-1) in aux [] len - | NCic.Ctx lctx -> lctx + | C.Ctx lctx -> lctx ;; let lookup_subst n subst = @@ -30,70 +36,58 @@ let lookup_subst n subst = with Not_found -> raise (Subst_not_found n) ;; -let lookup_meta index metasenv = +let lookup_meta n metasenv = try - List.find (fun (index', _, _, _) -> index = index') metasenv - with Not_found -> raise (Meta_not_found index) + List.assoc n metasenv + with Not_found -> raise (Meta_not_found n) ;; -let fold f g acc k = function - | NCic.Implicit _ - | NCic.Sort _ - | NCic.Const _ - | NCic.Meta _ - | NCic.Rel _ -> acc - | NCic.Appl l -> List.fold_left (f k) acc l - | NCic.Prod (_,s,t) - | NCic.Lambda (_,s,t) -> f (g k) (f k acc s) t - | NCic.LetIn (_,ty,t,bo) -> f (g k) (f k (f k acc ty) t) bo - | NCic.Match (_,oty,t,pl) -> List.fold_left (f k) (f k (f k acc oty) t) pl +let fold g k f acc = function + | C.Meta _ -> assert false + | C.Implicit _ + | C.Sort _ + | C.Const _ + | C.Rel _ -> acc + | C.Appl [] | C.Appl [_] -> assert false + | C.Appl l -> List.fold_left (f k) acc l + | C.Prod (n,s,t) + | C.Lambda (n,s,t) -> f (g (n,C.Decl s) k) (f k acc s) t + | C.LetIn (n,ty,t,bo) -> f (g (n,C.Def (t,ty)) k) (f k (f k acc ty) t) bo + | C.Match (_,oty,t,pl) -> List.fold_left (f k) (f k (f k acc oty) t) pl ;; -let sharing_map f l = - let unchanged = ref true in - let rec aux b = function - | [] as t -> unchanged := b; t - | he::tl -> - let he1 = f he in - he1 :: aux (b && he1 == he) tl - in - let l1 = aux true l in - if !unchanged then l else l1 -;; - -let map f g k = function - | NCic.Implicit _ - | NCic.Sort _ - | NCic.Const _ - | NCic.Meta _ - | NCic.Rel _ as t -> t - | NCic.Appl l -> NCic.Appl (sharing_map (f k) l) - | NCic.Prod (n,s,t) as orig -> - let s1 = f k s in let t1 = f (g k) t in - if t1 == t && s1 == s then orig else NCic.Prod (n,s1,t1) - | NCic.Lambda (n,s,t) as orig -> - let s1 = f k s in let t1 = f (g k) t in - if t1 == t && s1 == s then orig else NCic.Prod (n,s1,t1) - | NCic.LetIn (n,ty,t,b) as orig -> - let ty1 = f k ty in let t1 = f k t in let b1 = f (g k) b in - if ty1 == ty && t1 == t && b1 == b then orig else NCic.LetIn (n,ty1,t1,b1) - | NCic.Match (r,oty,t,pl) as orig -> - let oty1 = f k oty in let t1 = f k t in let pl1 = sharing_map (f k) pl in +let map g k f = function + | C.Meta _ -> assert false + | C.Implicit _ + | C.Sort _ + | C.Const _ + | C.Rel _ as t -> t + | C.Appl [] | C.Appl [_] -> assert false + | C.Appl l as orig -> + let fire_beta, upto = + match l with C.Meta _ :: _ -> true, List.length l - 1 | _ -> false, 0 + in + let l1 = HExtlib.sharing_map (f k) l in + if l == l1 then orig else + let t = + match l1 with + | C.Appl l :: tl -> C.Appl (l@tl) + | l1 -> C.Appl l1 + in + if fire_beta then !head_beta_reduce ~upto t + else t + | C.Prod (n,s,t) as orig -> + let s1 = f k s in let t1 = f (g (n,C.Decl s) k) t in + if t1 == t && s1 == s then orig else C.Prod (n,s1,t1) + | C.Lambda (n,s,t) as orig -> + let s1 = f k s in let t1 = f (g (n,C.Decl s) k) t in + if t1 == t && s1 == s then orig else C.Lambda (n,s1,t1) + | C.LetIn (n,ty,t,b) as orig -> + let ty1 = f k ty in let t1 = f k t in + let b1 = f (g (n,C.Def (t,ty)) k) b in + if ty1 == ty && t1 == t && b1 == b then orig else C.LetIn (n,ty1,t1,b1) + | C.Match (r,oty,t,pl) as orig -> + let oty1 = f k oty in let t1 = f k t in let pl1 = HExtlib.sharing_map (f k) pl in if oty1 == oty && t1 == t && pl1 == pl then orig - else NCic.Match(r,oty1,t1,pl1) -;; - -exception NotClosed - -let is_closed t = - try - let rec aux k _ = function - | NCic.Rel n when n > k -> raise NotClosed - | NCic.Meta (_, (s, NCic.Irl n)) when not (n+s <= k) -> raise NotClosed - | NCic.Meta (_, (s, NCic.Ctx l)) -> List.iter (aux (k+s) ()) l - | _ -> fold aux ((+) 1) () k t - in - aux 0 () t; true - with NotClosed -> false + else C.Match(r,oty1,t1,pl1) ;; -