]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/basic_rg/brg.ml
basic_rg: bugfix in AST to allow attributes in global entries
[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 = Item.uri
16 type id = Item.id
17
18 type attr = Name of bool * id  (* real?, name *)
19           | Apix of int        (* additional position index *)
20
21 type attrs = attr list
22
23 type bind = Void of attrs        (* attrs       *)
24           | Abst of attrs * term (* attrs, type *)
25           | Abbr of attrs * term (* attrs, body *)
26
27 and term = Sort of attrs * int         (* attrs, hierarchy index *)
28          | LRef of attrs * int         (* attrs, position index *)
29          | GRef of attrs * uri         (* attrs, reference *)
30          | Cast of attrs * term * term (* attrs, type, term *)
31          | Appl of attrs * term * term (* attrs, argument, function *)
32          | Bind of bind * term         (* binder, scope *)
33
34 type obj = bind Item.obj (* age, uri, binder *)
35
36 type item = bind Item.item
37
38 type context = Null
39 (* Cons: tail, relative context, binder *) 
40              | Cons of context * context option * bind 
41
42 type message = (context, term) Log.item list
43
44 (* Currified constructors ***************************************************)
45
46 let abst a w = Abst (a, w)
47
48 let abbr a v = Abbr (a, v)
49
50 let lref a i = LRef (a, i)
51
52 let cast a u t = Cast (a, u, t)
53
54 let appl a u t = Appl (a, u, t)
55
56 let bind b t = Bind (b, t)
57
58 let bind_abst a u t = Bind (Abst (a, u), t)
59
60 let bind_abbr a v t = Bind (Abbr (a, v), t)
61
62 (* context handling functions ***********************************************)
63
64 let empty_context = Null
65
66 let push f es ?c b =
67    let es = Cons (es, c, b) in f es
68
69 let get err f es i =
70    let rec aux j = function
71       | Null                            -> err i
72       | Cons (tl, None, b)   when j = 0 -> f tl b
73       | Cons (_, Some c, b) when j = 0  -> f c b
74       | Cons (tl, _, _)                 -> aux (pred j) tl
75    in
76    aux i es
77
78 let rec rev_iter f map = function   
79    | Null                 -> f ()
80    | Cons (tl, None, b)   -> 
81       let f () = map f tl b in rev_iter f map tl
82    | Cons (tl, Some c, b) -> 
83       let f () = map f c b in rev_iter f map tl
84
85 let rec fold_left f map x = function
86    | Null            -> f x
87    | Cons (tl, _, b) ->
88       let f x = fold_left f map x tl in
89       map f x b
90
91 let rec name err f = function
92    | []               -> err ()
93    | Name (r, n) :: _ -> f n r
94    | _ :: tl          -> name err f tl
95
96 let rec apix err f = function
97    | []          -> err ()
98    | Apix i :: _ -> f i
99    | _ :: tl     -> apix err f tl