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