src/HOL/Tools/split_rule.ML
author wenzelm
Fri Feb 02 22:20:43 2001 +0100 (2001-02-02 ago)
changeset 11037 37716a82a3d9
parent 11025 a70b796d9af8
child 11040 194406da4e43
permissions -rw-r--r--
module setup;
use hidden internal_split constants;
wenzelm@11037
     1
(*  Title:      Tools/split_rule.ML
wenzelm@11037
     2
    ID:         $Id$
wenzelm@11037
     3
    Author:     Stefan Berghofer, David von Oheimb, and Markus Wenzel, TU Muenchen
wenzelm@11037
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@11037
     5
wenzelm@11037
     6
Some tools for managing tupled arguments and abstractions in rules.
wenzelm@11037
     7
*)
wenzelm@11037
     8
wenzelm@11037
     9
signature BASIC_SPLIT_RULE =
wenzelm@11037
    10
sig
wenzelm@11037
    11
  val split_rule: thm -> thm
wenzelm@11037
    12
  val complete_split_rule: thm -> thm
wenzelm@11037
    13
end;
oheimb@11025
    14
wenzelm@11037
    15
signature SPLIT_RULE =
wenzelm@11037
    16
sig
wenzelm@11037
    17
  include BASIC_SPLIT_RULE
wenzelm@11037
    18
  val split_rule_var: term * thm -> thm
wenzelm@11037
    19
  val split_rule_goal: string list list -> thm -> thm
wenzelm@11037
    20
  val setup: (theory -> theory) list
wenzelm@11037
    21
end;
wenzelm@11037
    22
wenzelm@11037
    23
structure SplitRule: SPLIT_RULE =
wenzelm@11037
    24
struct
wenzelm@11037
    25
wenzelm@11037
    26
wenzelm@11037
    27
(** theory context references **)
wenzelm@11037
    28
wenzelm@11037
    29
fun internal_split_const (Ta, Tb, Tc) =
wenzelm@11037
    30
  Const ("Product_Type.internal_split", [[Ta, Tb] ---> Tc, HOLogic.mk_prodT (Ta, Tb)] ---> Tc);
wenzelm@11037
    31
wenzelm@11037
    32
val internal_split_def = thm "internal_split_def";
wenzelm@11037
    33
val internal_split_conv = thm "internal_split_conv";
wenzelm@11037
    34
wenzelm@11037
    35
wenzelm@11037
    36
wenzelm@11037
    37
(** split rules **)
wenzelm@11037
    38
wenzelm@11037
    39
val eval_internal_split = hol_simplify [internal_split_def] o hol_simplify [internal_split_conv];
wenzelm@11037
    40
val remove_internal_split = eval_internal_split o split_all;
wenzelm@11037
    41
oheimb@11025
    42
oheimb@11025
    43
(*In ap_split S T u, term u expects separate arguments for the factors of S,
oheimb@11025
    44
  with result type T.  The call creates a new term expecting one argument
oheimb@11025
    45
  of type S.*)
oheimb@11025
    46
fun ap_split (Type ("*", [T1, T2])) T3 u =
wenzelm@11037
    47
      internal_split_const (T1, T2, T3) $
wenzelm@11037
    48
      Abs ("v", T1,
oheimb@11025
    49
          ap_split T2 T3
oheimb@11025
    50
             ((ap_split T1 (HOLogic.prodT_factors T2 ---> T3) (incr_boundvars 1 u)) $
oheimb@11025
    51
              Bound 0))
oheimb@11025
    52
  | ap_split T T3 u = u;
oheimb@11025
    53
oheimb@11025
    54
(*Curries any Var of function type in the rule*)
oheimb@11025
    55
fun split_rule_var' (t as Var (v, Type ("fun", [T1, T2])), rl) =
wenzelm@11037
    56
      let val T' = HOLogic.prodT_factors T1 ---> T2;
