]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/common/entity.ml
- we added a parser for lambda-delta textual syntax (file extension .hln)
[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    g: Hierarchy.graph; (* sort hierarchy parameter *)
33    delta: bool;        (* global delta-expansion *)
34    rt: bool;           (* reference typing *)
35    si: bool;           (* sort inclusion *)
36    expand: bool        (* always expand global definitions *)
37 }
38
39 (* helpers ******************************************************************)
40
41 let common f (a, u, _) = f a u
42
43 let rec name err f = function
44    | Name (n, r) :: _ -> f n r
45    | _ :: tl          -> name err f tl
46    | []               -> err ()
47
48 let names f map l a =
49    let rec aux f i a = function   
50       | []                -> f a
51       | Name (n, r) :: tl -> aux (map f i n r) false a tl
52       | _ :: tl           -> aux f i a tl
53    in
54    aux f true a l
55
56 let rec get_name err f j = function
57    | []                          -> err ()
58    | Name (n, r) :: _ when j = 0 -> f n r
59    | Name _ :: tl                -> get_name err f (pred j) tl
60    | _ :: tl                     -> get_name err f j tl
61
62 let rec get_names f = function
63    | []                -> f [] []
64    | Name _ as n :: tl ->
65       let f a ns = f a (n :: ns) in get_names f tl
66    | e :: tl           ->
67       let f a = f (e :: a) in get_names f tl
68
69 let count_names a =
70    let rec aux k = function
71       | []           -> k
72       | Name _ :: tl -> aux (succ k) tl
73       | _ :: tl      -> aux k tl
74    in
75    aux 0 a
76
77 let rec apix err f = function
78    | Apix i :: _ -> f i
79    | _ :: tl     -> apix err f tl
80    | []          -> err ()
81
82 let rec mark err f = function
83    | Mark i :: _ -> f i
84    | _ :: tl     -> mark err f tl
85    | []          -> err ()
86
87 let rec priv err f = function
88    | Priv :: _ -> f ()
89    | _ :: tl   -> priv err f tl
90    | []        -> err ()
91
92 let resolve err f name a =
93    let rec aux i = function
94       | Name (n, true) :: _ when n = name -> f i
95       | _ :: tl                           -> aux (succ i) tl
96       | []                                -> err i
97    in
98    aux 0 a
99
100 let xlate f xlate_term = function
101    | a, uri, Abst t ->
102       let f t = f (a, uri, Abst t) in xlate_term f t
103    | a, uri, Abbr t ->
104       let f t = f (a, uri, Abbr t) in xlate_term f t
105    | _, _, Void   ->
106       assert false
107
108 let initial_status g expand si = {
109    g = g; delta = false; rt = false; si = si; expand = expand
110 }