src/Pure/Tools/adhoc_overloading.ML
changeset 82004 aaa7e388265a
parent 82003 abb40413c1e7
child 82005 b2d8d50b9efb
equal deleted inserted replaced
82003:abb40413c1e7 82004:aaa7e388265a
    78 fun map_data f =
    78 fun map_data f =
    79   Data.map (fn {variants, oconsts} =>
    79   Data.map (fn {variants, oconsts} =>
    80     let val (variants', oconsts') = f (variants, oconsts)
    80     let val (variants', oconsts') = f (variants, oconsts)
    81     in {variants = variants', oconsts = oconsts'} end);
    81     in {variants = variants', oconsts = oconsts'} end);
    82 
    82 
       
    83 val no_overloaded = Symtab.is_empty o #variants o Data.get;
    83 val is_overloaded = Symtab.defined o #variants o Data.get;
    84 val is_overloaded = Symtab.defined o #variants o Data.get;
    84 val get_variants = Symtab.lookup o #variants o Data.get;
    85 val get_variants = Symtab.lookup o #variants o Data.get;
    85 val get_overloaded = Termtab.lookup o #oconsts o Data.get;
    86 val get_overloaded = Termtab.lookup o #oconsts o Data.get;
    86 
    87 
    87 fun generic_add_overloaded oconst context =
    88 fun generic_add_overloaded oconst context =
   183       |> Option.map (Const o rpair (Term.type_of t));
   184       |> Option.map (Const o rpair (Term.type_of t));
   184   in
   185   in
   185     Pattern.rewrite_term_yoyo thy [] [proc]
   186     Pattern.rewrite_term_yoyo thy [] [proc]
   186   end;
   187   end;
   187 
   188 
       
   189 fun add_consts_overloaded ctxt =
       
   190   let
       
   191     val context = Context.Proof ctxt;
       
   192     val overloaded = is_overloaded context;
       
   193   in
       
   194     if no_overloaded context then K I
       
   195     else fold_aterms (fn Const (c, T) => if overloaded c then insert (op =) (c, T) else I | _ => I)
       
   196   end;
       
   197 
   188 in
   198 in
   189 
   199 
   190 fun check ctxt =
   200 fun check ctxt =
   191   map (fn t => Term.map_aterms (insert_variants ctxt t) t);
   201   if no_overloaded (Context.Proof ctxt) then I
       
   202   else map (fn t => Term.map_aterms (insert_variants ctxt t) t);
   192 
   203 
   193 fun uncheck ctxt ts =
   204 fun uncheck ctxt ts =
   194   if Config.get ctxt show_variants orelse exists (is_none o try Term.type_of) ts then ts
   205   if Config.get ctxt show_variants orelse exists (is_none o try Term.type_of) ts then ts
   195   else map (insert_overloaded ctxt) ts;
   206   else map (insert_overloaded ctxt) ts;
   196 
   207 
   197 fun reject_unresolved ctxt =
   208 fun reject_unresolved ctxt =
   198   let
   209   let
   199     fun check_unresolved t =
   210     fun check_unresolved t =
   200       (case filter (is_overloaded (Context.Proof ctxt) o fst) (Term.add_consts t []) of
   211       (case add_consts_overloaded ctxt t [] of
   201         [] => t
   212         [] => t
   202       | (cT :: _) => err_unresolved_overloading ctxt cT t (the_candidates ctxt cT));
   213       | const :: _ => err_unresolved_overloading ctxt const t (the_candidates ctxt const));
   203   in map check_unresolved end;
   214   in map check_unresolved end;
   204 
   215 
   205 end;
   216 end;
   206 
   217 
   207 
   218