]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/basic_rg/brg.ml
e7768238128dda734fb8202e7090bb72e4c7e222
[helm.git] / helm / software / lambda-delta / basic_rg / brg.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: basic, relative, global *)
13 (* note          : ufficial basic lambda-delta *) 
14
15 type uri = Entity.uri
16 type id = Entity.id
17 type attrs = Entity.attrs
18
19 type bind = Void of attrs        (* attrs       *)
20           | Abst of attrs * term (* attrs, type *)
21           | Abbr of attrs * term (* attrs, body *)
22
23 and term = Sort of attrs * int         (* attrs, hierarchy index *)
24          | LRef of attrs * int         (* attrs, position index *)
25          | GRef of attrs * uri         (* attrs, reference *)
26          | Cast of attrs * term * term (* attrs, type, term *)
27          | Appl of attrs * term * term (* attrs, argument, function *)
28          | Bind of bind * term         (* binder, scope *)
29
30 type entity = term Entity.entity (* attrs, uri, binder *)
31
32 type lenv = Null
33 (* Cons: tail, relative local environment, binder *) 
34              | Cons of lenv * lenv option * bind 
35
36 (* helpers ******************************************************************)
37
38 let mk_uri root s =
39    String.concat "/" ["ld:"; "brg"; root; s ^ ".ld"]
40
41 (* Currified constructors ***************************************************)
42
43 let abst a w = Abst (a, w)
44
45 let abbr a v = Abbr (a, v)
46
47 let lref a i = LRef (a, i)
48
49 let cast a u t = Cast (a, u, t)
50
51 let appl a u t = Appl (a, u, t)
52
53 let bind b t = Bind (b, t)
54
55 let bind_abst a u t = Bind (Abst (a, u), t)
56
57 let bind_abbr a v t = Bind (Abbr (a, v), t)
58
59 (* local environment handling functions *************************************)
60
61 let empty_lenv = Null
62
63 let push es ?c b = Cons (es, c, b)
64
65 let get err f es i =
66    let rec aux j = function
67       | Null                            -> err ()
68       | Cons (tl, None, b)   when j = 0 -> f tl b
69       | Cons (_, Some c, b) when j = 0  -> f c b
70       | Cons (tl, _, _)                 -> aux (pred j) tl
71    in
72    aux i es
73
74 (* check closure *)
75 let rec rev_iter f map = function   
76    | Null                 -> f ()
77    | Cons (tl, None, b)   -> 
78       let f _ = map f tl b in rev_iter f map tl
79    | Cons (tl, Some c, b) -> 
80       let f _ = map f c b in rev_iter f map tl
81
82 let rec fold_left map x = function
83    | Null            -> x
84    | Cons (tl, _, b) -> fold_left map (map x b) tl