src/HOL/Tools/Sledgehammer/sledgehammer_annotate.ML
changeset 50258 1c708d7728c7
child 50259 9c64a52ae499
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Tools/Sledgehammer/sledgehammer_annotate.ML	Wed Nov 28 12:22:05 2012 +0100
     1.3 @@ -0,0 +1,151 @@
     1.4 +signature SLEDGEHAMMER_ANNOTATE =
     1.5 +sig
     1.6 +  val annotate_types : Proof.context -> term -> term
     1.7 +end
     1.8 +
     1.9 +structure Sledgehammer_Annotate : SLEDGEHAMMER_ANNOTATE =
    1.10 +struct
    1.11 +
    1.12 +(* Util *)
    1.13 +fun post_traverse_term_type' f _ (t as Const (_, T)) s = f t T s
    1.14 +  | post_traverse_term_type' f _ (t as Free (_, T)) s = f t T s
    1.15 +  | post_traverse_term_type' f _ (t as Var (_, T)) s = f t T s
    1.16 +  | post_traverse_term_type' f env (t as Bound i) s = f t (nth env i) s
    1.17 +  | post_traverse_term_type' f env (Abs (x, T1, b)) s =
    1.18 +    let
    1.19 +      val ((b', s'), T2) = post_traverse_term_type' f (T1 :: env) b s
    1.20 +    in f (Abs (x, T1, b')) (T1 --> T2) s' end
    1.21 +  | post_traverse_term_type' f env (u $ v) s =
    1.22 +    let
    1.23 +      val ((u', s'), Type (_, [_, T])) = post_traverse_term_type' f env u s
    1.24 +      val ((v', s''), _) = post_traverse_term_type' f env v s'
    1.25 +    in f (u' $ v') T s'' end
    1.26 +
    1.27 +fun post_traverse_term_type f s t =
    1.28 +  post_traverse_term_type' (fn t => fn T => fn s => (f t T s, T)) [] t s |> fst
    1.29 +fun post_fold_term_type f s t =
    1.30 +  post_traverse_term_type (fn t => fn T => fn s => (t, f t T s)) s t |> snd
    1.31 +
    1.32 +(* Data structures, orders *)
    1.33 +val cost_ord = prod_ord int_ord (prod_ord int_ord int_ord)
    1.34 +
    1.35 +structure Var_Set_Tab = Table(
    1.36 +  type key = indexname list
    1.37 +  val ord = list_ord Term_Ord.fast_indexname_ord)
    1.38 +
    1.39 +(* (1) Generalize types *)
    1.40 +fun generalize_types ctxt t =
    1.41 +  t |> map_types (fn _ => dummyT)
    1.42 +    |> Syntax.check_term
    1.43 +         (Proof_Context.set_mode Proof_Context.mode_pattern ctxt)
    1.44 +
    1.45 +(* (2) Typing-spot table *)
    1.46 +local
    1.47 +fun key_of_atype (TVar (z, _)) =
    1.48 +    Ord_List.insert Term_Ord.fast_indexname_ord z
    1.49 +  | key_of_atype _ = I
    1.50 +fun key_of_type T = fold_atyps key_of_atype T []
    1.51 +fun update_tab t T (tab, pos) =
    1.52 +  (case key_of_type T of
    1.53 +     [] => tab
    1.54 +   | key =>
    1.55 +     let val cost = (size_of_typ T, (size_of_term t, pos)) in
    1.56 +       case Var_Set_Tab.lookup tab key of
    1.57 +         NONE => Var_Set_Tab.update_new (key, cost) tab
    1.58 +       | SOME old_cost =>
    1.59 +         (case cost_ord (cost, old_cost) of
    1.60 +            LESS => Var_Set_Tab.update (key, cost) tab
    1.61 +          | _ => tab)
    1.62 +     end,
    1.63 +   pos + 1)
    1.64 +in
    1.65 +val typing_spot_table =
    1.66 +  post_fold_term_type update_tab (Var_Set_Tab.empty, 0) #> fst
    1.67 +end
    1.68 +
    1.69 +(* (3) Reverse-greedy *)
    1.70 +fun reverse_greedy typing_spot_tab =
    1.71 +  let
    1.72 +    fun update_count z =
    1.73 +      fold (fn tvar => fn tab =>
    1.74 +        let val c = Vartab.lookup tab tvar |> the_default 0 in
    1.75 +          Vartab.update (tvar, c + z) tab
    1.76 +        end)
    1.77 +    fun superfluous tcount =
    1.78 +      forall (fn tvar => the (Vartab.lookup tcount tvar) > 1)
    1.79 +    fun drop_superfluous (tvars, (_, (_, spot))) (spots, tcount) =
    1.80 +      if superfluous tcount tvars then (spots, update_count ~1 tvars tcount)
    1.81 +      else (spot :: spots, tcount)
    1.82 +    val (typing_spots, tvar_count_tab) =
    1.83 +      Var_Set_Tab.fold
    1.84 +        (fn kv as (k, _) => apfst (cons kv) #> apsnd (update_count 1 k))
    1.85 +        typing_spot_tab ([], Vartab.empty)
    1.86 +      |>> sort_distinct (rev_order o cost_ord o pairself snd)
    1.87 +  in fold drop_superfluous typing_spots ([], tvar_count_tab) |> fst end
    1.88 +
    1.89 +(* (4) Introduce annotations *)
    1.90 +fun introduce_annotations ctxt spots t t' =
    1.91 +  let
    1.92 +    val thy = Proof_Context.theory_of ctxt
    1.93 +    val get_types = post_fold_term_type (K cons) []
    1.94 +    fun match_types tp =
    1.95 +      fold (Sign.typ_match thy) (op ~~ (pairself get_types tp)) Vartab.empty
    1.96 +    fun unica' b x [] = if b then [x] else []
    1.97 +      | unica' b x (y :: ys) =
    1.98 +        if x = y then unica' false x ys
    1.99 +        else unica' true y ys |> b ? cons x
   1.100 +    fun unica ord xs =
   1.101 +      case sort ord xs of x :: ys => unica' true x ys | [] => []
   1.102 +    val add_all_tfree_namesT = fold_atyps (fn TFree (x, _) => cons x | _ => I)
   1.103 +    fun erase_unica_tfrees env =
   1.104 +      let
   1.105 +        val unica =
   1.106 +          Vartab.fold (add_all_tfree_namesT o snd o snd) env []
   1.107 +          |> filter_out (Variable.is_declared ctxt)
   1.108 +          |> unica fast_string_ord
   1.109 +        val erase_unica = map_atyps
   1.110 +          (fn T as TFree (s, _) =>
   1.111 +              if Ord_List.member fast_string_ord unica s then dummyT else T
   1.112 +            | T => T)
   1.113 +      in Vartab.map (K (apsnd erase_unica)) env end
   1.114 +    val env = match_types (t', t) |> erase_unica_tfrees
   1.115 +    fun get_annot env (TFree _) = (false, (env, dummyT))
   1.116 +      | get_annot env (T as TVar (v, S)) =
   1.117 +        let val T' = Envir.subst_type env T in
   1.118 +          if T' = dummyT then (false, (env, dummyT))
   1.119 +          else (true, (Vartab.update (v, (S, dummyT)) env, T'))
   1.120 +        end
   1.121 +      | get_annot env (Type (S, Ts)) =
   1.122 +        (case fold_rev (fn T => fn (b, (env, Ts)) =>
   1.123 +                  let
   1.124 +                    val (b', (env', T)) = get_annot env T
   1.125 +                  in (b orelse b', (env', T :: Ts)) end)
   1.126 +                Ts (false, (env, [])) of
   1.127 +           (true, (env', Ts)) => (true, (env', Type (S, Ts)))
   1.128 +         | (false, (env', _)) => (false, (env', dummyT)))
   1.129 +    fun post1 _ T (env, cp, ps as p :: ps', annots) =
   1.130 +        if p <> cp then
   1.131 +          (env, cp + 1, ps, annots)
   1.132 +        else
   1.133 +          let val (_, (env', T')) = get_annot env T in
   1.134 +            (env', cp + 1, ps', (p, T') :: annots)
   1.135 +          end
   1.136 +      | post1 _ _ accum = accum
   1.137 +    val (_, _, _, annots) = post_fold_term_type post1 (env, 0, spots, []) t'
   1.138 +    fun post2 t _ (cp, annots as (p, T) :: annots') =
   1.139 +        if p <> cp then (t, (cp + 1, annots))
   1.140 +        else (Type.constraint T t, (cp + 1, annots'))
   1.141 +      | post2 t _ x = (t, x)
   1.142 +  in post_traverse_term_type post2 (0, rev annots) t |> fst end
   1.143 +
   1.144 +(* (5) Annotate *)
   1.145 +fun annotate_types ctxt t =
   1.146 +  let
   1.147 +    val t' = generalize_types ctxt t
   1.148 +    val typing_spots =
   1.149 +      t' |> typing_spot_table
   1.150 +         |> reverse_greedy
   1.151 +         |> sort int_ord
   1.152 +  in introduce_annotations ctxt typing_spots t t' end
   1.153 +
   1.154 +end