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 *)
37 (* FG : THIS FUNCTION IS BECOMING A REAL NONSENSE *)
40 let out s = query := ! query ^ s in
41 MQueryGenerator.set_confirm_query
43 query := ""; MQueryUtil.text_of_query out q ""; true);
45 out (!query ^ " <h1>Result:</h1> "); MQueryUtil.text_of_result out result "<br>";
49 (** This module provides a functor to disambiguate the input **)
50 (** given a set of user-interface call-backs **)
52 module type Callbacks =
54 val output_html : string -> unit
55 val interactive_user_uri_choice :
56 selection_mode:[`SINGLE | `EXTENDED] ->
58 ?enable_button_for_non_vars:bool ->
59 title:string -> msg:string -> id:string -> string list -> string list
60 val interactive_interpretation_choice :
61 (string * string) list list -> int
62 val input_or_locate_uri : title:string -> UriManager.uri
66 type domain_and_interpretation =
67 string list * (string -> CicTextualParser0.uri option)
70 module Make(C:Callbacks) =
73 let locate_one_id id =
74 let result = MQueryGenerator.locate id in
78 MQueryMisc.wrong_xpointer_format_from_wrong_xpointer_format' uri
81 " <h1>Locate Query: </h1><pre>" ^ get_last_query result ^ "</pre>"
87 [UriManager.string_of_uri
88 (C.input_or_locate_uri
89 ~title:("URI matching \"" ^ id ^ "\" unknown."))]
92 C.interactive_user_uri_choice
93 ~selection_mode:`EXTENDED
94 ~ok:"Try every selection."
95 ~enable_button_for_non_vars:true
96 ~title:"Ambiguous input."
98 ("Ambiguous input \"" ^ id ^
99 "\". Please, choose one or more interpretations:")
103 List.map MQueryMisc.cic_textual_parser_uri_of_string uris'
106 exception ThereDoesNotExistAnyWellTypedInterpretationOfTheInput
109 Ok of Cic.term * Cic.metasenv
113 let disambiguate_input context metasenv dom mk_metasenv_and_expr ~id_to_uris=
114 let known_ids,resolve_id = id_to_uris in
120 if List.mem he known_ids then filter tl else he::(filter tl)
124 (* for each id in dom' we get the list of uris associated to it *)
125 let list_of_uris = List.map locate_one_id dom' in
127 List.fold_left (fun i uris -> i * List.length uris) 1 list_of_uris
131 ("<h1>Disambiguation phase started: " ^
132 string_of_int tests_no ^ " cases will be tried.") ;
133 (* and now we compute the list of all possible assignments from *)
134 (* id to uris that generate well-typed terms *)
136 (* function to test if a partial interpretation is so far correct *)
137 let test resolve_id residual_dom =
138 (* We put implicits in place of every identifier that is not *)
139 (* resolved by resolve_id *)
143 match resolve_id id with
145 | Some uri -> Some (CicTextualParser0.Uri uri)
150 if id = id' then Some (CicTextualParser0.Implicit) else f id'
151 ) resolve_id' residual_dom
153 (* and we try to refine the term *)
154 let saved_status = !CicTextualParser0.metasenv in
155 let metasenv',expr = mk_metasenv_and_expr resolve_id'' in
156 (*CSC: Bug here: we do not try to typecheck also the metasenv' *)
157 (* The parser is imperative ==> we must restore the old status ;-(( *)
158 CicTextualParser0.metasenv := saved_status ;
160 let term,_,_,metasenv'' =
161 CicRefine.type_of_aux' metasenv' context expr
165 CicRefine.MutCaseFixAndCofixRefineNotImplemented ->
167 let term = CicTypeChecker.type_of_aux' metasenv' context expr in
171 | CicRefine.Uncertain _ ->
172 prerr_endline ("%%% UNCERTAIN!!! " ^ CicPp.ppterm expr) ;
175 prerr_endline ("%%% PRUNED!!! " ^ CicPp.ppterm expr) ;
178 let rec aux resolve_id ids list_of_uris =
179 match ids,list_of_uris with
181 (match test resolve_id [] with
182 Ok (term,metasenv) -> [resolve_id,term,metasenv]
183 | Ko | Uncertain -> [])
184 | id::idtl,uris::uristl ->
190 function id' -> if id = id' then Some uri else resolve_id id'
192 (match test resolve_id' idtl with
193 Ok (term,metasenv) ->
194 (* the next three ``if''s are used to avoid the base *)
195 (* case where the term would be refined a second time. *)
197 [resolve_id',term,metasenv]
199 (aux resolve_id' idtl uristl)
202 (if uristl = [] then []
204 (aux resolve_id' idtl uristl)
211 | _,_ -> assert false
213 aux resolve_id dom' list_of_uris
216 (function (resolve,term,newmetasenv) ->
217 (* If metasen <> newmetasenv is a normal condition, we should *)
218 (* be prepared to apply the returned substitution to the *)
219 (* whole current proof. *)
220 if metasenv <> newmetasenv then
223 ("+++++ ASSERTION FAILED: " ^
224 "a refine operation should not modify the metasenv") ;
225 (* an assert would raise an exception that could be caught *)
229 let resolve_id',term,metasenv' =
230 match resolve_ids with
231 [] -> raise ThereDoesNotExistAnyWellTypedInterpretationOfTheInput
232 | [resolve_id] -> resolve_id
236 (function (resolve,_,_) ->
240 match resolve id with
244 CicTextualParser0.ConUri uri
245 | CicTextualParser0.VarUri uri ->
246 UriManager.string_of_uri uri
247 | CicTextualParser0.IndTyUri (uri,tyno) ->
248 UriManager.string_of_uri uri ^ "#xpointer(1/" ^
249 string_of_int (tyno+1) ^ ")"
250 | CicTextualParser0.IndConUri (uri,tyno,consno) ->
251 UriManager.string_of_uri uri ^ "#xpointer(1/" ^
252 string_of_int (tyno+1) ^ "/" ^ string_of_int consno ^ ")"
256 let index = C.interactive_interpretation_choice choices in
257 List.nth resolve_ids index
259 (known_ids @ dom', resolve_id'), metasenv',term