src/HOL/Tools/ATP/atp_util.ML
author wenzelm
Tue Jun 06 13:13:25 2017 +0200 (2017-06-06)
changeset 66020 a31760eee09d
parent 61770 a20048c78891
child 67522 9e712280cc37
permissions -rw-r--r--
discontinued obsolete print mode;
blanchet@43085
     1
(*  Title:      HOL/Tools/ATP/atp_util.ML
blanchet@43085
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@43085
     3
blanchet@43085
     4
General-purpose functions used by the ATP module.
blanchet@43085
     5
*)
blanchet@43085
     6
blanchet@43085
     7
signature ATP_UTIL =
blanchet@43085
     8
sig
blanchet@43085
     9
  val timestamp : unit -> string
blanchet@53514
    10
  val hashw : word * word -> word
blanchet@53514
    11
  val hashw_string : string * word -> word
blanchet@43827
    12
  val hash_string : string -> int
blanchet@48323
    13
  val chunk_list : int -> 'a list -> 'a list list
blanchet@48251
    14
  val stringN_of_int : int -> int -> string
blanchet@43085
    15
  val strip_spaces : bool -> (char -> bool) -> string -> string
blanchet@44784
    16
  val strip_spaces_except_between_idents : string -> string
blanchet@48316
    17
  val elide_string : int -> string -> string
blanchet@52077
    18
  val find_enclosed : string -> string -> string -> string list
blanchet@43085
    19
  val nat_subscript : int -> string
blanchet@52076
    20
  val unquote_tvar : string -> string
wenzelm@58928
    21
  val maybe_quote : Keyword.keywords -> string -> string
blanchet@52031
    22
  val string_of_ext_time : bool * Time.time -> string
blanchet@52031
    23
  val string_of_time : Time.time -> string
blanchet@47150
    24
  val type_instance : theory -> typ -> typ -> bool
blanchet@47150
    25
  val type_generalization : theory -> typ -> typ -> bool
blanchet@47150
    26
  val type_intersect : theory -> typ -> typ -> bool
blanchet@47150
    27
  val type_equiv : theory -> typ * typ -> bool
blanchet@43085
    28
  val varify_type : Proof.context -> typ -> typ
blanchet@43085
    29
  val instantiate_type : theory -> typ -> typ -> typ -> typ
blanchet@43085
    30
  val varify_and_instantiate_type : Proof.context -> typ -> typ -> typ -> typ
blanchet@44393
    31
  val is_type_surely_finite : Proof.context -> typ -> bool
blanchet@44399
    32
  val is_type_surely_infinite : Proof.context -> bool -> typ list -> typ -> bool
blanchet@43863
    33
  val s_not : term -> term
blanchet@43863
    34
  val s_conj : term * term -> term
blanchet@43863
    35
  val s_disj : term * term -> term
blanchet@43863
    36
  val s_imp : term * term -> term
blanchet@43863
    37
  val s_iff : term * term -> term
blanchet@49983
    38
  val close_form : term -> term
blanchet@49982
    39
  val hol_close_form_prefix : string
blanchet@49982
    40
  val hol_close_form : term -> term
blanchet@49982
    41
  val hol_open_form : (string -> string) -> term -> term
blanchet@43085
    42
  val eta_expand : typ list -> term -> int -> term
wenzelm@59632
    43
  val cong_extensionalize_term : Proof.context -> term -> term
blanchet@47953
    44
  val abs_extensionalize_term : Proof.context -> term -> term
blanchet@47991
    45
  val unextensionalize_def : term -> term
blanchet@43085
    46
  val transform_elim_prop : term -> term
blanchet@43085
    47
  val specialize_type : theory -> (string * typ) -> term -> term
blanchet@56104
    48
  val strip_subgoal : thm -> int -> Proof.context -> (string * typ) list * term list * term
blanchet@57541
    49
  val extract_lambda_def : (term -> string * typ) -> term -> string * term
