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