]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/common/entity.ml
- we implemented the hierarchy and sort names declaration in text parser
[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           | Meta of string    (* metaliguistic annotation *)
19           | Priv              (* private global definition *)
20
21 type attrs = attr list (* attributes *)
22
23 type 'term bind = Abst of 'term (* declaration: domain *)
24                 | Abbr of 'term (* definition: body *)
25                 | Void          (* exclusion *)
26
27 type 'term entity = attrs * uri * 'term bind (* attrs, name, binder *)
28
29 type uri_generator = string -> string
30
31 type status = {
32    delta: bool;        (* global delta-expansion *)
33    rt: bool;           (* reference typing *)
34    si: bool;           (* sort inclusion *)
35    expand: bool        (* always expand global definitions *)
36 }
37
38 (* helpers ******************************************************************)
39
40 let common f (a, u, _) = f a u
41
42 let rec name err f = function
43    | Name (n, r) :: _ -> f n r
44    | _ :: tl          -> name err f tl
45    | []               -> err ()
46
47 let names f map l a =
48    let rec aux f i a = function   
49       | []                -> f a
50       | Name (n, r) :: tl -> aux (map f i n r) false a tl
51       | _ :: tl           -> aux f i a tl
52    in
53    aux f true a l
54
55 let rec get_name err f j = function
56    | []                          -> err ()
57    | Name (n, r) :: _ when j = 0 -> f n r
58    | Name _ :: tl                -> get_name err f (pred j) tl
59    | _ :: tl                     -> get_name err f j tl
60
61 let rec get_names f = function
62    | []                -> f [] []
63    | Name _ as n :: tl ->
64       let f a ns = f a (n :: ns) in get_names f tl
65    | e :: tl           ->
66       let f a = f (e :: a) in get_names f tl
67
68 let count_names a =
69    let rec aux k = function
70       | []           -> k
71       | Name _ :: tl -> aux (succ k) tl
72       | _ :: tl      -> aux k tl
73    in
74    aux 0 a
75
76 let rec apix err f = function
77    | Apix i :: _ -> f i
78    | _ :: tl     -> apix err f tl
79    | []          -> err ()
80
81 let rec mark err f = function
82    | Mark i :: _ -> f i
83    | _ :: tl     -> mark err f tl
84    | []          -> err ()
85
86 let rec priv err f = function
87    | Priv :: _ -> f ()
88    | _ :: tl   -> priv err f tl
89    | []        -> err ()
90
91 let resolve err f name a =
92    let rec aux i = function
93       | Name (n, true) :: _ when n = name -> f i
94       | _ :: tl                           -> aux (succ i) tl
95       | []                                -> err i
96    in
97    aux 0 a
98
99 let xlate f xlate_term = function
100    | a, uri, Abst t ->
101       let f t = f (a, uri, Abst t) in xlate_term f t
102    | a, uri, Abbr t ->
103       let f t = f (a, uri, Abbr t) in xlate_term f t
104    | _, _, Void   ->
105       assert false
106
107 let initial_status expand si = {
108    delta = false; rt = false; si = si; expand = expand
109 }