wenzelm@11037
    57
          val newt = ap_split T1 T2 (Var (v, T'));
wenzelm@11037
    58
          val cterm = Thm.cterm_of (#sign (Thm.rep_thm rl));
wenzelm@11037
    59
      in Thm.instantiate ([], [(cterm t, cterm newt)]) rl end
oheimb@11025
    60
  | split_rule_var' (t, rl) = rl;
oheimb@11025
    61
wenzelm@11037
    62
wenzelm@11037
    63
(* complete splitting of partially splitted rules *)
oheimb@11025
    64
oheimb@11025
    65
fun ap_split' (T::Ts) U u = Abs ("v", T, ap_split' Ts U
oheimb@11025
    66
      (ap_split T (flat (map HOLogic.prodT_factors Ts) ---> U)
oheimb@11025
    67
        (incr_boundvars 1 u) $ Bound 0))
oheimb@11025
    68
  | ap_split' _ _ u = u;
oheimb@11025
    69
oheimb@11025
    70
fun complete_split_rule_var ((t as Var (v, T), ts), (rl, vs)) =
oheimb@11025
    71
      let
wenzelm@11037
    72
        val cterm = Thm.cterm_of (#sign (Thm.rep_thm rl))
oheimb@11025
    73
        val (Us', U') = strip_type T;
oheimb@11025
    74
        val Us = take (length ts, Us');
oheimb@11025
    75
        val U = drop (length ts, Us') ---> U';
oheimb@11025
    76
        val T' = flat (map HOLogic.prodT_factors Us) ---> U;
oheimb@11025
    77
        fun mk_tuple ((xs, insts), v as Var ((a, _), T)) =
oheimb@11025
    78
              let
oheimb@11025
    79
                val Ts = HOLogic.prodT_factors T;
wenzelm@11037
    80
                val ys = Term.variantlist (replicate (length Ts) a, xs);
oheimb@11025
    81
              in (xs @ ys, (cterm v, cterm (HOLogic.mk_tuple T
oheimb@11025
    82
                (map (Var o apfst (rpair 0)) (ys ~~ Ts))))::insts)
oheimb@11025
    83
              end
oheimb@11025
    84
          | mk_tuple (x, _) = x;
oheimb@11025
    85
        val newt = ap_split' Us U (Var (v, T'));
wenzelm@11037
    86
        val cterm = Thm.cterm_of (#sign (Thm.rep_thm rl));
oheimb@11025
    87
        val (vs', insts) = foldl mk_tuple ((vs, []), ts);
oheimb@11025
    88
      in
oheimb@11025
    89
        (instantiate ([], [(cterm t, cterm newt)] @ insts) rl, vs')
oheimb@11025
    90
      end
oheimb@11025
    91
  | complete_split_rule_var (_, x) = x;
oheimb@11025
    92
oheimb@11025
    93
fun collect_vars (vs, Abs (_, _, t)) = collect_vars (vs, t)
oheimb@11025
    94
  | collect_vars (vs, t) = (case strip_comb t of
oheimb@11025
    95
        (v as Var _, ts) => (v, ts)::vs
oheimb@11025
    96
      | (t, ts) => foldl collect_vars (vs, ts));
oheimb@11025
    97
wenzelm@11037
    98
wenzelm@11037
    99
val split_rule_var = Drule.standard o remove_internal_split o split_rule_var';
oheimb@11025
   100
wenzelm@11037
   101
(*curries ALL function variables occurring in a rule's conclusion*)
wenzelm@11037
   102
fun split_rule rl =
wenzelm@11037
   103
  foldr split_rule_var' (Term.term_vars (concl_of rl), rl)
wenzelm@11037
   104
  |> remove_internal_split
wenzelm@11037
   105
  |> Drule.standard;
oheimb@11025
   106
oheimb@11025
   107
fun complete_split_rule rl =
wenzelm@11037
   108
  fst (foldr complete_split_rule_var
oheimb@11025
   109
    (collect_vars ([], concl_of rl),
wenzelm@11037
   110
      (rl, map (fst o fst o dest_Var) (Term.term_vars (#prop (Thm.rep_thm rl))))))
wenzelm@11037
   111
  |> remove_internal_split
wenzelm@11037
   112
  |> Drule.standard
wenzelm@11037
   113
  |> RuleCases.save rl;
wenzelm@11037
   114
wenzelm@11037
   115
wenzelm@11037
   116
val split_rule_ss = HOL_basic_ss addsimps [split_conv, fst_conv, snd_conv];
wenzelm@11037
   117
wenzelm@11037
   118
fun split_rule_goal xss rl =
wenzelm@11037
   119
  let
wenzelm@11037
   120
    fun one_split i (th, s) = Tactic.rule_by_tactic (pair_tac s i) th;
wenzelm@11037
   121
    fun one_goal (xs, i) th = foldl (one_split i) (th, xs);
wenzelm@11037
   122
  in
wenzelm@11037
   123
    Drule.standard (Simplifier.full_simplify split_rule_ss (Library.foldln one_goal xss rl))
wenzelm@11037
   124
    |> RuleCases.save rl
wenzelm@11037
   125
  end;
wenzelm@11037
   126
wenzelm@11037
   127
wenzelm@11037
   128
(* attribute syntax *)
wenzelm@11037
   129
wenzelm@11037
   130
fun complete_split x =
wenzelm@11037
   131
  Attrib.no_args (Drule.rule_attribute (K complete_split_rule)) x;
wenzelm@11037
   132
wenzelm@11037
   133
fun split_format x =
wenzelm@11037
   134
  Attrib.syntax (Args.and_list1 (Scan.lift (Scan.repeat Args.name))
wenzelm@11037
   135
    >> (fn xss => Drule.rule_attribute (K (split_rule_goal xss)))) x;
wenzelm@11037
   136
wenzelm@11037
   137
val split_attributes =
wenzelm@11037
   138
 [("complete_split", (complete_split, complete_split),
wenzelm@11037
   139
    "recursively split all pair-typed function arguments"),
wenzelm@11037
   140
  ("split_format", (split_format, split_format),
wenzelm@11037
   141
    "split given pair-typed subterms in premises")];
wenzelm@11037
   142
wenzelm@11037
   143
wenzelm@11037
   144
wenzelm@11037
   145
(** theory setup **)
wenzelm@11037
   146
wenzelm@11037
   147
val setup =
wenzelm@11037
   148
 [Attrib.add_attributes split_attributes];
wenzelm@11037
   149
oheimb@11025
   150
oheimb@11025
   151
end;
oheimb@11025
   152
wenzelm@11037
   153
structure BasicSplitRule: BASIC_SPLIT_RULE = SplitRule;
wenzelm@11037
   154
open BasicSplitRule;