]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/src/complete_rg/crg.ml
the old intermediate language (meta) is now obsolete
[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 empty_lenv = ESort
43
44 let push_bind f lenv a b = f (EBind (lenv, a, b))
45
46 let push_proj f lenv a e = f (EProj (lenv, a, e))
47
48 let push2 err f lenv ?attr ?t () = match lenv, attr, t with
49    | EBind (e, a, Abst (n, ws)), Some attr, Some t -> 
50       f (EBind (e, (attr :: a), Abst (n, t :: ws)))
51    | EBind (e, a, Abst (n, ws)), None, Some t      -> 
52       f (EBind (e, a, Abst (n, t :: ws)))
53    | EBind (e, a, Abbr vs), Some attr, Some t      ->
54       f (EBind (e, (attr :: a), Abbr (t :: vs)))
55    | EBind (e, a, Abbr vs), None, Some t           ->
56       f (EBind (e, a, Abbr (t :: vs)))
57    | EBind (e, a, Void n), Some attr, None         ->
58       f (EBind (e, (attr :: a), Void (succ n)))
59    | EBind (e, a, Void n), None, None              ->
60       f (EBind (e, a, Void (succ n)))
61    | _                                             -> err ()
62
63 (* this id not tail recursive *)
64 let resolve_lref err f id lenv =
65    let rec aux f i k = function
66      | ESort                  -> err ()
67      | EBind (tl, _, Abst (_, []))
68      | EBind (tl, _, Abbr [])
69      | EBind (tl, _, Void 0)  -> aux f i k tl
70      | EBind (tl, a, b)       ->
71         let err kk = aux f (succ i) (k + kk) tl in
72         let f j = f i j (k + j) in
73         E.resolve err f id a
74      | EProj _                -> assert false (* TODO *)
75    in
76    aux f 0 0 lenv
77
78 let rec get_name err f i j = function
79    | ESort                      -> err i
80    | EBind (tl, _, Abst (_, []))
81    | EBind (tl, _, Abbr [])
82    | EBind (tl, _, Void 0)      -> get_name err f i j tl
83    | EBind (_, a, _) when i = 0 -> 
84       let err () = err i in
85       E.get_name err f j a
86    | EBind (tl, _, _)           -> 
87       get_name err f (pred i) j tl
88    | EProj (tl, _, e)           ->
89       let err i = get_name err f i j tl in 
90       get_name err f i j e
91
92 let get_index err f i j lenv =
93    let rec aux f i k = function
94       | ESort                      -> err i
95       | EBind (tl, _, Abst (_, []))
96       | EBind (tl, _, Abbr [])
97       | EBind (tl, _, Void 0)      -> aux f i k tl
98       | EBind (_, a, _) when i = 0 ->
99          if E.count_names a > j then f (k + j) else err i
100       | EBind (tl, a, _)           -> 
101          aux f (pred i) (k + E.count_names a) tl
102       | EProj _                    -> assert false (* TODO *)
103    in
104    aux f i 0 lenv
105
106 let rec names_of_lenv ns = function
107    | ESort            -> ns
108    | EBind (tl, a, _) -> names_of_lenv (E.rev_append_names ns a) tl
109    | EProj (tl, _, e) -> names_of_lenv (names_of_lenv ns e) tl
110
111 let rec get i = function
112    | ESort                      -> ESort, [], Void 0 
113    | EBind (e, _, Abst (_, []))
114    | EBind (e, _, Abbr [])
115    | EBind (e, _, Void 0)       -> get i e
116    | EBind (e, a, b) when i = 0 -> e, a, b
117    | EBind (e, _, _)            -> get (pred i) e
118    | EProj _                    -> assert false (* TODO *)
119  
120 let get e i = get i e