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