]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/common/entity.ml
7b8dab9508c23c6351445d4f0d5b56c88693d992
[helm.git] / helm / software / lambda-delta / common / entity.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 type uri = NUri.uri
13 type id = Aut.id
14
15 type attr = Name of id * bool (* name, real? *)
16           | Apix of int       (* additional position index *)
17           | Mark of int       (* node marker *)
18           | Priv              (* private global definition *)
19
20 type attrs = attr list (* attributes *)
21
22 type 'term bind = Abst of 'term (* declaration: domain *)
23                 | Abbr of 'term (* definition: body *)
24
25 type 'term entity = attrs * uri * 'term bind (* attrs, name, binder *)
26
27 type uri_generator = string -> string (* this could be in CPS *) 
28
29 (* helpers ******************************************************************)
30
31 let common f (a, u, _) = f a u
32
33 let rec name err f = function
34    | Name (n, r) :: _ -> f n r
35    | _ :: tl          -> name err f tl
36    | []               -> err ()
37
38 let names f map l a =
39    let rec aux f i a = function   
40       | []                -> f a
41       | Name (n, r) :: tl -> aux (map f i n r) false a tl
42       | _ :: tl           -> aux f i a tl
43    in
44    aux f true a l
45
46 let rec get_name err f j = function
47    | []                          -> err ()
48    | Name (n, r) :: _ when j = 0 -> f n r
49    | Name _ :: tl                -> get_name err f (pred j) tl
50    | _ :: tl                     -> get_name err f j tl
51
52 let rec get_names f = function
53    | []                -> f [] []
54    | Name _ as n :: tl ->
55       let f a ns = f a (n :: ns) in get_names f tl
56    | e :: tl           ->
57       let f a = f (e :: a) in get_names f tl
58
59 let rec apix err f = function
60    | Apix i :: _ -> f i
61    | _ :: tl     -> apix err f tl
62    | []          -> err ()
63
64 let rec mark err f = function
65    | Mark i :: _ -> f i
66    | _ :: tl     -> mark err f tl
67    | []          -> err ()
68
69 let rec priv err f = function
70    | Priv :: _ -> f ()
71    | _ :: tl   -> priv err f tl
72    | []        -> err ()
73
74 let resolve err f name a =
75    let rec aux i = function
76       | Name (n, true) :: _ when n = name -> f i
77       | _ :: tl                           -> aux (succ i) tl
78       | []                                -> err i
79    in
80    aux 0 a
81
82 let xlate f xlate_term = function
83    | a, uri, Abst t ->
84       let f t = f (a, uri, Abst t) in xlate_term f t
85    | a, uri, Abbr t ->
86       let f t = f (a, uri, Abbr t) in xlate_term f t