]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/complete_rg/crg.ml
36a1c8d59bf83cf56932721080a99253ecfd9c38
[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 b_attrs = E.bind_attrs
23
24
25 type bind = Abst of bool * N.layer * term           (* x-reduced?, layer, type *)
26           | Abbr of term                            (* body *)
27           | Void                                    (* *)
28
29 and term = TSort of int                             (* hierarchy index *)
30          | TLRef of n_attrs * int                   (* attrs, position indexe *)
31          | TGRef of n_attrs * uri                   (* attrs, reference *)
32          | TCast of term * term                     (* domain, element *)
33          | TAppl of bool * term * term              (* extended?, argument, function *)
34          | TBind of b_attrs * bind * term           (* attrs, binder, scope *)
35          | TProj of lenv * term                     (* closure, member *)
36
37 and lenv = ESort                                    (* top *)
38          | EBind of lenv * n_attrs * b_attrs * bind (* environment, attrs, binder *)
39          | EAppl of lenv * bool * term              (* environment, extended?, argument *)
40          | EProj of lenv * lenv                     (* environment, closure *)
41
42 type entity = term E.entity
43
44 (* helpers ******************************************************************)
45
46 let empty_lenv = ESort
47
48 let push_bind f a y b lenv = f (EBind (lenv, a, y, b))
49
50 let push_appl f x t lenv = f (EAppl (lenv, x, t))
51
52 let push_proj f e lenv = f (EProj (lenv, e))
53
54 let add_bind f y b t = f (TBind (y, b, t))
55
56 let add_appl f x v t = f (TAppl (x, v, t))
57
58 let add_proj f e t = f (TProj (e, t))
59
60 let rec shift f c t = match c with
61    | ESort              -> f t
62    | EBind (e, _, y, b) -> add_bind (shift f e) y b t
63    | EAppl (e, x, v)    -> add_appl (shift f e) x v t
64    | EProj (e, d)       -> add_proj (shift f e) d t
65
66 let rec append f c = function
67    | ESort              -> f c
68    | EBind (e, a, y, b) -> append (push_bind f a y b) c e
69    | EAppl (e, x, t)    -> append (push_appl f x t) c e
70    | EProj (e, d)       -> append (push_proj f d) c e
71
72 let resolve_lref err f id lenv =
73    let rec aux i = function
74      | ESort                -> err ()
75      | EAppl (tl, _, _)     -> aux i tl
76      | EBind (tl, a, y, _)  ->
77         let f id0 _ = if id0 = id then f a y i else aux (succ i) tl in
78         let err () = aux (succ i) tl in
79         E.name err f y
80      | EProj (tl, d)        -> append (aux i) tl d
81    in
82    aux 0 lenv
83
84 let rec get_name err f i = function
85    | ESort                         -> err i
86    | EAppl (tl, _, _)              -> get_name err f i tl
87    | EBind (_, _, y, _) when i = 0 -> 
88       let err () = err i in
89       E.name err f y
90    | EBind (tl, _, _, _)           -> get_name err f (pred i) tl
91    | EProj (tl, e)                 ->
92       let err i = get_name err f i tl in 
93       get_name err f i e
94
95 let rec get e i = match e with 
96    | ESort                         -> ESort, E.empty_node, E.empty_bind, Void
97    | EBind (e, a, y, b) when i = 0 -> e, a, y, b
98    | EBind (e, _, _, _)            -> get e (pred i)
99    | EAppl (e, _, _)               -> get e i
100    | EProj (e, d)                  -> get (append C.start e d) i
101
102 let rec sub_list_strict f e l = match e, l with
103    | _, []                            -> f e
104    | EBind (e, _, _, Abst _), _ :: tl -> sub_list_strict f e tl
105    | _                                -> assert false
106
107 let rec fold_names f map x = function
108    | ESort                                     -> f x
109    | EBind (e, _, {E.b_name = Some n}, Abst _) -> fold_names f map (map x n) e
110    | _                                         -> assert false
111
112 let rec mem err f e y0 = match e with
113    | ESort              -> err ()
114    | EBind (e, _, y, _) ->
115       if y.E.b_name = y0.E.b_name then f () else mem err f e y0
116    | EAppl (e, _, _)    -> mem err f e y0
117    | EProj (e, d)       -> 
118       let err () = mem err f e y0 in mem err f d y0
119
120 let set_layer f n0 e =
121    let rec aux f = function
122       | ESort                           -> f ESort
123       | EBind (e, a, y, Abst (r, n, w)) -> aux (push_bind f a y (Abst (r, n0, w))) e
124       | EBind (e, a, y, b)              -> aux (push_bind f a y b) e
125       | EAppl (e, x, v)                 -> aux (push_appl f x v) e
126       | EProj (e, d)                    -> let f d = aux (push_proj f d) e in aux f d
127    in
128    aux f e
129
130 let set_attrs f y0 e =
131    let rec aux f = function
132       | ESort              -> f ESort
133       | EBind (e, a, y, b) -> 
134          let y = E.compose y y0 in
135          aux (push_bind f a y b) e
136       | EAppl (e, x, v)    ->
137          aux (push_appl f x v) e
138       | EProj (e, d)       ->
139          let f d = aux (push_proj f d) e in
140          aux f d
141    in
142    aux f e