]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_generator/mQGUtil.ml
test branch
[helm.git] / helm / ocaml / mathql_generator / mQGUtil.ml
1 (* Copyright (C) 2000, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 (*  AUTOR: Ferruccio Guidi <fguidi@cs.unibo.it>
27  *)
28
29 (* $Id$ *)
30
31 module T = MQGTypes
32
33 (* low level functions  *****************************************************)
34
35 let string_of_position p = 
36    let ns = "http://www.cs.unibo.it/helm/schemas/schema-helm#" in
37    match p with
38       | T.MainHypothesis -> ns ^ "MainHypothesis"
39       | T.InHypothesis   -> ns ^ "InHypothesis"
40       | T.MainConclusion -> ns ^ "MainConclusion"
41       | T.InConclusion   -> ns ^ "InConclusion"
42       | T.InBody         -> ns ^ "InBody"
43       
44 let string_of_sort = function
45    | T.Set   -> "Set"
46    | T.Prop  -> "Prop"
47    | T.Type  -> "Type"
48    | T.CProp -> "CProp"
49
50 let string_of_depth = string_of_int
51
52 let mathql_of_position = function
53    | T.MainHypothesis -> "$MH"
54    | T.InHypothesis   -> "$IH"
55    | T.MainConclusion -> "$MC"
56    | T.InConclusion   -> "$IC"
57    | T.InBody         -> "$IB"
58       
59 let mathql_of_sort = function
60    | T.Set   -> "$SET"
61    | T.Prop  -> "$PROP"
62    | T.Type  -> "$TYPE"
63    | T.CProp -> "$CPROP"
64
65 let mathql_of_depth = string_of_int
66
67 let mathql_of_uri u = u
68
69 let mathql_of_specs out l =
70    let rec iter f = function 
71       | []        -> ()
72       | [s]       -> out "\""; out (f s); out "\""
73       | s :: tail -> out "\""; out (f s); out "\", "; iter f tail
74    in
75    let txt_uri l = out "{"; iter mathql_of_uri l; out "} " in
76    let txt_pos l = out "{"; iter mathql_of_position l; out "} " in
77    let txt_sort l = out "{"; iter mathql_of_sort l; out "} " in
78    let txt_depth l = out "{"; iter mathql_of_depth l; out "} " in
79    let txt_spec = function
80       | T.MustObj  (u, p, d) -> out "mustobj  "; txt_uri u; txt_pos p; txt_depth d; out "\n" 
81       | T.MustSort (s, p, d) -> out "mustsort "; txt_sort s; txt_pos p; txt_depth d; out "\n" 
82       | T.MustRel  (   p, d) -> out "mustrel  "; txt_pos p; txt_depth d; out "\n" 
83       | T.OnlyObj  (u, p, d) -> out "onlyobj  "; txt_uri u; txt_pos p; txt_depth d; out "\n" 
84       | T.OnlySort (s, p, d) -> out "onlysort "; txt_sort s; txt_pos p; txt_depth d; out "\n" 
85       | T.OnlyRel  (   p, d) -> out "onlyrel  "; txt_pos p; txt_depth d; out "\n" 
86       | T.Universe (   p   ) -> out "universe "; txt_pos p; out "\n" 
87    in   
88    List.iter txt_spec l  
89
90 let position_of_mathql = function
91    | "$MH" -> T.MainHypothesis 
92    | "$IH" -> T.InHypothesis
93    | "$MC" -> T.MainConclusion
94    | "$IC" -> T.InConclusion
95    | "$IB" -> T.InBody
96    | _     -> raise Parsing.Parse_error 
97
98 let sort_of_mathql = function
99    | "$SET"   -> T.Set 
100    | "$PROP"  -> T.Prop
101    | "$TYPE"  -> T.Type
102    | "$CPROP" -> T.CProp
103    | _       -> raise Parsing.Parse_error 
104
105 let depth_of_mathql s =
106    try 
107       let d = int_of_string s in
108       if d < 0 then raise (Failure "") else d
109    with Failure _ -> raise Parsing.Parse_error
110
111 let uri_of_mathql s = s
112
113 (* high level functions  ****************************************************)
114
115 let text_of_position = function
116    | `MainHypothesis _ -> "MainHypothesis"
117    | `MainConclusion _ -> "MainConclusion"
118    | `InHypothesis     -> "InHypothesis" 
119    | `InConclusion     -> "InConclusion" 
120    | `InBody           -> "InBody" 
121
122 let text_of_depth pos no_depth_text = match pos with
123    | `MainHypothesis (Some d)
124    | `MainConclusion (Some d) -> string_of_int d
125    | _                        -> no_depth_text
126
127 let text_of_sort = function
128    | T.Set   -> "Set"
129    | T.Prop  -> "Prop"
130    | T.Type  -> "Type"
131    | T.CProp -> "CProp"
132
133 let is_main_position = function
134    | `MainHypothesis _
135    | `MainConclusion _ -> true
136    | _                 -> false
137
138 let is_conclusion = function
139    | `MainConclusion _ 
140    | `InConclusion     -> true
141    | _                 -> false
142
143 let set_full_position pos depth = match pos with
144    | `MainHypothesis _ -> `MainHypothesis depth
145    | `MainConclusion _ -> `MainConclusion depth
146    | _                 -> pos
147
148 let set_main_position pos depth = match pos with
149    | `MainHypothesis _ -> `MainHypothesis depth
150    | `MainConclusion _ -> `MainConclusion depth