]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/components/common/entity.ml
refactoring: helena sources are now in a dedicated directory
[helm.git] / helm / software / lambda-delta / components / 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 module O = Options
13
14 type uri = NUri.uri
15 type id = Aut.id
16 type name = id * bool (* token, real? *)
17
18 type names = name list
19
20 type attr = Name of name      (* name *)
21           | Apix of int       (* additional position index *)
22           | Mark of int       (* node marker *)
23           | Meta of string    (* metaliguistic annotation *)
24           | Priv              (* private global definition *)
25
26 type attrs = attr list (* attributes *)
27
28 type 'term bind = Abst of 'term (* declaration: domain *)
29                 | Abbr of 'term (* definition: body *)
30                 | Void          (* exclusion *)
31
32 type 'term entity = attrs * uri * 'term bind (* attrs, name, binder *)
33
34 type status = {
35    delta: bool;        (* global delta-expansion *)
36    rt: bool;           (* reference typing *)
37    si: bool;           (* sort inclusion *)
38    expand: bool        (* always expand global definitions *)
39 }
40
41 (* helpers ******************************************************************)
42
43 let common f (a, u, _) = f a u
44
45 let rec name err f = function
46    | Name (n, r) :: _ -> f n r
47    | _ :: tl          -> name err f tl
48    | []               -> err ()
49
50 let names f map l a =
51    let rec aux f i a = function   
52       | []                -> f a
53       | Name (n, r) :: tl -> aux (map f i n r) false a tl
54       | _ :: tl           -> aux f i a tl
55    in
56    aux f true a l
57
58 let rec get_name err f j = function
59    | []                          -> err ()
60    | Name (n, r) :: _ when j = 0 -> f n r
61    | Name _ :: tl                -> get_name err f (pred j) tl
62    | _ :: tl                     -> get_name err f j tl
63
64 let rec get_names f = function
65    | []                -> f [] []
66    | Name _ as n :: tl ->
67       let f a ns = f a (n :: ns) in get_names f tl
68    | e :: tl           ->
69       let f a = f (e :: a) in get_names f tl
70
71 let count_names a =
72    let rec aux k = function
73       | []           -> k
74       | Name _ :: tl -> aux (succ k) tl
75       | _ :: tl      -> aux k tl
76    in
77    aux 0 a
78
79 let rec apix err f = function
80    | Apix i :: _ -> f i
81    | _ :: tl     -> apix err f tl
82    | []          -> err ()
83
84 let rec mark err f = function
85    | Mark i :: _ -> f i
86    | _ :: tl     -> mark err f tl
87    | []          -> err ()
88
89 let rec priv err f = function
90    | Priv :: _ -> f ()
91    | _ :: tl   -> priv err f tl
92    | []        -> err ()
93
94 let rec meta err f = function
95    | Meta s :: _ -> f s
96    | _ :: tl     -> meta err f tl
97    | []          -> err ()
98
99 let resolve err f name a =
100    let rec aux i = function
101       | Name (n, true) :: _ when n = name -> f i
102       | _ :: tl                           -> aux (succ i) tl
103       | []                                -> err i
104    in
105    aux 0 a
106
107 let rec rev_append_names ns = function
108    | []           -> ns
109    | Name n :: tl -> rev_append_names (n :: ns) tl
110    | _ :: tl      -> rev_append_names ns tl
111
112 let xlate f xlate_term = function
113    | a, uri, Abst t ->
114       let f t = f (a, uri, Abst t) in xlate_term f t
115    | a, uri, Abbr t ->
116       let f t = f (a, uri, Abbr t) in xlate_term f t
117    | _, _, Void   ->
118       assert false
119
120 let initial_status () = {
121    delta = false; rt = false; si = !O.si; expand = !O.expand
122 }
123
124 let refresh_status st = {st with
125    si = !O.si; expand = !O.expand
126 }
127