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/
30 open DisambiguateTypes
33 module Ast = CicNotationPt
35 (* the integer is an offset to be added to each location *)
36 exception NoWellTypedInterpretation of
38 ((Stdpp.location list * string * string) list *
39 (DisambiguateTypes.domain_item * string) list *
40 (Stdpp.location * string) Lazy.t * bool) list
41 exception PathNotWellFormed
43 (** raised when an environment is not enough informative to decide *)
44 exception Try_again of string Lazy.t
46 type 'term aliases = bool * 'term DisambiguateTypes.environment
47 type 'a disambiguator_input = string * int * 'a
49 type domain = domain_tree list
50 and domain_tree = Node of Stdpp.location list * domain_item * domain
52 let rec string_of_domain =
55 | Node (_,domain_item,l)::tl ->
56 DisambiguateTypes.string_of_domain_item domain_item ^
57 " [ " ^ string_of_domain l ^ " ] " ^ string_of_domain tl
59 let rec filter_map_domain f =
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
67 let rec map_domain f =
70 | Node (locs,domain_item,l)::tl ->
71 f locs domain_item :: map_domain f l @ map_domain f tl
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
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
90 let debug_print s = if debug then prerr_endline (Lazy.force s) else ()
93 (** print benchmark information *)
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.
101 let descr_of_domain_item = function
104 | Num i -> string_of_int i
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
111 let resolve (env: 'term codomain_item Environment.t) (item: domain_item) ?(num = "") ?(args = []) () =
113 snd (Environment.find item env) env num args
115 failwith ("Domain item not found: " ^
116 (DisambiguateTypes.string_of_domain_item item))
118 (* TODO move it to Cic *)
119 let find_in_context name context =
120 let rec aux acc = function
121 | [] -> raise Not_found
122 | Cic.Name hd :: tl when hd = name -> acc
123 | _ :: tl -> aux (acc + 1) tl
127 let rec domain_of_term ?(loc = HExtlib.dummy_floc) ~context = function
128 | Ast.AttributedTerm (`Loc loc, term) ->
129 domain_of_term ~loc ~context term
130 | Ast.AttributedTerm (_, term) ->
131 domain_of_term ~loc ~context term
132 | Ast.Symbol (symbol, instance) ->
133 [ Node ([loc], Symbol (symbol, instance), []) ]
134 (* to be kept in sync with Ast.Appl (Ast.Symbol ...) *)
135 | Ast.Appl (Ast.Symbol (symbol, instance) as hd :: args)
136 | Ast.Appl (Ast.AttributedTerm (_,Ast.Symbol (symbol, instance)) as hd :: args)
140 (fun term acc -> domain_of_term ~loc ~context term @ acc)
144 Ast.AttributedTerm (`Loc loc,_) -> loc
147 [ Node ([loc], Symbol (symbol, instance), args_dom) ]
148 | Ast.Appl (Ast.Ident (name, subst) as hd :: args)
149 | Ast.Appl (Ast.AttributedTerm (_,Ast.Ident (name, subst)) as hd :: args) ->
152 (fun term acc -> domain_of_term ~loc ~context term @ acc)
156 Ast.AttributedTerm (`Loc loc,_) -> loc
160 (* the next line can raise Not_found *)
161 ignore(find_in_context name context);
162 if subst <> None then
163 Ast.fail loc "Explicit substitutions not allowed here"
168 | None -> [ Node ([loc], Id name, args_dom) ]
172 (fun dom (_, term) ->
173 let dom' = domain_of_term ~loc ~context term in
176 [ Node ([loc], Id name, terms @ args_dom) ]))
179 (fun term acc -> domain_of_term ~loc ~context term @ acc)
181 | Ast.Binder (kind, (var, typ), body) ->
182 let type_dom = domain_of_term_option ~loc ~context typ in
185 ~context:(CicNotationUtil.cic_name_of_name var :: context) body in
187 | `Exists -> [ Node ([loc], Symbol ("exists", 0), (type_dom @ body_dom)) ]
188 | _ -> type_dom @ body_dom)
189 | Ast.Case (term, indty_ident, outtype, branches) ->
190 let term_dom = domain_of_term ~loc ~context term in
191 let outtype_dom = domain_of_term_option ~loc ~context outtype in
192 let rec get_first_constructor = function
194 | (Ast.Pattern (head, _, _), _) :: _ -> [ Node ([loc], Id head, []) ]
195 | _ :: tl -> get_first_constructor tl in
198 Ast.Pattern (head, _, args), term ->
199 let (term_context, args_domain) =
201 (fun (cont, dom) (name, typ) ->
202 (CicNotationUtil.cic_name_of_name name :: cont,
205 | Some typ -> dom @ domain_of_term ~loc ~context:cont typ)))
208 domain_of_term ~loc ~context:term_context term @ args_domain
209 | Ast.Wildcard, term ->
210 domain_of_term ~loc ~context term
213 List.fold_left (fun dom branch -> dom @ do_branch branch) [] branches in
214 (match indty_ident with
215 | None -> get_first_constructor branches
216 | Some (ident, _) -> [ Node ([loc], Id ident, []) ])
217 @ term_dom @ outtype_dom @ branches_dom
218 | Ast.Cast (term, ty) ->
219 let term_dom = domain_of_term ~loc ~context term in
220 let ty_dom = domain_of_term ~loc ~context ty in
222 | Ast.LetIn ((var, typ), body, where) ->
223 let body_dom = domain_of_term ~loc ~context body in
224 let type_dom = domain_of_term_option ~loc ~context typ in
227 ~context:(CicNotationUtil.cic_name_of_name var :: context) where in
228 body_dom @ type_dom @ where_dom
229 | Ast.LetRec (kind, defs, where) ->
230 let add_defs context =
232 (fun acc (_, (var, _), _, _) ->
233 CicNotationUtil.cic_name_of_name var :: acc
235 let where_dom = domain_of_term ~loc ~context:(add_defs context) where in
238 (fun dom (params, (_, typ), body, _) ->
242 (fun acc (var,_) -> CicNotationUtil.cic_name_of_name var :: acc)
248 (fun (context,res) (var,ty) ->
249 CicNotationUtil.cic_name_of_name var :: context,
250 domain_of_term_option ~loc ~context ty @ res)
251 (add_defs context,[]) params))
253 @ domain_of_term_option ~loc ~context:context' typ
254 @ domain_of_term ~loc ~context:context' body
258 | Ast.Ident (name, subst) ->
260 (* the next line can raise Not_found *)
261 ignore(find_in_context name context);
262 if subst <> None then
263 Ast.fail loc "Explicit substitutions not allowed here"
268 | None -> [ Node ([loc], Id name, []) ]
272 (fun dom (_, term) ->
273 let dom' = domain_of_term ~loc ~context term in
276 [ Node ([loc], Id name, terms) ]))
279 | Ast.Num (num, i) -> [ Node ([loc], Num i, []) ]
280 | Ast.Meta (index, local_context) ->
282 (fun dom term -> dom @ domain_of_term_option ~loc ~context term)
289 | Ast.Variable _ -> assert false
291 and domain_of_term_option ~loc ~context = function
293 | Some t -> domain_of_term ~loc ~context t
295 let domain_of_term ~context term =
296 uniq_domain (domain_of_term ~context term)
298 let domain_of_obj ~context ast =
299 let context = List.map (function None -> Cic.Anonymous | Some (n,_) -> n) context in
300 assert (context = []);
302 | Ast.Theorem (_,_,ty,bo) ->
306 | Some bo -> domain_of_term [] bo)
307 | Ast.Inductive (params,tyl) ->
310 (fun (context, dom) (var, ty) ->
311 let context' = CicNotationUtil.cic_name_of_name var :: context in
313 None -> context', dom
314 | Some ty -> context', dom @ domain_of_term context ty
316 let context_w_types =
318 (fun (var, _, _, _) -> Cic.Name var) tyl
324 domain_of_term context ty
327 (fun (_,ty) -> domain_of_term context_w_types ty) cl))
329 | CicNotationPt.Record (params,var,ty,fields) ->
332 (fun (context, dom) (var, ty) ->
333 let context' = CicNotationUtil.cic_name_of_name var :: context in
335 None -> context', dom
336 | Some ty -> context', dom @ domain_of_term context ty
338 let context_w_types = Cic.Name var :: context in
340 @ domain_of_term context ty
343 (fun (context,res) (name,ty,_,_) ->
344 Cic.Name name::context, res @ domain_of_term context ty
345 ) (context_w_types,[]) fields)
347 let domain_of_obj ~context obj =
348 uniq_domain (domain_of_obj ~context obj)
350 let domain_of_ast_term = domain_of_term;;
352 let domain_of_term ~context term =
354 List.map (function None -> Cic.Anonymous | Some (n,_) -> n) context
356 domain_of_term ~context term
358 let domain_diff dom1 dom2 =
359 (* let domain_diff = Domain.diff *)
363 | Symbol (symb, 0) ->
365 Symbol (symb',_) when symb = symb' -> true
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)
382 module type Disambiguator =
384 val disambiguate_thing:
386 metasenv:'metasenv ->
388 mk_implicit:(bool -> 'refined_term) ->
389 initial_ugraph:'ugraph ->
391 ('metasenv -> 'raw_thing -> 'raw_thing) *
392 (('refined_thing,'metasenv,'subst,'ugraph) test_result ->
393 ('refined_thing,'metasenv,'subst,'ugraph) test_result) ->
394 aliases:'refined_term DisambiguateTypes.codomain_item
395 DisambiguateTypes.Environment.t ->
396 universe:'refined_term DisambiguateTypes.codomain_item list
397 DisambiguateTypes.Environment.t option ->
399 DisambiguateTypes.interactive_user_uri_choice_type ->
400 DisambiguateTypes.input_or_locate_uri_type ->
401 DisambiguateTypes.Environment.key ->
402 'refined_term DisambiguateTypes.codomain_item list) ->
404 pp_thing:('ast_thing -> string) ->
405 domain_of_thing:(context:'context -> 'ast_thing -> domain) ->
408 env:'refined_term DisambiguateTypes.codomain_item
409 DisambiguateTypes.Environment.t ->
413 localization_tbl:'cichash ->
416 'metasenv -> 'subst -> 'context -> 'uri ->
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 *
422 'refined_term DisambiguateTypes.codomain_item) list *
423 'metasenv * 'subst * 'refined_thing * 'ugraph)
427 module Make (C: Callbacks) =
429 let refine_profiler = HExtlib.profile "disambiguate_thing.refine_thing"
431 let disambiguate_thing
432 ~context ~metasenv ~subst ~mk_implicit
433 ~initial_ugraph:base_univ ~hint
434 ~aliases ~universe ~lookup_in_library
435 ~uri ~pp_thing ~domain_of_thing ~interpretate_thing ~refine_thing
437 (thing_txt,thing_txt_prefix_len,thing)
439 debug_print (lazy "DISAMBIGUATE INPUT");
440 debug_print (lazy ("TERM IS: " ^ (pp_thing thing)));
441 let thing_dom = domain_of_thing ~context thing in
443 (lazy (sprintf "DISAMBIGUATION DOMAIN: %s"(string_of_domain thing_dom)));
445 debug_print (lazy (sprintf "DISAMBIGUATION ENVIRONMENT: %s"
446 (DisambiguatePp.pp_environment aliases)));
447 debug_print (lazy (sprintf "DISAMBIGUATION UNIVERSE: %s"
448 (match universe with None -> "None" | Some _ -> "Some _")));
451 Environment.fold (fun item _ dom -> item :: dom) aliases [] in
452 let todo_dom = domain_diff thing_dom current_dom in
454 (lazy (sprintf "DISAMBIGUATION DOMAIN AFTER DIFF: %s"(string_of_domain todo_dom)));
455 (* (2) lookup function for any item (Id/Symbol/Num) *)
462 C.interactive_user_uri_choice
463 C.input_or_locate_uri item
468 | Symbol (symb, _) -> Symbol (symb, 0)
471 Environment.find item e
474 C.interactive_user_uri_choice
475 C.input_or_locate_uri item)
482 if benchmark then begin
483 let per_item_choices =
487 let len = List.length (lookup_choices dom_item) in
488 debug_print (lazy (sprintf "BENCHMARK %s: %d"
489 (string_of_domain_item dom_item) len));
491 with No_choices _ -> 0)
494 max_refinements := List.fold_left ( * ) 1 per_item_choices;
495 actual_refinements := 0;
496 domain_size := List.length thing_dom;
498 (float_of_int !max_refinements) ** (1. /. float_of_int !domain_size)
504 (* (3) test an interpretation filling with meta uninterpreted identifiers
506 let test_env aliases todo_dom ugraph =
507 let rec aux env = function
509 | Node (_, item, l) :: tl ->
515 (fun _ _ _ -> mk_implicit true)
516 | Symbol _ -> (fun _ _ _ -> mk_implicit false)))
518 aux (aux env l) tl in
519 let filled_env = aux aliases todo_dom in
522 interpretate_thing ~context ~env:filled_env
523 ~uri ~is_path:false thing ~localization_tbl
525 let cic_thing = (fst hint) metasenv cic_thing in
528 refine_thing metasenv subst
529 context uri cic_thing ugraph ~localization_tbl
531 let k = (snd hint) k in
533 in refine_profiler.HExtlib.profile foo ()
535 | Try_again msg -> Uncertain (lazy (Stdpp.dummy_loc,Lazy.force msg))
536 | Invalid_choice loc_msg -> Ko loc_msg
538 (* (4) build all possible interpretations *)
539 let (@@) (l1,l2,l3) (l1',l2',l3') = l1@l1', l2@l2', l3@l3' in
540 (* aux returns triples Ok/Uncertain/Ko *)
541 (* rem_dom is the concatenation of all the remainin domains *)
542 let rec aux aliases diff lookup_in_todo_dom todo_dom rem_dom =
543 debug_print (lazy ("ZZZ: " ^ string_of_domain todo_dom));
546 assert (lookup_in_todo_dom = None);
547 (match test_env aliases rem_dom base_univ with
548 | Ok (thing, metasenv,subst,new_univ) ->
549 [ aliases, diff, metasenv, subst, thing, new_univ ], [], []
550 | Ko loc_msg -> [],[],[aliases,diff,loc_msg,true]
551 | Uncertain loc_msg ->
552 [],[aliases,diff,loc_msg],[])
553 | Node (locs,item,inner_dom) :: remaining_dom ->
554 debug_print (lazy (sprintf "CHOOSED ITEM: %s"
555 (string_of_domain_item item)));
557 match lookup_in_todo_dom with
558 None -> lookup_choices item
559 | Some choices -> choices in
565 "No choices for " ^ string_of_domain_item item)),
569 (* just one choice. We perform a one-step look-up and
570 if the next set of choices is also a singleton we
571 skip this refinement step *)
572 debug_print(lazy (sprintf "%s CHOSEN" (fst codomain_item)));
573 let new_env = Environment.add item codomain_item aliases in
574 let new_diff = (item,codomain_item)::diff in
575 let lookup_in_todo_dom,next_choice_is_single =
576 match remaining_dom with
579 let choices = lookup_choices he in
580 Some choices,List.length choices = 1
582 if next_choice_is_single then
583 aux new_env new_diff lookup_in_todo_dom remaining_dom
586 (match test_env new_env remaining_dom base_univ with
587 | Ok (thing, metasenv),new_univ ->
588 (match remaining_dom with
590 [ new_env, new_diff, metasenv, thing, new_univ ], []
592 aux new_env new_diff lookup_in_todo_dom
593 remaining_dom new_univ)
594 | Uncertain (loc,msg),new_univ ->
595 (match remaining_dom with
596 | [] -> [], [new_env,new_diff,loc,msg,true]
598 aux new_env new_diff lookup_in_todo_dom
599 remaining_dom new_univ)
600 | Ko (loc,msg),_ -> [], [new_env,new_diff,loc,msg,true])
603 let mark_not_significant failures =
605 (fun (env, diff, loc_msg, _b) ->
606 env, diff, loc_msg, false)
608 let classify_errors ((ok_l,uncertain_l,error_l) as outcome) =
609 if ok_l <> [] || uncertain_l <> [] then
610 ok_l,uncertain_l,mark_not_significant error_l
613 let rec filter = function
615 | codomain_item :: tl ->
616 debug_print(lazy (sprintf "%s CHOSEN" (fst codomain_item)));
617 let new_env = Environment.add item codomain_item aliases in
618 let new_diff = (item,codomain_item)::diff in
621 (inner_dom@remaining_dom@rem_dom) base_univ
623 | Ok (thing, metasenv,subst,new_univ) ->
625 (match inner_dom with
627 [new_env,new_diff,metasenv,subst,thing,new_univ],
630 aux new_env new_diff None inner_dom
631 (remaining_dom@rem_dom)
635 | Uncertain loc_msg ->
637 (match inner_dom with
638 | [] -> [],[new_env,new_diff,loc_msg],[]
640 aux new_env new_diff None inner_dom
641 (remaining_dom@rem_dom)
646 let res = [],[],[new_env,new_diff,loc_msg,true] in
649 let ok_l,uncertain_l,error_l =
650 classify_errors (filter choices)
654 (fun (env,diff,_,_,_,_) res ->
655 aux env diff None remaining_dom rem_dom @@ res
656 ) ok_l ([],[],error_l)
659 (fun (env,diff,_) res ->
660 aux env diff None remaining_dom rem_dom @@ res
661 ) uncertain_l res_after_ok_l
663 let aux' aliases diff lookup_in_todo_dom todo_dom =
664 match test_env aliases todo_dom base_univ with
667 aux aliases diff lookup_in_todo_dom todo_dom []
668 | Ko (loc_msg) -> [],[],[aliases,diff,loc_msg,true] in
671 match aux' aliases [] None todo_dom with
672 | [],uncertain,errors ->
675 (fun (env,diff,loc_msg) -> (env,diff,loc_msg,true)
680 (fun (env,diff,loc_msg,significant) ->
683 (fun locs domain_item ->
686 fst (Environment.find domain_item env)
688 Some (locs,descr_of_domain_item domain_item,description)
693 let diff= List.map (fun a,b -> a,fst b) diff in
694 env',diff,loc_msg,significant
697 raise (NoWellTypedInterpretation (0,errors))
698 | [_,diff,metasenv,subst,t,ugraph],_,_ ->
699 debug_print (lazy "SINGLE INTERPRETATION");
700 [diff,metasenv,subst,t,ugraph], false
703 (lazy (sprintf "MANY INTERPRETATIONS (%d)" (List.length l)));
706 (fun (env, _, _, _, _, _) ->
708 (fun locs domain_item ->
710 fst (Environment.find domain_item env)
712 locs,descr_of_domain_item domain_item, description)
717 C.interactive_interpretation_choice
718 thing_txt thing_txt_prefix_len choices
720 (List.map (fun n->let _,d,m,s,t,u= List.nth l n in d,m,s,t,u)
726 CicEnvironment.CircularDependency s ->
727 failwith "Disambiguate: circular dependency"