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 (* The following two functions are used to save/restore the metasenv *)
44 (* before/after the parsing. *)
45 (*CSC: This should be made functional sooner or later! *)
46 val get_metasenv : unit -> Cic.metasenv
47 val set_metasenv : Cic.metasenv -> unit
49 val output_html : ?append_NL:bool -> Ui_logger.html_msg -> unit
50 val interactive_user_uri_choice :
51 selection_mode:[`SINGLE | `MULTIPLE] ->
53 ?enable_button_for_non_vars:bool ->
54 title:string -> msg:string -> id:string -> string list -> string list
55 val interactive_interpretation_choice :
56 (string * string) list list -> int
57 val input_or_locate_uri : title:string -> UriManager.uri
61 type domain_and_interpretation =
62 CicTextualParser0.interpretation_domain_item list *
63 CicTextualParser0.interpretation
66 module Make(C:Callbacks) =
69 let locate_one_id mqi_handle id =
70 let query = MQueryGenerator.locate id in
71 let result = MQueryInterpreter.execute mqi_handle query in
75 MQueryMisc.wrong_xpointer_format_from_wrong_xpointer_format' uri
77 C.output_html (`Msg (`T "Locate query:"));
78 MQueryUtil.text_of_query
79 (fun s -> C.output_html ~append_NL:false (`Msg (`T s)))
81 C.output_html (`Msg (`T "Result:"));
82 MQueryUtil.text_of_result (fun s -> C.output_html (`Msg (`T s))) "" result;
86 [UriManager.string_of_uri
87 (C.input_or_locate_uri
88 ~title:("URI matching \"" ^ id ^ "\" unknown."))]
91 C.interactive_user_uri_choice
92 ~selection_mode:`MULTIPLE
93 ~ok:"Try every selection."
94 ~enable_button_for_non_vars:true
95 ~title:"Ambiguous input."
97 ("Ambiguous input \"" ^ id ^
98 "\". Please, choose one or more interpretations:")
102 List.map MQueryMisc.cic_textual_parser_uri_of_string uris'
105 exception ThereDoesNotExistAnyWellTypedInterpretationOfTheInput
108 Ok of Cic.term * Cic.metasenv
112 type ambiguous_choices =
113 Uris of CicTextualParser0.uri list
114 | Symbols of (CicTextualParser0.interpretation -> Cic.term) list
116 let disambiguate_input mqi_handle context metasenv dom mk_metasenv_and_expr ~id_to_uris=
117 let known_ids,resolve_id = id_to_uris in
123 if List.mem he known_ids then filter tl else he::(filter tl)
127 (* for each id in dom' we get the list of uris associated to it *)
131 CicTextualParser0.Id id -> Uris (locate_one_id mqi_handle id)
132 | CicTextualParser0.Symbol (descr,choices) ->
133 (* CSC: Implementare la funzione di filtraggio manuale *)
134 (* CSC: corrispondente alla locate_one_id *)
135 Symbols (List.map snd choices)
142 Uris l -> List.length l
143 | Symbols l -> List.length l
149 C.output_html (`Msg (`T (sprintf
150 "Disambiguation phase started: up to %d cases will be tried"
152 (* and now we compute the list of all possible assignments from *)
153 (* id to uris that generate well-typed terms *)
155 (* function to test if a partial interpretation is so far correct *)
156 let test resolve_id residual_dom =
157 (* We put implicits in place of every identifier that is not *)
158 (* resolved by resolve_id *)
163 if id = id' then Some (CicTextualParser0.Implicit) else f id'
164 ) resolve_id residual_dom
166 (* and we try to refine the term *)
167 let saved_status = C.get_metasenv () in
168 let metasenv',expr = mk_metasenv_and_expr resolve_id' in
169 (*CSC: Bug here: we do not try to typecheck also the metasenv' *)
170 (* The parser is imperative ==> we must restore the old status ;-(( *)
171 C.set_metasenv saved_status ;
173 let term,_,_,metasenv'' =
174 CicRefine.type_of_aux' metasenv' context expr
178 CicRefine.MutCaseFixAndCofixRefineNotImplemented ->
180 let term = CicTypeChecker.type_of_aux' metasenv' context expr in
184 | CicRefine.Uncertain _ ->
185 prerr_endline ("%%% UNCERTAIN!!! " ^ CicPp.ppterm expr) ;
188 prerr_endline ("%%% PRUNED!!! " ^ CicPp.ppterm expr) ;
191 let rec aux resolve_id ids list_of_uris =
192 match ids,list_of_uris with
194 (match test resolve_id [] with
195 Ok (term,metasenv) -> [resolve_id,term,metasenv]
196 | Ko | Uncertain -> [])
197 | id::idtl,uris::uristl ->
201 | (uri : CicTextualParser0.interpretation_codomain_item)::uritl ->
203 function id' -> if id = id' then Some uri else resolve_id id'
205 (match test resolve_id' idtl with
206 Ok (term,metasenv) ->
207 (* the next three ``if''s are used to avoid the base *)
208 (* case where the term would be refined a second time. *)
210 [resolve_id',term,metasenv]
212 (aux resolve_id' idtl uristl)
215 (if uristl = [] then []
217 (aux resolve_id' idtl uristl)
226 (List.map (function uri -> CicTextualParser0.Uri uri) uris)
230 (function sym -> CicTextualParser0.Term sym) symbols))
231 | _,_ -> assert false
233 aux resolve_id dom' list_of_uris
236 (function (resolve,term,newmetasenv) ->
237 (* If metasen <> newmetasenv is a normal condition, we should *)
238 (* be prepared to apply the returned substitution to the *)
239 (* whole current proof. *)
240 if metasenv <> newmetasenv then
243 ("+++++ ASSERTION FAILED: " ^
244 "a refine operation should not modify the metasenv") ;
245 (* an assert would raise an exception that could be caught *)
249 let resolve_id',term,metasenv' =
250 match resolve_ids with
251 [] -> raise ThereDoesNotExistAnyWellTypedInterpretationOfTheInput
252 | [resolve_id] -> resolve_id
256 (function (resolve,_,_) ->
260 CicTextualParser0.Id id -> id
261 | CicTextualParser0.Symbol (descr,_) -> descr
263 match resolve id with
265 | Some (CicTextualParser0.Uri uri) ->
267 CicTextualParser0.ConUri uri
268 | CicTextualParser0.VarUri uri ->
269 UriManager.string_of_uri uri
270 | CicTextualParser0.IndTyUri (uri,tyno) ->
271 UriManager.string_of_uri uri ^ "#xpointer(1/" ^
272 string_of_int (tyno+1) ^ ")"
273 | CicTextualParser0.IndConUri (uri,tyno,consno) ->
274 UriManager.string_of_uri uri ^ "#xpointer(1/" ^
275 string_of_int (tyno+1) ^ "/" ^ string_of_int consno ^ ")")
276 | Some (CicTextualParser0.Term term) ->
277 (* CSC: Implementare resa delle scelte *)
278 "To be implemented XXX01"
279 | Some CicTextualParser0.Implicit -> assert false
283 let index = C.interactive_interpretation_choice choices in
284 List.nth resolve_ids index
286 (known_ids @ dom', resolve_id'), metasenv',term