src/HOL/Tools/Sledgehammer/sledgehammer_isar_annotate.ML
author blanchet
Fri, 31 Jan 2014 16:26:43 +0100
changeset 55213 dcb36a2540bc
parent 55212 5832470d956e
child 55243 66709d41601e
permissions -rw-r--r--
tuned ML function names
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
(* Util *)
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
    24
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
    25
  | post_traverse_term_type' f _ (t as Free (_, T)) s = f t T s
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
    26
  | post_traverse_term_type' f _ (t as Var (_, T)) s = f t T s
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
    27
  | 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
    28
  | post_traverse_term_type' f env (Abs (x, T1, b)) s =
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
    29
    let
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
    30
      val ((b', s'), T2) = post_traverse_term_type' f (T1 :: env) b s
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
    31
    in f (Abs (x, T1, b')) (T1 --> T2) s' end
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
    32
  | post_traverse_term_type' f env (u $ v) s =
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
    33
    let
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
    34
      val ((u', s'), Type (_, [_, T])) = post_traverse_term_type' f env u s
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
    35
      val ((v', s''), _) = post_traverse_term_type' f env v s'
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
    36
    in f (u' $ v') T s'' end
55202
824c48a539c9 renamed many Sledgehammer ML files to clarify structure
blanchet
parents: 54821
diff changeset
    37
    handle Bind => raise Fail "Sledgehammer_Isar_Annotate: post_traverse_term_type'"
50258
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
    38
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
    39
fun post_traverse_term_type f s t =
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
    40
  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
    41
fun post_fold_term_type f s t =
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
    42
  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
    43
52452
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    44
fun fold_map_atypes f T s =
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    45
  case T of
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    46
    Type (name, Ts) =>
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    47
        let val (Ts, s) = fold_map (fold_map_atypes f) Ts s in
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    48
          (Type (name, Ts), s)
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    49
        end
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    50
  | _ => f T s
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    51
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    52
(** get unique elements of a list **)
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    53
local
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    54
  fun unique' b x [] = if b then [x] else []
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    55
    | unique' b x (y :: ys) =
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    56
      if x = y then unique' false x ys
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    57
      else unique' true y ys |> b ? cons x
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    58
in
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    59
  fun unique ord xs =
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    60
    case sort ord xs of x :: ys => unique' true x ys | [] => []
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    61
end
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    62
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    63
(** Data structures, orders **)
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    64
val indexname_ord = Term_Ord.fast_indexname_ord
50258
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
    65
val cost_ord = prod_ord int_ord (prod_ord int_ord int_ord)
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
    66
structure Var_Set_Tab = Table(
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
    67
  type key = indexname list
52452
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    68
  val ord = list_ord indexname_ord)
50258
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
    69
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
    70
(* (1) Generalize types *)
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
    71
fun generalize_types ctxt t =
52369
0b395800fdf0 uncheck terms before annotation to avoid awkward syntax
smolkas
parents: 52366
diff changeset
    72
  let
0b395800fdf0 uncheck terms before annotation to avoid awkward syntax
smolkas
parents: 52366
diff changeset
    73
    val erase_types = map_types (fn _ => dummyT)
0b395800fdf0 uncheck terms before annotation to avoid awkward syntax
smolkas
parents: 52366
diff changeset
    74
    (* use schematic type variables *)
0b395800fdf0 uncheck terms before annotation to avoid awkward syntax
smolkas
parents: 52366
diff changeset
    75
    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
    76
    val infer_types = singleton (Type_Infer_Context.infer_types ctxt)
0b395800fdf0 uncheck terms before annotation to avoid awkward syntax
smolkas
parents: 52366
diff changeset
    77
  in
0b395800fdf0 uncheck terms before annotation to avoid awkward syntax
smolkas
parents: 52366
diff changeset
    78
     t |> erase_types |> infer_types
0b395800fdf0 uncheck terms before annotation to avoid awkward syntax
smolkas
parents: 52366
diff changeset
    79
  end
50258
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
    80
52452
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    81
(* (2) match types *)
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    82
fun match_types ctxt t1 t2 =
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    83
  let
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    84
    val thy = Proof_Context.theory_of ctxt
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    85
    val get_types = post_fold_term_type (K cons) []
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    86
  in
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    87
    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
    88
    handle Type.TYPE_MATCH => raise Fail "Sledgehammer_Isar_Annotate: match_types"
52452
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    89
  end
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    90
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    91
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    92
(* (3) handle trivial tfrees  *)
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    93
fun handle_trivial_tfrees ctxt (t', subst) =
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    94
  let
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    95
    val add_tfree_names =
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    96
      snd #> snd #> fold_atyps (fn TFree (x, _) => cons x | _ => I)
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    97
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    98
    val trivial_tfree_names =
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
    99
      Vartab.fold add_tfree_names subst []
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   100
      |> filter_out (Variable.is_declared ctxt)
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   101
      |> unique fast_string_ord
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   102
    val tfree_name_trivial = Ord_List.member fast_string_ord trivial_tfree_names
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   103
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   104
    val trivial_tvar_names =
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   105
      Vartab.fold
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   106
        (fn (tvar_name, (_, TFree (tfree_name, _))) =>
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   107
               tfree_name_trivial tfree_name ? cons tvar_name
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   108
          | _ => I)
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   109
        subst
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   110
        []
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   111
      |> sort indexname_ord
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   112
    val tvar_name_trivial = Ord_List.member indexname_ord trivial_tvar_names
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   113
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   114
    val t' =
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   115
      t' |> map_types
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   116
              (map_type_tvar
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   117
                (fn (idxn, sort) =>
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   118
                  if tvar_name_trivial idxn then dummyT else TVar (idxn, sort)))
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   119
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   120
    val subst =
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   121
      subst |> fold Vartab.delete trivial_tvar_names
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   122
            |> Vartab.map
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   123
               (K (apsnd (map_type_tfree
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   124
                           (fn (name, sort) =>
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   125
                              if tfree_name_trivial name then dummyT
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   126
                              else TFree (name, sort)))))
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   127
  in
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   128
    (t', subst)
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   129
  end
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   130
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   131
(* (4) Typing-spot table *)
50258
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   132
local
54821
blanchet
parents: 54504
diff changeset
   133
fun key_of_atype (TVar (z, _)) = Ord_List.insert indexname_ord z
50258
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   134
  | key_of_atype _ = I
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   135
fun key_of_type T = fold_atyps key_of_atype T []
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   136
fun update_tab t T (tab, pos) =
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   137
  (case key_of_type T of
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   138
     [] => tab
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   139
   | key =>
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   140
     let val cost = (size_of_typ T, (size_of_term t, pos)) in
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   141
       case Var_Set_Tab.lookup tab key of
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   142
         NONE => Var_Set_Tab.update_new (key, cost) tab
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   143
       | SOME old_cost =>
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   144
         (case cost_ord (cost, old_cost) of
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   145
            LESS => Var_Set_Tab.update (key, cost) tab
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   146
          | _ => tab)
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   147
     end,
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   148
   pos + 1)
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   149
in
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   150
val typing_spot_table =
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   151
  post_fold_term_type update_tab (Var_Set_Tab.empty, 0) #> fst
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   152
end
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   153
52452
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   154
(* (5) Reverse-greedy *)
50258
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   155
fun reverse_greedy typing_spot_tab =
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   156
  let
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   157
    fun update_count z =
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   158
      fold (fn tvar => fn tab =>
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   159
        let val c = Vartab.lookup tab tvar |> the_default 0 in
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   160
          Vartab.update (tvar, c + z) tab
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   161
        end)
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   162
    fun superfluous tcount =
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   163
      forall (fn tvar => the (Vartab.lookup tcount tvar) > 1)
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   164
    fun drop_superfluous (tvars, (_, (_, spot))) (spots, tcount) =
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   165
      if superfluous tcount tvars then (spots, update_count ~1 tvars tcount)
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   166
      else (spot :: spots, tcount)
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   167
    val (typing_spots, tvar_count_tab) =
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   168
      Var_Set_Tab.fold
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   169
        (fn kv as (k, _) => apfst (cons kv) #> apsnd (update_count 1 k))
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   170
        typing_spot_tab ([], Vartab.empty)
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   171
      |>> sort_distinct (rev_order o cost_ord o pairself snd)
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   172
  in fold drop_superfluous typing_spots ([], tvar_count_tab) |> fst end
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   173
52452
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   174
(* (6) Introduce annotations *)
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   175
fun introduce_annotations subst spots t t' =
50258
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   176
  let
52452
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   177
    fun subst_atype (T as TVar (idxn, S)) subst =
54821
blanchet
parents: 54504
diff changeset
   178
        (Envir.subst_type subst T, Vartab.update (idxn, (S, dummyT)) subst)
52452
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   179
      | subst_atype T subst = (T, subst)
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   180
    val subst_type = fold_map_atypes subst_atype
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   181
    fun collect_annot _ T (subst, cp, ps as p :: ps', annots) =
50258
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   182
        if p <> cp then
52452
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   183
          (subst, cp + 1, ps, annots)
50258
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   184
        else
52452
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   185
          let val (T, subst) = subst_type T subst in
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   186
            (subst, cp + 1, ps', (p, T)::annots)
50258
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   187
          end
52452
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   188
      | collect_annot _ _ x = x
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   189
    val (_, _, _, annots) =
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   190
      post_fold_term_type collect_annot (subst, 0, spots, []) t'
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   191
    fun insert_annot t _ (cp, annots as (p, T) :: annots') =
54821
blanchet
parents: 54504
diff changeset
   192
        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
   193
      | insert_annot t _ x = (t, x)
52110
411db77f96f2 prevent pretty printer from automatically annotating numerals
smolkas
parents: 51877
diff changeset
   194
  in
52452
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   195
    t |> post_traverse_term_type insert_annot (0, rev annots)
52110
411db77f96f2 prevent pretty printer from automatically annotating numerals
smolkas
parents: 51877
diff changeset
   196
      |> fst
411db77f96f2 prevent pretty printer from automatically annotating numerals
smolkas
parents: 51877
diff changeset
   197
  end
50258
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   198
52452
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   199
(* (7) Annotate *)
55213
dcb36a2540bc tuned ML function names
blanchet
parents: 55212
diff changeset
   200
fun annotate_types_in_term ctxt t =
50258
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   201
  let
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   202
    val t' = generalize_types ctxt t
52452
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   203
    val subst = match_types ctxt t' t
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   204
    val (t', subst) = (t', subst) |> handle_trivial_tfrees ctxt
50258
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   205
    val typing_spots =
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   206
      t' |> typing_spot_table
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   207
         |> reverse_greedy
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   208
         |> sort int_ord
52452
2207825d67f3 ommit trivial tfrees in annotations
smolkas
parents: 52369
diff changeset
   209
  in introduce_annotations subst typing_spots t t' end
50258
1c708d7728c7 put annotate in own structure
smolkas
parents:
diff changeset
   210
54504
blanchet
parents: 52627
diff changeset
   211
end;