]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/src/complete_rg/crg.ml
cc93d6dc3fdac485a014a5e046cd698978e18b1c
[helm.git] / helm / software / lambda-delta / 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 E = Entity
16
17 type uri = E.uri
18 type id = E.id
19 type attrs = E.attrs
20
21 type bind = Abst of term list (* domains *)
22           | Abbr of term list (* bodies  *)
23           | Void of int       (* number of exclusions *)
24
25 and term = TSort of attrs * int              (* attrs, hierarchy index *)
26          | TLRef of attrs * int * int        (* attrs, position indexes *)
27          | TGRef of attrs * uri              (* attrs, reference *)
28          | TCast of attrs * term * term      (* attrs, domain, element *)
29          | TAppl of attrs * term list * term (* attrs, arguments, function *)
30          | TProj of attrs * lenv * term      (* attrs, closure, member *)
31          | TBind of attrs * bind * term      (* attrs, binder, scope *)
32
33 and lenv = ESort                        (* top *)
34          | EProj of lenv * attrs * lenv (* environment, attrs, closure *) 
35          | EBind of lenv * attrs * bind (* environment, attrs, binder *)
36
37 type entity = term E.entity
38
39 (* helpers ******************************************************************)
40
41 let mk_uri si root s =
42    let kernel = if si then "crg-si" else "crg" in
43    String.concat "/" ["ld:"; kernel; root; s ^ ".ld"]
44
45 let empty_lenv = ESort
46
47 let push_bind f lenv a b = f (EBind (lenv, a, b))
48
49 let push_proj f lenv a e = f (EProj (lenv, a, e))
50
51 let push2 err f lenv attr ?t () = match lenv, t with
52    | EBind (e, a, Abst ws), Some t -> f (EBind (e, (attr :: a), Abst (t :: ws)))
53    | EBind (e, a, Abbr vs), Some t -> f (EBind (e, (attr :: a), Abbr (t :: vs)))
54    | EBind (e, a, Void n), None    -> f (EBind (e, (attr :: a), Void (succ n)))
55    | _                             -> err ()
56
57 (* this id not tail recursive *)
58 let resolve_lref err f id lenv =
59    let rec aux f i k = function
60      | ESort                  -> err ()
61      | EBind (tl, _, Abst [])
62      | EBind (tl, _, Abbr [])
63      | EBind (tl, _, Void 0)  -> aux f i k tl
64      | EBind (tl, a, _)       ->
65         let err kk = aux f (succ i) (k + kk) tl in
66         let f j = f i j (k + j) in
67         E.resolve err f id a
68      | EProj _                -> assert false (* TODO *)
69    in
70    aux f 0 0 lenv
71
72 let rec get_name err f i j = function
73    | ESort                      -> err i
74    | EBind (tl, _, Abst [])
75    | EBind (tl, _, Abbr [])
76    | EBind (tl, _, Void 0)      -> get_name err f i j tl
77    | EBind (_, a, _) when i = 0 -> 
78       let err () = err i in
79       E.get_name err f j a
80    | EBind (tl, _, _)           -> 
81       get_name err f (pred i) j tl
82    | EProj (tl, _, e)           ->
83       let err i = get_name err f i j tl in 
84       get_name err f i j e
85
86 let get_index err f i j lenv =
87    let rec aux f i k = function
88       | ESort                      -> err i
89       | EBind (tl, _, Abst [])
90       | EBind (tl, _, Abbr [])
91       | EBind (tl, _, Void 0)      -> aux f i k tl
92       | EBind (_, a, _) when i = 0 ->
93          if E.count_names a > j then f (k + j) else err i
94       | EBind (tl, a, _)           -> 
95          aux f (pred i) (k + E.count_names a) tl
96       | EProj _                    -> assert false (* TODO *)
97    in
98    aux f i 0 lenv
99
100 let rec names_of_lenv ns = function
101    | ESort            -> ns
102    | EBind (tl, a, _) -> names_of_lenv (E.rev_append_names ns a) tl
103    | EProj (tl, _, e) -> names_of_lenv (names_of_lenv ns e) tl