]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/common/entity.ml
3aa1ef6b59cfb086430cd408d734d64ccc36898b
[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 rec name err f = function
32    | Name (n, r) :: _ -> f n r
33    | _ :: tl          -> name err f tl
34    | []               -> err ()
35
36 let names f map l a =
37    let rec aux f i a = function   
38       | []                -> f a
39       | Name (n, r) :: tl -> aux (map f i n r) false a tl
40       | _ :: tl           -> aux f i a tl
41    in
42    aux f true a l
43
44 let rec get_name err f j = function
45    | []                          -> err ()
46    | Name (n, r) :: _ when j = 0 -> f n r
47    | Name _ :: tl                -> get_name err f (pred j) tl
48    | _ :: tl                     -> get_name err f j tl
49
50 let rec get_names f = function
51    | []                -> f [] []
52    | Name _ as n :: tl ->
53       let f a ns = f a (n :: ns) in get_names f tl
54    | e :: tl           ->
55       let f a = f (e :: a) in get_names f tl
56
57 let rec apix err f = function
58    | Apix i :: _ -> f i
59    | _ :: tl     -> apix err f tl
60    | []          -> err ()
61
62 let rec mark err f = function
63    | Mark i :: _ -> f i
64    | _ :: tl     -> mark err f tl
65    | []          -> err ()
66
67 let rec priv err f = function
68    | Priv :: _ -> f ()
69    | _ :: tl   -> priv err f tl
70    | []        -> err ()
71
72 let resolve err f name a =
73    let rec aux i = function
74       | Name (n, true) :: _ when n = name -> f i
75       | _ :: tl                           -> aux (succ i) tl
76       | []                                -> err i
77    in
78    aux 0 a
79
80 let xlate f xlate_term = function
81    | a, uri, Abst t ->
82       let f t = f (a, uri, Abst t) in xlate_term f t
83    | a, uri, Abbr t ->
84       let f t = f (a, uri, Abbr t) in xlate_term f t