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 output_html : ?append_NL:bool -> Ui_logger.html_msg -> unit
44 val interactive_user_uri_choice :
45 selection_mode:[`SINGLE | `MULTIPLE] ->
47 ?enable_button_for_non_vars:bool ->
48 title:string -> msg:string -> id:string -> string list -> string list
49 val interactive_interpretation_choice :
50 (string * string) list list -> int
51 val input_or_locate_uri : title:string -> UriManager.uri
55 type domain_and_interpretation =
56 CicTextualParser0.interpretation_domain_item list *
57 CicTextualParser0.interpretation
60 module Make(C:Callbacks) =
63 let locate_one_id mqi_handle id =
64 let query = MQueryGenerator.locate id in
65 let result = MQueryInterpreter.execute mqi_handle query in
69 MQueryMisc.wrong_xpointer_format_from_wrong_xpointer_format' uri
71 C.output_html (`Msg (`T "Locate query:"));
72 MQueryUtil.text_of_query
73 (fun s -> C.output_html ~append_NL:false (`Msg (`T s)))
75 C.output_html (`Msg (`T "Result:"));
76 MQueryUtil.text_of_result (fun s -> C.output_html (`Msg (`T s))) "" result;
80 [UriManager.string_of_uri
81 (C.input_or_locate_uri
82 ~title:("URI matching \"" ^ id ^ "\" unknown."))]
85 C.interactive_user_uri_choice
86 ~selection_mode:`MULTIPLE
87 ~ok:"Try every selection."
88 ~enable_button_for_non_vars:true
89 ~title:"Ambiguous input."
91 ("Ambiguous input \"" ^ id ^
92 "\". Please, choose one or more interpretations:")
96 List.map MQueryMisc.cic_textual_parser_uri_of_string uris'
99 exception ThereDoesNotExistAnyWellTypedInterpretationOfTheInput
102 Ok of Cic.term * Cic.metasenv
106 type ambiguous_choices =
107 Uris of CicTextualParser0.uri list
108 | Symbols of (CicTextualParser0.interpretation -> Cic.term) list
110 let disambiguate_input mqi_handle context metasenv dom mk_metasenv_and_expr ~id_to_uris=
111 let known_ids,resolve_id = id_to_uris in
117 if List.mem he known_ids then filter tl else he::(filter tl)
121 (* for each id in dom' we get the list of uris associated to it *)
125 CicTextualParser0.Id id -> Uris (locate_one_id mqi_handle id)
126 | CicTextualParser0.Symbol (descr,choices) ->
127 (* CSC: Implementare la funzione di filtraggio manuale *)
128 (* CSC: corrispondente alla locate_one_id *)
129 Symbols (List.map snd choices)
136 Uris l -> List.length l
137 | Symbols l -> List.length l
143 C.output_html (`Msg (`T (sprintf
144 "Disambiguation phase started: up to %d cases will be tried"
146 (* and now we compute the list of all possible assignments from *)
147 (* id to uris that generate well-typed terms *)
149 (* function to test if a partial interpretation is so far correct *)
150 let test resolve_id residual_dom =
151 (* We put implicits in place of every identifier that is not *)
152 (* resolved by resolve_id *)
157 if id = id' then Some (CicTextualParser0.Implicit) else f id'
158 ) resolve_id residual_dom
160 (* and we try to refine the term *)
161 let metasenv',expr = mk_metasenv_and_expr resolve_id' in
162 (*CSC: Bug here: we do not try to typecheck also the metasenv' *)
164 let term,_,_,metasenv'' =
165 CicRefine.type_of_aux' metasenv' context expr
169 CicRefine.MutCaseFixAndCofixRefineNotImplemented ->
171 let term = CicTypeChecker.type_of_aux' metasenv' context expr in
175 | CicRefine.Uncertain _ ->
176 prerr_endline ("%%% UNCERTAIN!!! " ^ CicPp.ppterm expr) ;
179 prerr_endline ("%%% PRUNED!!! " ^ CicPp.ppterm expr) ;
182 let rec aux resolve_id ids list_of_uris =
183 match ids,list_of_uris with
185 (match test resolve_id [] with
186 Ok (term,metasenv) -> [resolve_id,term,metasenv]
187 | Ko | Uncertain -> [])
188 | id::idtl,uris::uristl ->
192 | (uri : CicTextualParser0.interpretation_codomain_item)::uritl ->
194 function id' -> if id = id' then Some uri else resolve_id id'
196 (match test resolve_id' idtl with
197 Ok (term,metasenv) ->
198 (* the next three ``if''s are used to avoid the base *)
199 (* case where the term would be refined a second time. *)
201 [resolve_id',term,metasenv]
203 (aux resolve_id' idtl uristl)
206 (if uristl = [] then []
208 (aux resolve_id' idtl uristl)
217 (List.map (function uri -> CicTextualParser0.Uri uri) uris)
221 (function sym -> CicTextualParser0.Term sym) symbols))
222 | _,_ -> assert false
224 aux resolve_id dom' list_of_uris
227 (function (resolve,term,newmetasenv) ->
228 (* If metasen <> newmetasenv is a normal condition, we should *)
229 (* be prepared to apply the returned substitution to the *)
230 (* whole current proof. *)
231 if metasenv <> newmetasenv then
235 "+++++ ASSERTION FAILED: a refine operation should not modify the metasenv. Old metasenv:\n %s\n New metasenv:\n %s\n"
236 (CicMetaSubst.ppmetasenv [] metasenv)
237 (CicMetaSubst.ppmetasenv [] newmetasenv)) ;
238 (* an assert would raise an exception that could be caught *)
242 let resolve_id',term,metasenv' =
243 match resolve_ids with
244 [] -> raise ThereDoesNotExistAnyWellTypedInterpretationOfTheInput
245 | [resolve_id] -> resolve_id
249 (function (resolve,_,_) ->
253 CicTextualParser0.Id id -> id
254 | CicTextualParser0.Symbol (descr,_) -> descr
256 match resolve id with
258 | Some (CicTextualParser0.Uri uri) ->
260 CicTextualParser0.ConUri uri
261 | CicTextualParser0.VarUri uri ->
262 UriManager.string_of_uri uri
263 | CicTextualParser0.IndTyUri (uri,tyno) ->
264 UriManager.string_of_uri uri ^ "#xpointer(1/" ^
265 string_of_int (tyno+1) ^ ")"
266 | CicTextualParser0.IndConUri (uri,tyno,consno) ->
267 UriManager.string_of_uri uri ^ "#xpointer(1/" ^
268 string_of_int (tyno+1) ^ "/" ^ string_of_int consno ^ ")")
269 | Some (CicTextualParser0.Term term) ->
270 (* CSC: Implementare resa delle scelte *)
271 "To be implemented XXX01"
272 | Some CicTextualParser0.Implicit -> assert false
276 let index = C.interactive_interpretation_choice choices in
277 List.nth resolve_ids index
279 (known_ids @ dom', resolve_id'), metasenv',term
283 module EnvironmentP3 =
285 type t = domain_and_interpretation
289 let to_string (dom,resolve_id) =
290 let string_of_cic_textual_parser_uri uri =
291 let module C = Cic in
292 let module CTP = CicTextualParser0 in
295 CTP.ConUri uri -> UriManager.string_of_uri uri
296 | CTP.VarUri uri -> UriManager.string_of_uri uri
297 | CTP.IndTyUri (uri,tyno) ->
298 UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (tyno + 1)
299 | CTP.IndConUri (uri,tyno,consno) ->
300 UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (tyno + 1) ^ "/" ^
303 (* 4 = String.length "cic:" *)
304 String.sub uri' 4 (String.length uri' - 4)
310 match resolve_id v with
312 | Some (CicTextualParser0.Uri uri) -> uri
313 | Some (CicTextualParser0.Term _)
314 | Some CicTextualParser0.Implicit -> assert false
318 CicTextualParser0.Id id -> id
319 | CicTextualParser0.Symbol (descr,_) ->
320 (* CSC: To be implemented *)
322 )^ " " ^ (string_of_cic_textual_parser_uri uri)
325 let of_string inputtext =
327 let alfa = "[a-zA-Z_-]" in
328 let digit = "[0-9]" in
329 let ident = alfa ^ "\(" ^ alfa ^ "\|" ^ digit ^ "\)*" in
330 let blanks = "\( \|\t\|\n\)+" in
331 let nonblanks = "[^ \t\n]+" in
332 let uri = "/\(" ^ ident ^ "/\)*" ^ nonblanks in (* not very strict check *)
334 ("alias" ^ blanks ^ "\(" ^ ident ^ "\)" ^ blanks ^ "\(" ^ uri ^ "\)")
338 let n' = Str.search_forward regexpr inputtext n in
339 let id = CicTextualParser0.Id (Str.matched_group 2 inputtext) in
341 MQueryMisc.cic_textual_parser_uri_of_string
342 ("cic:" ^ (Str.matched_group 5 inputtext))
344 let dom,resolve_id = aux (n' + 1) in
345 if List.mem id dom then
351 Some (CicTextualParser0.Uri uri)
354 Not_found -> ([],function _ -> None)