1 (* Copyright (C) 2004, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
28 open DisambiguateTypes
31 exception No_choices of domain_item
32 exception NoWellTypedInterpretation
33 exception PathNotWellFormed
35 (** raised when an environment is not enough informative to decide *)
38 type aliases = bool * DisambiguateTypes.environment
41 let debug_print = if debug then prerr_endline else ignore
44 (** print benchmark information *)
46 let max_refinements = ref 0 (* benchmarking is not thread safe *)
47 let actual_refinements = ref 0
48 let domain_size = ref 0
49 let choices_avg = ref 0.
52 let floc_of_loc (loc_begin, loc_end) =
54 { Lexing.pos_fname = ""; Lexing.pos_lnum = -1; Lexing.pos_bol = -1;
55 Lexing.pos_cnum = loc_begin }
57 let floc_end = { floc_begin with Lexing.pos_cnum = loc_end } in
58 (floc_begin, floc_end)
60 let dummy_floc = floc_of_loc (-1, -1)
62 let descr_of_domain_item = function
65 | Num i -> string_of_int i
68 | Ok of 'a * Cic.metasenv
72 let refine_term metasenv context uri term ugraph =
73 (* if benchmark then incr actual_refinements; *)
76 CicMkImplicit.expand_implicits metasenv [] context term in
77 debug_print (sprintf "TEST_INTERPRETATION: %s" (CicPp.ppterm term));
79 let term', _, metasenv',ugraph1 =
80 CicRefine.type_of_aux' metasenv context term ugraph in
81 (Ok (term', metasenv')),ugraph1
83 | CicRefine.Uncertain s ->
84 debug_print ("UNCERTAIN!!! [" ^ s ^ "] " ^ CicPp.ppterm term) ;
86 | CicRefine.RefineFailure msg ->
87 debug_print (sprintf "PRUNED!!!\nterm%s\nmessage:%s"
88 (CicPp.ppterm term) msg);
91 let refine_obj metasenv context uri obj ugraph =
92 assert (context = []);
93 let metasenv, obj = CicMkImplicit.expand_implicits_in_obj metasenv [] obj in
94 debug_print (sprintf "TEST_INTERPRETATION: %s" (CicPp.ppobj obj));
96 let obj', metasenv,ugraph = CicRefine.typecheck metasenv uri obj in
97 (Ok (obj', metasenv)),ugraph
99 | CicRefine.Uncertain s ->
100 debug_print ("UNCERTAIN!!! [" ^ s ^ "] " ^ CicPp.ppobj obj) ;
102 | CicRefine.RefineFailure msg ->
103 debug_print (sprintf "PRUNED!!!\nterm%s\nmessage:%s"
104 (CicPp.ppobj obj) msg);
107 let resolve (env: codomain_item Environment.t) (item: domain_item) ?(num = "") ?(args = []) () =
109 snd (Environment.find item env) env num args
111 failwith ("Domain item not found: " ^
112 (DisambiguateTypes.string_of_domain_item item))
114 (* TODO move it to Cic *)
115 let find_in_environment name (context: Cic.name list) =
116 let rec aux acc = function
117 | [] -> raise Not_found
118 | Cic.Name hd :: tl when hd = name -> acc
119 | _ :: tl -> aux (acc + 1) tl
123 let interpretate_term ~(context: Cic.name list) ~env ~uri ~is_path ast =
125 let rec aux loc (context: Cic.name list) = function
126 | CicNotationPt.AttributedTerm (`Loc loc, term) ->
128 | CicNotationPt.AttributedTerm (_, term) -> aux loc context term
129 | CicNotationPt.Appl (CicNotationPt.Symbol (symb, i) :: args) ->
130 let cic_args = List.map (aux loc context) args in
131 resolve env (Symbol (symb, i)) ~args:cic_args ()
132 | CicNotationPt.Appl terms -> Cic.Appl (List.map (aux loc context) terms)
133 | CicNotationPt.Binder (binder_kind, (var, typ), body) ->
134 let cic_type = aux_option loc context (Some `Type) typ in
135 let cic_name = CicNotationUtil.cic_name_of_name var in
136 let cic_body = aux loc (cic_name :: context) body in
137 (match binder_kind with
138 | `Lambda -> Cic.Lambda (cic_name, cic_type, cic_body)
140 | `Forall -> Cic.Prod (cic_name, cic_type, cic_body)
142 resolve env (Symbol ("exists", 0))
143 ~args:[ cic_type; Cic.Lambda (cic_name, cic_type, cic_body) ] ())
144 | CicNotationPt.Case (term, indty_ident, outtype, branches) ->
145 let cic_term = aux loc context term in
146 let cic_outtype = aux_option loc context None outtype in
147 let do_branch ((head, args), term) =
148 let rec do_branch' context = function
149 | [] -> aux loc context term
150 | (name, typ) :: tl ->
151 let cic_name = CicNotationUtil.cic_name_of_name name in
152 let cic_body = do_branch' (cic_name :: context) tl in
155 | None -> Cic.Implicit (Some `Type)
156 | Some typ -> aux loc context typ
158 Cic.Lambda (cic_name, typ, cic_body)
160 do_branch' context args
162 let (indtype_uri, indtype_no) =
163 match indty_ident with
164 | Some indty_ident ->
165 (match resolve env (Id indty_ident) () with
166 | Cic.MutInd (uri, tyno, _) -> (uri, tyno)
167 | Cic.Implicit _ -> raise Try_again
168 | _ -> raise Invalid_choice)
170 let fst_constructor =
172 | ((head, _), _) :: _ -> head
173 | [] -> raise Invalid_choice
175 (match resolve env (Id fst_constructor) () with
176 | Cic.MutConstruct (indtype_uri, indtype_no, _, _) ->
177 (indtype_uri, indtype_no)
178 | Cic.Implicit _ -> raise Try_again
179 | _ -> raise Invalid_choice)
181 Cic.MutCase (indtype_uri, indtype_no, cic_outtype, cic_term,
182 (List.map do_branch branches))
183 | CicNotationPt.Cast (t1, t2) ->
184 let cic_t1 = aux loc context t1 in
185 let cic_t2 = aux loc context t2 in
186 Cic.Cast (cic_t1, cic_t2)
187 | CicNotationPt.LetIn ((name, typ), def, body) ->
188 let cic_def = aux loc context def in
189 let cic_name = CicNotationUtil.cic_name_of_name name in
193 | Some t -> Cic.Cast (cic_def, aux loc context t)
195 let cic_body = aux loc (cic_name :: context) body in
196 Cic.LetIn (cic_name, cic_def, cic_body)
197 | CicNotationPt.LetRec (kind, defs, body) ->
200 (fun acc ((name, _), _, _) ->
201 CicNotationUtil.cic_name_of_name name :: acc)
204 let cic_body = aux loc context' body in
207 (fun ((name, typ), body, decr_idx) ->
208 let cic_body = aux loc context' body in
209 let cic_type = aux_option loc context (Some `Type) typ in
211 match CicNotationUtil.cic_name_of_name name with
213 CicNotationPt.fail loc
214 "Recursive functions cannot be anonymous"
215 | Cic.Name name -> name
217 (name, decr_idx, cic_type, cic_body))
220 let counter = ref ~-1 in
221 let build_term funs =
222 (* this is the body of the fold_right function below. Rationale: Fix
223 * and CoFix cases differs only in an additional index in the
224 * inductiveFun list, see Cic.term *)
227 (fun (var, _, _, _) cic ->
229 Cic.LetIn (Cic.Name var, Cic.Fix (!counter, funs), cic))
232 List.map (fun (name, _, typ, body) -> (name, typ, body)) funs
234 (fun (var, _, _, _) cic ->
236 Cic.LetIn (Cic.Name var, Cic.CoFix (!counter, funs), cic))
238 List.fold_right (build_term inductiveFuns) inductiveFuns cic_body
239 | CicNotationPt.Ident _
240 | CicNotationPt.Uri _ when is_path -> raise PathNotWellFormed
241 | CicNotationPt.Ident (name, subst)
242 | CicNotationPt.Uri (name, subst) as ast ->
243 let is_uri = function CicNotationPt.Uri _ -> true | _ -> false in
245 if is_uri ast then raise Not_found;(* don't search the env for URIs *)
246 let index = find_in_environment name context in
247 if subst <> None then
248 CicNotationPt.fail loc "Explicit substitutions not allowed here";
252 if is_uri ast then (* we have the URI, build the term out of it *)
254 CicUtil.term_of_uri (UriManager.uri_of_string name)
255 with UriManager.IllFormedUri _ ->
256 CicNotationPt.fail loc "Ill formed URI"
258 resolve env (Id name) ()
262 List.map (fun uri -> UriManager.name_of_uri uri, uri) uris
269 List.assoc s ids_to_uris, aux loc context term
271 raise Invalid_choice))
273 | None -> List.map (fun uri -> uri, Cic.Implicit None) uris)
277 | Cic.Const (uri, []) ->
278 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
279 let uris = CicUtil.params_of_obj o in
280 Cic.Const (uri, mk_subst uris)
281 | Cic.Var (uri, []) ->
282 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
283 let uris = CicUtil.params_of_obj o in
284 Cic.Var (uri, mk_subst uris)
285 | Cic.MutInd (uri, i, []) ->
287 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
288 let uris = CicUtil.params_of_obj o in
289 Cic.MutInd (uri, i, mk_subst uris)
291 CicEnvironment.Object_not_found _ ->
292 (* if we are here it is probably the case that during the
293 definition of a mutual inductive type we have met an
294 occurrence of the type in one of its constructors.
295 However, the inductive type is not yet in the environment
297 (*here the explicit_named_substituion is assumed to be of length 0 *)
298 Cic.MutInd (uri,i,[]))
299 | Cic.MutConstruct (uri, i, j, []) ->
300 let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
301 let uris = CicUtil.params_of_obj o in
302 Cic.MutConstruct (uri, i, j, mk_subst uris)
303 | Cic.Meta _ | Cic.Implicit _ as t ->
306 "Warning: %s must be instantiated with _[%s] but we do not enforce it"
310 (fun (s, term) -> s ^ " := " ^ CicNotationPtPp.pp_term term)
317 CicEnvironment.CircularDependency _ ->
318 raise Invalid_choice))
319 | CicNotationPt.Implicit -> Cic.Implicit None
320 | CicNotationPt.UserInput -> Cic.Implicit (Some `Hole)
321 | CicNotationPt.Num (num, i) -> resolve env (Num i) ~num ()
322 | CicNotationPt.Meta (index, subst) ->
325 (function None -> None | Some term -> Some (aux loc context term))
328 Cic.Meta (index, cic_subst)
329 | CicNotationPt.Sort `Prop -> Cic.Sort Cic.Prop
330 | CicNotationPt.Sort `Set -> Cic.Sort Cic.Set
331 | CicNotationPt.Sort `Type -> Cic.Sort (Cic.Type (CicUniv.fresh())) (* TASSI *)
332 | CicNotationPt.Sort `CProp -> Cic.Sort Cic.CProp
333 | CicNotationPt.Symbol (symbol, instance) ->
334 resolve env (Symbol (symbol, instance)) ()
335 | _ -> assert false (* god bless Bologna *)
336 and aux_option loc (context: Cic.name list) annotation = function
337 | None -> Cic.Implicit annotation
338 | Some term -> aux loc context term
341 | CicNotationPt.AttributedTerm (`Loc loc, term) -> aux loc context term
342 | term -> aux dummy_floc context term
344 let interpretate_path ~context path =
345 interpretate_term ~context ~env:Environment.empty ~uri:None ~is_path:true path
347 let interpretate_obj ~context ~env ~uri ~is_path obj =
348 assert (context = []);
349 assert (is_path = false);
351 | GrafiteAst.Inductive (params,tyl) ->
352 let uri = match uri with Some uri -> uri | None -> assert false in
356 (fun (context,res) (name,t) ->
357 Cic.Name name :: context,
358 (name, interpretate_term context env None false t)::res
361 context,List.rev res in
364 (fun (name,ty) t -> Cic.Prod (Cic.Name name,ty,t)) params in
368 (*here the explicit_named_substituion is assumed to be of length 0 *)
369 (fun (i,res) (name,_,_,_) ->
370 i + 1,(name,name,Cic.MutInd (uri,i,[]))::res
372 let con_env = DisambiguateTypes.singleton_env_of_list name_to_uris env in
376 (fun (name,_,_,_) (i,t) ->
377 (*here the explicit_named_substituion is assumed to be of length 0 *)
378 let t' = Cic.MutInd (uri,i,[]) in
379 let t = CicSubstitution.subst t' t in
381 ) tyl (List.length tyl - 1,t)) in
384 (fun (name,b,ty,cl) ->
385 let ty' = add_params (interpretate_term context env None false ty) in
390 add_params (interpretate_term context con_env None false ty)
398 Cic.InductiveDefinition (tyl,[],List.length params,[])
399 | GrafiteAst.Record (params,name,ty,fields) ->
400 let uri = match uri with Some uri -> uri | None -> assert false in
404 (fun (context,res) (name,t) ->
405 (Cic.Name name :: context),
406 (name, interpretate_term context env None false t)::res
409 context,List.rev res in
412 (fun (name,ty) t -> Cic.Prod (Cic.Name name,ty,t)) params in
413 let ty' = add_params (interpretate_term context env None false ty) in
417 (fun (context,res) (name,ty) ->
418 let context' = Cic.Name name :: context in
419 context',(name,interpretate_term context env None false ty)::res
420 ) (context,[]) fields) in
422 (*here the explicit_named_substituion is assumed to be of length 0 *)
423 let mutind = Cic.MutInd (uri,0,[]) in
424 if params = [] then mutind
427 (mutind::CicUtil.mk_rels (List.length params) (List.length fields)) in
430 (fun t (name,ty) -> Cic.Prod (Cic.Name name,ty,t))
432 let con' = add_params con in
433 let tyl = [name,true,ty',["mk_" ^ name,con']] in
434 let field_names = List.map fst fields in
435 Cic.InductiveDefinition
436 (tyl,[],List.length params,[`Class (`Record field_names)])
437 | GrafiteAst.Theorem (flavour, name, ty, bo) ->
438 let attrs = [`Flavour flavour] in
439 let ty' = interpretate_term [] env None false ty in
442 Cic.CurrentProof (name,[],Cic.Implicit None,ty',[],attrs)
444 let bo' = Some (interpretate_term [] env None false bo) in
445 Cic.Constant (name,bo',ty',[],attrs))
448 (* e.g. [5;1;1;1;2;3;4;1;2] -> [2;1;4;3;5] *)
450 let module SortedItem =
452 type t = DisambiguateTypes.domain_item
453 let compare = Pervasives.compare
456 let module Set = Set.Make (SortedItem) in
458 let rev_l = List.rev l in
459 let (_, uniq_rev_l) =
461 (fun (members, rev_l) elt ->
462 if Set.mem elt members then
465 Set.add elt members, elt :: rev_l)
466 (Set.empty, []) rev_l
470 (* "aux" keeps domain in reverse order and doesn't care about duplicates.
471 * Domain item more in deep in the list will be processed first.
473 let rec domain_rev_of_term ?(loc = dummy_floc) context = function
474 | CicNotationPt.AttributedTerm (`Loc loc, term) ->
475 domain_rev_of_term ~loc context term
476 | CicNotationPt.AttributedTerm (_, term) ->
477 domain_rev_of_term ~loc context term
478 | CicNotationPt.Appl terms ->
480 (fun dom term -> domain_rev_of_term ~loc context term @ dom) [] terms
481 | CicNotationPt.Binder (kind, (var, typ), body) ->
484 | `Exists -> [ Symbol ("exists", 0) ]
487 let type_dom = domain_rev_of_term_option loc context typ in
489 domain_rev_of_term ~loc
490 (CicNotationUtil.cic_name_of_name var :: context) body
492 body_dom @ type_dom @ kind_dom
493 | CicNotationPt.Case (term, indty_ident, outtype, branches) ->
494 let term_dom = domain_rev_of_term ~loc context term in
495 let outtype_dom = domain_rev_of_term_option loc context outtype in
496 let get_first_constructor = function
498 | ((head, _), _) :: _ -> [ Id head ]
500 let do_branch ((head, args), term) =
501 let (term_context, args_domain) =
503 (fun (cont, dom) (name, typ) ->
504 (CicNotationUtil.cic_name_of_name name :: cont,
507 | Some typ -> domain_rev_of_term ~loc cont typ @ dom)))
510 args_domain @ domain_rev_of_term ~loc term_context term
513 List.fold_left (fun dom branch -> do_branch branch @ dom) [] branches
515 branches_dom @ outtype_dom @ term_dom @
516 (match indty_ident with
517 | None -> get_first_constructor branches
518 | Some ident -> [ Id ident ])
519 | CicNotationPt.Cast (term, ty) ->
520 let term_dom = domain_rev_of_term ~loc context term in
521 let ty_dom = domain_rev_of_term ~loc context ty in
523 | CicNotationPt.LetIn ((var, typ), body, where) ->
524 let body_dom = domain_rev_of_term ~loc context body in
525 let type_dom = domain_rev_of_term_option loc context typ in
527 domain_rev_of_term ~loc
528 (CicNotationUtil.cic_name_of_name var :: context) where
530 where_dom @ type_dom @ body_dom
531 | CicNotationPt.LetRec (kind, defs, where) ->
534 (fun acc ((var, typ), _, _) ->
535 CicNotationUtil.cic_name_of_name var :: acc)
538 let where_dom = domain_rev_of_term ~loc context' where in
541 (fun dom ((_, typ), body, _) ->
542 domain_rev_of_term ~loc context' body @
543 domain_rev_of_term_option loc context typ)
547 | CicNotationPt.Ident (name, subst) ->
549 let index = find_in_environment name context in
550 if subst <> None then
551 CicNotationPt.fail loc "Explicit substitutions not allowed here"
559 (fun dom (_, term) ->
560 let dom' = domain_rev_of_term ~loc context term in
563 | CicNotationPt.Uri _ -> []
564 | CicNotationPt.Implicit -> []
565 | CicNotationPt.Num (num, i) -> [ Num i ]
566 | CicNotationPt.Meta (index, local_context) ->
568 (fun dom term -> domain_rev_of_term_option loc context term @ dom) []
570 | CicNotationPt.Sort _ -> []
571 | CicNotationPt.Symbol (symbol, instance) -> [ Symbol (symbol, instance) ]
572 | CicNotationPt.UserInput
573 | CicNotationPt.Literal _
574 | CicNotationPt.Layout _
575 | CicNotationPt.Magic _
576 | CicNotationPt.Variable _ -> assert false
578 and domain_rev_of_term_option loc context = function
580 | Some t -> domain_rev_of_term ~loc context t
582 let domain_of_term ~context ast = rev_uniq (domain_rev_of_term context ast)
584 let domain_of_obj ~context ast =
585 assert (context = []);
588 | GrafiteAst.Theorem (_,_,ty,bo) ->
591 | Some bo -> domain_rev_of_term [] bo) @
593 | GrafiteAst.Inductive (params,tyl) ->
600 (fun (_,ty) -> domain_rev_of_term [] ty) cl) @
601 domain_rev_of_term [] ty) tyl) in
605 domain_rev_of_term [] ty @ dom
610 not ( List.exists (fun (name',_) -> name = Id name') params
611 || List.exists (fun (name',_,_,_) -> name = Id name') tyl)
613 | GrafiteAst.Record (params,_,ty,fields) ->
616 (List.rev_map (fun (_,ty) -> domain_rev_of_term [] ty) fields) in
620 not ( List.exists (fun (name',_) -> name = Id name') params
621 || List.exists (fun (name',_) -> name = Id name') fields)
626 domain_rev_of_term [] ty @ dom
627 ) (dom @ domain_rev_of_term [] ty) params
632 let domain_diff dom1 dom2 =
633 (* let domain_diff = Domain.diff *)
635 List.fold_left (fun pred elt -> (fun elt' -> elt' = elt || pred elt'))
636 (fun _ -> false) dom2
638 List.filter (fun elt -> not (is_in_dom2 elt)) dom1
640 module type Disambiguator =
642 val disambiguate_term :
643 ?fresh_instances:bool ->
645 context:Cic.context ->
646 metasenv:Cic.metasenv ->
647 ?initial_ugraph:CicUniv.universe_graph ->
648 aliases:aliases -> (* previous interpretation status *)
649 CicNotationPt.term ->
650 (environment * (* new interpretation status *)
651 Cic.metasenv * (* new metasenv *)
653 CicUniv.universe_graph) list * (* disambiguated term *)
656 val disambiguate_obj :
657 ?fresh_instances:bool ->
659 aliases:aliases -> (* previous interpretation status *)
660 uri:UriManager.uri option -> (* required only for inductive types *)
662 (environment * (* new interpretation status *)
663 Cic.metasenv * (* new metasenv *)
665 CicUniv.universe_graph) list * (* disambiguated obj *)
669 module Make (C: Callbacks) =
671 let choices_of_id dbd id =
672 let uris = MetadataQuery.locate ~dbd id in
676 [(C.input_or_locate_uri
677 ~title:("URI matching \"" ^ id ^ "\" unknown.") ~id ())]
680 C.interactive_user_uri_choice ~selection_mode:`MULTIPLE
681 ~ok:"Try selected." ~enable_button_for_non_vars:true
682 ~title:"Ambiguous input." ~id
683 ~msg: ("Ambiguous input \"" ^ id ^
684 "\". Please, choose one or more interpretations:")
689 (UriManager.string_of_uri uri,
692 CicUtil.term_of_uri uri
694 debug_print (UriManager.string_of_uri uri);
695 debug_print (Printexc.to_string exn);
701 let disambiguate_thing ~dbd ~context ~metasenv
702 ?(initial_ugraph = CicUniv.empty_ugraph) ~aliases
703 ~uri ~pp_thing ~domain_of_thing ~interpretate_thing ~refine_thing thing
705 debug_print "DISAMBIGUATE INPUT";
706 let current_env, multi_aliases_env =
708 | false, e -> Environment.hd e, None
709 | true, e -> Environment.empty, Some e
711 let disambiguate_context = (* cic context -> disambiguate context *)
713 (function None -> Cic.Anonymous | Some (name, _) -> name)
716 debug_print ("TERM IS: " ^ (pp_thing thing));
717 let thing_dom = domain_of_thing ~context:disambiguate_context thing in
718 debug_print (sprintf "DISAMBIGUATION DOMAIN: %s"
719 (string_of_domain thing_dom));
720 debug_print (sprintf "DISAMBIGUATION ENVIRONMENT: %s"
721 (DisambiguatePp.pp_environment (snd aliases)));
723 Environment.fold (fun item _ dom -> item :: dom) current_env []
725 let todo_dom = domain_diff thing_dom current_dom in
726 (* (2) lookup function for any item (Id/Symbol/Num) *)
728 let id_choices = Hashtbl.create 1023 in
731 let lookup_in_library () =
733 | Id id -> choices_of_id dbd id
734 | Symbol (symb, _) ->
735 List.map DisambiguateChoices.mk_choice
736 (CicNotationRew.lookup_interpretations symb)
738 DisambiguateChoices.lookup_num_choices ()
740 match multi_aliases_env with
741 | None -> lookup_in_library ()
744 Environment.find item e
745 with Not_found -> lookup_in_library ())
747 if choices = [] then raise (No_choices item);
753 if benchmark then begin
754 let per_item_choices =
758 let len = List.length (lookup_choices dom_item) in
759 debug_print (sprintf "BENCHMARK %s: %d"
760 (string_of_domain_item dom_item) len);
762 with No_choices _ -> 0)
765 max_refinements := List.fold_left ( * ) 1 per_item_choices;
766 actual_refinements := 0;
767 domain_size := List.length thing_dom;
769 (float_of_int !max_refinements) ** (1. /. float_of_int !domain_size)
775 (* (3) test an interpretation filling with meta uninterpreted identifiers
777 let test_env current_env todo_dom ugraph =
784 | Id _ | Num _ -> (fun _ _ _ -> Cic.Implicit (Some `Closed))
785 | Symbol _ -> (fun _ _ _ -> Cic.Implicit None))) env)
790 interpretate_thing ~context:disambiguate_context ~env:filled_env
791 ~uri ~is_path:false thing
793 let k,ugraph1 = refine_thing metasenv context uri cic_thing ugraph in
796 | Try_again -> Uncertain, ugraph
797 | Invalid_choice -> Ko, ugraph
799 (* (4) build all possible interpretations *)
800 let rec aux current_env todo_dom base_univ =
803 (match test_env current_env [] base_univ with
804 | Ok (thing, metasenv),new_univ ->
805 [ current_env, metasenv, thing, new_univ ]
806 | Ko,_ | Uncertain,_ -> [])
807 | item :: remaining_dom ->
808 debug_print (sprintf "CHOOSED ITEM: %s"
809 (string_of_domain_item item));
810 let choices = lookup_choices item in
811 let rec filter univ = function
813 | codomain_item :: tl ->
814 debug_print (sprintf "%s CHOSEN" (fst codomain_item)) ;
816 Environment.add item codomain_item current_env
818 (match test_env new_env remaining_dom univ with
819 | Ok (thing, metasenv),new_univ ->
820 (match remaining_dom with
821 | [] -> [ new_env, metasenv, thing, new_univ ]
822 | _ -> aux new_env remaining_dom new_univ )@
824 | Uncertain,new_univ ->
825 (match remaining_dom with
827 | _ -> aux new_env remaining_dom new_univ )@
829 | Ko,_ -> filter univ tl)
831 filter base_univ choices
833 let base_univ = initial_ugraph in
836 match aux current_env todo_dom base_univ with
837 | [] -> raise NoWellTypedInterpretation
839 debug_print "SINGLE INTERPRETATION";
842 debug_print (sprintf "MANY INTERPRETATIONS (%d)" (List.length l));
845 (fun (env, _, _, _) ->
849 fst (Environment.find domain_item env)
851 (descr_of_domain_item domain_item, description))
855 let choosed = C.interactive_interpretation_choice choices in
856 (List.map (List.nth l) choosed), true
860 let res_size = List.length res in
862 ("BENCHMARK: %d/%d refinements performed, domain size %d, interps %d, k %.2f\n" ^^
863 "BENCHMARK: estimated %.2f")
864 !actual_refinements !max_refinements !domain_size res_size
866 (float_of_int (!domain_size - 1) *. !choices_avg *. (float_of_int res_size) +. !choices_avg)));
868 let choices, b = res in
870 (fun (env, metasenv, t, ugraph) ->
871 Environment.fold Environment.cons env (snd aliases),
876 CicEnvironment.CircularDependency s ->
877 failwith "Disambiguate: circular dependency"
879 let disambiguate_term ?(fresh_instances=false) ~dbd ~context ~metasenv
880 ?(initial_ugraph = CicUniv.empty_ugraph) ~aliases term
883 if fresh_instances then CicNotationUtil.freshen_term term else term
885 disambiguate_thing ~dbd ~context ~metasenv ~initial_ugraph ~aliases
886 ~uri:None ~pp_thing:CicNotationPp.pp_term
887 ~domain_of_thing:domain_of_term ~interpretate_thing:interpretate_term
888 ~refine_thing:refine_term term
890 let disambiguate_obj ?(fresh_instances=false) ~dbd ~aliases ~uri obj =
892 if fresh_instances then CicNotationUtil.freshen_obj obj else obj
894 disambiguate_thing ~dbd ~context:[] ~metasenv:[] ~aliases ~uri
895 ~pp_thing:GrafiteAstPp.pp_obj ~domain_of_thing:domain_of_obj
896 ~interpretate_thing:interpretate_obj ~refine_thing:refine_obj
902 exception Ambiguous_term of string
906 let interactive_user_uri_choice ~selection_mode ?ok
907 ?(enable_button_for_non_vars = true) ~title ~msg ~id uris =
909 let interactive_interpretation_choice interp = raise Exit
910 let input_or_locate_uri ~(title:string) ?id = raise Exit
912 module Disambiguator = Make (Callbacks)
913 let disambiguate_string ~dbd ?(context = []) ?(metasenv = []) ?initial_ugraph
914 ?(aliases = DisambiguateTypes.Environment.empty) term
916 let ast = CicNotationParser.parse_level2_ast (Stream.of_string term) in
918 fst (Disambiguator.disambiguate_term ~dbd ~context ~metasenv ast
919 ?initial_ugraph ~aliases:(false, aliases))
920 with Exit -> raise (Ambiguous_term term)