]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/complete_rg/crg.ml
- we simplified the parser return values
[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, a, _)       ->
60         let err kk = aux f (succ i) (k + kk) tl in
61         let f j = f i j (k + j) in
62         Entity.resolve err f id a
63      | EProj _                -> assert false (* TODO *)
64    in
65    aux f 0 0 lenv
66
67 let rec get_name err f i j = function
68    | ESort                      -> err i
69    | EBind (_, a, _) when i = 0 -> 
70       let err () = err i in
71       Entity.get_name err f j a
72    | EBind (tl, _, _)           -> 
73       get_name err f (pred i) j tl
74    | EProj (tl, _, e)           ->
75       let err i = get_name err f i j tl in 
76       get_name err f i j e
77
78 let get_index err f i j lenv =
79    let rec aux f i k = function
80       | ESort                      -> err i
81       | EBind (_, a, _) when i = 0 ->
82          if Entity.count_names a > j then f (k + j) else err i
83       | EBind (tl, a, _)           -> 
84          aux f (pred i) (k + Entity.count_names a) tl
85       | EProj _                    -> assert false (* TODO *)
86    in
87    aux f i 0 lenv