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