]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/helena/src/complete_rg/crg.ml
new intermediate language complete_rg,
[helm.git] / helm / software / helena / src / complete_rg / crg.ml
index 9518ff60bc1889556e03255c43b088afd03af5e2..7ba80ddfb39a7c01156f656b75fc41f7b37c31c0 100644 (file)
@@ -20,44 +20,76 @@ type uri = E.uri
 type id = E.id
 type attrs = E.attrs
 
-type bind = Abst of N.level * term list (* level, domains *)
-          | Abbr of term list           (* bodies *)
-          | Void of int                 (* number of exclusions *)
-
-and term = TSort of attrs * int              (* attrs, hierarchy index *)
-         | TLRef of attrs * int * int        (* attrs, position indexes *)
-         | TGRef of attrs * uri              (* attrs, reference *)
-         | TCast of attrs * term * term      (* attrs, domain, element *)
-         | TAppl of attrs * term list * term (* attrs, arguments, function *)
-        | TProj of attrs * lenv * term      (* attrs, closure, member *)
-        | TBind of attrs * bind * term      (* attrs, binder, scope *)
+type bind = Abst of N.level * term      (* level, type *)
+          | Abbr of term                (* body *)
+          | Void                        (* *)
+
+and term = TSort of attrs * int         (* attrs, hierarchy index *)
+         | TLRef of attrs * int         (* attrs, position indexe *)
+         | TGRef of attrs * uri         (* attrs, reference *)
+         | TCast of attrs * term * term (* attrs, domain, element *)
+         | TAppl of attrs * term * term (* attrs, argument, function *)
+        | TBind of attrs * bind * term (* attrs, binder, scope *)
+        | TProj of attrs * lenv * term (* attrs, closure, member *)
 
 and lenv = ESort                        (* top *)
-         | EProj of lenv * attrs * lenv (* environment, attrs, closure *) 
          | EBind of lenv * attrs * bind (* environment, attrs, binder *)
+         | EAppl of lenv * attrs * term (* environment, attrs, argument *)
+         | EProj of lenv * attrs * lenv (* environment, attrs, closure *) 
 
 type entity = term E.entity
 
 (* helpers ******************************************************************)
 
-let rec tshift t = function
-   | ESort           -> t
-   | EBind (e, a, b) -> tshift (TBind (a, b, t)) e
-   | EProj (e, a, d) -> tshift (TProj (a, d, t)) e
+let empty_lenv = ESort
 
-let tshift c t = tshift t c
+let push_bind f a b lenv = f (EBind (lenv, a, b))
 
-let rec eshift f c = function
-   | ESort           -> f c
-   | EBind (e, a, b) -> let f ee = f (EBind (ee, a, b)) in eshift f c e
-   | EProj (e, a, d) -> let f ee = f (EProj (ee, a, d)) in eshift f c e
+let push_appl f a t lenv = f (EAppl (lenv, a, t))
 
-let empty_lenv = ESort
+let push_proj f a e lenv = f (EProj (lenv, a, e))
+
+let add_bind f a b t = f (TBind (a, b, t))
+
+let add_appl f a v t = f (TAppl (a, v, t))
 
-let push_bind f lenv a b = f (EBind (lenv, a, b))
+let add_proj f a e t = f (TProj (a, e, t))
 
-let push_proj f lenv a e = f (EProj (lenv, a, e))
+let rec shift f c t = match c with
+   | ESort           -> f t
+   | EBind (e, a, b) -> add_bind (shift f e) a b t
+   | EAppl (e, a, v) -> add_appl (shift f e) a v t
+   | EProj (e, a, d) -> add_proj (shift f e) a d t
+
+let rec append f c = function
+   | ESort           -> f c
+   | EBind (e, a, b) -> append (push_bind f a b) c e
+   | EAppl (e, a, t) -> append (push_appl f a t) c e
+   | EProj (e, a, d) -> append (push_proj f a d) c e
+
+let resolve_lref err f id lenv =
+   let rec aux i = function
+     | ESort             -> err ()
+     | EAppl (tl, _, _)  -> aux i tl
+     | EBind (tl, a, _)  ->
+        let f id0 _ = if id0 = id then f i else aux (succ i) tl in
+        E.name err f a 
+     | EProj (tl, _, d)  -> append (aux i) tl d
+   in
+   aux 0 lenv
+
+let rec get_name err f i = function
+   | ESort                      -> err i
+   | EAppl (tl, _, _)           -> get_name err f i tl
+   | EBind (_, a, _) when i = 0 -> 
+      let err () = err i in
+      E.name err f a
+   | EBind (tl, _, _)           -> get_name err f (pred i) tl
+   | EProj (tl, _, e)           ->
+      let err i = get_name err f i tl in 
+      get_name err f i e
 
