(* ||M|| This file is part of HELM, an Hypertextual, Electronic ||A|| Library of Mathematics, developed at the Computer Science ||T|| Department, University of Bologna, Italy. ||I|| ||T|| HELM is free software; you can redistribute it and/or ||A|| modify it under the terms of the GNU General Public License \ / version 2 or (at your option) any later version. \ / This software is distributed as is, NO WARRANTY. V_______________________________________________________________ *) (* kernel version: complete, relative, global *) (* note : fragment of complete \lambda\delta serving as abstract layer *) module C = Cps module E = Entity module N = Level type uri = E.uri type id = E.id type attrs = E.attrs 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 *) | 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 empty_lenv = ESort let push_bind f a b lenv = f (EBind (lenv, a, b)) let push_appl f a t lenv = f (EAppl (lenv, a, t)) 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 add_proj f a e t = f (TProj (a, e, t)) 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))) | EBind (e, a, Abst (n, ws)), None, Some t -> f (EBind (e, a, Abst (n, t :: ws))) | EBind (e, a, Abbr vs), Some attr, Some t -> f (EBind (e, (attr :: a), Abbr (t :: vs))) | EBind (e, a, Abbr vs), None, Some t -> f (EBind (e, a, Abbr (t :: vs))) | EBind (e, a, Void n), Some attr, None -> f (EBind (e, (attr :: a), Void (succ n))) | EBind (e, a, Void n), None, None -> f (EBind (e, a, Void (succ n))) | _ -> err () let get_index err f i j lenv = let rec aux f i k = function | ESort -> err i | EBind (tl, _, Abst (_, [])) | EBind (tl, _, Abbr []) | EBind (tl, _, Void 0) -> aux f i k tl | EBind (_, a, _) when i = 0 -> if E.count_names a > j then f (k + j) else err i | EBind (tl, a, _) -> aux f (pred i) (k + E.count_names a) tl | 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 e i = match e with | ESort -> ESort, [], Void | EBind (e, a, b) when i = 0 -> e, a, b | 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