]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/basic_ag/bag.ml
74c305156ecb3c609b06a563ed95b2828d6e83fd
[helm.git] / helm / software / helena / src / basic_ag / bag.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, absolute, global *)
13 (* note          : experimental *) 
14
15 module J = Marks
16 module E = Entity
17
18 type uri = E.uri
19 type attrs = E.node_attrs
20
21 type bind = Void         (* exclusion *)
22           | Abst of term (* abstraction *)
23           | Abbr of term (* abbreviation *)
24
25 and term = Sort of int                          (* hierarchy index *)
26          | LRef of J.mark                       (* location *)
27          | GRef of uri                          (* reference *)
28          | Cast of term * term                  (* domain, element *)
29          | Appl of term * term                  (* argument, function *)
30          | Bind of attrs * J.mark * bind * term (* name, location, binder, scope *)
31
32 type entity = term E.entity (* attrs, uri, binder *)
33
34 type lenv = (attrs * J.mark * bind) list (* name, location, binder *) 
35
36 type message = (lenv, term) Log.item list
37
38 (* Currified constructors ***************************************************)
39
40 let abst w = Abst w
41
42 let abbr v = Abbr v
43
44 let lref i = LRef i
45
46 let cast u t = Cast (u, t)
47
48 let appl u t = Appl (u, t)
49
50 let bind a l b t = Bind (a, l, b, t)
51
52 let bind_abst a l u t = Bind (a, l, Abst u, t)
53
54 let bind_abbr a l v t = Bind (a, l, Abbr v, t)
55
56 (* local environment handling functions *************************************)
57
58 let empty_lenv = []
59
60 let push msg f es a l b =
61    let rec does_not_occur loc = function
62       | []                          -> true
63       | (_, l, _) :: _ when l = loc -> false
64       | _ :: es                     -> does_not_occur l es
65    in
66    if not (does_not_occur l es) then failwith msg else
67    let c = (a, l, b) :: es in f c
68
69 let append f es1 es2 = 
70    f (List.append es2 es1)
71
72 let map f map es =
73    Cps.list_map f map es
74
75 let contents f es = f es
76
77 let get err f es i =
78    let rec aux = function
79       | []              -> err ()
80       | (a, l, b) :: tl -> if l = i then f a b else aux tl
81    in
82    aux es
83
84 let nth err f loc e =
85    let rec aux n = function
86       | []                          -> err loc
87       | (_, l, _) :: _ when l = loc -> f n
88       | _ :: e                      -> aux (succ n) e
89    in
90    aux 0 e