+(*
 let push2 err f lenv ?attr ?t () = match lenv, attr, t with
    | EBind (e, a, Abst (n, ws)), Some attr, Some t -> 
       f (EBind (e, (attr :: a), Abst (n, t :: ws)))
@@ -73,35 +105,6 @@ let push2 err f lenv ?attr ?t () = match lenv, attr, t with
       f (EBind (e, a, Void (succ n)))
    | _                                             -> err ()
 
-(* this id not tail recursive *)
-let resolve_lref err f id lenv =
-   let rec aux f i k = function
-     | ESort                  -> err ()
-     | EBind (tl, _, Abst (_, []))
-     | EBind (tl, _, Abbr [])
-     | EBind (tl, _, Void 0)  -> aux f i k tl
-     | EBind (tl, a, b)       ->
-       let err kk = aux f (succ i) (k + kk) tl in
-       let f j = f i j (k + j) in
-       E.resolve err f id a
-     | EProj (tl, _, d)       -> aux f i k (eshift C.start tl d)
-   in
-   aux f 0 0 lenv
-
-let rec get_name err f i j = function
-   | ESort                      -> err i
-   | EBind (tl, _, Abst (_, []))
-   | EBind (tl, _, Abbr [])
-   | EBind (tl, _, Void 0)      -> get_name err f i j tl
-   | EBind (_, a, _) when i = 0 -> 
-      let err () = err i in
-      E.get_name err f j a
-   | EBind (tl, _, _)           -> 
-      get_name err f (pred i) j tl
-   | EProj (tl, _, e)           ->
-      let err i = get_name err f i j tl in 
-      get_name err f i j e
-
 let get_index err f i j lenv =
    let rec aux f i k = function
       | ESort                      -> err i
@@ -115,19 +118,26 @@ let get_index err f i j lenv =
       | EProj (tl, _, d)           -> aux f i k (eshift C.start tl d)
    in
    aux f i 0 lenv
-
+*)
 let rec names_of_lenv ns = function
    | ESort            -> ns
    | EBind (tl, a, _) -> names_of_lenv (E.rev_append_names ns a) tl
+   | EAppl (tl, _, _) -> names_of_lenv ns tl
    | EProj (tl, _, e) -> names_of_lenv (names_of_lenv ns e) tl
 
-let rec get i = function
-   | ESort                      -> ESort, [], Void 0 
-   | EBind (e, _, Abst (_, []))
-   | EBind (e, _, Abbr [])
-   | EBind (e, _, Void 0)       -> get i e
+let rec get e i = match e with 
+   | ESort                      -> ESort, [], Void
    | EBind (e, a, b) when i = 0 -> e, a, b
-   | EBind (e, _, _)            -> get (pred i) e
-   | EProj (e, _, d)            -> get i (eshift C.start e d)
-let get e i = get i e
+   | EBind (e, _, _)            -> get e (pred i)
+   | EAppl (e, _, _)            -> get e i
+   | EProj (e, _, d)            -> get (append C.start e d) i
+
+let rec sub_list_strict f e l = match e, l with
+   | _, []                         -> f e
+   | EBind (e, _, Abst _), _ :: tl -> sub_list_strict f e tl
+   | _                             -> assert false
+
+let rec fold_attrs f map a0 = function
+   | ESort                -> f a0
+   | EBind (e, a, Abst _) -> fold_attrs f map (map a0 a) e
+   | _                    -> assert false