]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/complete_rg/crg.ml
Additional contribs.
[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 root s =
40    String.concat "/" ["ld:"; "crg"; root; s ^ ".ld"]
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, t with
49    | EBind (e, a, Abst ws), Some t -> f (EBind (e, (attr :: a), Abst (t :: ws)))
50    | EBind (e, a, Abbr vs), Some t -> f (EBind (e, (attr :: a), Abbr (t :: vs)))
51    | EBind (e, a, Void n), None    -> f (EBind (e, (attr :: a), Void (succ n)))
52    | _                             -> err ()
53
54 (* this id not tail recursive *)
55 let resolve_lref err f id lenv =
56    let rec aux f i k = function
57      | ESort            -> err ()
58      | EBind (tl, a, _) ->
59         let err kk = aux f (succ i) (k + kk) tl in
60         let f j = f i j (k + j) in
61         Entity.resolve err f id a
62      | EProj _          -> assert false (* TODO *)
63    in
64    aux f 0 0 lenv
65
66 let rec get_name err f i j = function
67    | ESort                      -> err i
68    | EBind (tl, a, Abst [])     -> get_name err f i j tl
69    | EBind (tl, a, Abbr [])     -> get_name err f i j tl
70    | EBind (tl, a, Void 0)      -> get_name err f i j tl
71    | EBind (_, a, _) when i = 0 -> 
72       let err () = err i in
73       Entity.get_name err f j a
74    | EBind (tl, _, _)           -> 
75       get_name err f (pred i) j tl
76    | EProj (tl, _, e)           ->
77       let err i = get_name err f i j tl in 
78       get_name err f i j e