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