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