]> matita.cs.unibo.it Git - helm.git/blob - matita/components/disambiguation/multiPassDisambiguator.ml
Most warnings turned into errors and avoided
[helm.git] / matita / components / disambiguation / multiPassDisambiguator.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 (* $Id$ *)
27
28 let debug = ref false;;
29 let debug_print s =
30  if !debug then prerr_endline (Lazy.force s) else ();;
31
32 exception DisambiguationError of
33  int *
34  ((Stdpp.location list * string * string) list *
35   (DisambiguateTypes.domain_item * string) list * 
36   (Stdpp.location * string) Lazy.t * bool) list list
37   (** parameters are: option name, error message *)
38
39 (* implement module's API *)
40
41 let only_one_pass = ref false;;
42 let use_library = ref false;;
43
44 let passes () = (* <fresh_instances?, aliases, coercions?> *)
45  if !only_one_pass then
46   [ (true, `Mono, false) ]
47  else if !use_library then
48   [ (true, `Library, false); 
49       (* for demo to reduce the number of interpretations *)
50     (true, `Library, true);
51   ]
52  else if !debug then
53   [ (true, `Multi, true); ]
54  else
55   [ (true, `Mono, false);
56     (true, `Multi, false);
57     (true, `Mono, true);
58     (true, `Multi, true);
59   ]
60 ;;
61
62 let drop_aliases ?(minimize_instances=false) ~description_of_alias
63  (choices, user_asked)
64 =
65  let module D = DisambiguateTypes in
66  let compare ci1 ci2 = description_of_alias ci1 = description_of_alias ci2 in
67  let minimize d =
68   if not minimize_instances then
69    d
70   else
71    let rec aux =
72     function
73        [] -> []
74      | (D.Symbol (s,n),ci1) as he::tl when n > 0 ->
75          if
76           List.for_all
77            (function
78                (D.Symbol (s2,_),ci2) -> s2 <> s || compare ci1 ci2
79              | _ -> true
80            ) d
81          then
82           (D.Symbol (s,0),ci1)::(aux tl)
83          else
84           he::(aux tl)
85      | (D.Num n,ci1) as he::tl when n > 0 ->
86          if
87           List.for_all
88            (function (D.Num _,ci2) -> compare ci1 ci2 | _ -> true) d
89          then
90           (D.Num 0,ci1)::(aux tl)
91          else
92           he::(aux tl)
93       | he::tl -> he::(aux tl)
94    in
95     aux d
96  in
97   (List.map (fun (d, a, b, c, e) -> minimize d, a, b, c, e) choices),
98   user_asked
99
100 let drop_aliases_and_clear_diff (choices, user_asked) =
101   (List.map (fun (_, a, b, c, d) -> [], a, b, c, d) choices),
102   user_asked
103
104 let disambiguate_thing ~description_of_alias ~passes ~aliases ~universe ~f thing
105 =
106   assert (universe <> None);
107   let library = false, DisambiguateTypes.Environment.empty, None in
108   let multi_aliases = false, DisambiguateTypes.Environment.empty, universe in
109   let mono_aliases = true, aliases, Some DisambiguateTypes.Environment.empty in
110   let passes =
111     List.map
112      (function (fresh_instances,env,use_coercions) ->
113        fresh_instances,
114        (match env with `Mono -> mono_aliases | `Multi -> multi_aliases |
115        `Library -> library),
116        use_coercions) passes in
117   let try_pass (fresh_instances, (_, aliases, universe), use_coercions) =
118     f ~fresh_instances ~aliases ~universe ~use_coercions thing
119   in
120   let set_aliases (_instances,(use_mono_aliases,_,_),_) (_, user_asked as res) =
121    if use_mono_aliases then
122     drop_aliases ~minimize_instances:true ~description_of_alias res (* one shot aliases *)
123    else if user_asked then
124     drop_aliases ~minimize_instances:true ~description_of_alias res (* one shot aliases *)
125    else
126     drop_aliases_and_clear_diff res
127   in
128   let rec aux i errors passes =
129   debug_print (lazy ("Pass: " ^ string_of_int i));
130    match passes with
131       [ pass ] ->
132         (try
133           set_aliases pass (try_pass pass)
134          with Disambiguate.NoWellTypedInterpretation (offset,newerrors) ->
135           raise (DisambiguationError (offset, errors @ [newerrors])))
136     | hd :: tl ->
137         (try
138           set_aliases hd (try_pass hd)
139         with Disambiguate.NoWellTypedInterpretation (_offset,newerrors) ->
140          aux (i+1) (errors @ [newerrors]) tl)
141     | [] -> assert false
142   in
143    aux 1 [] passes
144 ;;
145
146 let disambiguate_thing ~passes ~freshen_thing ~context ~metasenv ~subst
147   ~string_context_of_context ~initial_ugraph ~expty ~mk_implicit
148   ~description_of_alias ~fix_instance ~aliases ~universe ~lookup_in_library
149   ~uri ~pp_thing ~domain_of_thing ~interpretate_thing ~refine_thing
150   ~mk_localization_tbl thing
151  =
152   let f ~fresh_instances ~aliases ~universe ~use_coercions (txt,len,thing) =
153     let thing = if fresh_instances then freshen_thing thing else thing in
154      Disambiguate.disambiguate_thing
155       ~context ~metasenv ~subst ~use_coercions ~string_context_of_context
156       ~initial_ugraph ~expty ~mk_implicit ~description_of_alias ~fix_instance
157       ~aliases ~universe ~lookup_in_library 
158       ~uri ~pp_thing ~domain_of_thing ~interpretate_thing ~refine_thing 
159       ~mk_localization_tbl (txt,len,thing)
160   in
161   disambiguate_thing ~description_of_alias ~passes ~aliases
162    ~universe ~f thing