]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_disambiguation/disambiguate.ml
More debugging infos.
[helm.git] / helm / ocaml / cic_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 open Printf
27
28 open DisambiguateTypes
29 open UriManager
30
31 exception No_choices of domain_item
32 exception NoWellTypedInterpretation
33
34   (** raised when an environment is not enough informative to decide *)
35 exception Try_again
36
37 let debug = false
38 let debug_print = if debug then prerr_endline else ignore
39
40 (*
41   (** print benchmark information *)
42 let benchmark = true
43 let max_refinements = ref 0       (* benchmarking is not thread safe *)
44 let actual_refinements = ref 0
45 let domain_size = ref 0
46 let choices_avg = ref 0.
47 *)
48
49 let descr_of_domain_item = function
50  | Id s -> s
51  | Symbol (s, _) -> s
52  | Num i -> string_of_int i
53
54 type test_result =
55   | Ok of Cic.term * Cic.metasenv
56   | Ko
57   | Uncertain
58
59 let refine metasenv context term ugraph =
60 (*   if benchmark then incr actual_refinements; *)
61   let metasenv, term = 
62     CicMkImplicit.expand_implicits metasenv [] context term in
63     debug_print (sprintf "TEST_INTERPRETATION: %s" (CicPp.ppterm term));
64     try
65       let term', _, metasenv',ugraph1 = 
66         CicRefine.type_of_aux' metasenv context term ugraph in
67         (Ok (term', metasenv')),ugraph1
68     with
69       | CicRefine.Uncertain s ->
70           debug_print ("UNCERTAIN!!! [" ^ s ^ "] " ^ CicPp.ppterm term) ;
71           Uncertain,ugraph
72       | CicRefine.RefineFailure msg ->
73           debug_print (sprintf "PRUNED!!!\nterm%s\nmessage:%s"
74             (CicPp.ppterm term) msg);
75           Ko,ugraph
76
77 let resolve (env: environment) (item: domain_item) ?(num = "") ?(args = []) () =
78   try
79     snd (Environment.find item env) env num args
80   with Not_found -> 
81     failwith ("Domain item not found: " ^ 
82       (DisambiguateTypes.string_of_domain_item item))
83
84   (* TODO move it to Cic *)
85 let find_in_environment name context =
86   let rec aux acc = function
87     | [] -> raise Not_found
88     | Cic.Name hd :: tl when hd = name -> acc
89     | _ :: tl ->  aux (acc + 1) tl
90   in
91   aux 1 context
92
93 let interpretate ~context ~env ast =
94   let rec aux loc context = function
95     | CicAst.AttributedTerm (`Loc loc, term) ->
96         aux loc context term
97     | CicAst.AttributedTerm (_, term) -> aux loc context term
98     | CicAst.Appl (CicAst.Symbol (symb, i) :: args) ->
99         let cic_args = List.map (aux loc context) args in
100         resolve env (Symbol (symb, i)) ~args:cic_args ()
101     | CicAst.Appl terms -> Cic.Appl (List.map (aux loc context) terms)
102     | CicAst.Binder (binder_kind, (var, typ), body) ->
103         let cic_type = aux_option loc context typ in
104         let cic_body = aux loc (var :: context) body in
105         (match binder_kind with
106         | `Lambda -> Cic.Lambda (var, cic_type, cic_body)
107         | `Pi | `Forall -> Cic.Prod (var, cic_type, cic_body)
108         | `Exists ->
109             resolve env (Symbol ("exists", 0))
110               ~args:[ cic_type; Cic.Lambda (var, cic_type, cic_body) ] ())
111     | CicAst.Case (term, indty_ident, outtype, branches) ->
112         let cic_term = aux loc context term in
113         let cic_outtype = aux_option loc context outtype in
114         let do_branch ((head, args), term) =
115           let rec do_branch' context = function
116             | [] -> aux loc context term
117             | (name, typ) :: tl ->
118                 let cic_body = do_branch' (name :: context) tl in
119                 let typ =
120                   match typ with
121                   | None -> Cic.Implicit (Some `Type)
122                   | Some typ -> aux loc context typ
123                 in
124                 Cic.Lambda (name, typ, cic_body)
125           in
126           do_branch' context args
127         in
128         let (indtype_uri, indtype_no) =
129           match indty_ident with
130           | Some indty_ident ->
131               (match resolve env (Id indty_ident) () with
132               | Cic.MutInd (uri, tyno, _) -> (uri, tyno)
133               | Cic.Implicit _ -> raise Try_again
134               | _ -> raise DisambiguateChoices.Invalid_choice)
135           | None ->
136               let fst_constructor =
137                 match branches with
138                 | ((head, _), _) :: _ -> head
139                 | [] -> raise DisambiguateChoices.Invalid_choice
140               in
141               (match resolve env (Id fst_constructor) () with
142               | Cic.MutConstruct (indtype_uri, indtype_no, _, _) ->
143                   (indtype_uri, indtype_no)
144               | Cic.Implicit _ -> raise Try_again
145               | _ -> raise DisambiguateChoices.Invalid_choice)
146         in
147         Cic.MutCase (indtype_uri, indtype_no, cic_outtype, cic_term,
148           (List.map do_branch branches))
149     | CicAst.LetIn ((name, typ), def, body) ->
150         let cic_def = aux loc context def in
151         let cic_def =
152           match typ with
153           | None -> cic_def
154           | Some t -> Cic.Cast (cic_def, aux loc context t)
155         in
156         let cic_body = aux loc (name :: context) body in
157         Cic.LetIn (name, cic_def, cic_body)
158     | CicAst.LetRec (kind, defs, body) ->
159         let context' =
160           List.fold_left (fun acc ((name, _), _, _) -> name :: acc)
161             context defs
162         in
163         let cic_body = aux loc context' body in
164         let inductiveFuns =
165           List.map
166             (fun ((name, typ), body, decr_idx) ->
167               let cic_body = aux loc context' body in
168               let cic_type = aux_option loc context typ in
169               let name =
170                 match name with
171                 | Cic.Anonymous ->
172                     CicTextualParser2.fail loc
173                       "Recursive functions cannot be anonymous"
174                 | Cic.Name name -> name
175               in
176               (name, decr_idx, cic_type, cic_body))
177             defs
178         in
179         let counter = ref ~-1 in
180         let build_term funs =
181           (* this is the body of the fold_right function below. Rationale: Fix
182            * and CoFix cases differs only in an additional index in the
183            * inductiveFun list, see Cic.term *)
184           match kind with
185           | `Inductive ->
186               (fun (var, _, _, _) cic ->
187                 incr counter;
188                 Cic.LetIn (Cic.Name var, Cic.Fix (!counter, funs), cic))
189           | `CoInductive ->
190               let funs =
191                 List.map (fun (name, _, typ, body) -> (name, typ, body)) funs
192               in
193               (fun (var, _, _, _) cic ->
194                 incr counter;
195                 Cic.LetIn (Cic.Name var, Cic.CoFix (!counter, funs), cic))
196         in
197         List.fold_right (build_term inductiveFuns) inductiveFuns cic_body
198     | CicAst.Ident (name, subst)
199     | CicAst.Uri (name, subst) as ast ->
200         let is_uri = function CicAst.Uri _ -> true | _ -> false in
201         (try
202           if is_uri ast then raise Not_found;(* don't search the env for URIs *)
203           let index = find_in_environment name context in
204           if subst <> None then
205             CicTextualParser2.fail loc
206               "Explicit substitutions not allowed here";
207           Cic.Rel index
208         with Not_found ->
209           let cic =
210             if is_uri ast then  (* we have the URI, build the term out of it *)
211               try
212                 CicUtil.term_of_uri name
213               with UriManager.IllFormedUri _ ->
214                 CicTextualParser2.fail loc "Ill formed URI"
215             else
216               resolve env (Id name) ()
217           in
218           let mk_subst uris =
219             let ids_to_uris =
220               List.map (fun uri -> UriManager.name_of_uri uri, uri) uris
221             in
222             (match subst with
223             | Some subst ->
224                 List.map
225                   (fun (s, term) ->
226                     (try
227                       List.assoc s ids_to_uris, aux loc context term
228                      with Not_found ->
229                        raise DisambiguateChoices.Invalid_choice))
230                   subst
231             | None -> List.map (fun uri -> uri, Cic.Implicit None) uris)
232           in
233           (try 
234             match cic with
235             | Cic.Const (uri, []) ->
236                 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
237                 let uris = CicUtil.params_of_obj o in
238                 Cic.Const (uri, mk_subst uris)
239             | Cic.Var (uri, []) ->
240                 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
241                 let uris = CicUtil.params_of_obj o in
242                 Cic.Var (uri, mk_subst uris)
243             | Cic.MutInd (uri, i, []) ->
244                 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
245                 let uris = CicUtil.params_of_obj o in
246                 Cic.MutInd (uri, i, mk_subst uris)
247             | Cic.MutConstruct (uri, i, j, []) ->
248                 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
249                 let uris = CicUtil.params_of_obj o in
250                 Cic.MutConstruct (uri, i, j, mk_subst uris)
251             | Cic.Meta _ | Cic.Implicit _ as t ->
252 (*
253                 debug_print (sprintf
254                   "Warning: %s must be instantiated with _[%s] but we do not enforce it"
255                   (CicPp.ppterm t)
256                   (String.concat "; "
257                     (List.map
258                       (fun (s, term) -> s ^ " := " ^ CicAstPp.pp_term term)
259                       subst)));
260 *)
261                 t
262             | _ ->
263               raise DisambiguateChoices.Invalid_choice
264            with 
265              CicEnvironment.CircularDependency _ -> 
266                raise DisambiguateChoices.Invalid_choice))
267     | CicAst.Implicit -> Cic.Implicit None
268     | CicAst.UserInput -> Cic.Implicit (Some `Hole)
269 (*    | CicAst.UserInput -> assert false*)
270     | CicAst.Num (num, i) -> resolve env (Num i) ~num ()
271     | CicAst.Meta (index, subst) ->
272         let cic_subst =
273           List.map
274             (function None -> None | Some term -> Some (aux loc context term))
275             subst
276         in
277         Cic.Meta (index, cic_subst)
278     | CicAst.Sort `Prop -> Cic.Sort Cic.Prop
279     | CicAst.Sort `Set -> Cic.Sort Cic.Set
280     | CicAst.Sort `Type -> Cic.Sort (Cic.Type (CicUniv.fresh())) (* TASSI *)
281     | CicAst.Sort `CProp -> Cic.Sort Cic.CProp
282     | CicAst.Symbol (symbol, instance) ->
283         resolve env (Symbol (symbol, instance)) ()
284   and aux_option loc context = function
285     | None -> Cic.Implicit (Some `Type)
286     | Some term -> aux loc context term
287   in
288   match ast with
289   | CicAst.AttributedTerm (`Loc loc, term) -> aux loc context term
290   | term -> aux CicAst.dummy_floc context term
291
292 let domain_of_term ~context ast =
293     (* "aux" keeps domain in reverse order and doesn't care about duplicates.
294      * Domain item more in deep in the list will be processed first.
295      *)
296   let rec aux loc context = function
297     | CicAst.AttributedTerm (`Loc loc, term) -> aux loc context term
298     | CicAst.AttributedTerm (_, term) -> aux loc context term
299     | CicAst.Appl terms ->
300         List.fold_left (fun dom term -> aux loc context term @ dom) [] terms
301     | CicAst.Binder (kind, (var, typ), body) ->
302         let kind_dom =
303           match kind with
304           | `Exists -> [ Symbol ("exists", 0) ]
305           | _ -> []
306         in
307         let type_dom = aux_option loc context typ in
308         let body_dom = aux loc (var :: context) body in
309         body_dom @ type_dom @ kind_dom
310     | CicAst.Case (term, indty_ident, outtype, branches) ->
311         let term_dom = aux loc context term in
312         let outtype_dom = aux_option loc context outtype in
313         let get_first_constructor = function
314           | [] -> []
315           | ((head, _), _) :: _ -> [ Id head ]
316         in
317         let do_branch ((head, args), term) =
318           let (term_context, args_domain) =
319             List.fold_left
320               (fun (cont, dom) (name, typ) ->
321                 (name :: cont,
322                  (match typ with
323                  | None -> dom
324                  | Some typ -> aux loc cont typ @ dom)))
325               (context, []) args
326           in
327           args_domain @ aux loc term_context term
328         in
329         let branches_dom =
330           List.fold_left (fun dom branch -> do_branch branch @ dom) [] branches
331         in
332         branches_dom @ outtype_dom @ term_dom @
333         (match indty_ident with
334          | None -> get_first_constructor branches
335          | Some ident -> [ Id ident ])
336     | CicAst.LetIn ((var, typ), body, where) ->
337         let body_dom = aux loc context body in
338         let type_dom = aux_option loc context typ in
339         let where_dom = aux loc (var :: context) where in
340         where_dom @ type_dom @ body_dom
341     | CicAst.LetRec (kind, defs, where) ->
342         let context' =
343           List.fold_left (fun acc ((var, typ), _, _) -> var :: acc)
344             context defs
345         in
346         let where_dom = aux loc context' where in
347         let defs_dom =
348           List.fold_left
349             (fun dom ((_, typ), body, _) ->
350               aux loc context' body @ aux_option loc context typ)
351             [] defs
352         in
353         where_dom @ defs_dom
354     | CicAst.Ident (name, subst) ->
355         (try
356           let index = find_in_environment name context in
357           if subst <> None then
358             CicTextualParser2.fail loc
359               "Explicit substitutions not allowed here"
360           else
361             []
362         with Not_found ->
363           (match subst with
364           | None -> [Id name]
365           | Some subst ->
366               List.fold_left
367                 (fun dom (_, term) ->
368                   let dom' = aux loc context term in
369                   dom' @ dom)
370                 [Id name] subst))
371     | CicAst.Uri _ -> []
372     | CicAst.Implicit -> []
373     | CicAst.Num (num, i) -> [ Num i ]
374     | CicAst.Meta (index, local_context) ->
375         List.fold_left (fun dom term -> aux_option loc context term @ dom) []
376           local_context
377     | CicAst.Sort _ -> []
378     | CicAst.Symbol (symbol, instance) -> [ Symbol (symbol, instance) ]
379     | CicAst.UserInput -> assert false
380
381   and aux_option loc context = function
382     | None -> []
383     | Some t -> aux loc context t
384   in
385
386     (* e.g. [5;1;1;1;2;3;4;1;2] -> [2;1;4;3;5] *)
387   let rev_uniq =
388     let module SortedItem =
389       struct
390         type t = DisambiguateTypes.domain_item
391         let compare = Pervasives.compare
392       end
393     in
394     let module Set = Set.Make (SortedItem) in
395     fun l ->
396       let rev_l = List.rev l in
397       let (_, uniq_rev_l) =
398         List.fold_left
399           (fun (members, rev_l) elt ->
400             if Set.mem elt members then
401               (members, rev_l)
402             else
403               Set.add elt members, elt :: rev_l)
404           (Set.empty, []) rev_l
405       in
406       List.rev uniq_rev_l
407   in
408             
409   rev_uniq
410     (match ast with
411     | CicAst.AttributedTerm (`Loc loc, term) -> aux loc context term
412     | term -> aux CicAst.dummy_floc context term)
413
414
415   (* dom1 \ dom2 *)
416 let domain_diff dom1 dom2 =
417 (* let domain_diff = Domain.diff *)
418   let is_in_dom2 =
419     List.fold_left (fun pred elt -> (fun elt' -> elt' = elt || pred elt'))
420       (fun _ -> false) dom2
421   in
422   List.filter (fun elt -> not (is_in_dom2 elt)) dom1
423
424 module type Disambiguator =
425 sig
426   val disambiguate_term :
427     dbd:Mysql.dbd ->
428     context:Cic.context ->
429     metasenv:Cic.metasenv ->
430     ?initial_ugraph:CicUniv.universe_graph -> 
431     aliases:environment ->  (* previous interpretation status *)
432     CicAst.term ->
433     (environment *                   (* new interpretation status *)
434      Cic.metasenv *                  (* new metasenv *)
435      Cic.term*
436      CicUniv.universe_graph) list    (* disambiguated term *)
437 end
438
439 module Make (C: Callbacks) =
440   struct
441     let choices_of_id dbd id =
442       let uris = MetadataQuery.locate ~dbd id in
443       let uris =
444        match uris with
445         | [] ->
446            [UriManager.string_of_uri (C.input_or_locate_uri
447             ~title:("URI matching \"" ^ id ^ "\" unknown.") ~id ())]
448         | [uri] -> [uri]
449         | _ ->
450             C.interactive_user_uri_choice ~selection_mode:`MULTIPLE
451              ~ok:"Try selected." ~enable_button_for_non_vars:true
452              ~title:"Ambiguous input." ~id
453              ~msg: ("Ambiguous input \"" ^ id ^
454                 "\". Please, choose one or more interpretations:")
455              uris
456       in
457       List.map
458         (fun uri ->
459           (uri,
460            let term =
461              try
462                CicUtil.term_of_uri uri
463              with exn ->
464                debug_print uri;
465                debug_print (Printexc.to_string exn);
466                assert false
467             in
468            fun _ _ _ -> term))
469         uris
470
471     let disambiguate_term ~(dbd:Mysql.dbd) ~context ~metasenv
472       ?(initial_ugraph = CicUniv.empty_ugraph) ~aliases:current_env
473       term
474     =
475       debug_print "NEW DISAMBIGUATE INPUT";
476       let disambiguate_context =  (* cic context -> disambiguate context *)
477         List.map
478           (function None -> Cic.Anonymous | Some (name, _) -> name)
479           context
480       in
481       debug_print ("TERM IS: " ^ (CicAstPp.pp_term term));
482       let term_dom = domain_of_term ~context:disambiguate_context term in
483       debug_print (sprintf "DISAMBIGUATION DOMAIN: %s"
484         (string_of_domain term_dom));
485       let current_dom =
486         Environment.fold (fun item _ dom -> item :: dom) current_env []
487       in
488       let todo_dom = domain_diff term_dom current_dom in
489       (* (2) lookup function for any item (Id/Symbol/Num) *)
490       let lookup_choices =
491         let id_choices = Hashtbl.create 1023 in
492         fun item ->
493         let choices =
494           match item with
495           | Id id ->
496               (try
497                 Hashtbl.find id_choices id
498               with Not_found ->
499                 let choices = choices_of_id dbd id in
500                 Hashtbl.add id_choices id choices;
501                 choices)
502           | Symbol (symb, _) -> DisambiguateChoices.lookup_symbol_choices symb
503           | Num instance -> DisambiguateChoices.lookup_num_choices ()
504         in
505         if choices = [] then raise (No_choices item);
506         choices
507       in
508
509 (*
510       (* <benchmark> *)
511       let _ =
512         if benchmark then begin
513           let per_item_choices =
514             List.map
515               (fun dom_item ->
516                 try
517                   let len = List.length (lookup_choices dom_item) in
518                   debug_print (sprintf "BENCHMARK %s: %d"
519                     (string_of_domain_item dom_item) len);
520                   len
521                 with No_choices _ -> 0)
522               term_dom
523           in
524           max_refinements := List.fold_left ( * ) 1 per_item_choices;
525           actual_refinements := 0;
526           domain_size := List.length term_dom;
527           choices_avg :=
528             (float_of_int !max_refinements) ** (1. /. float_of_int !domain_size)
529         end
530       in
531       (* </benchmark> *)
532 *)
533
534       (* (3) test an interpretation filling with meta uninterpreted identifiers
535        *)
536       let test_env current_env todo_dom ugraph = 
537         let filled_env =
538           List.fold_left
539             (fun env item ->
540                Environment.add item
541                ("Implicit",
542                  (match item with
543                     | Id _ | Num _ -> (fun _ _ _ -> Cic.Implicit (Some `Closed))
544                     | Symbol _ -> (fun _ _ _ -> Cic.Implicit None))) env)
545             current_env todo_dom 
546         in
547         try
548           let cic_term =
549             interpretate ~context:disambiguate_context ~env:filled_env term
550           in
551           let k,ugraph1 = refine metasenv context cic_term ugraph in
552             (k , ugraph1 )
553         with
554         | Try_again -> Uncertain,ugraph
555         | DisambiguateChoices.Invalid_choice -> Ko,ugraph
556       in
557       (* (4) build all possible interpretations *)
558       let rec aux current_env todo_dom base_univ =
559         match todo_dom with
560         | [] ->
561             (match test_env current_env [] base_univ with
562             | Ok (term, metasenv),new_univ -> 
563                                [ current_env, metasenv, term, new_univ ]
564             | Ko,_ | Uncertain,_ -> [])
565         | item :: remaining_dom ->
566             debug_print (sprintf "CHOOSED ITEM: %s"
567              (string_of_domain_item item));
568             let choices = lookup_choices item in
569             let rec filter univ = function 
570               | [] -> []
571               | codomain_item :: tl ->
572                   debug_print (sprintf "%s CHOSEN" (fst codomain_item)) ;
573                   let new_env =
574                     Environment.add item codomain_item current_env
575                   in
576                   (match test_env new_env remaining_dom univ with
577                   | Ok (term, metasenv),new_univ ->
578                       (match remaining_dom with
579                       | [] -> [ new_env, metasenv, term, new_univ ]
580                       | _ -> aux new_env remaining_dom new_univ )@ 
581                         filter univ tl
582                   | Uncertain,new_univ ->
583                       (match remaining_dom with
584                       | [] -> []
585                       | _ -> aux new_env remaining_dom new_univ )@ 
586                         filter univ tl
587                   | Ko,_ -> filter univ tl)
588             in
589             filter base_univ choices 
590       in
591       let base_univ = initial_ugraph in
592       try
593         let res =
594          match aux current_env todo_dom base_univ with
595          | [] -> raise NoWellTypedInterpretation
596          | [ e,me,t,u ] as l ->
597              debug_print "UNA SOLA SCELTA";
598              [ e,me,t,u]
599          | l ->
600              debug_print (sprintf "PIU' SCELTE (%d)" (List.length l));
601              let choices =
602                List.map
603                  (fun (env, _, _, _) ->
604                    List.map
605                      (fun domain_item ->
606                        let description =
607                          fst (Environment.find domain_item env)
608                        in
609                        (descr_of_domain_item domain_item, description))
610                      term_dom)
611                  l
612              in
613              let choosed = C.interactive_interpretation_choice choices in
614              List.map (List.nth l) choosed
615         in
616 (*
617         (if benchmark then
618           let res_size = List.length res in
619           debug_print (sprintf
620             ("BENCHMARK: %d/%d refinements performed, domain size %d, interps %d, k %.2f\n" ^^
621             "BENCHMARK:   estimated %.2f")
622             !actual_refinements !max_refinements !domain_size res_size
623             !choices_avg
624             (float_of_int (!domain_size - 1) *. !choices_avg *. (float_of_int res_size) +. !choices_avg)));
625 *)
626         res
627      with
628       CicEnvironment.CircularDependency s -> 
629         failwith "Disambiguate: circular dependency"
630   end
631
632 module Trivial =
633 struct
634   exception Ambiguous_term of string
635   exception Exit
636   module Callbacks =
637   struct
638     let interactive_user_uri_choice ~selection_mode ?ok
639           ?(enable_button_for_non_vars = true) ~title ~msg ~id uris =
640               raise Exit
641     let interactive_interpretation_choice interp = raise Exit
642     let input_or_locate_uri ~(title:string) ?id = raise Exit
643   end
644   module Disambiguator = Make (Callbacks)
645   let disambiguate_string ~dbd ?(context=[]) ?(metasenv=[]) ?initial_ugraph
646         ?(aliases=DisambiguateTypes.Environment.empty) term =
647     let ast = CicTextualParser2.parse_term (Stream.of_string term) in
648     try
649       Disambiguator.disambiguate_term ~dbd ~context ~metasenv ast
650         ?initial_ugraph ~aliases
651     with Exit -> raise (Ambiguous_term term)
652 end
653