]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/components/ng_kernel/nCicUtils.ml
Use seed to avoid further name clashes.
[helm.git] / helm / software / components / ng_kernel / nCicUtils.ml
index 0ff437347894fb4c539c9892d6b04281197ff949..1d43c2cbc3a69e18e18916a354f5ff9040859cd3 100644 (file)
@@ -11,6 +11,9 @@
 
 (* $Id: nCicSubstitution.ml 8135 2008-02-13 15:35:43Z tassi $ *)
 
+exception Subst_not_found of int
+exception Meta_not_found of int
+
 let expand_local_context = function
   | NCic.Irl len -> 
       let rec aux acc = function 
@@ -21,23 +24,29 @@ let expand_local_context = function
   | NCic.Ctx lctx -> lctx
 ;;
 
-exception Subst_not_found of int
-
 let lookup_subst n subst =
   try
     List.assoc n subst
   with Not_found -> raise (Subst_not_found n)
+;;
 
-let fold f g acc k = function
+let lookup_meta index metasenv =
+  try
+    List.find (fun (index', _, _, _) -> index = index') metasenv
+  with Not_found -> raise (Meta_not_found index)
+;;
+
+let fold g k f acc = function
+ | NCic.Meta _ -> assert false
  | NCic.Implicit _
  | NCic.Sort _
  | NCic.Const _
- | NCic.Meta _
  | NCic.Rel _ -> acc
+ | NCic.Appl [] | NCic.Appl [_] -> assert false
  | 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.Prod (n,s,t)
+ | NCic.Lambda (n,s,t) -> f (g (n,NCic.Decl s) k) (f k acc s) t
+ | NCic.LetIn (n,ty,t,bo) -> f (g (n,NCic.Def (t,ty)) 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
 ;;
 
@@ -53,21 +62,27 @@ let sharing_map f l =
   if !unchanged then l else l1
 ;;
         
-let map f g k = function
+let map g k f = function
+ | NCic.Meta _ -> assert false
  | NCic.Implicit _
  | NCic.Sort _
  | NCic.Const _
- | NCic.Meta _
  | NCic.Rel _ as t -> t
- | NCic.Appl l -> NCic.Appl (sharing_map (f k) l)
+ | NCic.Appl [] | NCic.Appl [_] -> assert false
+ | NCic.Appl l as orig ->
+    (match sharing_map (f k) l with
+      | NCic.Appl l :: tl -> NCic.Appl (l@tl)
+      | l1 when l == l1 -> orig
+      | l1 -> NCic.Appl l1)
  | NCic.Prod (n,s,t) as orig ->
-     let s1 = f k s in let t1 = f (g k) t in
+     let s1 = f k s in let t1 = f (g (n,NCic.Decl s) 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)
+     let s1 = f k s in let t1 = f (g (n,NCic.Decl s) k) t in
+     if t1 == t && s1 == s then orig else NCic.Lambda (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
+     let ty1 = f k ty in let t1 = f k t in
+     let b1 = f (g (n,NCic.Def (t,ty)) 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
@@ -83,7 +98,7 @@ let is_closed t =
       | 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
+      | _ -> fold (fun _ k -> k + 1) k aux () t
     in 
       aux 0 () t; true
   with NotClosed -> false