]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/grafite_parser/matitaDisambiguator.ml
1. matitaEngine splitted into disambiguation (now in grafite_parser) and
[helm.git] / helm / ocaml / grafite_parser / matitaDisambiguator.ml
1 (* Copyright (C) 2004-2005, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 open Printf
27
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
34
35 type choose_uris_callback =
36   id:string -> UriManager.uri list -> UriManager.uri list
37
38 type choose_interp_callback = (string * string) list list -> int list
39
40 let mono_uris_callback ~id =
41  if Helm_registry.get_bool "matita.auto_disambiguation" then
42   function l -> l
43  else
44   raise Ambiguous_input
45
46 let mono_interp_callback _ = raise Ambiguous_input
47
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
52
53 module Callbacks =
54   struct
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
58
59     let interactive_interpretation_choice interp =
60       !_choose_interp_callback interp
61
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)
68   end
69   
70 module Disambiguator = Disambiguate.Make (Callbacks)
71
72 (* implement module's API *)
73
74 let disambiguate_thing ~aliases ~universe
75   ~(f:?fresh_instances:bool ->
76       aliases:DisambiguateTypes.environment ->
77       universe:DisambiguateTypes.multiple_environment option ->
78       'a -> 'b)
79   ~(drop_aliases: 'b -> 'b)
80   ~(drop_aliases_and_clear_diff: 'b -> 'b)
81   (thing: 'a)
82 =
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);
95     ]
96   in
97   let try_pass (fresh_instances, (_, aliases, universe), use_coercions) =
98     CoercDb.use_coercions := use_coercions;
99     f ~fresh_instances ~aliases ~universe thing
100   in
101   let set_aliases (instances,(use_mono_aliases,_,_),_) (_, user_asked as res) =
102    if use_mono_aliases && not instances then
103     drop_aliases res
104    else if user_asked then
105     drop_aliases res (* one shot aliases *)
106    else
107     drop_aliases_and_clear_diff res
108   in
109   let rec aux errors =
110     function
111     | [ pass ] ->
112         (try
113           set_aliases pass (try_pass pass)
114          with Disambiguate.NoWellTypedInterpretation (offset,newerrors) ->
115           raise (DisambiguationError (offset, errors @ [newerrors])))
116     | hd :: tl ->
117         (try
118           set_aliases hd (try_pass hd)
119         with Disambiguate.NoWellTypedInterpretation (_offset,newerrors) ->
120          aux (errors @ [newerrors]) tl)
121     | [] -> assert false
122   in
123   let saved_use_coercions = !CoercDb.use_coercions in
124   try
125     let res = aux [] passes in
126     CoercDb.use_coercions := saved_use_coercions;
127     res
128   with exn ->
129     CoercDb.use_coercions := saved_use_coercions;
130     raise exn
131
132 type disambiguator_thing =
133  { do_it :
134     'a 'b.
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 ->
140        'a -> 'b * bool) ->
141     drop_aliases:('b * bool -> 'b * bool) ->
142     drop_aliases_and_clear_diff:('b * bool -> 'b * bool) -> 'a -> 'b * bool
143  }
144
145 let disambiguate_thing =
146  let profiler = HExtlib.profile "disambiguate_thing" in
147   { do_it =
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
152   }
153
154 let drop_aliases (choices, user_asked) =
155   (List.map (fun (d, a, b, c) -> d, a, b, c) choices),
156   user_asked
157
158 let drop_aliases_and_clear_diff (choices, user_asked) =
159   (List.map (fun (_, a, b, c) -> [], a, b, c) choices),
160   user_asked
161
162 let disambiguate_term ?fresh_instances ~dbd ~context ~metasenv ?initial_ugraph
163   ~aliases ~universe term
164  =
165   assert (fresh_instances = None);
166   let f =
167     Disambiguator.disambiguate_term ~dbd ~context ~metasenv ?initial_ugraph
168   in
169   disambiguate_thing.do_it ~aliases ~universe ~f ~drop_aliases
170    ~drop_aliases_and_clear_diff term
171
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