]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/lambda-delta/basic_rg/brg.ml
- proper KAM with closures implemented for the brg kernel
[helm.git] / helm / software / lambda-delta / basic_rg / brg.ml
index d62a130a4c526c06e6e9f38fa2b864f11a6c686e..5ad05a72f7da075221bedd10490b63900ddcf438 100644 (file)
 type uri = Item.uri
 type id = Item.id
 
+type attr = Name of bool * id  (* real?, name *)
+          | Apix of int        (* additional position index *)
+
+type attrs = attr list
+
 type bind = Void         (* exclusion *)
           | Abst of term (* abstraction *)
           | Abbr of term (* abbreviation *)
@@ -26,16 +31,13 @@ and term = Sort of attrs * int         (* attrs, hierarchy index *)
          | Appl of attrs * term * term (* attrs, argument, function *)
          | Bind of attrs * bind * term (* attrs, binder, scope *)
 
-and attr = Name of bool * id   (* real?, name *)
-         | Entry of int * bind (* age, binder *)
-
-and attrs = attr list
-
 type obj = bind Item.obj (* age, uri, binder *)
 
 type item = bind Item.item
 
-type context = (attrs * bind) list (* attrs, binder *) 
+type context = Null
+(* Cons: tail, relative context, attrs, binder *) 
+             | Cons of context * context option * attrs * bind 
 
 type message = (context, term) Log.item list
 
@@ -59,30 +61,39 @@ let bind_abbr a v t = Bind (a, Abbr v, t)
 
 (* context handling functions ***********************************************)
 
-let empty_context = []
+let empty_context = Null
 
-let push f es a b =
-   let c = (a, b) :: es in f c
+let push f es ?c a b =
+   let es = Cons (es, c, a, b) in f es
 
-let append f es1 es2 = 
-   f (List.append es2 es1)
-
-let map f map es =
-   Cps.list_map f map es
-
-let contents f es = f es
-
-let get f es i =
-   let rec aux e i = function
-      | []       -> f e None
-      | hd :: tl -> 
-         if i = 0 then f e (Some hd) else 
-        let e = match hd with _, Abst _ -> succ e | _ -> e in  
-        aux e (pred i) tl
+let get err f es i =
+   let rec aux j = function
+      | Null                               -> err i
+      | Cons (tl, None, a, b)   when j = 0 -> f tl a b
+      | Cons (_, Some c, a, b) when j = 0  -> f c a b
+      | Cons (tl, _, _, _)                 -> aux (pred j) tl
    in
-   aux 0 i es
-
-let rec name f = function
-   | []               -> f None
-   | Name (r, n) :: _ -> f (Some (r, n))
-   | _ :: tl          -> name f tl
+   aux i es
+
+let rec rev_iter f map = function   
+   | Null                    -> f ()
+   | Cons (tl, None, a, b)   -> 
+      let f () = map f tl a b in rev_iter f map tl
+   | Cons (tl, Some c, a, b) -> 
+      let f () = map f c a b in rev_iter f map tl
+
+let rec fold_left f map x = function
+   | Null               -> f x
+   | Cons (tl, _, a, b) ->
+      let f x = fold_left f map x tl in
+      map f x a b
+
+let rec name err f = function
+   | []               -> err ()
+   | Name (r, n) :: _ -> f n r
+   | _ :: tl          -> name err f tl
+
+let rec apix err f = function
+   | []          -> err ()
+   | Apix i :: _ -> f i
+   | _ :: tl     -> apix err f tl