src/HOL/Tools/Sledgehammer/sledgehammer_util.ML
author blanchet
Thu, 05 May 2011 12:40:48 +0200
changeset 43567 9bc5dc48f1a5
parent 43550 b6c27cf04fe9
child 43595 d6db5a815477
permissions -rw-r--r--
query typedefs as well for monotonicity
blanchet@36062
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_util.ML
blanchet@35961
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@35961
     3
blanchet@35961
     4
General-purpose functions used by the Sledgehammer modules.
blanchet@35961
     5
*)
blanchet@35961
     6
blanchet@35961
     7
signature SLEDGEHAMMER_UTIL =
blanchet@35961
     8
sig
blanchet@36140
     9
  val plural_s : int -> string
blanchet@35961
    10
  val serial_commas : string -> string list -> string list
blanchet@38977
    11
  val simplify_spaces : string -> string
blanchet@35961
    12
  val parse_bool_option : bool -> string -> string -> bool option
blanchet@35961
    13
  val parse_time_option : string -> string -> Time.time option
blanchet@40582
    14
  val string_from_time : Time.time -> string
blanchet@36482
    15
  val nat_subscript : int -> string
blanchet@36474
    16
  val unyxml : string -> string
blanchet@36474
    17
  val maybe_quote : string -> string
blanchet@43550
    18
  val typ_of_dtyp :
blanchet@43550
    19
    Datatype_Aux.descr -> (Datatype_Aux.dtyp * typ) list -> Datatype_Aux.dtyp
blanchet@43550
    20
    -> typ
blanchet@43567
    21
  val varify_type : Proof.context -> typ -> typ
blanchet@43567
    22
  val instantiate_type : theory -> typ -> typ -> typ -> typ
blanchet@43567
    23
  val varify_and_instantiate_type : Proof.context -> typ -> typ -> typ -> typ
blanchet@36553
    24
  val monomorphic_term : Type.tyenv -> term -> term
blanchet@38890
    25
  val eta_expand : typ list -> term -> int -> term
blanchet@38890
    26
  val transform_elim_term : term -> term
blanchet@36553
    27
  val specialize_type : theory -> (string * typ) -> term -> term
blanchet@38290
    28
  val subgoal_count : Proof.state -> int
blanchet@38229
    29
  val strip_subgoal : thm -> int -> (string * typ) list * term list * term
blanchet@38935
    30
  val reserved_isar_keyword_table : unit -> unit Symtab.table
blanchet@35961
    31
end;
blanchet@39564
    32
blanchet@35961
    33
structure Sledgehammer_Util : SLEDGEHAMMER_UTIL =
blanchet@35961
    34
struct
blanchet@35961
    35
blanchet@36140
    36
fun plural_s n = if n = 1 then "" else "s"
blanchet@36062
    37
blanchet@35961
    38
fun serial_commas _ [] = ["??"]
blanchet@35961
    39
  | serial_commas _ [s] = [s]
blanchet@35961
    40
  | serial_commas conj [s1, s2] = [s1, conj, s2]
blanchet@35961
    41
  | serial_commas conj [s1, s2, s3] = [s1 ^ ",", s2 ^ ",", conj, s3]
blanchet@35961
    42
  | serial_commas conj (s :: ss) = s ^ "," :: serial_commas conj ss
blanchet@35961
    43
blanchet@39697
    44
val simplify_spaces = ATP_Proof.strip_spaces (K true)
blanchet@38199
    45
blanchet@35961
    46
fun parse_bool_option option name s =
blanchet@35961
    47
  (case s of
blanchet@35961
    48
     "smart" => if option then NONE else raise Option
blanchet@35961
    49
   | "false" => SOME false
blanchet@35961
    50
   | "true" => SOME true
blanchet@35961
    51
   | "" => SOME true
blanchet@35961
    52
   | _ => raise Option)
blanchet@35961
    53
  handle Option.Option =>
blanchet@35961
    54
         let val ss = map quote ((option ? cons "smart") ["true", "false"]) in
blanchet@35961
    55
           error ("Parameter " ^ quote name ^ " must be assigned " ^
blanchet@35961
    56
                  space_implode " " (serial_commas "or" ss) ^ ".")
blanchet@35961
    57
         end
