]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql/mQueryMisc.ml
Now mathql_generator compiles before mathql_interpreter.
[helm.git] / helm / ocaml / mathql / mQueryMisc.ml
1 (* Copyright (C) 2000-2002, 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 (****************************************************************************)
27 (*                                                                          *)
28 (*                               PROJECT HELM                               *)
29 (*                                                                          *)
30 (*                Claudio Sacerdoti Coen <sacerdot@cs.unibo.it>             *)
31 (*                Ferruccio Guidi        <fguidi@cs.unibo.it>               *)
32 (*                                 15/01/2003                               *)
33 (*                                                                          *)
34 (*                                                                          *)
35 (****************************************************************************)
36
37 exception IllFormedUri of string;;
38
39 let string_of_cic_textual_parser_uri uri =
40  let module C = Cic in
41  let module CTP = CicTextualParser0 in
42   let uri' =
43    match uri with
44       CTP.ConUri uri -> UriManager.string_of_uri uri
45     | CTP.VarUri uri -> UriManager.string_of_uri uri
46     | CTP.IndTyUri (uri,tyno) ->
47        UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (tyno + 1)
48     | CTP.IndConUri (uri,tyno,consno) ->
49        UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (tyno + 1) ^ "/" ^
50         string_of_int consno
51   in
52    (* 4 = String.length "cic:" *)
53    String.sub uri' 4 (String.length uri' - 4)
54 ;;
55
56 let cic_textual_parser_uri_of_string uri' =
57  prerr_endline ("cic_textual_parser_uri_of_string INPUT = " ^ uri');
58  try
59   (* Constant *)
60   if String.sub uri' (String.length uri' - 4) 4 = ".con" then
61    CicTextualParser0.ConUri (UriManager.uri_of_string uri')
62   else
63    if String.sub uri' (String.length uri' - 4) 4 = ".var" then
64     CicTextualParser0.VarUri (UriManager.uri_of_string uri')
65    else
66     (try
67       (* Inductive Type *)
68       let uri'',typeno = CicTextualLexer.indtyuri_of_uri uri' in
69        CicTextualParser0.IndTyUri (uri'',typeno)
70      with
71         UriManager.IllFormedUri _
72       | CicTextualParser0.LexerFailure _
73       | Invalid_argument _ ->
74          (* Constructor of an Inductive Type *)
75          let uri'',typeno,consno =
76           CicTextualLexer.indconuri_of_uri uri'
77          in
78           CicTextualParser0.IndConUri (uri'',typeno,consno)
79     )
80  with
81     UriManager.IllFormedUri _
82   | CicTextualParser0.LexerFailure _
83   | Invalid_argument _ ->
84      raise (IllFormedUri uri')
85 ;;
86 let cic_textual_parser_uri_of_string uri' =
87   let res = cic_textual_parser_uri_of_string uri' in
88   prerr_endline ("RESULT: " ^ (string_of_cic_textual_parser_uri res));
89   res
90
91 (* CSC: quick fix: a function from [uri#xpointer(path)] to [uri#path] *)
92 let wrong_xpointer_format_from_wrong_xpointer_format' uri =
93  try
94   let index_sharp =  String.index uri '#' in
95   let index_rest = index_sharp + 10 in
96    let baseuri = String.sub uri 0 index_sharp in
97    let rest =
98     String.sub uri index_rest (String.length uri - index_rest - 1)
99    in
100     baseuri ^ "#" ^ rest
101  with Not_found -> uri
102 ;;
103
104 let term_of_cic_textual_parser_uri uri =
105  let module C = Cic in
106  let module CTP = CicTextualParser0 in
107   match uri with
108      CTP.ConUri uri -> C.Const (uri,[])
109    | CTP.VarUri uri -> C.Var (uri,[])
110    | CTP.IndTyUri (uri,tyno) -> C.MutInd (uri,tyno,[])
111    | CTP.IndConUri (uri,tyno,consno) -> C.MutConstruct (uri,tyno,consno,[])
112 ;;
113
114 (* conversion functions *****************************************************)
115
116 type uriref = UriManager.uri * (int list)
117
118 let string_of_uriref (uri, fi) =
119    let module UM = UriManager in
120    let str = UM.string_of_uri uri in
121    let xp t = "#xpointer(1/" ^ string_of_int (t + 1) in
122    match fi with
123       | []          -> str 
124       | [t]         -> str ^ xp t ^ ")" 
125       | t :: c :: _ -> str ^ xp t ^ "/" ^ string_of_int c ^ ")"