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