1 (* Copyright (C) 2000-2002, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
26 (******************************************************************************)
30 (* Claudio Sacerdoti Coen <sacerdot@cs.unibo.it> *)
34 (******************************************************************************)
38 (** This module provides a functor to disambiguate the input **)
39 (** given a set of user-interface call-backs **)
41 module type Callbacks =
43 val interactive_user_uri_choice :
44 selection_mode:[`SINGLE | `MULTIPLE] ->
46 ?enable_button_for_non_vars:bool ->
47 title:string -> msg:string -> id:string -> string list -> string list
48 val interactive_interpretation_choice :
49 (string * string) list list -> int list
50 val input_or_locate_uri : title:string -> UriManager.uri
54 type domain_and_interpretation =
55 CicTextualParser0.interpretation_domain_item list *
56 CicTextualParser0.interpretation
59 module Make(C:Callbacks) =
62 let locate_one_id mqi_handle id =
63 let query = MQueryGenerator.locate id in
64 let result = MQueryInterpreter.execute mqi_handle query in
68 MQueryMisc.wrong_xpointer_format_from_wrong_xpointer_format' uri
70 HelmLogger.log (`Msg (`T "Locate query:"));
71 MQueryUtil.text_of_query
72 (fun s -> HelmLogger.log ~append_NL:false (`Msg (`T s)))
74 HelmLogger.log (`Msg (`T "Result:"));
75 MQueryUtil.text_of_result (fun s -> HelmLogger.log (`Msg (`T s))) "" result;
79 [UriManager.string_of_uri
80 (C.input_or_locate_uri
81 ~title:("URI matching \"" ^ id ^ "\" unknown."))]
84 C.interactive_user_uri_choice
85 ~selection_mode:`MULTIPLE
86 ~ok:"Try every selection."
87 ~enable_button_for_non_vars:true
88 ~title:"Ambiguous input."
90 ("Ambiguous input \"" ^ id ^
91 "\". Please, choose one or more interpretations:")
95 List.map MQueryMisc.cic_textual_parser_uri_of_string uris'
98 exception ThereDoesNotExistAnyWellTypedInterpretationOfTheInput
101 Ok of Cic.term * Cic.metasenv
105 type ambiguous_choices =
106 Uris of CicTextualParser0.uri list
107 | Symbols of (CicTextualParser0.interpretation -> Cic.term) list
109 let disambiguate_input mqi_handle context metasenv dom mk_metasenv_and_expr ~id_to_uris=
110 let known_ids,resolve_id = id_to_uris in
116 if List.mem he known_ids then filter tl else he::(filter tl)
120 (* for each id in dom' we get the list of uris associated to it *)
124 CicTextualParser0.Id id -> Uris (locate_one_id mqi_handle id)
125 | CicTextualParser0.Symbol (descr,choices) ->
126 (* CSC: Implementare la funzione di filtraggio manuale *)
127 (* CSC: corrispondente alla locate_one_id *)
128 Symbols (List.map snd choices)
135 Uris l -> List.length l
136 | Symbols l -> List.length l
142 HelmLogger.log (`Msg (`T (sprintf
143 "Disambiguation phase started: up to %d cases will be tried"
145 (* and now we compute the list of all possible assignments from *)
146 (* id to uris that generate well-typed terms *)
148 (* function to test if a partial interpretation is so far correct *)
149 let test resolve_id residual_dom =
150 (* We put implicits in place of every identifier that is not *)
151 (* resolved by resolve_id *)
156 if id = id' then Some (CicTextualParser0.Implicit) else f id'
157 ) resolve_id residual_dom
159 (* and we try to refine the term *)
160 let metasenv',expr = mk_metasenv_and_expr resolve_id' in
161 (*CSC: Bug here: we do not try to typecheck also the metasenv' *)
163 let term,_,metasenv'' =
164 CicRefine.type_of_aux' metasenv' context expr
168 CicRefine.Uncertain _ ->
169 prerr_endline ("%%% UNCERTAIN!!! " ^ CicPp.ppterm expr) ;
172 prerr_endline ("%%% PRUNED!!! " ^ CicPp.ppterm expr) ;
175 let rec aux resolve_id ids list_of_uris =
176 match ids,list_of_uris with
178 (match test resolve_id [] with
179 Ok (term,metasenv) -> [resolve_id,term,metasenv]
180 | Ko | Uncertain -> [])
181 | id::idtl,uris::uristl ->
185 | (uri : CicTextualParser0.interpretation_codomain_item)::uritl ->
187 function id' -> if id = id' then Some uri else resolve_id id'
189 (match test resolve_id' idtl with
190 Ok (term,metasenv) ->
191 (* the next three ``if''s are used to avoid the base *)
192 (* case where the term would be refined a second time. *)
194 [resolve_id',term,metasenv]
196 (aux resolve_id' idtl uristl)
199 (if uristl = [] then []
201 (aux resolve_id' idtl uristl)
210 (List.map (function uri -> CicTextualParser0.Uri uri) uris)
214 (function sym -> CicTextualParser0.Term sym) symbols))
215 | _,_ -> assert false
217 aux resolve_id dom' list_of_uris
220 (function (resolve,term,newmetasenv) ->
221 (* If metasen <> newmetasenv is a normal condition, we should *)
222 (* be prepared to apply the returned substitution to the *)
223 (* whole current proof. *)
224 if metasenv <> newmetasenv then
228 "+++++ ASSERTION FAILED: a refine operation should not modify the metasenv. Old metasenv:\n %s\n New metasenv:\n %s\n"
229 (CicMetaSubst.ppmetasenv metasenv [])
230 (CicMetaSubst.ppmetasenv newmetasenv [])) ;
231 (* an assert would raise an exception that could be caught *)
236 match resolve_ids with
237 [] -> raise ThereDoesNotExistAnyWellTypedInterpretationOfTheInput
242 (function (resolve,_,_) ->
246 CicTextualParser0.Id id -> id
247 | CicTextualParser0.Symbol (descr,_) -> descr
249 match resolve id with
251 | Some (CicTextualParser0.Uri uri) ->
253 CicTextualParser0.ConUri uri
254 | CicTextualParser0.VarUri uri ->
255 UriManager.string_of_uri uri
256 | CicTextualParser0.IndTyUri (uri,tyno) ->
257 UriManager.string_of_uri uri ^ "#xpointer(1/" ^
258 string_of_int (tyno+1) ^ ")"
259 | CicTextualParser0.IndConUri (uri,tyno,consno) ->
260 UriManager.string_of_uri uri ^ "#xpointer(1/" ^
261 string_of_int (tyno+1) ^ "/" ^ string_of_int consno ^ ")")
262 | Some (CicTextualParser0.Term term) ->
263 (* CSC: Implementare resa delle scelte *)
264 "To be implemented XXX01"
265 | Some CicTextualParser0.Implicit -> assert false
269 let indexes = C.interactive_interpretation_choice choices in
270 List.map (List.nth resolve_ids) indexes
273 (fun (resolve_id',term,metasenv') ->
274 (known_ids @ dom', resolve_id'), metasenv',term
279 module EnvironmentP3 =
281 type t = domain_and_interpretation
285 let to_string (dom,resolve_id) =
286 let string_of_cic_textual_parser_uri uri =
287 let module C = Cic in
288 let module CTP = CicTextualParser0 in
291 CTP.ConUri uri -> UriManager.string_of_uri uri
292 | CTP.VarUri uri -> UriManager.string_of_uri uri
293 | CTP.IndTyUri (uri,tyno) ->
294 UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (tyno + 1)
295 | CTP.IndConUri (uri,tyno,consno) ->
296 UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (tyno + 1) ^ "/" ^
299 (* 4 = String.length "cic:" *)
300 String.sub uri' 4 (String.length uri' - 4)
306 match resolve_id v with
308 | Some (CicTextualParser0.Uri uri) -> uri
309 | Some (CicTextualParser0.Term _)
310 | Some CicTextualParser0.Implicit -> assert false
314 CicTextualParser0.Id id -> id
315 | CicTextualParser0.Symbol (descr,_) ->
316 (* CSC: To be implemented *)
318 )^ " " ^ (string_of_cic_textual_parser_uri uri)
321 let of_string inputtext =
323 let alfa = "[a-zA-Z_-]" in
324 let digit = "[0-9]" in
325 let ident = alfa ^ "\(" ^ alfa ^ "\|" ^ digit ^ "\)*" in
326 let blanks = "\( \|\t\|\n\)+" in
327 let nonblanks = "[^ \t\n]+" in
328 let uri = "/\(" ^ ident ^ "/\)*" ^ nonblanks in (* not very strict check *)
330 ("alias" ^ blanks ^ "\(" ^ ident ^ "\)" ^ blanks ^ "\(" ^ uri ^ "\)")
334 let n' = Str.search_forward regexpr inputtext n in
335 let id = CicTextualParser0.Id (Str.matched_group 2 inputtext) in
337 MQueryMisc.cic_textual_parser_uri_of_string
338 ("cic:" ^ (Str.matched_group 5 inputtext))
340 let dom,resolve_id = aux (n' + 1) in
341 if List.mem id dom then
347 Some (CicTextualParser0.Uri uri)
350 Not_found -> ([],function _ -> None)