blanchet@35961
    58
blanchet@40582
    59
val has_junk =
wenzelm@40875
    60
  exists (fn s => not (Symbol.is_digit s) andalso s <> ".") o raw_explode (* FIXME Symbol.explode (?) *)
blanchet@40582
    61
blanchet@35961
    62
fun parse_time_option _ "none" = NONE
blanchet@35961
    63
  | parse_time_option name s =
blanchet@40582
    64
    let val secs = if has_junk s then NONE else Real.fromString s in
blanchet@40582
    65
      if is_none secs orelse Real.<= (the secs, 0.0) then
blanchet@40582
    66
        error ("Parameter " ^ quote name ^ " must be assigned a positive \
blanchet@40582
    67
               \number of seconds (e.g., \"60\", \"0.5\") or \"none\".")
blanchet@35961
    68
      else
blanchet@40582
    69
        SOME (seconds (the secs))
blanchet@35961
    70
    end
blanchet@35961
    71
blanchet@40582
    72
fun string_from_time t =
blanchet@40582
    73
  string_of_real (0.01 * Real.fromInt (Time.toMilliseconds t div 10)) ^ " s"
blanchet@40582
    74
wenzelm@40875
    75
val subscript = implode o map (prefix "\<^isub>") o raw_explode  (* FIXME Symbol.explode (?) *)
blanchet@37318
    76
fun nat_subscript n =
blanchet@37318
    77
  n |> string_of_int |> print_mode_active Symbol.xsymbolsN ? subscript
blanchet@36482
    78
wenzelm@39812
    79
val unyxml = XML.content_of o YXML.parse_body
blanchet@36474
    80
wenzelm@43162
    81
val is_long_identifier = forall Lexicon.is_identifier o space_explode "."
blanchet@36474
    82
fun maybe_quote y =
blanchet@36474
    83
  let val s = unyxml y in
blanchet@36474
    84
    y |> ((not (is_long_identifier (perhaps (try (unprefix "'")) s)) andalso
blanchet@36474
    85
           not (is_long_identifier (perhaps (try (unprefix "?")) s))) orelse
wenzelm@36970
    86
           Keyword.is_keyword s) ? quote
blanchet@36474
    87
  end
blanchet@36474
    88
blanchet@43550
    89
fun typ_of_dtyp _ typ_assoc (Datatype_Aux.DtTFree a) =
blanchet@43550
    90
    the (AList.lookup (op =) typ_assoc (Datatype_Aux.DtTFree a))
blanchet@43550
    91
  | typ_of_dtyp descr typ_assoc (Datatype_Aux.DtType (s, Us)) =
blanchet@43550
    92
    Type (s, map (typ_of_dtyp descr typ_assoc) Us)
blanchet@43550
    93
  | typ_of_dtyp descr typ_assoc (Datatype_Aux.DtRec i) =
blanchet@43550
    94
    let val (s, ds, _) = the (AList.lookup (op =) descr i) in
blanchet@43550
    95
      Type (s, map (typ_of_dtyp descr typ_assoc) ds)
blanchet@43550
    96
    end
blanchet@43550
    97
blanchet@43567
    98
fun varify_type ctxt T =
blanchet@43567
    99
  Variable.polymorphic_types ctxt [Const (@{const_name undefined}, T)]
blanchet@43567
   100
  |> snd |> the_single |> dest_Const |> snd
blanchet@43567
   101
blanchet@43567
   102
(* TODO: use "Term_Subst.instantiateT" instead? *)
blanchet@43567
   103
fun instantiate_type thy T1 T1' T2 =
blanchet@43567
   104
  Same.commit (Envir.subst_type_same
blanchet@43567
   105
                   (Sign.typ_match thy (T1, T1') Vartab.empty)) T2
blanchet@43567
   106
  handle Type.TYPE_MATCH => raise TYPE ("instantiate_type", [T1, T1'], [])
blanchet@43567
   107
blanchet@43567
   108
fun varify_and_instantiate_type ctxt T1 T1' T2 =
blanchet@43567
   109
  let val thy = Proof_Context.theory_of ctxt in
blanchet@43567
   110
    instantiate_type thy (varify_type ctxt T1) T1' (varify_type ctxt T2)
blanchet@43567
   111
  end
blanchet@43567
   112
blanchet@36553
   113
fun monomorphic_term subst t =
blanchet@36553
   114
  map_types (map_type_tvar (fn v =>
blanchet@36553
   115
      case Type.lookup subst v of
blanchet@36553
   116
        SOME typ => typ
blanchet@36553
   117
      | NONE => raise TERM ("monomorphic_term: uninstanitated schematic type \
blanchet@36553
   118
                            \variable", [t]))) t
blanchet@36553
   119
blanchet@38890
   120
fun eta_expand _ t 0 = t
blanchet@38890
   121
  | eta_expand Ts (Abs (s, T, t')) n =
blanchet@38890
   122
    Abs (s, T, eta_expand (T :: Ts) t' (n - 1))
blanchet@38890
   123
  | eta_expand Ts t n =
blanchet@38890
   124
    fold_rev (fn T => fn t' => Abs ("x" ^ nat_subscript n, T, t'))
blanchet@38890
   125
             (List.take (binder_types (fastype_of1 (Ts, t)), n))
blanchet@38890
   126
             (list_comb (incr_boundvars n t, map Bound (n - 1 downto 0)))
blanchet@38890
   127
blanchet@38890
   128
(* Converts an elim-rule into an equivalent theorem that does not have the
blanchet@38890
   129
   predicate variable. Leaves other theorems unchanged. We simply instantiate
blanchet@38890
   130
   the conclusion variable to False. (Cf. "transform_elim_theorem" in
blanchet@40071
   131
   "Meson_Clausify".) *)
blanchet@38890
   132
fun transform_elim_term t =
blanchet@38890
   133
  case Logic.strip_imp_concl t of
blanchet@38890
   134
    @{const Trueprop} $ Var (z, @{typ bool}) =>
blanchet@38890
   135
    subst_Vars [(z, @{const False})] t
blanchet@38890
   136
  | Var (z, @{typ prop}) => subst_Vars [(z, @{prop False})] t
blanchet@38890
   137
  | _ => t
blanchet@38890
   138
blanchet@36553
   139
fun specialize_type thy (s, T) t =
blanchet@36553
   140
  let
blanchet@36553
   141
    fun subst_for (Const (s', T')) =
blanchet@36553
   142
      if s = s' then
blanchet@36553
   143
        SOME (Sign.typ_match thy (T', T) Vartab.empty)
blanchet@36553
   144
        handle Type.TYPE_MATCH => NONE
blanchet@36553
   145
      else
blanchet@36553
   146
        NONE
blanchet@36553
   147
    | subst_for (t1 $ t2) =
blanchet@36553
   148
      (case subst_for t1 of SOME x => SOME x | NONE => subst_for t2)
blanchet@36553
   149
    | subst_for (Abs (_, _, t')) = subst_for t'
blanchet@36553
   150
    | subst_for _ = NONE
blanchet@36553
   151
  in
blanchet@36553
   152
    case subst_for t of
blanchet@36553
   153
      SOME subst => monomorphic_term subst t
blanchet@36553
   154
    | NONE => raise Type.TYPE_MATCH
blanchet@36553
   155
  end
blanchet@36553
   156
blanchet@38290
   157
val subgoal_count = Logic.count_prems o prop_of o #goal o Proof.goal
blanchet@38290
   158
blanchet@38229
   159
fun strip_subgoal goal i =
blanchet@38229
   160
  let
blanchet@38229
   161
    val (t, frees) = Logic.goal_params (prop_of goal) i
blanchet@38229
   162
    val hyp_ts = t |> Logic.strip_assums_hyp |> map (curry subst_bounds frees)
blanchet@38229
   163
    val concl_t = t |> Logic.strip_assums_concl |> curry subst_bounds frees
blanchet@38229
   164
  in (rev (map dest_Free frees), hyp_ts, concl_t) end
blanchet@36553
   165
blanchet@38935
   166
fun reserved_isar_keyword_table () =
blanchet@38935
   167
  union (op =) (Keyword.dest_keywords ()) (Keyword.dest_commands ())
blanchet@38935
   168
  |> map (rpair ()) |> Symtab.make
blanchet@38935
   169
blanchet@35961
   170
end;