src/HOL/Tools/split_rule.ML
author haftmann
Fri Oct 10 19:55:32 2014 +0200 (2014-10-10)
changeset 58646 cd63a4b12a33
parent 58468 d1f6a38f9415
child 58820 3ad2759acc52
permissions -rw-r--r--
specialized specification: avoid trivial instances
     1 (*  Title:      HOL/Tools/split_rule.ML
     2     Author:     Stefan Berghofer, David von Oheimb, and Markus Wenzel, TU Muenchen
     3 
     4 Some tools for managing tupled arguments and abstractions in rules.
     5 *)
     6 
     7 signature SPLIT_RULE =
     8 sig
     9   val split_rule_var: Proof.context -> term -> thm -> thm
    10   val split_rule: Proof.context -> thm -> thm
    11   val complete_split_rule: Proof.context -> thm -> thm
    12   val setup: theory -> theory
    13 end;
    14 
    15 structure Split_Rule: SPLIT_RULE =
    16 struct
    17 
    18 (** split rules **)
    19 
    20 fun internal_split_const (Ta, Tb, Tc) =
    21   Const (@{const_name Product_Type.internal_split},
    22     [[Ta, Tb] ---> Tc, HOLogic.mk_prodT (Ta, Tb)] ---> Tc);
    23 
    24 fun eval_internal_split ctxt =
    25   hol_simplify ctxt @{thms internal_split_def} o
    26   hol_simplify ctxt @{thms internal_split_conv};
    27 
    28 fun remove_internal_split ctxt = eval_internal_split ctxt o split_all ctxt;
    29 
    30 
    31 (*In ap_split S T u, term u expects separate arguments for the factors of S,
    32   with result type T.  The call creates a new term expecting one argument
    33   of type S.*)
    34 fun ap_split (Type (@{type_name Product_Type.prod}, [T1, T2])) T3 u =
    35       internal_split_const (T1, T2, T3) $
    36       Abs ("v", T1,
    37           ap_split T2 T3
    38              ((ap_split T1 (HOLogic.flatten_tupleT T2 ---> T3) (incr_boundvars 1 u)) $
    39               Bound 0))
    40   | ap_split _ T3 u = u;
    41 
    42 (*Curries any Var of function type in the rule*)
    43 fun split_rule_var' (t as Var (v, Type ("fun", [T1, T2]))) rl =
    44       let val T' = HOLogic.flatten_tupleT T1 ---> T2;
    45           val newt = ap_split T1 T2 (Var (v, T'));
    46           val cterm = Thm.cterm_of (Thm.theory_of_thm rl);
    47       in Thm.instantiate ([], [(cterm t, cterm newt)]) rl end
    48   | split_rule_var' _ rl = rl;
    49 
    50 
    51 (* complete splitting of partially split rules *)
    52 
    53 fun ap_split' (T::Ts) U u = Abs ("v", T, ap_split' Ts U
    54       (ap_split T (maps HOLogic.flatten_tupleT Ts ---> U)
    55         (incr_boundvars 1 u) $ Bound 0))
    56   | ap_split' _ _ u = u;
    57 
    58 fun complete_split_rule_var (t as Var (v, T), ts) (rl, vs) =
    59       let
    60         val cterm = Thm.cterm_of (Thm.theory_of_thm rl)
    61         val (Us', U') = strip_type T;
    62         val Us = take (length ts) Us';
    63         val U = drop (length ts) Us' ---> U';
    64         val T' = maps HOLogic.flatten_tupleT Us ---> U;
    65         fun mk_tuple (v as Var ((a, _), T)) (xs, insts) =
    66               let
    67                 val Ts = HOLogic.flatten_tupleT T;
    68                 val ys = Name.variant_list xs (replicate (length Ts) a);
    69               in (xs @ ys, (cterm v, cterm (HOLogic.mk_ptuple (HOLogic.flat_tupleT_paths T) T
    70                 (map (Var o apfst (rpair 0)) (ys ~~ Ts))))::insts)
    71               end
    72           | mk_tuple _ x = x;
    73         val newt = ap_split' Us U (Var (v, T'));
    74         val cterm = Thm.cterm_of (Thm.theory_of_thm rl);
    75         val (vs', insts) = fold mk_tuple ts (vs, []);
    76       in
    77         (Drule.instantiate_normalize ([], [(cterm t, cterm newt)] @ insts) rl, vs')
    78       end
    79   | complete_split_rule_var _ x = x;
    80 
    81 fun collect_vars (Abs (_, _, t)) = collect_vars t
    82   | collect_vars t =
    83       (case strip_comb t of
    84         (v as Var _, ts) => cons (v, ts)
    85       | (_, ts) => fold collect_vars ts);
    86 
    87 
    88 fun split_rule_var ctxt =
    89   (Drule.export_without_context o remove_internal_split ctxt) oo split_rule_var';
    90 
    91 (*curries ALL function variables occurring in a rule's conclusion*)
    92 fun split_rule ctxt rl =
    93   fold_rev split_rule_var' (Misc_Legacy.term_vars (concl_of rl)) rl
    94   |> remove_internal_split ctxt
    95   |> Drule.export_without_context;
    96 
    97 (*curries ALL function variables*)
    98 fun complete_split_rule ctxt rl =
    99   let
   100     val prop = Thm.prop_of rl;
   101     val xs = Term.fold_aterms (fn Var ((x, _), _) => insert (op =) x | _ => I) prop [];
   102     val vars = collect_vars prop [];
   103   in
   104     fst (fold_rev complete_split_rule_var vars (rl, xs))
   105     |> remove_internal_split ctxt
   106     |> Drule.export_without_context
   107     |> Rule_Cases.save rl
   108   end;
   109 
   110 
   111 (* attribute syntax *)
   112 
   113 val setup =
   114   Attrib.setup @{binding split_format}
   115     (Scan.lift (Args.parens (Args.$$$ "complete")
   116       >> K (Thm.rule_attribute (complete_split_rule o Context.proof_of))))
   117     "split pair-typed subterms in premises, or function arguments" #>
   118   Attrib.setup @{binding split_rule}
   119     (Scan.succeed (Thm.rule_attribute (split_rule o Context.proof_of)))
   120     "curries ALL function variables occurring in a rule's conclusion";
   121 
   122 end;
   123