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