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 (******************************************************************************)
36 (* CSC: IMPERATIVE AND NOT VERY CLEAN, TO GET THE LAST ISSUED QUERY *)
39 MQueryGenerator.set_confirm_query
40 (function q -> query := MQueryUtil.text_of_query q ; true) ;
42 !query ^ " <h1>Result:</h1> " ^ MQueryUtil.text_of_result result "<br>"
45 (** This module provides a functor to disambiguate the input **)
46 (** given a set of user-interface call-backs **)
48 module type Callbacks =
50 val output_html : string -> unit
51 val interactive_user_uri_choice :
52 selection_mode:[`SINGLE | `EXTENDED] ->
54 ?enable_button_for_non_vars:bool ->
55 title:string -> msg:string -> id:string -> string list -> string list
56 val interactive_interpretation_choice :
57 (string * string) list list -> int
58 val input_or_locate_uri : title:string -> UriManager.uri
62 type domain_and_interpretation =
63 string list * (string -> CicTextualParser0.uri option)
66 module Make(C:Callbacks) =
69 let locate_one_id id =
70 let result = MQueryGenerator.locate id in
74 Misc.wrong_xpointer_format_from_wrong_xpointer_format' uri
77 " <h1>Locate Query: </h1><pre>" ^ get_last_query result ^ "</pre>"
83 [UriManager.string_of_uri
84 (C.input_or_locate_uri
85 ~title:("URI matching \"" ^ id ^ "\" unknown."))]
88 C.interactive_user_uri_choice
89 ~selection_mode:`EXTENDED
90 ~ok:"Try every selection."
91 ~enable_button_for_non_vars:true
92 ~title:"Ambiguous input."
94 ("Ambiguous input \"" ^ id ^
95 "\". Please, choose one or more interpretations:")
99 List.map Misc.cic_textual_parser_uri_of_string uris'
102 exception ThereDoesNotExistAnyWellTypedInterpretationOfTheInput
105 Ok of Cic.term * Cic.metasenv
109 let disambiguate_input 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 *)
121 let list_of_uris = List.map locate_one_id dom' in
123 List.fold_left (fun i uris -> i * List.length uris) 1 list_of_uris
127 ("<h1>Disambiguation phase started: " ^
128 string_of_int tests_no ^ " cases will be tried.") ;
129 (* and now we compute the list of all possible assignments from *)
130 (* id to uris that generate well-typed terms *)
132 (* function to test if a partial interpretation is so far correct *)
133 let test resolve_id residual_dom =
134 (* We put implicits in place of every identifier that is not *)
135 (* resolved by resolve_id *)
139 match resolve_id id with
141 | Some uri -> Some (CicTextualParser0.Uri uri)
146 if id = id' then Some (CicTextualParser0.Implicit) else f id'
147 ) resolve_id' residual_dom
149 (* and we try to refine the term *)
150 let saved_status = !CicTextualParser0.metasenv in
151 let metasenv',expr = mk_metasenv_and_expr resolve_id'' in
152 (*CSC: Bug here: we do not try to typecheck also the metasenv' *)
153 (* The parser is imperative ==> we must restore the old status ;-(( *)
154 CicTextualParser0.metasenv := saved_status ;
156 let term,_,_,metasenv'' =
157 CicRefine.type_of_aux' metasenv' context expr
161 CicRefine.MutCaseFixAndCofixRefineNotImplemented ->
163 let term = CicTypeChecker.type_of_aux' metasenv' context expr in
167 | CicRefine.Uncertain _ ->
168 prerr_endline ("%%% UNCERTAIN!!! " ^ CicPp.ppterm expr) ;
171 prerr_endline ("%%% PRUNED!!! " ^ CicPp.ppterm expr) ;
174 let rec aux resolve_id ids list_of_uris =
175 match ids,list_of_uris with
177 (match test resolve_id [] with
178 Ok (term,metasenv) -> [resolve_id,term,metasenv]
179 | Ko | Uncertain -> [])
180 | id::idtl,uris::uristl ->
186 function id' -> if id = id' then Some uri else resolve_id id'
188 (match test resolve_id' idtl with
189 Ok (term,metasenv) ->
190 (* the next three ``if''s are used to avoid the base *)
191 (* case where the term would be refined a second time. *)
193 [resolve_id',term,metasenv]
195 (aux resolve_id' idtl uristl)
198 (if uristl = [] then []
200 (aux resolve_id' idtl uristl)
207 | _,_ -> assert false
209 aux resolve_id dom' list_of_uris
212 (function (resolve,term,newmetasenv) ->
213 (* If metasen <> newmetasenv is a normal condition, we should *)
214 (* be prepared to apply the returned substitution to the *)
215 (* whole current proof. *)
216 if metasenv <> newmetasenv then
219 ("+++++ ASSERTION FAILED: " ^
220 "a refine operation should not modify the metasenv") ;
221 (* an assert would raise an exception that could be caught *)
225 let resolve_id',term,metasenv' =
226 match resolve_ids with
227 [] -> raise ThereDoesNotExistAnyWellTypedInterpretationOfTheInput
228 | [resolve_id] -> resolve_id
232 (function (resolve,_,_) ->
236 match resolve id with
240 CicTextualParser0.ConUri uri
241 | CicTextualParser0.VarUri uri ->
242 UriManager.string_of_uri uri
243 | CicTextualParser0.IndTyUri (uri,tyno) ->
244 UriManager.string_of_uri uri ^ "#xpointer(1/" ^
245 string_of_int (tyno+1) ^ ")"
246 | CicTextualParser0.IndConUri (uri,tyno,consno) ->
247 UriManager.string_of_uri uri ^ "#xpointer(1/" ^
248 string_of_int (tyno+1) ^ "/" ^ string_of_int consno ^ ")"
252 let index = C.interactive_interpretation_choice choices in
253 List.nth resolve_ids index
255 (known_ids @ dom', resolve_id'), metasenv',term