1 (* Copyright (C) 2004-2005, 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://helm.cs.unibo.it/
28 exception Ambiguous_input
29 (* the integer is an offset to be added to each location *)
30 exception DisambiguationError of
31 int * (Token.flocation option * string Lazy.t) list list
32 (** parameters are: option name, error message *)
33 exception Unbound_identifier of string
35 type choose_uris_callback =
36 id:string -> UriManager.uri list -> UriManager.uri list
38 type choose_interp_callback = (string * string) list list -> int list
40 let mono_uris_callback ~id =
41 if Helm_registry.get_bool "matita.auto_disambiguation" then
46 let mono_interp_callback _ = raise Ambiguous_input
48 let _choose_uris_callback = ref mono_uris_callback
49 let _choose_interp_callback = ref mono_interp_callback
50 let set_choose_uris_callback f = _choose_uris_callback := f
51 let set_choose_interp_callback f = _choose_interp_callback := f
55 let interactive_user_uri_choice ~selection_mode ?ok
56 ?(enable_button_for_non_vars = true) ~title ~msg ~id uris =
57 !_choose_uris_callback ~id uris
59 let interactive_interpretation_choice interp =
60 !_choose_interp_callback interp
62 let input_or_locate_uri ~(title:string) ?id =
63 (* Zack: I try to avoid using this callback. I therefore assume that
64 * the presence of an identifier that can't be resolved via "locate"
65 * query is a syntax error *)
66 let msg = match id with Some id -> id | _ -> "_" in
67 raise (Unbound_identifier msg)
70 module Disambiguator = Disambiguate.Make (Callbacks)
72 (* implement module's API *)
74 let disambiguate_thing ~aliases ~universe
75 ~(f:?fresh_instances:bool ->
76 aliases:DisambiguateTypes.environment ->
77 universe:DisambiguateTypes.multiple_environment option ->
79 ~(drop_aliases: 'b -> 'b)
80 ~(drop_aliases_and_clear_diff: 'b -> 'b)
83 assert (universe <> None);
84 let library = false, DisambiguateTypes.Environment.empty, None in
85 let multi_aliases = false, DisambiguateTypes.Environment.empty, universe in
86 let mono_aliases = true, aliases, Some DisambiguateTypes.Environment.empty in
87 let passes = (* <fresh_instances?, aliases, coercions?> *)
88 [ (false, mono_aliases, false);
89 (false, multi_aliases, false);
90 (true, mono_aliases, false);
91 (true, multi_aliases, false);
92 (true, mono_aliases, true);
93 (true, multi_aliases, true);
94 (true, library, true);
97 let try_pass (fresh_instances, (_, aliases, universe), insert_coercions) =
98 CicRefine.insert_coercions := insert_coercions;
99 f ~fresh_instances ~aliases ~universe thing
101 let set_aliases (instances,(use_mono_aliases,_,_),_) (_, user_asked as res) =
102 if use_mono_aliases && not instances then
104 else if user_asked then
105 drop_aliases res (* one shot aliases *)
107 drop_aliases_and_clear_diff res
113 set_aliases pass (try_pass pass)
114 with Disambiguate.NoWellTypedInterpretation (offset,newerrors) ->
115 raise (DisambiguationError (offset, errors @ [newerrors])))
118 set_aliases hd (try_pass hd)
119 with Disambiguate.NoWellTypedInterpretation (_offset,newerrors) ->
120 aux (errors @ [newerrors]) tl)
123 let saved_insert_coercions = !CicRefine.insert_coercions in
125 let res = aux [] passes in
126 CicRefine.insert_coercions := saved_insert_coercions;
129 CicRefine.insert_coercions := saved_insert_coercions;
132 type disambiguator_thing =
135 aliases:DisambiguateTypes.environment ->
136 universe:DisambiguateTypes.multiple_environment option ->
137 f:(?fresh_instances:bool ->
138 aliases:DisambiguateTypes.environment ->
139 universe:DisambiguateTypes.multiple_environment option ->
141 drop_aliases:('b * bool -> 'b * bool) ->
142 drop_aliases_and_clear_diff:('b * bool -> 'b * bool) -> 'a -> 'b * bool
145 let disambiguate_thing =
146 let profiler = HExtlib.profile "disambiguate_thing" in
148 fun ~aliases ~universe ~f ~drop_aliases ~drop_aliases_and_clear_diff thing
149 -> profiler.HExtlib.profile
150 (disambiguate_thing ~aliases ~universe ~f ~drop_aliases
151 ~drop_aliases_and_clear_diff) thing
154 let drop_aliases (choices, user_asked) =
155 (List.map (fun (d, a, b, c) -> d, a, b, c) choices),
158 let drop_aliases_and_clear_diff (choices, user_asked) =
159 (List.map (fun (_, a, b, c) -> [], a, b, c) choices),
162 let disambiguate_term ?fresh_instances ~dbd ~context ~metasenv ?initial_ugraph
163 ~aliases ~universe term
165 assert (fresh_instances = None);
167 Disambiguator.disambiguate_term ~dbd ~context ~metasenv ?initial_ugraph
169 disambiguate_thing.do_it ~aliases ~universe ~f ~drop_aliases
170 ~drop_aliases_and_clear_diff term
172 let disambiguate_obj ?fresh_instances ~dbd ~aliases ~universe ~uri obj =
173 assert (fresh_instances = None);
174 let f = Disambiguator.disambiguate_obj ~dbd ~uri in
175 disambiguate_thing.do_it ~aliases ~universe ~f ~drop_aliases
176 ~drop_aliases_and_clear_diff obj