]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/complete_rg/crg.ml
- dehypenation involves helena as well
[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 E = Entity
17 module N = Level
18
19 type uri = E.uri
20 type id = E.id
21 type attrs = E.attrs
22
23 type bind = Abst of N.level * term list (* level, domains *)
24           | Abbr of term list           (* bodies *)
25           | Void of int                 (* number of exclusions *)
26
27 and term = TSort of attrs * int              (* attrs, hierarchy index *)
28          | TLRef of attrs * int * int        (* attrs, position indexes *)
29          | TGRef of attrs * uri              (* attrs, reference *)
30          | TCast of attrs * term * term      (* attrs, domain, element *)
31          | TAppl of attrs * term list * term (* attrs, arguments, function *)
32          | TProj of attrs * lenv * term      (* attrs, closure, member *)
33          | TBind of attrs * bind * term      (* attrs, binder, scope *)
34
35 and lenv = ESort                        (* top *)
36          | EProj of lenv * attrs * lenv (* environment, attrs, closure *) 
37          | EBind of lenv * attrs * bind (* environment, attrs, binder *)
38
39 type entity = term E.entity
40
41 (* helpers ******************************************************************)
42
43 let rec tshift t = function
44    | ESort           -> t
45    | EBind (e, a, b) -> tshift (TBind (a, b, t)) e
46    | EProj (e, a, d) -> tshift (TProj (a, d, t)) e
47
48 let tshift c t = tshift t c
49
50 let rec eshift f c = function
51    | ESort           -> f c
52    | EBind (e, a, b) -> let f ee = f (EBind (ee, a, b)) in eshift f c e
53    | EProj (e, a, d) -> let f ee = f (EProj (ee, a, d)) in eshift f c e
54
55 let empty_lenv = ESort
56
57 let push_bind f lenv a b = f (EBind (lenv, a, b))
58
59 let push_proj f lenv a e = f (EProj (lenv, a, e))
60
61 let push2 err f lenv ?attr ?t () = match lenv, attr, t with
62    | EBind (e, a, Abst (n, ws)), Some attr, Some t -> 
63       f (EBind (e, (attr :: a), Abst (n, t :: ws)))
64    | EBind (e, a, Abst (n, ws)), None, Some t      -> 
65       f (EBind (e, a, Abst (n, t :: ws)))
66    | EBind (e, a, Abbr vs), Some attr, Some t      ->
67       f (EBind (e, (attr :: a), Abbr (t :: vs)))
68    | EBind (e, a, Abbr vs), None, Some t           ->
69       f (EBind (e, a, Abbr (t :: vs)))
70    | EBind (e, a, Void n), Some attr, None         ->
71       f (EBind (e, (attr :: a), Void (succ n)))
72    | EBind (e, a, Void n), None, None              ->
73       f (EBind (e, a, Void (succ n)))
74    | _                                             -> err ()
75
76 (* this id not tail recursive *)
77 let resolve_lref err f id lenv =
78    let rec aux f i k = function
79      | ESort                  -> err ()
80      | EBind (tl, _, Abst (_, []))
81      | EBind (tl, _, Abbr [])
82      | EBind (tl, _, Void 0)  -> aux f i k tl
83      | EBind (tl, a, b)       ->
84         let err kk = aux f (succ i) (k + kk) tl in
85         let f j = f i j (k + j) in
86         E.resolve err f id a
87      | EProj (tl, _, d)       -> aux f i k (eshift C.start tl d)
88    in
89    aux f 0 0 lenv
90
91 let rec get_name err f i j = function
92    | ESort                      -> err i
93    | EBind (tl, _, Abst (_, []))
94    | EBind (tl, _, Abbr [])
95    | EBind (tl, _, Void 0)      -> get_name err f i j tl
96    | EBind (_, a, _) when i = 0 -> 
97       let err () = err i in
98       E.get_name err f j a
99    | EBind (tl, _, _)           -> 
100       get_name err f (pred i) j tl
101    | EProj (tl, _, e)           ->
102       let err i = get_name err f i j tl in 
103       get_name err f i j e
104
105 let get_index err f i j lenv =
106    let rec aux f i k = function
107       | ESort                      -> err i
108       | EBind (tl, _, Abst (_, []))
109       | EBind (tl, _, Abbr [])
110       | EBind (tl, _, Void 0)      -> aux f i k tl
111       | EBind (_, a, _) when i = 0 ->
112          if E.count_names a > j then f (k + j) else err i
113       | EBind (tl, a, _)           -> 
114          aux f (pred i) (k + E.count_names a) tl
115       | EProj (tl, _, d)           -> aux f i k (eshift C.start tl d)
116    in
117    aux f i 0 lenv
118
119 let rec names_of_lenv ns = function
120    | ESort            -> ns
121    | EBind (tl, a, _) -> names_of_lenv (E.rev_append_names ns a) tl
122    | EProj (tl, _, e) -> names_of_lenv (names_of_lenv ns e) tl
123
124 let rec get i = function
125    | ESort                      -> ESort, [], Void 0 
126    | EBind (e, _, Abst (_, []))
127    | EBind (e, _, Abbr [])
128    | EBind (e, _, Void 0)       -> get i e
129    | EBind (e, a, b) when i = 0 -> e, a, b
130    | EBind (e, _, _)            -> get (pred i) e
131    | EProj (e, _, d)            -> get i (eshift C.start e d)
132  
133 let get e i = get i e