(* Copyright (C) 2004, HELM Team. * * This file is part of HELM, an Hypertextual, Electronic * Library of Mathematics, developed at the Computer Science * Department, University of Bologna, Italy. * * HELM is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * HELM is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HELM; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. * * For details, see the HELM World-Wide-Web page, * http://helm.cs.unibo.it/ *) (* $Id$ *) open Printf open DisambiguateTypes module Ast = NotationPt (* hard errors, spurious errors *) exception NoWellTypedInterpretation of ((Stdpp.location * string) list * (Stdpp.location * string) list) exception PathNotWellFormed (** raised when an environment is not enough informative to decide *) exception Try_again of string Lazy.t type ('alias,'term) aliases = bool * 'term DisambiguateTypes.codomain_item DisambiguateTypes.Environment.t type 'a disambiguator_input = string * int * 'a type domain = domain_tree list and domain_tree = Node of Stdpp.location list * domain_item * domain (* let mono_uris_callback ~selection_mode ?ok ?(enable_button_for_non_vars = true) ~title ~msg ~id = if Helm_registry.get_opt_default Helm_registry.get_bool ~default:true "matita.auto_disambiguation" then function l -> l else raise Ambiguous_input let mono_interp_callback _ _ _ = assert false let mono_disamb_callback _ = assert false let _choose_uris_callback = ref mono_uris_callback let _choose_interp_callback = ref mono_interp_callback let _choose_disamb_callback = ref mono_disamb_callback let set_choose_uris_callback f = _choose_uris_callback := f let set_choose_interp_callback f = _choose_interp_callback := f let set_choose_disamb_callback f = _choose_disamb_callback := f let interactive_user_uri_choice = !_choose_uris_callback let interactive_interpretation_choice interp = !_choose_interp_callback interp let interactive_ast_choice interp = !_choose_disamb_callback interp *) let input_or_locate_uri ~(title:string) ?id () = None (* Zack: I try to avoid using this callback. I therefore assume that * the presence of an identifier that can't be resolved via "locate" * query is a syntax error *) let rec string_of_domain = function [] -> "" | Node (_,domain_item,l)::tl -> DisambiguateTypes.string_of_domain_item domain_item ^ " [ " ^ string_of_domain l ^ " ] " ^ string_of_domain tl let rec filter_map_domain f = function [] -> [] | Node (locs,domain_item,l)::tl -> match f locs domain_item with None -> filter_map_domain f l @ filter_map_domain f tl | Some res -> res :: filter_map_domain f l @ filter_map_domain f tl let rec map_domain f = function [] -> [] | Node (locs,domain_item,l)::tl -> f locs domain_item :: map_domain f l @ map_domain f tl let uniq_domain dom = let rec aux seen = function [] -> seen,[] | Node (locs,domain_item,l)::tl -> if List.mem domain_item seen then let seen,l = aux seen l in let seen,tl = aux seen tl in seen, l @ tl else let seen,l = aux (domain_item::seen) l in let seen,tl = aux seen tl in seen, Node (locs,domain_item,l)::tl in snd (aux [] dom) let debug = false let debug_print s = if debug then prerr_endline (Lazy.force s) else () (* (** print benchmark information *) let benchmark = true let max_refinements = ref 0 (* benchmarking is not thread safe *) let actual_refinements = ref 0 let domain_size = ref 0 let choices_avg = ref 0. *) type ('term,'metasenv,'subst,'graph) test_result = | Ok of 'term * 'metasenv * 'subst * 'graph | Ko of (Stdpp.location * string) Lazy.t | Uncertain of (Stdpp.location * string) Lazy.t type ('alias,'ast_thing,'metasenv,'subst,'thing,'ugraph) disamb_result = | Disamb_success of ('ast_thing * 'metasenv * 'subst * 'thing * 'ugraph) list (* each element of this list is a list of partially instantiated asts, * sharing the last instantiated node, together with the location of that * node; each ast is associated with the actual instantiation of the node, * the refinement error, and the location at which refinement fails *) | Disamb_failure of (* hard errors, spurious errors *) (('ast_thing * 'alias option * Stdpp.location * string) list * Stdpp.location) list * (('ast_thing * 'alias option * Stdpp.location * string) list * Stdpp.location) list let resolve ~env ~universe ~mk_choice item interpr arg = (* let _ = (mk_choice : (GrafiteAst.alias_spec -> 'b DisambiguateTypes.codomain_item)) in *) try let interpr = match interpr with | None -> InterprEnv.find item env | Some i -> i in (* one, and only one interpretation is returned (or Not_found) *) (*if (List.length interpr <> 1) then (let strinterpr = String.concat ", " (List.map GrafiteAst.description_of_alias interpr) in prerr_endline (Printf.sprintf "choices for %s: %s" (DisambiguateTypes.string_of_domain_item item) strinterpr); assert false) else let interpr = List.hd interpr in*) match snd (mk_choice interpr), arg with `Num_interp f, `Num_arg n -> f n | `Sym_interp f, `Args l -> f l | `Sym_interp f, `Num_arg n -> (* Implicit number! *) f [] | _,_ -> assert false with Not_found -> failwith (": InterprEnv.find failed") (* TODO move it to Cic *) let find_in_context name context = let rec aux acc = function | [] -> raise Not_found | Some hd :: tl when hd = name -> acc | _ :: tl -> aux (acc + 1) tl in aux 1 context let string_of_name = function | Ast.Ident (n, _) -> Some n | _ -> assert false ;; (* XXX: assuming the domain is composed of uninterpreted items only *) let rec domain_of_term ?(loc = HExtlib.dummy_floc) ~context = function | Ast.AttributedTerm (`Loc loc, term) -> domain_of_term ~loc ~context term | Ast.AttributedTerm (_, term) -> domain_of_term ~loc ~context term | Ast.Symbol (name, attr) -> (match attr with | None -> [ Node ([loc], Symbol name, []) ] | _ -> []) (* to be kept in sync with Ast.Appl (Ast.Symbol ...) *) | Ast.Appl (Ast.Symbol (name, None) as hd :: args) | Ast.Appl (Ast.AttributedTerm (_,Ast.Symbol (name,None)) as hd :: args) -> let args_dom = List.fold_right (fun term acc -> domain_of_term ~loc ~context term @ acc) args [] in let loc = match hd with Ast.AttributedTerm (`Loc loc,_) -> loc | _ -> loc in [ Node ([loc], Symbol name, args_dom) ] | Ast.Appl (Ast.Ident (id,`Ambiguous) as hd :: args) | Ast.Appl (Ast.AttributedTerm (_,Ast.Ident (id,`Ambiguous)) as hd :: args) -> let args_dom = List.fold_right (fun term acc -> domain_of_term ~loc ~context term @ acc) args [] in let loc = match hd with Ast.AttributedTerm (`Loc loc,_) -> loc | _ -> loc in (try (* the next line can raise Not_found *) ignore(find_in_context id context); args_dom with Not_found -> [ Node ([loc], Id id, args_dom) ] ) | Ast.Appl terms -> List.fold_right (fun term acc -> domain_of_term ~loc ~context term @ acc) terms [] | Ast.Binder (kind, (var, typ), body) -> let type_dom = domain_of_term_option ~loc ~context typ in let body_dom = domain_of_term ~loc ~context:(string_of_name var :: context) body in (match kind with | `Exists -> [ Node ([loc], Symbol "exists", (type_dom @ body_dom)) ] | _ -> type_dom @ body_dom) | Ast.Case (term, indty_ident, outtype, branches) -> let term_dom = domain_of_term ~loc ~context term in let outtype_dom = domain_of_term_option ~loc ~context outtype in let rec get_first_constructor = function | [] -> [] | (Ast.Pattern (head, Some r, _), _) :: _ -> [] (* [ Node ([loc], Id (head,Some (NReference.string_of_reference r)), []) ] *) | (Ast.Pattern (head, None, _), _) :: _ -> [ Node ([loc], Id head, []) ] | _ :: tl -> get_first_constructor tl in let do_branch = function Ast.Pattern (head, _, args), term -> let (term_context, args_domain) = List.fold_left (fun (cont, dom) (name, typ) -> (string_of_name name :: cont, (match typ with | None -> dom | Some typ -> dom @ domain_of_term ~loc ~context:cont typ))) (context, []) args in domain_of_term ~loc ~context:term_context term @ args_domain | Ast.Wildcard, term -> domain_of_term ~loc ~context term in let branches_dom = List.fold_left (fun dom branch -> dom @ do_branch branch) [] branches in (match indty_ident with | None -> get_first_constructor branches | Some (ident, None) -> [ Node ([loc], Id ident , []) ] | Some (ident, Some r) -> []) (* [ Node ([loc], Id (ident,Some (NReference.string_of_reference r)), []) ]) *) @ term_dom @ outtype_dom @ branches_dom | Ast.Cast (term, ty) -> let term_dom = domain_of_term ~loc ~context term in let ty_dom = domain_of_term ~loc ~context ty in term_dom @ ty_dom | Ast.LetIn ((var, typ), body, where) -> let body_dom = domain_of_term ~loc ~context body in let type_dom = domain_of_term_option ~loc ~context typ in let where_dom = domain_of_term ~loc ~context:(string_of_name var :: context) where in body_dom @ type_dom @ where_dom | Ast.LetRec (kind, defs, where) -> let add_defs context = List.fold_left (fun acc (_, (var, _), _, _) -> string_of_name var :: acc ) context defs in let where_dom = domain_of_term ~loc ~context:(add_defs context) where in let defs_dom = List.fold_left (fun dom (params, (_, typ), body, _) -> let context' = add_defs (List.fold_left (fun acc (var,_) -> string_of_name var :: acc) context params) in List.rev (snd (List.fold_left (fun (context,res) (var,ty) -> string_of_name var :: context, domain_of_term_option ~loc ~context ty @ res) (add_defs context,[]) params)) @ dom @ domain_of_term_option ~loc ~context:context' typ @ domain_of_term ~loc ~context:context' body ) [] defs in defs_dom @ where_dom | Ast.Ident (name, _x) -> (* let x = match x with | `Uri u -> Some u | _ -> None in *) (try (* the next line can raise Not_found *) ignore(find_in_context name context); [] with Not_found -> (* (match subst with | None -> [ Node ([loc], Id name, []) ] | Some subst -> let terms = List.fold_left (fun dom (_, term) -> let dom' = domain_of_term ~loc ~context term in dom @ dom') [] subst in [ Node ([loc], Id name, terms) ]))*) [ Node ([loc], Id name, []) ]) | Ast.NRef _ -> [] | Ast.NCic _ -> [] | Ast.Implicit _ -> [] | Ast.Num (num, i) -> [ Node ([loc], Num, []) ] | Ast.Meta (index, local_context) -> List.fold_left (fun dom term -> dom @ domain_of_term_option ~loc ~context term) [] local_context | Ast.Sort _ -> [] | Ast.UserInput | Ast.Literal _ | Ast.Layout _ | Ast.Magic _ | Ast.Variable _ -> assert false and domain_of_term_option ~loc ~context = function | None -> [] | Some t -> domain_of_term ~loc ~context t let domain_of_term ~context term = uniq_domain (domain_of_term ~context term) let domain_of_obj ~context ast = assert (context = []); match ast with | Ast.Theorem (_,_,ty,bo,_) -> domain_of_term [] ty @ (match bo with None -> [] | Some bo -> domain_of_term [] bo) | Ast.Inductive (params,tyl) -> let context, dom = List.fold_left (fun (context, dom) (var, ty) -> let context' = string_of_name var :: context in match ty with None -> context', dom | Some ty -> context', dom @ domain_of_term context ty ) ([], []) params in let context_w_types = List.rev_map (fun (var, _, _, _) -> Some var) tyl @ context in dom @ List.flatten ( List.map (fun (_,_,ty,cl) -> domain_of_term context ty @ List.flatten ( List.map (fun (_,ty) -> domain_of_term context_w_types ty) cl)) tyl) | Ast.Record (params,var,ty,fields) -> let context, dom = List.fold_left (fun (context, dom) (var, ty) -> let context' = string_of_name var :: context in match ty with None -> context', dom | Some ty -> context', dom @ domain_of_term context ty ) ([], []) params in let context_w_types = Some var :: context in dom @ domain_of_term context ty @ snd (List.fold_left (fun (context,res) (name,ty,_,_) -> Some name::context, res @ domain_of_term context ty ) (context_w_types,[]) fields) let domain_of_obj ~context obj = uniq_domain (domain_of_obj ~context obj) (* dom1 \ dom2 *) (* XXX: possibly should take into account locs? *) let domain_diff dom1 dom2 = (* let domain_diff = Domain.diff *) let is_in_dom2 elt = List.exists (function | Symbol symb -> (match elt with Symbol symb' when symb = symb' -> true | _ -> false) | Num -> (match elt with Num -> true | _ -> false) | item -> elt = item ) dom2 in let rec aux = function [] -> [] | Node (_,elt,l)::tl when is_in_dom2 elt -> aux (l @ tl) | Node (loc,elt,l)::tl -> Node (loc,elt,aux l)::(aux tl) in aux dom1 let refine_profiler = HExtlib.profile "disambiguate_thing.refine_thing" (* generic function lifting splitting from type T to type (T list) *) let rec list_split item_split ctx pre post = match post with | [] -> [] | x :: post' -> (item_split (fun v1 -> ctx (pre@v1::post')) x)@ list_split item_split ctx (pre@[x]) post' ;; (* splits a 'term capture_variable *) let var_split ctx = function | (_,None) -> [] | (v,Some ty) -> [(fun x -> ctx (v,Some x)),ty] ;; let ity_split ctx (n,isind,ty,cl) = [(fun x -> ctx (n,isind,x,cl)),ty]@ list_split (fun ctx0 (v,ty) -> [(fun x -> ctx0 (v,x)),ty]) (fun x -> ctx (n,isind,ty,x)) [] cl ;; let field_split ctx (n,ty,b,m) = [(fun x -> ctx (n,x,b,m)),ty] ;; (* splits a defn. in a mutual LetRec block *) let letrecaux_split ctx (args,f,bo,n) = list_split var_split (fun x -> ctx (x,f,bo,n)) [] args@ var_split (fun x -> ctx (args,x,bo,n)) f@ [(fun x -> ctx (args,f,x,n)),bo] ;; let mk_ident s = function | None -> Ast.Ident (s,`Ambiguous) | Some u -> Ast.Ident (s,`Uri (NReference.string_of_reference u)) ;; (* splits a pattern (for case analysis) *) let pattern_split ctx = function | Ast.Wildcard -> [] | Ast.Pattern (s,href,pl) -> let rec auxpatt pre = function | [] -> [] | (x,Some y as x')::post' -> ((fun v -> ctx (Ast.Pattern (s,href,pre@(x,Some v)::post'))),y) ::auxpatt (pre@[x']) post' | (x,None as x')::post' -> auxpatt (pre@[x']) post' in ((fun x -> let id0, href0 = match x with | Ast.Ident (id,`Ambiguous) -> id, None | Ast.Ident (id,`Uri u) -> id, Some u | _ -> assert false in let href0 = HExtlib.map_option NReference.reference_of_string href0 in ctx (Ast.Pattern (id0,href0,pl))), mk_ident s href)::auxpatt [] pl ;; (* this function is used to get the children of a given node, together with * their context and their location * contexts are expressed in the form of functions Ast -> Ast *) (* split : location -> ('term -> 'a) -> 'term -> (('term -> 'a) * 'term * loc) list *) let rec split loc ctx t = let loc_of_attr oldloc = function | `Loc l -> l | _ -> oldloc in match t with | Ast.AttributedTerm (attr,t) -> [(fun x -> ctx (Ast.AttributedTerm(attr,x))),t, loc_of_attr loc attr] | Ast.Appl tl -> list_split (*split*) (fun ctx0 t0 -> [ctx0,t0,loc]) (fun x -> ctx (Ast.Appl x)) [] tl | Ast.Binder (k,((n,None) as n'),body) -> [ (fun v -> ctx (Ast.Binder (k,n',v))),body, loc] | Ast.Binder (k,((n,Some ty) as n'),body) -> [(fun v -> ctx (Ast.Binder (k,(n,Some v),body))), ty, loc ;(fun v -> ctx (Ast.Binder (k,n',v))),body, loc] | Ast.Case (t,ity,outty,pl) -> let outty_split = match outty with | None -> [] | Some u -> [(fun x -> ctx (Ast.Case (t,ity,Some x,pl))),u] in List.map (fun (a,b) -> a,b,loc) (((fun x -> ctx (Ast.Case (x,ity,outty,pl))),t):: outty_split@list_split (fun ctx0 (p,x) -> ((fun y -> ctx0 (p,y)),x)::pattern_split (fun y -> ctx0 (y,x)) p) (fun x -> ctx (Ast.Case(t,ity,outty,x))) [] pl) | Ast.Cast (u,v) -> [ (fun x -> ctx (Ast.Cast (x,v))),u,loc ; (fun x -> ctx (Ast.Cast (u,x))),v,loc ] | Ast.LetIn ((n,None) as n',u,v) -> [ (fun x -> ctx (Ast.LetIn (n',x,v))), u,loc ; (fun x -> ctx (Ast.LetIn (n',u,x))), v,loc ] | Ast.LetIn ((n,Some t) as n',u,v) -> [ (fun x -> ctx (Ast.LetIn ((n,Some x),u,v))), t,loc ; (fun x -> ctx (Ast.LetIn (n',x,v))), u, loc ; (fun x -> ctx (Ast.LetIn (n',u,x))), v, loc ] | Ast.LetRec (k,funs,where) -> (* we do not use where any more?? *) List.map (fun (a,b) -> a,b,loc) (list_split letrecaux_split (fun x -> ctx (Ast.LetRec(k,x,where))) [] funs) | _ -> [] (* leaves *) ;; type 'ast marked_ast = (NotationPt.term -> 'ast) * NotationPt.term * Stdpp.location (* top_split : 'a -> ((term -> 'a) * term * loc) list * such that it returns all the possible top-level (ctx,t,dummy_loc) for its input *) let bfvisit ~pp_term top_split test t = let rec aux = function | [] -> None | (ctx0,t0,loc as hd)::tl -> (* prerr_endline ("ok! length tl = " ^ string_of_int (List.length tl)); *) debug_print (lazy ("visiting t0 = " ^ pp_term t0)); if test t0 then (debug_print (lazy "t0 is ambiguous"); Some hd) else (* (prerr_endline ("splitting not ambiguous t0:"); let t0' = split ctx0 t0 in List.iter (fun (ctx',t') -> prerr_endline ("-- subnode " ^ (pp_term t'))) t0'; aux (tl@t0')) *) let t0' = split loc ctx0 t0 in aux (tl@t0') (* in aux [(fun x -> x),t] *) in aux (top_split t) ;; let obj_split (o:Ast.term Ast.obj) = match o with | Ast.Inductive (ls,itl) -> List.map (fun (a,b) -> a,b,Stdpp.dummy_loc) (list_split var_split (fun x -> Ast.Inductive (x,itl)) [] ls@ list_split ity_split (fun x -> Ast.Inductive (ls,x)) [] itl) | Ast.Theorem (flav,n,ty,None,p) -> [(fun x -> Ast.Theorem (flav,n,x,None,p)), ty, Stdpp.dummy_loc] | Ast.Theorem (flav,n,ty,(Some bo as bo'),p) -> [(fun x -> Ast.Theorem (flav,n,x,bo',p)), ty, Stdpp.dummy_loc ;(fun x -> Ast.Theorem (flav,n,ty,Some x,p)), bo, Stdpp.dummy_loc] | Ast.Record (ls,n,ty,fl) -> List.map (fun (a,b) -> a,b,Stdpp.dummy_loc) (list_split var_split (fun x -> Ast.Record (x,n,ty,fl)) [] ls@ [(fun x -> Ast.Record (ls,n,x,fl)),ty]@ list_split field_split (fun x -> Ast.Record (ls,n,ty,x)) [] fl) ;; let bfvisit_obj = bfvisit obj_split;; let bfvisit = bfvisit (fun t -> [(fun x -> x),t,Stdpp.dummy_loc]) ;; let domain_item_of_ast = function | Ast.Ident (id,_) -> DisambiguateTypes.Id id | Ast.Symbol (sym,_) -> DisambiguateTypes.Symbol sym | Ast.Num (n,_) -> DisambiguateTypes.Num | Ast.Case(_,Some (ityname,_),_,_) -> DisambiguateTypes.Id ityname | _ -> assert false ;; let ast_of_alias_spec t alias = match (t,alias) with | Ast.Ident _, GrafiteAst.Ident_alias (id,uri) -> Ast.Ident(id,`Uri uri) | Ast.Case (t,_,oty,pl), GrafiteAst.Ident_alias (id,uri) -> Ast.Case (t,Some (id,Some (NReference.reference_of_string uri)),oty,pl) | _, GrafiteAst.Symbol_alias (sym,uri,desc) -> Ast.Symbol (sym,Some (uri,desc)) | Ast.Num(m,_), GrafiteAst.Number_alias (uri,desc) -> Ast.Num(m,Some (uri,desc)) | _ -> assert false ;; (* returns an optional (loc*string): * - None means success (the ast is refinable) * - Some (loc,err) means refinement has failed with error err at location loc *) let test_interpr ~context ~metasenv ~subst ~use_coercions ~expty ~env ~universe ~uri ~interpretate_thing ~refine_thing ~ugraph ~mk_localization_tbl t = try let localization_tbl = mk_localization_tbl 503 in let cic_thing = interpretate_thing ~context ~env ~universe ~uri ~is_path:false t ~localization_tbl in let foo () = refine_thing metasenv subst context uri ~use_coercions cic_thing expty ugraph ~localization_tbl in match (refine_profiler.HExtlib.profile foo ()) with | Ko x -> let loc,err = Lazy.force x in debug_print (lazy ("test_interpr error: " ^ err)); Some (loc,err) | _ -> None with (*| Try_again msg -> Uncertain (lazy (Stdpp.dummy_loc,Lazy.force msg)) | Invalid_choice loc_msg -> Ko loc_msg*) | Invalid_choice x -> let loc,err = Lazy.force x in Some (loc,err) | _ -> None ;; exception Not_ambiguous;; let rec disambiguate_list ~context ~metasenv ~subst ~use_coercions ~expty ~env ~uri ~interpretate_thing ~refine_thing ~ugraph ~visit ~universe ~mk_localization_tbl ~pp_thing ~pp_term ts errors = let disambiguate_list = disambiguate_list ~context ~metasenv ~subst ~use_coercions ~expty ~env ~uri ~interpretate_thing ~refine_thing ~ugraph ~visit ~universe ~mk_localization_tbl ~pp_thing ~pp_term in let test_interpr = test_interpr ~context ~metasenv ~subst ~use_coercions ~expty ~env ~universe ~uri ~interpretate_thing ~refine_thing ~ugraph ~mk_localization_tbl in let find_choices item = let a2s = function | GrafiteAst.Ident_alias (_id,uri) -> uri | GrafiteAst.Symbol_alias (_id,_,desc) -> desc | GrafiteAst.Number_alias (_,desc) -> desc in let d2s = function | Id id | Symbol id -> id | Num -> "NUM" in let res = Environment.find item universe in debug_print (lazy (Printf.sprintf "choices for %s :\n%s" (d2s item) (String.concat ", " (List.map a2s res)))); res in let get_instances ctx t = try let choices = find_choices (domain_item_of_ast t) in List.map (fun t0 -> ctx (ast_of_alias_spec t t0), t0) choices with | Not_found -> [] in match ts with | [] -> [], errors | _ -> (* debug_print (lazy ("disambiguate_list: t0 = " ^ pp_thing t0)); *) let is_ambiguous = function | Ast.Ident (_,`Ambiguous) | Ast.Num (_,None) | Ast.Symbol (_,None) -> true | Ast.Case (_,Some (ity,None),_,_) -> true | _ -> false in let astpp = function | Ast.Ident (id,_) -> "ID " ^ id | Ast.Num _ -> "NUM" | Ast.Symbol (sym,_) -> "SYM " ^ sym | _ -> "ERROR!" in let process_ast t0 = (* get first ambiguous subterm (or return disambiguated term) *) match visit ~pp_term is_ambiguous t0 with | None -> (* debug_print (lazy ("visit -- not ambiguous node:\n" ^ pp_thing * t0));*) (* Some (t0,tl), errors *) raise Not_ambiguous | Some (ctx, t, loc_t) -> debug_print (lazy ("visit -- found ambiguous node: " ^ astpp t ^ "\nin " ^ pp_thing (ctx t))); (* get possible instantiations of t *) let instances = get_instances ctx t in debug_print (lazy "-- possible instances:"); (* List.iter (fun u0 -> debug_print (lazy ("-- instance: " ^ (pp_thing u0)))) instances; *) (* perforate ambiguous subterms and test refinement *) let instances = List.map (fun (x,a) -> (x,Some a),test_interpr x) instances in debug_print (lazy "-- survivors:"); let survivors, defuncts = List.partition (fun (_,o) -> o = None) instances in survivors, (defuncts, loc_t) in try let ts', new_errors = List.split (List.map process_ast ts) in let ts' = List.map (fun ((t,_),_) -> t) (List.flatten ts') in let errors' = match new_errors with | (_,l)::_ -> let ne' = List.map (fun (a,b) -> a, HExtlib.unopt b) (List.flatten (List.map fst new_errors)) in let ne' = List.map (fun ((x,a),(l,e)) -> x,a,l,e) ne' in (ne',l)::errors | _ -> errors in disambiguate_list ts' errors' with Not_ambiguous -> ts,errors ;; let disambiguate_thing ~context ~metasenv ~subst ~use_coercions ~string_context_of_context ~initial_ugraph:base_univ ~expty ~mk_implicit ~description_of_alias ~fix_instance ~aliases ~universe ~lookup_in_library ~uri ~pp_thing ~pp_term ~domain_of_thing ~interpretate_thing ~refine_thing ~visit ~mk_localization_tbl (thing_txt,thing_txt_prefix_len,thing) = let env = aliases in let test_interpr = test_interpr ~context ~metasenv ~subst ~use_coercions ~expty ~env ~universe ~uri ~interpretate_thing ~refine_thing ~ugraph:base_univ ~mk_localization_tbl in (* real function *) let aux tl = disambiguate_list ~mk_localization_tbl ~context ~metasenv ~subst ~interpretate_thing ~refine_thing ~ugraph:base_univ ~visit ~use_coercions ~expty ~uri ~env ~universe ~pp_thing ~pp_term tl [] in let refine t = let localization_tbl = mk_localization_tbl 503 in debug_print (lazy "before interpretate_thing"); let t' = interpretate_thing ~context ~env ~universe ~uri ~is_path:false t ~localization_tbl in debug_print (lazy "after interpretate_thing"); match refine_thing metasenv subst context uri ~use_coercions t' expty base_univ ~localization_tbl with | Ok (t',m',s',u') -> t,m',s',t',u' | Uncertain x -> let _,err = Lazy.force x in debug_print (lazy ("refinement uncertain after disambiguation: " ^ err)); assert false | _ -> assert false in match (test_interpr thing) with | Some (loc,err) -> (* debug_print (lazy ("preliminary test fail: " ^ pp_thing thing)); *) Disamb_failure ([[thing,None,loc,err],loc],[]) | None -> let res,errors = aux [thing] in let res = HExtlib.filter_map (fun t -> try Some (refine t) with _ -> (* debug_print (lazy ("can't interpretate/refine " ^ (pp_thing t)));*) None) res in (match res with | [] -> Disamb_failure (errors,[]) | _ -> Disamb_success res) ;;