src/HOL/Tools/Sledgehammer/sledgehammer_isar_annotate.ML
author blanchet
Tue Jul 01 16:47:10 2014 +0200 (2014-07-01 ago)
changeset 57467 03345dad8430
parent 55286 7bbbd9393ce0
child 59058 a78612c67ec0
permissions -rw-r--r--
robustness in the face of ill-typed "unchecked" terms (e.g. case expressions)
     1 (*  Title:      HOL/Tools/Sledgehammer/sledgehammer_isar_annotate.ML
     2     Author:     Steffen Juilf Smolka, TU Muenchen
     3     Author:     Jasmin Blanchette, TU Muenchen
     4 
     5 Supplements term with a locally minmal, complete set of type constraints. Complete: The constraints
     6 suffice to infer the term's types. Minimal: Reducing the set of constraints further will make it
     7 incomplete.
     8 
     9 When configuring the pretty printer appropriately, the constraints will show up as type annotations
    10 when printing the term. This allows the term to be printed and reparsed without a change of types.
    11 
    12 Note: Terms should be unchecked before calling "annotate_types_in_term" to avoid awkward syntax.
    13 *)
    14 
    15 signature SLEDGEHAMMER_ISAR_ANNOTATE =
    16 sig
    17   val annotate_types_in_term : Proof.context -> term -> term
    18 end;
    19 
    20 structure Sledgehammer_Isar_Annotate : SLEDGEHAMMER_ISAR_ANNOTATE =
    21 struct
    22 
    23 fun post_traverse_term_type' f _ (t as Const (_, T)) s = f t T s
    24   | post_traverse_term_type' f _ (t as Free (_, T)) s = f t T s
    25   | post_traverse_term_type' f _ (t as Var (_, T)) s = f t T s
    26   | post_traverse_term_type' f env (t as Bound i) s = f t (nth env i) s
    27   | post_traverse_term_type' f env (Abs (x, T1, b)) s =
    28     let val ((b', s'), T2) = post_traverse_term_type' f (T1 :: env) b s in
    29       f (Abs (x, T1, b')) (T1 --> T2) s'
    30     end
    31   | post_traverse_term_type' f env (u $ v) s =
    32     let
    33       val ((u', s'), Type (_, [_, T])) = post_traverse_term_type' f env u s
    34       val ((v', s''), _) = post_traverse_term_type' f env v s'
    35     in f (u' $ v') T s'' end
    36     handle Bind => raise Fail "Sledgehammer_Isar_Annotate: post_traverse_term_type'"
    37 
    38 fun post_traverse_term_type f s t =
    39   post_traverse_term_type' (fn t => fn T => fn s => (f t T s, T)) [] t s |> fst
    40 fun post_fold_term_type f s t =
    41   post_traverse_term_type (fn t => fn T => fn s => (t, f t T s)) s t |> snd
    42 
    43 fun fold_map_atypes f T s =
    44   (case T of
    45     Type (name, Ts) =>
    46     let val (Ts, s) = fold_map (fold_map_atypes f) Ts s in
    47       (Type (name, Ts), s)
    48     end
    49   | _ => f T s)
    50 
    51 val indexname_ord = Term_Ord.fast_indexname_ord
    52 val cost_ord = prod_ord int_ord (prod_ord int_ord int_ord)
    53 
    54 structure Var_Set_Tab = Table(
    55   type key = indexname list
    56   val ord = list_ord indexname_ord)
    57 
    58 fun generalize_types ctxt t =
    59   let
    60     val erase_types = map_types (fn _ => dummyT)
    61     (* use schematic type variables *)
    62     val ctxt = ctxt |> Proof_Context.set_mode Proof_Context.mode_pattern
    63     val infer_types = singleton (Type_Infer_Context.infer_types ctxt)
    64   in
    65      t |> erase_types |> infer_types
    66   end
    67 
    68 fun match_types ctxt t1 t2 =
    69   let
    70     val thy = Proof_Context.theory_of ctxt
    71     val get_types = post_fold_term_type (K cons) []
    72   in
    73     fold (perhaps o try o Sign.typ_match thy) (get_types t1 ~~ get_types t2) Vartab.empty
    74   end
    75 
    76 fun handle_trivial_tfrees ctxt t' subst =
    77   let
    78     val add_tfree_names = snd #> snd #> fold_atyps (fn TFree (x, _) => cons x | _ => I)
    79 
    80     val trivial_tfree_names =
    81       Vartab.fold add_tfree_names subst []
    82       |> filter_out (Variable.is_declared ctxt)
    83       |> distinct (op =)
    84     val tfree_name_trivial = Ord_List.member fast_string_ord trivial_tfree_names
    85 
    86     val trivial_tvar_names =
    87       Vartab.fold
    88         (fn (tvar_name, (_, TFree (tfree_name, _))) =>
    89                tfree_name_trivial tfree_name ? cons tvar_name
    90           | _ => I)
    91         subst
    92         []
    93       |> sort indexname_ord
    94     val tvar_name_trivial = Ord_List.member indexname_ord trivial_tvar_names
    95 
    96     val t' =
    97       t' |> map_types
    98               (map_type_tvar
    99                 (fn (idxn, sort) =>
   100                   if tvar_name_trivial idxn then dummyT else TVar (idxn, sort)))
   101 
   102     val subst =
   103       subst |> fold Vartab.delete trivial_tvar_names
   104             |> Vartab.map
   105                (K (apsnd (map_type_tfree
   106                            (fn (name, sort) =>
   107                               if tfree_name_trivial name then dummyT
   108                               else TFree (name, sort)))))
   109   in
   110     (t', subst)
   111   end
   112 
   113 fun key_of_atype (TVar (z, _)) = Ord_List.insert indexname_ord z
   114   | key_of_atype _ = I
   115 fun key_of_type T = fold_atyps key_of_atype T []
   116 
   117 fun update_tab t T (tab, pos) =
   118   ((case key_of_type T of
   119      [] => tab
   120    | key =>
   121      let val cost = (size_of_typ T, (size_of_term t, pos)) in
   122        (case Var_Set_Tab.lookup tab key of
   123          NONE => Var_Set_Tab.update_new (key, cost) tab
   124        | SOME old_cost =>
   125          (case cost_ord (cost, old_cost) of
   126            LESS => Var_Set_Tab.update (key, cost) tab
   127          | _ => tab))
   128      end),
   129    pos + 1)
   130 
   131 val typing_spot_table = post_fold_term_type update_tab (Var_Set_Tab.empty, 0) #> fst
   132 
   133 fun reverse_greedy typing_spot_tab =
   134   let
   135     fun update_count z =
   136       fold (fn tvar => fn tab =>
   137         let val c = Vartab.lookup tab tvar |> the_default 0 in
   138           Vartab.update (tvar, c + z) tab
   139         end)
   140     fun superfluous tcount = forall (fn tvar => the (Vartab.lookup tcount tvar) > 1)
   141     fun drop_superfluous (tvars, (_, (_, spot))) (spots, tcount) =
   142       if superfluous tcount tvars then (spots, update_count ~1 tvars tcount)
   143       else (spot :: spots, tcount)
   144 
   145     val (typing_spots, tvar_count_tab) =
   146       Var_Set_Tab.fold (fn kv as (k, _) => apfst (cons kv) #> apsnd (update_count 1 k))
   147         typing_spot_tab ([], Vartab.empty)
   148       |>> sort_distinct (rev_order o cost_ord o pairself snd)
   149   in
   150     fold drop_superfluous typing_spots ([], tvar_count_tab) |> fst
   151   end
   152 
   153 fun introduce_annotations subst spots t t' =
   154   let
   155     fun subst_atype (T as TVar (idxn, S)) subst =
   156         (Envir.subst_type subst T, Vartab.update (idxn, (S, dummyT)) subst)
   157       | subst_atype T subst = (T, subst)
   158 
   159     val subst_type = fold_map_atypes subst_atype
   160 
   161     fun collect_annot _ T (subst, cp, ps as p :: ps', annots) =
   162         if p <> cp then
   163           (subst, cp + 1, ps, annots)
   164         else
   165           let val (T, subst) = subst_type T subst in
   166             (subst, cp + 1, ps', (p, T) :: annots)
   167           end
   168       | collect_annot _ _ x = x
   169 
   170     val (_, _, _, annots) = post_fold_term_type collect_annot (subst, 0, spots, []) t'
   171 
   172     fun insert_annot t _ (cp, annots as (p, T) :: annots') =
   173         if p <> cp then (t, (cp + 1, annots)) else (Type.constraint T t, (cp + 1, annots'))
   174       | insert_annot t _ x = (t, x)
   175   in
   176     t |> post_traverse_term_type insert_annot (0, rev annots) |> fst
   177   end
   178 
   179 fun annotate_types_in_term ctxt t =
   180   let
   181     val t' = generalize_types ctxt t
   182     val subst = match_types ctxt t' t
   183     val (t'', subst') = handle_trivial_tfrees ctxt t' subst
   184     val typing_spots = t'' |> typing_spot_table |> reverse_greedy |> sort int_ord
   185   in
   186     introduce_annotations subst' typing_spots t t''
   187   end
   188 
   189 end;