]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/src/common/entity.ml
when sort inclusion is enabled, we can produce conversion constraints in xml
[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 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 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 N.level * 'term (* declaration: level, domain *)
32                 | Abbr of 'term           (* definition: body           *)
33                 | Void                    (* exclusion                  *)
34
35 type 'term entity = attrs * uri * 'term bind (* attrs, name, binder *)
36
37 (* helpers ******************************************************************)
38
39 let common f (a, u, _) = f a u
40
41 let rec name err f = function
42    | Name (n, r) :: _ -> f n r
43    | _ :: tl          -> name err f tl
44    | []               -> err ()
45
46 let names f map l a =
47    let rec aux f i a = function   
48       | []                -> f a
49       | Name (n, r) :: tl -> aux (map f i n r) false a tl
50       | _ :: tl           -> aux f i a tl
51    in
52    aux f true a l
53
54 let rec get_name err f j = function
55    | []                          -> err ()
56    | Name (n, r) :: _ when j = 0 -> f n r
57    | Name _ :: tl                -> get_name err f (pred j) tl
58    | _ :: tl                     -> get_name err f j tl
59
60 let rec get_names f = function
61    | []                -> f [] []
62    | Name _ as n :: tl ->
63       let f a ns = f a (n :: ns) in get_names f tl
64    | e :: tl           ->
65       let f a = f (e :: a) in get_names f tl
66
67 let count_names a =
68    let rec aux k = function
69       | []           -> k
70       | Name _ :: tl -> aux (succ k) tl
71       | _ :: tl      -> aux k tl
72    in
73    aux 0 a
74
75 let rec apix err f = function
76    | Apix i :: _ -> f i
77    | _ :: tl     -> apix err f tl
78    | []          -> err ()
79
80 let rec mark err f = function
81    | Mark i :: _ -> f i
82    | _ :: tl     -> mark err f tl
83    | []          -> err ()
84
85 let rec priv err f = function
86    | Priv :: _ -> f ()
87    | _ :: tl   -> priv err f tl
88    | []        -> err ()
89
90 let rec meta err f = function
91    | Meta s :: _ -> f s
92    | _ :: tl     -> meta err f tl
93    | []          -> err ()
94
95 let resolve err f name a =
96    let rec aux i = function
97       | Name (n, true) :: _ when n = name -> f i
98       | _ :: tl                           -> aux (succ i) tl
99       | []                                -> err i
100    in
101    aux 0 a
102
103 let rec rev_append_names ns = function
104    | []           -> ns
105    | Name n :: tl -> rev_append_names (n :: ns) tl
106    | _ :: tl      -> rev_append_names ns tl
107
108 let xlate f xlate_term = function
109    | a, uri, Abst (n, t) ->
110       let f t = f (a, uri, Abst (n, t)) in xlate_term f t
111    | a, uri, Abbr t      ->
112       let f t = f (a, uri, Abbr t) in xlate_term f t
113    | _, _, Void          ->
114       assert false