blanchet@56104
    50
  val short_thm_name : Proof.context -> thm -> string
blanchet@43085
    51
end;
blanchet@43085
    52
blanchet@43085
    53
structure ATP_Util : ATP_UTIL =
blanchet@43085
    54
struct
blanchet@43085
    55
blanchet@53800
    56
fun timestamp_format time =
blanchet@53800
    57
  Date.fmt "%Y-%m-%d %H:%M:%S." (Date.fromTimeLocal time) ^
blanchet@53800
    58
  (StringCvt.padLeft #"0" 3 (string_of_int (Time.toMilliseconds time - 1000 * Time.toSeconds time)))
blanchet@53800
    59
blanchet@53800
    60
val timestamp = timestamp_format o Time.now
blanchet@43085
    61
blanchet@43085
    62
(* This hash function is recommended in "Compilers: Principles, Techniques, and
blanchet@43085
    63
   Tools" by Aho, Sethi, and Ullman. The "hashpjw" function, which they
blanchet@43085
    64
   particularly recommend, triggers a bug in versions of Poly/ML up to 4.2.0. *)
blanchet@43085
    65
fun hashw (u, w) = Word.+ (u, Word.* (0w65599, w))
blanchet@43085
    66
fun hashw_char (c, w) = hashw (Word.fromInt (Char.ord c), w)
blanchet@43085
    67
fun hashw_string (s : string, w) = CharVector.foldl hashw_char w s
blanchet@43827
    68
fun hash_string s = Word.toInt (hashw_string (s, 0w0))
blanchet@43085
    69
blanchet@48323
    70
fun chunk_list _ [] = []
blanchet@48323
    71
  | chunk_list k xs =
blanchet@48323
    72
    let val (xs1, xs2) = chop k xs in xs1 :: chunk_list k xs2 end
blanchet@48323
    73
blanchet@48251
    74
fun stringN_of_int 0 _ = ""
blanchet@48251
    75
  | stringN_of_int k n =
blanchet@48251
    76
    stringN_of_int (k - 1) (n div 10) ^ string_of_int (n mod 10)
blanchet@48251
    77
blanchet@57728
    78
fun is_spaceish c = Char.isSpace c orelse c = #"\127" (* DEL -- no idea where these come from *)
blanchet@57728
    79
blanchet@43085
    80
fun strip_spaces skip_comments is_evil =
blanchet@44935
    81
  let
blanchet@44935
    82
    fun strip_c_style_comment [] accum = accum
blanchet@57728
    83
      | strip_c_style_comment (#"*" :: #"/" :: cs) accum = strip_spaces_in_list true cs accum
blanchet@44935
    84
      | strip_c_style_comment (_ :: cs) accum = strip_c_style_comment cs accum
blanchet@48766
    85
    and strip_spaces_in_list _ [] accum = accum
blanchet@44935
    86
      | strip_spaces_in_list true (#"%" :: cs) accum =
blanchet@57728
    87
        strip_spaces_in_list true (cs |> take_prefix (not_equal #"\n") |> snd) accum
blanchet@57728
    88
      | strip_spaces_in_list true (#"/" :: #"*" :: cs) accum = strip_c_style_comment cs accum
blanchet@57728
    89
      | strip_spaces_in_list _ [c1] accum = accum |> not (is_spaceish c1) ? cons c1
blanchet@44935
    90
      | strip_spaces_in_list skip_comments (cs as [_, _]) accum =
blanchet@44935
    91
        accum |> fold (strip_spaces_in_list skip_comments o single) cs
blanchet@44935
    92
      | strip_spaces_in_list skip_comments (c1 :: c2 :: c3 :: cs) accum =
blanchet@57728
    93
        if is_spaceish c1 then
blanchet@44935
    94
          strip_spaces_in_list skip_comments (c2 :: c3 :: cs) accum
blanchet@57728
    95
        else if is_spaceish c2 then
blanchet@57728
    96
          if is_spaceish c3 then
blanchet@44935
    97
            strip_spaces_in_list skip_comments (c1 :: c3 :: cs) accum
blanchet@44935
    98
          else
blanchet@44935
    99
            strip_spaces_in_list skip_comments (c3 :: cs)
blanchet@57728
   100
              (c1 :: accum |> forall is_evil [c1, c3] ? cons #" ")
blanchet@44935
   101
        else
blanchet@44935
   102
          strip_spaces_in_list skip_comments (c2 :: c3 :: cs) (cons c1 accum)
blanchet@44935
   103
  in
blanchet@44935
   104
    String.explode
blanchet@44935
   105
    #> rpair [] #-> strip_spaces_in_list skip_comments
blanchet@44935
   106
    #> rev #> String.implode
blanchet@44935
   107
  end
blanchet@43085
   108
blanchet@44784
   109
fun is_ident_char c = Char.isAlphaNum c orelse c = #"_"
blanchet@44784
   110
val strip_spaces_except_between_idents = strip_spaces true is_ident_char
blanchet@44784
   111
blanchet@48316
   112
fun elide_string threshold s =
blanchet@48316
   113
  if size s > threshold then
blanchet@48316
   114
    String.extract (s, 0, SOME (threshold div 2 - 5)) ^ " ...... " ^
blanchet@48316
   115
    String.extract (s, size s - (threshold + 1) div 2 + 6, NONE)
blanchet@48316
   116
  else
blanchet@48316
   117
    s
blanchet@48316
   118
blanchet@52077
   119
fun find_enclosed left right s =
blanchet@52077
   120
  case first_field left s of
blanchet@52077
   121
    SOME (_, s) =>
blanchet@52077
   122
    (case first_field right s of
blanchet@52077
   123
       SOME (enclosed, s) => enclosed :: find_enclosed left right s
blanchet@52077
   124
     | NONE => [])
blanchet@52077
   125
  | NONE => []
blanchet@52077
   126
wenzelm@53015
   127
val subscript = implode o map (prefix "\<^sub>") o raw_explode  (* FIXME Symbol.explode (?) *)
blanchet@43085
   128
fun nat_subscript n =
wenzelm@66020
   129
  n |> string_of_int |> not (print_mode_active Print_Mode.ASCII) ? subscript
blanchet@43085
   130
blanchet@52076
   131
val unquote_tvar = perhaps (try (unprefix "'"))
blanchet@52076
   132
val unquery_var = perhaps (try (unprefix "?"))
blanchet@52076
   133
wenzelm@50239
   134
val is_long_identifier = forall Symbol_Pos.is_identifier o Long_Name.explode
wenzelm@58928
   135
fun maybe_quote keywords y =
wenzelm@59433
   136
  let val s = YXML.content_of y in
blanchet@52076
   137
    y |> ((not (is_long_identifier (unquote_tvar s)) andalso
blanchet@52076
   138
           not (is_long_identifier (unquery_var s))) orelse
wenzelm@58928
   139
           Keyword.is_literal keywords s) ? quote
blanchet@43085
   140
  end
blanchet@43085
   141
blanchet@52031
   142
fun string_of_ext_time (plus, time) =
blanchet@58081
   143
  let val us = Time.toMicroseconds time in
blanchet@43085
   144
    (if plus then "> " else "") ^
blanchet@58088
   145
    (if us < 1000 then string_of_real (Real.fromInt (us div 100) / 10.0) ^ " ms"
blanchet@58081
   146
     else if us < 1000000 then signed_string_of_int (us div 1000) ^ " ms"
blanchet@58088
   147
     else string_of_real (Real.fromInt (us div 100000) / 10.0) ^ " s")
blanchet@43085
   148
  end
blanchet@43085
   149
blanchet@52031
   150
val string_of_time = string_of_ext_time o pair false
blanchet@43085
   151
blanchet@47150
   152
fun type_instance thy T T' = Sign.typ_instance thy (T, T')
blanchet@47150
   153
fun type_generalization thy T T' = Sign.typ_instance thy (T', T)
blanchet@48247
   154
blanchet@48247
   155
fun type_intersect _ (TVar _) _ = true
blanchet@48247
   156
  | type_intersect _ _ (TVar _) = true
blanchet@48247
   157
  | type_intersect thy T T' =
blanchet@48247
   158
    let
blanchet@48247
   159
      val tvars = Term.add_tvar_namesT T []
blanchet@48247
   160
      val tvars' = Term.add_tvar_namesT T' []
blanchet@50968
   161
      val maxidx' = maxidx_of_typ T'
blanchet@48247
   162
      val T =
blanchet@50968
   163
        T |> exists (member (op =) tvars') tvars ? Logic.incr_tvar (maxidx' + 1)
blanchet@50968
   164
      val maxidx = Integer.max (maxidx_of_typ T) maxidx'
blanchet@50968
   165
    in can (Sign.typ_unify thy (T, T')) (Vartab.empty, maxidx) end
blanchet@48247
   166
blanchet@47150
   167
val type_equiv = Sign.typ_equiv
blanchet@44399
   168
blanchet@43085
   169
fun varify_type ctxt T =
blanchet@43085
   170
  Variable.polymorphic_types ctxt [Const (@{const_name undefined}, T)]
blanchet@43085
   171
  |> snd |> the_single |> dest_Const |> snd
blanchet@43085
   172
blanchet@43085
   173
(* TODO: use "Term_Subst.instantiateT" instead? *)
blanchet@43085
   174
fun instantiate_type thy T1 T1' T2 =
blanchet@43085
   175
  Same.commit (Envir.subst_type_same
blanchet@43085
   176
                   (Sign.typ_match thy (T1, T1') Vartab.empty)) T2
blanchet@43085
   177
  handle Type.TYPE_MATCH => raise TYPE ("instantiate_type", [T1, T1'], [])
blanchet@43085
   178
blanchet@43085
   179
fun varify_and_instantiate_type ctxt T1 T1' T2 =
blanchet@43085
   180
  let val thy = Proof_Context.theory_of ctxt in
blanchet@43085
   181
    instantiate_type thy (varify_type ctxt T1) T1' (varify_type ctxt T2)
blanchet@43085
   182
  end
blanchet@43085
   183
blanchet@54554
   184
fun free_constructors_of ctxt (Type (s, Ts)) =
blanchet@54554
   185
    (case Ctr_Sugar.ctr_sugar_of ctxt s of
blanchet@54554
   186
      SOME {ctrs, ...} => map_filter (try dest_Const o Ctr_Sugar.mk_ctr Ts) ctrs
blanchet@54554
   187
    | NONE => [])
blanchet@54554
   188
  | free_constructors_of _ _ = []
blanchet@43085
   189
blanchet@43085
   190
(* Similar to "Nitpick_HOL.bounded_exact_card_of_type".
blanchet@43085
   191
   0 means infinite type, 1 means singleton type (e.g., "unit"), and 2 means
blanchet@43085
   192
   cardinality 2 or more. The specified default cardinality is returned if the
blanchet@43085
   193
   cardinality of the type can't be determined. *)
blanchet@44500
   194
fun tiny_card_of_type ctxt sound assigns default_card T =
blanchet@43085
   195
  let
blanchet@43085
   196
    val thy = Proof_Context.theory_of ctxt
blanchet@43085
   197
    val max = 2 (* 1 would be too small for the "fun" case *)
blanchet@43085
   198
    fun aux slack avoid T =
blanchet@43085
   199
      if member (op =) avoid T then
blanchet@43085
   200
        0
blanchet@47150
   201
      else case AList.lookup (type_equiv thy) assigns T of
blanchet@44393
   202
        SOME k => k
blanchet@44393
   203
      | NONE =>
blanchet@44392
   204
        case T of
blanchet@44392
   205
          Type (@{type_name fun}, [T1, T2]) =>
blanchet@44392
   206
          (case (aux slack avoid T1, aux slack avoid T2) of
blanchet@44392
   207
             (k, 1) => if slack andalso k = 0 then 0 else 1
blanchet@44392
   208
           | (0, _) => 0
blanchet@44392
   209
           | (_, 0) => 0
blanchet@44392
   210
           | (k1, k2) =>
blanchet@44392
   211
             if k1 >= max orelse k2 >= max then max
blanchet@44392
   212
             else Int.min (max, Integer.pow k2 k1))
blanchet@48230
   213
        | Type (@{type_name set}, [T']) => aux slack avoid (T' --> @{typ bool})
blanchet@44392
   214
        | @{typ prop} => 2
blanchet@44392
   215
        | @{typ bool} => 2 (* optimization *)
blanchet@44392
   216
        | @{typ nat} => 0 (* optimization *)
blanchet@44392
   217
        | Type ("Int.int", []) => 0 (* optimization *)
blanchet@44392
   218
        | Type (s, _) =>
blanchet@54554
   219
          (case free_constructors_of ctxt T of
blanchet@44392
   220
             constrs as _ :: _ =>
blanchet@44392
   221
             let
blanchet@44392
   222
               val constr_cards =
blanchet@54554
   223
                 map (Integer.prod o map (aux slack (T :: avoid)) o binder_types o snd) constrs
blanchet@44392
   224
             in
blanchet@44392
   225
               if exists (curry (op =) 0) constr_cards then 0
blanchet@44392
   226
               else Int.min (max, Integer.sum constr_cards)
blanchet@44392
   227
             end
blanchet@44392
   228
           | [] =>
blanchet@44392
   229
             case Typedef.get_info ctxt s of
blanchet@44392
   230
               ({abs_type, rep_type, ...}, _) :: _ =>
blanchet@45299
   231
               if not sound then
blanchet@45299
   232
                 (* We cheat here by assuming that typedef types are infinite if
blanchet@45299
   233
                    their underlying type is infinite. This is unsound in
blanchet@45299
   234
                    general but it's hard to think of a realistic example where
blanchet@45299
   235
                    this would not be the case. We are also slack with
blanchet@45299
   236
                    representation types: If a representation type has the form
blanchet@45299
   237
                    "sigma => tau", we consider it enough to check "sigma" for
blanchet@45299
   238
                    infiniteness. *)
blanchet@45299
   239
                 (case varify_and_instantiate_type ctxt
blanchet@45299
   240
                           (Logic.varifyT_global abs_type) T
blanchet@45299
   241
                           (Logic.varifyT_global rep_type)
blanchet@45299
   242
                       |> aux true avoid of
blanchet@45299
   243
                    0 => 0
blanchet@45299
   244
                  | 1 => 1
blanchet@45299
   245
                  | _ => default_card)
blanchet@45299
   246
               else
blanchet@45299
   247
                 default_card
blanchet@44392
   248
             | [] => default_card)
blanchet@54554
   249
        | TFree _ =>
blanchet@44392
   250
          (* Very slightly unsound: Type variables are assumed not to be
blanchet@44392
   251
             constrained to cardinality 1. (In practice, the user would most
blanchet@44392
   252
             likely have used "unit" directly anyway.) *)
blanchet@44500
   253
          if not sound andalso default_card = 1 then 2 else default_card
blanchet@44392
   254
        | TVar _ => default_card
blanchet@43085
   255
  in Int.min (max, aux false [] T) end
blanchet@43085
   256
blanchet@44500
   257
fun is_type_surely_finite ctxt T = tiny_card_of_type ctxt true [] 0 T <> 0
blanchet@44500
   258
fun is_type_surely_infinite ctxt sound infinite_Ts T =
blanchet@44500
   259
  tiny_card_of_type ctxt sound (map (rpair 0) infinite_Ts) 1 T = 0
blanchet@43085
   260
blanchet@43863
   261
(* Simple simplifications to ensure that sort annotations don't leave a trail of
blanchet@43863
   262
   spurious "True"s. *)
blanchet@43863
   263
fun s_not (Const (@{const_name All}, T) $ Abs (s, T', t')) =
blanchet@43863
   264
    Const (@{const_name Ex}, T) $ Abs (s, T', s_not t')
blanchet@43863
   265
  | s_not (Const (@{const_name Ex}, T) $ Abs (s, T', t')) =
blanchet@43863
   266
    Const (@{const_name All}, T) $ Abs (s, T', s_not t')
blanchet@43863
   267
  | s_not (@{const HOL.implies} $ t1 $ t2) = @{const HOL.conj} $ t1 $ s_not t2
blanchet@43863
   268
  | s_not (@{const HOL.conj} $ t1 $ t2) =
blanchet@43863
   269
    @{const HOL.disj} $ s_not t1 $ s_not t2
blanchet@43863
   270
  | s_not (@{const HOL.disj} $ t1 $ t2) =
blanchet@43863
   271
    @{const HOL.conj} $ s_not t1 $ s_not t2
blanchet@43863
   272
  | s_not (@{const False}) = @{const True}
blanchet@43863
   273
  | s_not (@{const True}) = @{const False}
blanchet@43863
   274
  | s_not (@{const Not} $ t) = t
blanchet@43863
   275
  | s_not t = @{const Not} $ t
blanchet@54758
   276
blanchet@43863
   277
fun s_conj (@{const True}, t2) = t2
blanchet@43863
   278
  | s_conj (t1, @{const True}) = t1
blanchet@51209
   279
  | s_conj (@{const False}, _) = @{const False}
blanchet@51209
   280
  | s_conj (_, @{const False}) = @{const False}
blanchet@51197
   281
  | s_conj (t1, t2) = if t1 aconv t2 then t1 else HOLogic.mk_conj (t1, t2)
blanchet@54758
   282
blanchet@43863
   283
fun s_disj (@{const False}, t2) = t2
blanchet@43863
   284
  | s_disj (t1, @{const False}) = t1
blanchet@51209
   285
  | s_disj (@{const True}, _) = @{const True}
blanchet@51209
   286
  | s_disj (_, @{const True}) = @{const True}
blanchet@51197
   287
  | s_disj (t1, t2) = if t1 aconv t2 then t1 else HOLogic.mk_disj (t1, t2)
blanchet@54757
   288
blanchet@43863
   289
fun s_imp (@{const True}, t2) = t2
blanchet@43863
   290
  | s_imp (t1, @{const False}) = s_not t1
blanchet@51209
   291
  | s_imp (@{const False}, _) = @{const True}
blanchet@51209
   292
  | s_imp (_, @{const True}) = @{const True}
blanchet@43863
   293
  | s_imp p = HOLogic.mk_imp p
blanchet@54757
   294
blanchet@43863
   295
fun s_iff (@{const True}, t2) = t2
blanchet@43863
   296
  | s_iff (t1, @{const True}) = t1
blanchet@51209
   297
  | s_iff (@{const False}, t2) = s_not t2
blanchet@51209
   298
  | s_iff (t1, @{const False}) = s_not t1
blanchet@56126
   299
  | s_iff (t1, t2) = if t1 aconv t2 then @{const True} else HOLogic.eq_const HOLogic.boolT $ t1 $ t2
blanchet@43863
   300
blanchet@49983
   301
fun close_form t =
blanchet@49983
   302
  fold (fn ((s, i), T) => fn t' =>
blanchet@54757
   303
      Logic.all_const T $ Abs (s, T, abstract_over (Var ((s, i), T), t')))
blanchet@54757
   304
    (Term.add_vars t []) t
blanchet@49983
   305
blanchet@58091
   306
val hol_close_form_prefix = "ATP."
blanchet@46385
   307
blanchet@49982
   308
fun hol_close_form t =
blanchet@45570
   309
  fold (fn ((s, i), T) => fn t' =>
blanchet@45511
   310
           HOLogic.all_const T
blanchet@49982
   311
           $ Abs (hol_close_form_prefix ^ s, T,
blanchet@46385
   312
                  abstract_over (Var ((s, i), T), t')))
blanchet@43864
   313
       (Term.add_vars t []) t
blanchet@43864
   314
blanchet@49982
   315
fun hol_open_form unprefix
blanchet@49982
   316
      (t as Const (@{const_name All}, _) $ Abs (s, T, t')) =
blanchet@47718
   317
    (case try unprefix s of
blanchet@47718
   318
       SOME s =>
blanchet@47718
   319
       let
blanchet@47718
   320
         val names = Name.make_context (map fst (Term.add_var_names t' []))
blanchet@47718
   321
         val (s, _) = Name.variant s names
blanchet@49982
   322
       in hol_open_form unprefix (subst_bound (Var ((s, 0), T), t')) end
blanchet@47718
   323
     | NONE => t)
blanchet@49982
   324
  | hol_open_form _ t = t
blanchet@47718
   325
blanchet@43085
   326
fun eta_expand _ t 0 = t
blanchet@43085
   327
  | eta_expand Ts (Abs (s, T, t')) n =
blanchet@43085
   328
    Abs (s, T, eta_expand (T :: Ts) t' (n - 1))
blanchet@43085
   329
  | eta_expand Ts t n =
blanchet@43085
   330
    fold_rev (fn T => fn t' => Abs ("x" ^ nat_subscript n, T, t'))
blanchet@43085
   331
             (List.take (binder_types (fastype_of1 (Ts, t)), n))
blanchet@43085
   332
             (list_comb (incr_boundvars n t, map Bound (n - 1 downto 0)))
blanchet@43085
   333
wenzelm@59632
   334
fun cong_extensionalize_term ctxt t =
blanchet@47954
   335
  if exists_Const (fn (s, _) => s = @{const_name Not}) t then
wenzelm@59632
   336
    t |> Skip_Proof.make_thm (Proof_Context.theory_of ctxt)
wenzelm@59632
   337
      |> Meson.cong_extensionalize_thm ctxt
wenzelm@59582
   338
      |> Thm.prop_of
blanchet@47954
   339
  else
blanchet@47954
   340
    t
blanchet@47954
   341
blanchet@47715
   342
fun is_fun_equality (@{const_name HOL.eq},
blanchet@47715
   343
                     Type (_, [Type (@{type_name fun}, _), _])) = true
blanchet@47715
   344
  | is_fun_equality _ = false
blanchet@47715
   345
blanchet@47953
   346
fun abs_extensionalize_term ctxt t =
blanchet@47715
   347
  if exists_Const is_fun_equality t then
wenzelm@59632
   348
    t |> Thm.cterm_of ctxt |> Meson.abs_extensionalize_conv ctxt
wenzelm@59632
   349
      |> Thm.prop_of |> Logic.dest_equals |> snd
blanchet@47715
   350
  else
blanchet@47715
   351
    t
blanchet@47715
   352
blanchet@47991
   353
fun unextensionalize_def t =
blanchet@47991
   354
  case t of
blanchet@47991
   355
    @{const Trueprop} $ (Const (@{const_name HOL.eq}, _) $ lhs $ rhs) =>
blanchet@47991
   356
    (case strip_comb lhs of
blanchet@47991
   357
       (c as Const (_, T), args) =>
blanchet@47991
   358
       if forall is_Var args andalso not (has_duplicates (op =) args) then
blanchet@47991
   359
         @{const Trueprop}
blanchet@47991
   360
         $ (Const (@{const_name HOL.eq}, T --> T --> @{typ bool})
blanchet@47991
   361
            $ c $ fold_rev lambda args rhs)
blanchet@47991
   362
       else
blanchet@47991
   363
         t
blanchet@47991
   364
     | _ => t)
blanchet@47991
   365
  | _ => t
blanchet@47991
   366
blanchet@43085
   367
(* Converts an elim-rule into an equivalent theorem that does not have the
blanchet@43085
   368
   predicate variable. Leaves other theorems unchanged. We simply instantiate
blanchet@44460
   369
   the conclusion variable to "False". (Cf. "transform_elim_theorem" in
blanchet@43085
   370
   "Meson_Clausify".) *)
blanchet@43085
   371
fun transform_elim_prop t =
blanchet@43085
   372
  case Logic.strip_imp_concl t of
blanchet@43085
   373
    @{const Trueprop} $ Var (z, @{typ bool}) =>
blanchet@43085
   374
    subst_Vars [(z, @{const False})] t
blanchet@43085
   375
  | Var (z, @{typ prop}) => subst_Vars [(z, @{prop False})] t
blanchet@43085
   376
  | _ => t
blanchet@43085
   377
blanchet@43085
   378
fun specialize_type thy (s, T) t =
blanchet@43085
   379
  let
blanchet@43085
   380
    fun subst_for (Const (s', T')) =
blanchet@61769
   381
        if s = s' then
blanchet@61769
   382
          SOME (Sign.typ_match thy (T', T) Vartab.empty)
blanchet@61769
   383
          handle Type.TYPE_MATCH => NONE
blanchet@61769
   384
        else
blanchet@61769
   385
          NONE
blanchet@61769
   386
      | subst_for (t1 $ t2) = (case subst_for t1 of SOME x => SOME x | NONE => subst_for t2)
blanchet@61769
   387
      | subst_for (Abs (_, _, t')) = subst_for t'
blanchet@61769
   388
      | subst_for _ = NONE
blanchet@43085
   389
  in
blanchet@61769
   390
    (case subst_for t of
blanchet@61770
   391
      SOME subst => Envir.subst_term_types subst t
blanchet@61769
   392
    | NONE => raise Type.TYPE_MATCH)
blanchet@43085
   393
  end
blanchet@43085
   394
blanchet@52125
   395
fun strip_subgoal goal i ctxt =
blanchet@43085
   396
  let
blanchet@52196
   397
    val (t, (frees, params)) =
wenzelm@59582
   398
      Logic.goal_params (Thm.prop_of goal) i
blanchet@52196
   399
      ||> (map dest_Free #> Variable.variant_frees ctxt [] #> `(map Free))
blanchet@43085
   400
    val hyp_ts = t |> Logic.strip_assums_hyp |> map (curry subst_bounds frees)
blanchet@43085
   401
    val concl_t = t |> Logic.strip_assums_concl |> curry subst_bounds frees
blanchet@52196
   402
  in (rev params, hyp_ts, concl_t) end
blanchet@43085
   403
blanchet@57541
   404
fun extract_lambda_def dest_head (Const (@{const_name HOL.eq}, _) $ t $ u) =
blanchet@57541
   405
    let val (head, args) = strip_comb t in
blanchet@57541
   406
      (head |> dest_head |> fst,
blanchet@57541
   407
       fold_rev (fn t as Var ((s, _), T) =>
blanchet@57541
   408
                    (fn u => Abs (s, T, abstract_over (t, u)))
blanchet@57541
   409
                  | _ => raise Fail "expected \"Var\"") args u)
blanchet@57541
   410
    end
blanchet@57541
   411
  | extract_lambda_def _ _ = raise Fail "malformed lifted lambda"
blanchet@57541
   412
blanchet@56104
   413
fun short_thm_name ctxt th =
blanchet@56104
   414
  let
blanchet@56104
   415
    val long = Thm.get_name_hint th
blanchet@56104
   416
    val short = Long_Name.base_name long
blanchet@56104
   417
  in
blanchet@56104
   418
    if Thm.eq_thm_prop (th, singleton (Attrib.eval_thms ctxt) (Facts.named short, [])) then short
blanchet@56104
   419
    else long
blanchet@56104
   420
  end
blanchet@56104
   421
blanchet@43085
   422
end;