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