]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/complete_rg/crg.ml
update in helena
[helm.git] / helm / software / helena / src / complete_rg / crg.ml
1 (*
2     ||M||  This file is part of HELM, an Hypertextual, Electronic        
3     ||A||  Library of Mathematics, developed at the Computer Science     
4     ||T||  Department, University of Bologna, Italy.                     
5     ||I||                                                                
6     ||T||  HELM is free software; you can redistribute it and/or         
7     ||A||  modify it under the terms of the GNU General Public License   
8     \   /  version 2 or (at your option) any later version.              
9      \ /   This software is distributed as is, NO WARRANTY.              
10       V_______________________________________________________________ *)
11
12 (* kernel version: complete, relative, global *)
13 (* note          : fragment of complete \lambda\delta serving as abstract layer *) 
14
15 module C = Cps
16 module N = Layer
17 module E = Entity
18
19 type uri = E.uri
20 type id = E.id
21 type n_attrs = E.node_attrs
22 type a_attrs = E.appl_attrs
23 type b_attrs = E.bind_attrs
24
25
26 type bind = Abst of bool * N.layer * term           (* x-reduced?, layer, type *)
27           | Abbr of term                            (* body *)
28           | Void                                    (* *)
29
30 and term = TSort of int                             (* hierarchy index *)
31          | TLRef of n_attrs * int                   (* attrs, position indexe *)
32          | TGRef of n_attrs * uri                   (* attrs, reference *)
33          | TCast of term * term                     (* domain, element *)
34          | TAppl of a_attrs * term * term           (* attrs, argument, function *)
35          | TBind of b_attrs * bind * term           (* attrs, binder, scope *)
36          | TProj of lenv * term                     (* closure, member *)
37
38 and lenv = ESort                                    (* top *)
39          | EBind of lenv * n_attrs * b_attrs * bind (* environment, attrs, binder *)
40          | EAppl of lenv * a_attrs * term           (* environment, attrs, argument *)
41          | EProj of lenv * lenv                     (* environment, closure *)
42
43 type entity = term E.entity
44
45 (* helpers ******************************************************************)
46
47 let empty_lenv = ESort
48
49 let push_bind f a y b lenv = f (EBind (lenv, a, y, b))
50
51 let push_appl f a t lenv = f (EAppl (lenv, a, t))
52
53 let push_proj f e lenv = f (EProj (lenv, e))
54
55 let add_bind f y b t = f (TBind (y, b, t))
56
57 let add_appl f a v t = f (TAppl (a, v, t))
58
59 let add_proj f e t = f (TProj (e, t))
60
61 let rec shift f c t = match c with
62    | ESort              -> f t
63    | EBind (e, _, a, b) -> add_bind (shift f e) a b t
64    | EAppl (e, a, v)    -> add_appl (shift f e) a v t
65    | EProj (e, d)       -> add_proj (shift f e) d t
66
67 let rec append f c = function
68    | ESort              -> f c
69    | EBind (e, y, a, b) -> append (push_bind f y a b) c e
70    | EAppl (e, a, t)    -> append (push_appl f a t) c e
71    | EProj (e, d)       -> append (push_proj f d) c e
72
73 let resolve_lref err f id lenv =
74    let rec aux i = function
75      | ESort                -> err ()
76      | EAppl (tl, _, _)     -> aux i tl
77      | EBind (tl, y, a, _)  ->
78         let f id0 _ = if id0 = id then f y a i else aux (succ i) tl in
79         let err () = aux (succ i) tl in
80         E.name err f a
81      | EProj (tl, d)        -> append (aux i) tl d
82    in
83    aux 0 lenv
84
85 let rec get_name err f i = function
86    | ESort                         -> err i
87    | EAppl (tl, _, _)              -> get_name err f i tl
88    | EBind (_, _, a, _) when i = 0 -> 
89       let err () = err i in
90       E.name err f a
91    | EBind (tl, _, _, _)           -> get_name err f (pred i) tl
92    | EProj (tl, e)                 ->
93       let err i = get_name err f i tl in 
94       get_name err f i e
95
96 let rec get e i = match e with 
97    | ESort                         -> ESort, E.empty_node, E.empty_bind, Void
98    | EBind (e, y, a, b) when i = 0 -> e, y, a, b
99    | EBind (e, _, _, _)            -> get e (pred i)
100    | EAppl (e, _, _)               -> get e i
101    | EProj (e, d)                  -> get (append C.start e d) i
102
103 let rec sub_list_strict f e l = match e, l with
104    | _, []                            -> f e
105    | EBind (e, _, _, Abst _), _ :: tl -> sub_list_strict f e tl
106    | _                                -> assert false
107
108 let rec fold_names f map x = function
109    | ESort                                     -> f x
110    | EBind (e, _, {E.b_name = Some n}, Abst _) -> fold_names f map (map x n) e
111    | _                                         -> assert false
112
113 let rec mem err f e a0 = match e with
114    | ESort              -> err ()
115    | EBind (e, _, a, _) ->
116       if a.E.b_name = a0.E.b_name then f () else mem err f e a0
117    | EAppl (e, _, _)    -> mem err f e a0
118    | EProj (e, d)       -> 
119       let err () = mem err f e a0 in mem err f d a0
120
121 let set_layer f n0 e =
122    let rec aux f = function
123       | ESort                           -> f ESort
124       | EBind (e, y, a, Abst (r, n, w)) -> aux (push_bind f y a (Abst (r, n0, w))) e
125       | EBind (e, y, a, b)              -> aux (push_bind f y a b) e
126       | EAppl (e, a, v)                 -> aux (push_appl f a v) e
127       | EProj (e, d)                    -> let f d = aux (push_proj f d) e in aux f d
128    in
129    aux f e
130
131 let set_attrs f a0 e =
132    let rec aux f = function
133       | ESort              -> f ESort
134       | EBind (e, y, a, b) -> 
135          let a = E.compose a a0 in
136          aux (push_bind f y a b) e
137       | EAppl (e, a, v)    ->
138          aux (push_appl f a v) e
139       | EProj (e, d)       ->
140          let f d = aux (push_proj f d) e in
141          aux f d
142    in
143    aux f e