]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/disambiguation/disambiguate.ml
The aliases and multi_aliases in the lexicon status are now
[helm.git] / helm / software / components / disambiguation / disambiguate.ml
1 (* Copyright (C) 2004, 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 open DisambiguateTypes
31 open UriManager
32
33 module Ast = CicNotationPt
34
35 (* the integer is an offset to be added to each location *)
36 exception NoWellTypedInterpretation of
37  int *
38  ((Stdpp.location list * string * string) list *
39   (DisambiguateTypes.domain_item * string) list *
40   (Stdpp.location * string) Lazy.t * bool) list
41 exception PathNotWellFormed
42
43   (** raised when an environment is not enough informative to decide *)
44 exception Try_again of string Lazy.t
45
46 type ('alias,'term) aliases= bool * 'term DisambiguateTypes.environment
47 type 'a disambiguator_input = string * int * 'a
48
49 type domain = domain_tree list
50 and domain_tree = Node of Stdpp.location list * domain_item * domain
51
52 let rec string_of_domain =
53  function
54     [] -> ""
55   | Node (_,domain_item,l)::tl ->
56      DisambiguateTypes.string_of_domain_item domain_item ^
57       " [ " ^ string_of_domain l ^ " ] " ^ string_of_domain tl
58
59 let rec filter_map_domain f =
60  function
61     [] -> []
62   | Node (locs,domain_item,l)::tl ->
63      match f locs domain_item with
64         None -> filter_map_domain f l @ filter_map_domain f tl
65       | Some res -> res :: filter_map_domain f l @ filter_map_domain f tl
66
67 let rec map_domain f =
68  function
69     [] -> []
70   | Node (locs,domain_item,l)::tl ->
71      f locs domain_item :: map_domain f l @ map_domain f tl
72
73 let uniq_domain dom =
74  let rec aux seen =
75   function
76      [] -> seen,[]
77    | Node (locs,domain_item,l)::tl ->
78       if List.mem domain_item seen then
79        let seen,l = aux seen l in
80        let seen,tl = aux seen tl in
81         seen, l @ tl
82       else
83        let seen,l = aux (domain_item::seen) l in
84        let seen,tl = aux seen tl in
85         seen, Node (locs,domain_item,l)::tl
86  in
87   snd (aux [] dom)
88
89 let debug = false
90 let debug_print s = if debug then prerr_endline (Lazy.force s) else ()
91
92 (*
93   (** print benchmark information *)
94 let benchmark = true
95 let max_refinements = ref 0       (* benchmarking is not thread safe *)
96 let actual_refinements = ref 0
97 let domain_size = ref 0
98 let choices_avg = ref 0.
99 *)
100
101 let descr_of_domain_item = function
102  | Id s -> s
103  | Symbol (s, _) -> s
104  | Num i -> string_of_int i
105
106 type ('term,'metasenv,'subst,'graph) test_result =
107   | Ok of 'term * 'metasenv * 'subst * 'graph
108   | Ko of (Stdpp.location * string) Lazy.t
109   | Uncertain of (Stdpp.location * string) Lazy.t
110
111 let resolve ~env ~mk_choice (item: domain_item) arg =
112   try
113    match snd (mk_choice (Environment.find item env)), arg with
114       `Num_interp f, `Num_arg n -> f n
115     | `Sym_interp f, `Args l -> f l
116     | `Sym_interp f, `Num_arg n -> (* Implicit number! *) f []
117     | _,_ -> assert false
118   with Not_found -> 
119     failwith ("Domain item not found: " ^ 
120       (DisambiguateTypes.string_of_domain_item item))
121
122   (* TODO move it to Cic *)
123 let find_in_context name context =
124   let rec aux acc = function
125     | [] -> raise Not_found
126     | Some hd :: tl when hd = name -> acc
127     | _ :: tl ->  aux (acc + 1) tl
128   in
129   aux 1 context
130
131 let string_of_name =
132  function
133   | Ast.Ident (n, None) -> Some n
134   | _ -> assert false
135 ;;
136
137 let rec domain_of_term ?(loc = HExtlib.dummy_floc) ~context = function
138   | Ast.AttributedTerm (`Loc loc, term) ->
139      domain_of_term ~loc ~context term
140   | Ast.AttributedTerm (_, term) ->
141       domain_of_term ~loc ~context term
142   | Ast.Symbol (symbol, instance) ->
143       [ Node ([loc], Symbol (symbol, instance), []) ]
144       (* to be kept in sync with Ast.Appl (Ast.Symbol ...) *)
145   | Ast.Appl (Ast.Symbol (symbol, instance) as hd :: args)
146   | Ast.Appl (Ast.AttributedTerm (_,Ast.Symbol (symbol, instance)) as hd :: args)
147      ->
148       let args_dom =
149         List.fold_right
150           (fun term acc -> domain_of_term ~loc ~context term @ acc)
151           args [] in
152       let loc =
153        match hd with
154           Ast.AttributedTerm (`Loc loc,_)  -> loc
155         | _ -> loc
156       in
157        [ Node ([loc], Symbol (symbol, instance), args_dom) ]
158   | Ast.Appl (Ast.Ident (name, subst) as hd :: args)
159   | Ast.Appl (Ast.AttributedTerm (_,Ast.Ident (name, subst)) as hd :: args) ->
160       let args_dom =
161         List.fold_right
162           (fun term acc -> domain_of_term ~loc ~context term @ acc)
163           args [] in
164       let loc =
165        match hd with
166           Ast.AttributedTerm (`Loc loc,_)  -> loc
167         | _ -> loc
168       in
169       (try
170         (* the next line can raise Not_found *)
171         ignore(find_in_context name context);
172         if subst <> None then
173           Ast.fail loc "Explicit substitutions not allowed here"
174         else
175           args_dom
176       with Not_found ->
177         (match subst with
178         | None -> [ Node ([loc], Id name, args_dom) ]
179         | Some subst ->
180             let terms =
181               List.fold_left
182                 (fun dom (_, term) ->
183                   let dom' = domain_of_term ~loc ~context term in
184                   dom @ dom')
185                 [] subst in
186             [ Node ([loc], Id name, terms @ args_dom) ]))
187   | Ast.Appl terms ->
188       List.fold_right
189         (fun term acc -> domain_of_term ~loc ~context term @ acc)
190         terms []
191   | Ast.Binder (kind, (var, typ), body) ->
192       let type_dom = domain_of_term_option ~loc ~context typ in
193       let body_dom =
194         domain_of_term ~loc
195           ~context:(string_of_name var :: context) body in
196       (match kind with
197       | `Exists -> [ Node ([loc], Symbol ("exists", 0), (type_dom @ body_dom)) ]
198       | _ -> type_dom @ body_dom)
199   | Ast.Case (term, indty_ident, outtype, branches) ->
200       let term_dom = domain_of_term ~loc ~context term in
201       let outtype_dom = domain_of_term_option ~loc ~context outtype in
202       let rec get_first_constructor = function
203         | [] -> []
204         | (Ast.Pattern (head, _, _), _) :: _ -> [ Node ([loc], Id head, []) ]
205         | _ :: tl -> get_first_constructor tl in
206       let do_branch =
207        function
208           Ast.Pattern (head, _, args), term ->
209            let (term_context, args_domain) =
210              List.fold_left
211                (fun (cont, dom) (name, typ) ->
212                  (string_of_name name :: cont,
213                   (match typ with
214                   | None -> dom
215                   | Some typ -> dom @ domain_of_term ~loc ~context:cont typ)))
216                (context, []) args
217            in
218             domain_of_term ~loc ~context:term_context term @ args_domain
219         | Ast.Wildcard, term ->
220             domain_of_term ~loc ~context term
221       in
222       let branches_dom =
223         List.fold_left (fun dom branch -> dom @ do_branch branch) [] branches in
224       (match indty_ident with
225        | None -> get_first_constructor branches
226        | Some (ident, _) -> [ Node ([loc], Id ident, []) ])
227       @ term_dom @ outtype_dom @ branches_dom
228   | Ast.Cast (term, ty) ->
229       let term_dom = domain_of_term ~loc ~context term in
230       let ty_dom = domain_of_term ~loc ~context ty in
231       term_dom @ ty_dom
232   | Ast.LetIn ((var, typ), body, where) ->
233       let body_dom = domain_of_term ~loc ~context body in
234       let type_dom = domain_of_term_option ~loc ~context typ in
235       let where_dom =
236         domain_of_term ~loc ~context:(string_of_name var :: context) where in
237       body_dom @ type_dom @ where_dom
238   | Ast.LetRec (kind, defs, where) ->
239       let add_defs context =
240         List.fold_left
241           (fun acc (_, (var, _), _, _) -> string_of_name var :: acc
242           ) context defs in
243       let where_dom = domain_of_term ~loc ~context:(add_defs context) where in
244       let defs_dom =
245         List.fold_left
246           (fun dom (params, (_, typ), body, _) ->
247             let context' =
248              add_defs
249               (List.fold_left
250                 (fun acc (var,_) -> string_of_name var :: acc)
251                 context params)
252             in
253             List.rev
254              (snd
255                (List.fold_left
256                 (fun (context,res) (var,ty) ->
257                   string_of_name var :: context,
258                   domain_of_term_option ~loc ~context ty @ res)
259                 (add_defs context,[]) params))
260             @ dom
261             @ domain_of_term_option ~loc ~context:context' typ
262             @ domain_of_term ~loc ~context:context' body
263           ) [] defs
264       in
265       defs_dom @ where_dom
266   | Ast.Ident (name, subst) ->
267       (try
268         (* the next line can raise Not_found *)
269         ignore(find_in_context name context);
270         if subst <> None then
271           Ast.fail loc "Explicit substitutions not allowed here"
272         else
273           []
274       with Not_found ->
275         (match subst with
276         | None -> [ Node ([loc], Id name, []) ]
277         | Some subst ->
278             let terms =
279               List.fold_left
280                 (fun dom (_, term) ->
281                   let dom' = domain_of_term ~loc ~context term in
282                   dom @ dom')
283                 [] subst in
284             [ Node ([loc], Id name, terms) ]))
285   | Ast.Uri _ -> []
286   | Ast.Implicit -> []
287   | Ast.Num (num, i) -> [ Node ([loc], Num i, []) ]
288   | Ast.Meta (index, local_context) ->
289       List.fold_left
290        (fun dom term -> dom @ domain_of_term_option ~loc ~context term)
291        [] local_context
292   | Ast.Sort _ -> []
293   | Ast.UserInput
294   | Ast.Literal _
295   | Ast.Layout _
296   | Ast.Magic _
297   | Ast.Variable _ -> assert false
298
299 and domain_of_term_option ~loc ~context = function
300   | None -> []
301   | Some t -> domain_of_term ~loc ~context t
302
303 let domain_of_term ~context term = 
304  uniq_domain (domain_of_term ~context term)
305
306 let domain_of_obj ~context ast =
307  assert (context = []);
308   match ast with
309    | Ast.Theorem (_,_,ty,bo) ->
310       domain_of_term [] ty
311       @ (match bo with
312           None -> []
313         | Some bo -> domain_of_term [] bo)
314    | Ast.Inductive (params,tyl) ->
315       let context, dom = 
316        List.fold_left
317         (fun (context, dom) (var, ty) ->
318           let context' = string_of_name var :: context in
319           match ty with
320              None -> context', dom
321            | Some ty -> context', dom @ domain_of_term context ty
322         ) ([], []) params in
323       let context_w_types =
324         List.rev_map (fun (var, _, _, _) -> Some var) tyl @ context in
325       dom
326       @ List.flatten (
327          List.map
328           (fun (_,_,ty,cl) ->
329             domain_of_term context ty
330             @ List.flatten (
331                List.map
332                 (fun (_,ty) -> domain_of_term context_w_types ty) cl))
333                 tyl)
334    | Ast.Record (params,var,ty,fields) ->
335       let context, dom = 
336        List.fold_left
337         (fun (context, dom) (var, ty) ->
338           let context' = string_of_name var :: context in
339           match ty with
340              None -> context', dom
341            | Some ty -> context', dom @ domain_of_term context ty
342         ) ([], []) params in
343       let context_w_types = Some var :: context in
344       dom
345       @ domain_of_term context ty
346       @ snd
347          (List.fold_left
348           (fun (context,res) (name,ty,_,_) ->
349             Some name::context, res @ domain_of_term context ty
350           ) (context_w_types,[]) fields)
351
352 let domain_of_obj ~context obj = 
353  uniq_domain (domain_of_obj ~context obj)
354
355 let domain_of_ast_term = domain_of_term;;
356
357   (* dom1 \ dom2 *)
358 let domain_diff dom1 dom2 =
359 (* let domain_diff = Domain.diff *)
360   let is_in_dom2 elt =
361     List.exists
362      (function
363        | Symbol (symb, 0) ->
364           (match elt with
365               Symbol (symb',_) when symb = symb' -> true
366             | _ -> false)
367        | Num i ->
368           (match elt with
369               Num _ -> true
370             | _ -> false)
371        | item -> elt = item
372      ) dom2
373   in
374    let rec aux =
375     function
376        [] -> []
377      | Node (_,elt,l)::tl when is_in_dom2 elt -> aux (l @ tl)
378      | Node (loc,elt,l)::tl -> Node (loc,elt,aux l)::(aux tl)
379    in
380     aux dom1
381
382 module type Disambiguator =
383 sig
384   val disambiguate_thing:
385     context:'context ->
386     metasenv:'metasenv ->
387     subst:'subst ->
388     use_coercions:bool ->
389     string_context_of_context:('context -> string option list) ->
390     initial_ugraph:'ugraph ->
391     hint: 
392       ('metasenv -> 'raw_thing -> 'raw_thing) * 
393       (('refined_thing,'metasenv,'subst,'ugraph) test_result ->
394          ('refined_thing,'metasenv,'subst,'ugraph) test_result) ->
395     mk_implicit:(bool -> 'alias) ->
396     description_of_alias:('alias -> string) ->
397     aliases:'alias DisambiguateTypes.Environment.t ->
398     universe:'alias list DisambiguateTypes.Environment.t option ->
399     lookup_in_library:(
400       DisambiguateTypes.interactive_user_uri_choice_type ->
401       DisambiguateTypes.input_or_locate_uri_type ->
402       DisambiguateTypes.Environment.key ->
403       'alias list) ->
404     uri:'uri ->
405     pp_thing:('ast_thing -> string) ->
406     domain_of_thing:(context: string option list -> 'ast_thing -> domain) ->
407     interpretate_thing:(
408       context:'context ->
409       env:'alias DisambiguateTypes.Environment.t ->
410       uri:'uri ->
411       is_path:bool -> 
412       'ast_thing -> 
413       localization_tbl:'cichash -> 
414         'raw_thing) ->
415     refine_thing:(
416       'metasenv -> 'subst -> 'context -> 'uri -> use_coercions:bool ->
417       'raw_thing -> 'ugraph -> localization_tbl:'cichash -> 
418         ('refined_thing, 'metasenv,'subst,'ugraph) test_result) ->
419     localization_tbl:'cichash ->
420     string * int * 'ast_thing ->
421     ((DisambiguateTypes.Environment.key * 'alias) list * 
422      'metasenv * 'subst * 'refined_thing * 'ugraph)
423     list * bool
424 end
425
426 module Make (C: Callbacks) =
427   struct
428 let refine_profiler = HExtlib.profile "disambiguate_thing.refine_thing"
429
430     let disambiguate_thing 
431       ~context ~metasenv ~subst ~use_coercions
432       ~string_context_of_context
433       ~initial_ugraph:base_univ ~hint
434       ~mk_implicit ~description_of_alias
435       ~aliases ~universe ~lookup_in_library 
436       ~uri ~pp_thing ~domain_of_thing ~interpretate_thing ~refine_thing 
437       ~localization_tbl
438       (thing_txt,thing_txt_prefix_len,thing)
439     =
440       debug_print (lazy "DISAMBIGUATE INPUT");
441       debug_print (lazy ("TERM IS: " ^ (pp_thing thing)));
442       let thing_dom =
443         domain_of_thing ~context:(string_context_of_context context) thing in
444       debug_print
445        (lazy (sprintf "DISAMBIGUATION DOMAIN: %s"(string_of_domain thing_dom)));
446 (*
447       debug_print (lazy (sprintf "DISAMBIGUATION ENVIRONMENT: %s"
448         (DisambiguatePp.pp_environment aliases)));
449       debug_print (lazy (sprintf "DISAMBIGUATION UNIVERSE: %s"
450         (match universe with None -> "None" | Some _ -> "Some _")));
451 *)
452       let current_dom =
453         Environment.fold (fun item _ dom -> item :: dom) aliases [] in
454       let todo_dom = domain_diff thing_dom current_dom in
455       debug_print
456        (lazy (sprintf "DISAMBIGUATION DOMAIN AFTER DIFF: %s"(string_of_domain todo_dom)));
457       (* (2) lookup function for any item (Id/Symbol/Num) *)
458       let lookup_choices =
459         fun item ->
460          match universe with
461          | None -> 
462              lookup_in_library 
463                C.interactive_user_uri_choice 
464                C.input_or_locate_uri item
465          | Some e ->
466              (try
467                let item =
468                  match item with
469                  | Symbol (symb, _) -> Symbol (symb, 0)
470                  | item -> item
471                in
472                Environment.find item e
473              with Not_found -> 
474                lookup_in_library 
475                  C.interactive_user_uri_choice 
476                  C.input_or_locate_uri item)
477       in
478 (*
479       (* <benchmark> *)
480       let _ =
481         if benchmark then begin
482           let per_item_choices =
483             List.map
484               (fun dom_item ->
485                 try
486                   let len = List.length (lookup_choices dom_item) in
487                   debug_print (lazy (sprintf "BENCHMARK %s: %d"
488                     (string_of_domain_item dom_item) len));
489                   len
490                 with No_choices _ -> 0)
491               thing_dom
492           in
493           max_refinements := List.fold_left ( * ) 1 per_item_choices;
494           actual_refinements := 0;
495           domain_size := List.length thing_dom;
496           choices_avg :=
497             (float_of_int !max_refinements) ** (1. /. float_of_int !domain_size)
498         end
499       in
500       (* </benchmark> *)
501 *)
502
503       (* (3) test an interpretation filling with meta uninterpreted identifiers
504        *)
505       let test_env aliases todo_dom ugraph = 
506         try
507          let rec aux env = function
508           | [] -> env
509           | Node (_, item, l) :: tl ->
510               let env =
511                 Environment.add item
512                  (mk_implicit
513                    (match item with | Id _ | Num _ -> true | Symbol _ -> false))
514                  env in
515               aux (aux env l) tl in
516          let filled_env = aux aliases todo_dom in
517           let cic_thing =
518             interpretate_thing ~context ~env:filled_env
519              ~uri ~is_path:false thing ~localization_tbl
520           in
521           let cic_thing = (fst hint) metasenv cic_thing in
522 let foo () =
523           let k =
524            refine_thing metasenv subst context uri
525             ~use_coercions cic_thing ugraph ~localization_tbl
526           in
527           let k = (snd hint) k in
528             k
529 in refine_profiler.HExtlib.profile foo ()
530         with
531         | Try_again msg -> Uncertain (lazy (Stdpp.dummy_loc,Lazy.force msg))
532         | Invalid_choice loc_msg -> Ko loc_msg
533       in
534       (* (4) build all possible interpretations *)
535       let (@@) (l1,l2,l3) (l1',l2',l3') = l1@l1', l2@l2', l3@l3' in
536       (* aux returns triples Ok/Uncertain/Ko *)
537       (* rem_dom is the concatenation of all the remainin domains *)
538       let rec aux aliases diff lookup_in_todo_dom todo_dom rem_dom =
539         debug_print (lazy ("ZZZ: " ^ string_of_domain todo_dom));
540         match todo_dom with
541         | [] ->
542             assert (lookup_in_todo_dom = None);
543             (match test_env aliases rem_dom base_univ with
544             | Ok (thing, metasenv,subst,new_univ) -> 
545                [ aliases, diff, metasenv, subst, thing, new_univ ], [], []
546             | Ko loc_msg -> [],[],[aliases,diff,loc_msg,true]
547             | Uncertain loc_msg ->
548                [],[aliases,diff,loc_msg],[])
549         | Node (locs,item,inner_dom) :: remaining_dom ->
550             debug_print (lazy (sprintf "CHOOSED ITEM: %s"
551              (string_of_domain_item item)));
552             let choices =
553              match lookup_in_todo_dom with
554                 None -> lookup_choices item
555               | Some choices -> choices in
556             match choices with
557                [] ->
558                 [], [],
559                  [aliases, diff, 
560                   (lazy (List.hd locs,
561                     "No choices for " ^ string_of_domain_item item)),
562                   true]
563 (*
564              | [codomain_item] ->
565                  (* just one choice. We perform a one-step look-up and
566                     if the next set of choices is also a singleton we
567                     skip this refinement step *)
568                  debug_print(lazy (sprintf "%s CHOSEN" (fst codomain_item)));
569                  let new_env = Environment.add item codomain_item aliases in
570                  let new_diff = (item,codomain_item)::diff in
571                  let lookup_in_todo_dom,next_choice_is_single =
572                   match remaining_dom with
573                      [] -> None,false
574                    | (_,he)::_ ->
575                       let choices = lookup_choices he in
576                        Some choices,List.length choices = 1
577                  in
578                   if next_choice_is_single then
579                    aux new_env new_diff lookup_in_todo_dom remaining_dom
580                     base_univ
581                   else
582                     (match test_env new_env remaining_dom base_univ with
583                     | Ok (thing, metasenv),new_univ ->
584                         (match remaining_dom with
585                         | [] ->
586                            [ new_env, new_diff, metasenv, thing, new_univ ], []
587                         | _ ->
588                            aux new_env new_diff lookup_in_todo_dom
589                             remaining_dom new_univ)
590                     | Uncertain (loc,msg),new_univ ->
591                         (match remaining_dom with
592                         | [] -> [], [new_env,new_diff,loc,msg,true]
593                         | _ ->
594                            aux new_env new_diff lookup_in_todo_dom
595                             remaining_dom new_univ)
596                     | Ko (loc,msg),_ -> [], [new_env,new_diff,loc,msg,true])
597 *)
598              | _::_ ->
599                  let mark_not_significant failures =
600                    List.map
601                     (fun (env, diff, loc_msg, _b) ->
602                       env, diff, loc_msg, false)
603                     failures in
604                  let classify_errors ((ok_l,uncertain_l,error_l) as outcome) =
605                   if ok_l <> [] || uncertain_l <> [] then
606                    ok_l,uncertain_l,mark_not_significant error_l
607                   else
608                    outcome in
609                let rec filter = function 
610                 | [] -> [],[],[]
611                 | codomain_item :: tl ->
612                     (*debug_print(lazy (sprintf "%s CHOSEN" (fst codomain_item)));*)
613                     let new_env = Environment.add item codomain_item aliases in
614                     let new_diff = (item,codomain_item)::diff in
615                     (match
616                       test_env new_env 
617                         (inner_dom@remaining_dom@rem_dom) base_univ
618                      with
619                     | Ok (thing, metasenv,subst,new_univ) ->
620                         let res = 
621                           (match inner_dom with
622                           | [] ->
623                              [new_env,new_diff,metasenv,subst,thing,new_univ],
624                              [], []
625                           | _ ->
626                              aux new_env new_diff None inner_dom
627                               (remaining_dom@rem_dom) 
628                           ) 
629                         in
630                          res @@ filter tl
631                     | Uncertain loc_msg ->
632                         let res =
633                           (match inner_dom with
634                           | [] -> [],[new_env,new_diff,loc_msg],[]
635                           | _ ->
636                              aux new_env new_diff None inner_dom
637                               (remaining_dom@rem_dom) 
638                           )
639                         in
640                          res @@ filter tl
641                     | Ko loc_msg ->
642                         let res = [],[],[new_env,new_diff,loc_msg,true] in
643                          res @@ filter tl)
644                in
645                 let ok_l,uncertain_l,error_l =
646                  classify_errors (filter choices)
647                 in
648                  let res_after_ok_l =
649                   List.fold_right
650                    (fun (env,diff,_,_,_,_) res ->
651                      aux env diff None remaining_dom rem_dom @@ res
652                    ) ok_l ([],[],error_l)
653                 in
654                  List.fold_right
655                   (fun (env,diff,_) res ->
656                     aux env diff None remaining_dom rem_dom @@ res
657                   ) uncertain_l res_after_ok_l
658       in
659       let aux' aliases diff lookup_in_todo_dom todo_dom =
660        match test_env aliases todo_dom base_univ with
661         | Ok _
662         | Uncertain _ ->
663            aux aliases diff lookup_in_todo_dom todo_dom [] 
664         | Ko (loc_msg) -> [],[],[aliases,diff,loc_msg,true]
665       in
666         let res =
667          match aux' aliases [] None todo_dom with
668          | [],uncertain,errors ->
669             let errors =
670              List.map
671               (fun (env,diff,loc_msg) -> (env,diff,loc_msg,true)
672               ) uncertain @ errors
673             in
674             let errors =
675              List.map
676               (fun (env,diff,loc_msg,significant) ->
677                 let env' =
678                  filter_map_domain
679                    (fun locs domain_item ->
680                      try
681                       let description =
682                        description_of_alias (Environment.find domain_item env)
683                       in
684                        Some (locs,descr_of_domain_item domain_item,description)
685                      with
686                       Not_found -> None)
687                    thing_dom
688                 in
689                 let diff= List.map (fun a,b -> a,description_of_alias b) diff in
690                  env',diff,loc_msg,significant
691               ) errors
692             in
693              raise (NoWellTypedInterpretation (0,errors))
694          | [_,diff,metasenv,subst,t,ugraph],_,_ ->
695              debug_print (lazy "SINGLE INTERPRETATION");
696              [diff,metasenv,subst,t,ugraph], false
697          | l,_,_ ->
698              debug_print 
699                (lazy (sprintf "MANY INTERPRETATIONS (%d)" (List.length l)));
700              let choices =
701                List.map
702                  (fun (env, _, _, _, _, _) ->
703                    map_domain
704                      (fun locs domain_item ->
705                        let description =
706                          description_of_alias (Environment.find domain_item env)
707                        in
708                        locs,descr_of_domain_item domain_item, description)
709                      thing_dom)
710                  l
711              in
712              let choosed = 
713                C.interactive_interpretation_choice 
714                  thing_txt thing_txt_prefix_len choices 
715              in
716              (List.map (fun n->let _,d,m,s,t,u= List.nth l n in d,m,s,t,u)
717                choosed),
718               true
719         in
720          res
721
722   end