]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/common/entity.ml
refactoring ...
[helm.git] / helm / software / helena / 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 N = Level
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 meta = Main     (* main object *)
24           | InProp   (* inhabitant of a proposition *)
25           | Progress (* uncompleted object *)
26           | Private  (* private global definition *)
27
28 type attr = Name of name              (* name *)
29           | Apix of int               (* additional position index *)
30           | Mark of int               (* node marker *)
31           | Meta of meta list         (* metaliguistic classification *) 
32           | Info of (string * string) (* metaliguistic annotation: language (defaults to "en-US"), text *)
33
34 type attrs = attr list (* attributes *)
35
36 type 'term bind = Abst of N.level * 'term (* declaration: level, domain *)
37                 | Abbr of 'term           (* definition: body           *)
38                 | Void                    (* exclusion                  *)
39
40 type 'term entity = attrs * uri * 'term bind (* attrs, name, binder *)
41
42 (* helpers ******************************************************************)
43
44 let common f (a, u, _) = f a u
45
46 let rec name err f = function
47    | Name (n, r) :: _ -> f n r
48    | _ :: tl          -> name err f tl
49    | []               -> err ()
50
51 let names f map l a =
52    let rec aux f i a = function   
53       | []                -> f a
54       | Name (n, r) :: tl -> aux (map f i n r) false a tl
55       | _ :: tl           -> aux f i a tl
56    in
57    aux f true a l
58
59 let rec get_name err f j = function
60    | []                          -> err ()
61    | Name (n, r) :: _ when j = 0 -> f n r
62    | Name _ :: tl                -> get_name err f (pred j) tl
63    | _ :: tl                     -> get_name err f j tl
64
65 let rec get_names f = function
66    | []                -> f [] []
67    | Name _ as n :: tl ->
68       let f a ns = f a (n :: ns) in get_names f tl
69    | e :: tl           ->
70       let f a = f (e :: a) in get_names f tl
71
72 let count_names a =
73    let rec aux k = function
74       | []           -> k
75       | Name _ :: tl -> aux (succ k) tl
76       | _ :: tl      -> aux k tl
77    in
78    aux 0 a
79
80 let rec apix err f = function
81    | Apix i :: _ -> f i
82    | _ :: tl     -> apix err f tl
83    | []          -> err ()
84
85 let rec mark err f = function
86    | Mark i :: _ -> f i
87    | _ :: tl     -> mark err f tl
88    | []          -> err ()
89
90 let rec meta err f = function
91    | Meta ms :: _ -> f ms
92    | _ :: tl      -> meta err f tl
93    | []           -> err ()
94
95 let rec info err f = function
96    | Info (lg, tx) :: _ -> f lg tx
97    | _ :: tl            -> info err f tl
98    | []                 -> err ()
99
100 let resolve err f name a =
101    let rec aux i = function
102       | Name (n, true) :: _ when n = name -> f i
103       | _ :: tl                           -> aux (succ i) tl
104       | []                                -> err i
105    in
106    aux 0 a
107
108 let rec rev_append_names ns = function
109    | []           -> ns
110    | Name n :: tl -> rev_append_names (n :: ns) tl
111    | _ :: tl      -> rev_append_names ns tl
112
113 let xlate f xlate_term = function
114    | a, uri, Abst (n, t) ->
115       let f t = f (a, uri, Abst (n, t)) in xlate_term f t
116    | a, uri, Abbr t      ->
117       let f t = f (a, uri, Abbr t) in xlate_term f t
118    | _, _, Void          ->
119       assert false