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