src/HOL/Nominal/nominal_induct.ML
author wenzelm
Wed Nov 30 15:24:32 2005 +0100 (2005-11-30)
changeset 18299 af72dfc4b9f9
parent 18297 116fe71fad51
child 18311 b83b00cbaecf
permissions -rw-r--r--
added rename_params_rule: recover orginal fresh names in subgoals/cases;
wenzelm@18283
     1
(*  ID:         $Id$
wenzelm@18288
     2
    Author:     Christian Urban and Makarius
wenzelm@18283
     3
wenzelm@18288
     4
The nominal induct proof method.
wenzelm@18283
     5
*)
wenzelm@18283
     6
wenzelm@18283
     7
structure NominalInduct:
wenzelm@18283
     8
sig
wenzelm@18283
     9
  val nominal_induct_tac: Proof.context -> (string option * term) option list ->
wenzelm@18297
    10
    (string * typ) list -> (string * typ) list list -> thm ->
wenzelm@18297
    11
    thm list -> int -> RuleCases.cases_tactic
wenzelm@18283
    12
  val nominal_induct_method: Method.src -> Proof.context -> Method.method
wenzelm@18283
    13
end =
wenzelm@18283
    14
struct
wenzelm@18283
    15
wenzelm@18283
    16
wenzelm@18288
    17
(* proper tuples -- nested left *)
wenzelm@18283
    18
wenzelm@18288
    19
fun tupleT Ts = HOLogic.unitT |> fold (fn T => fn U => HOLogic.mk_prodT (U, T)) Ts;
wenzelm@18288
    20
fun tuple ts = HOLogic.unit |> fold (fn t => fn u => HOLogic.mk_prod (u, t)) ts;
wenzelm@18288
    21
wenzelm@18288
    22
fun tuple_fun Ts (xi, T) =
wenzelm@18288
    23
  Library.funpow (length Ts) HOLogic.mk_split
wenzelm@18288
    24
    (Var (xi, (HOLogic.unitT :: Ts) ---> Term.range_type T));
wenzelm@18283
    25
wenzelm@18288
    26
val split_all_tuples =
wenzelm@18288
    27
  Simplifier.full_simplify (HOL_basic_ss addsimps
wenzelm@18288
    28
    [split_conv, split_paired_all, unit_all_eq1, thm "fresh_unit_elim", thm "fresh_prod_elim"]);
wenzelm@18283
    29
wenzelm@18288
    30
wenzelm@18297
    31
(* prepare rule *)
wenzelm@18288
    32
wenzelm@18297
    33
(*conclusion: ?P fresh_struct ... insts*)
wenzelm@18288
    34
fun inst_rule thy insts fresh rule =
wenzelm@18283
    35
  let
wenzelm@18288
    36
    val vars = InductAttrib.vars_of (Thm.concl_of rule);
wenzelm@18288
    37
    val m = length vars and n = length insts;
wenzelm@18288
    38
    val _ = if m >= n + 2 then () else error "Too few variables in conclusion of rule";
wenzelm@18288
    39
    val P :: x :: ys = vars;
wenzelm@18288
    40
    val zs = Library.drop (m - n - 2, ys);
wenzelm@18288
    41
wenzelm@18288
    42
    val subst =
wenzelm@18297
    43
      (P, tuple_fun (map #2 fresh) (Term.dest_Var P)) ::
wenzelm@18297
    44
      (x, tuple (map Free fresh)) ::
wenzelm@18288
    45
      List.mapPartial (fn (z, SOME t) => SOME (z, t) | _ => NONE) (zs ~~ insts);
wenzelm@18283
    46
  in
wenzelm@18288
    47
    rule
wenzelm@18297
    48
    |> Drule.cterm_instantiate (map (pairself (Thm.cterm_of thy)) subst)
wenzelm@18283
    49
  end;
wenzelm@18283
    50
wenzelm@18299
    51
fun rename_params_rule internal xs rule =
wenzelm@18297
    52
  let
wenzelm@18299
    53
    val tune =
wenzelm@18299
    54
      if internal then Syntax.internal
wenzelm@18299
    55
      else fn x => the_default x (try Syntax.dest_internal x);
wenzelm@18299
    56
    val n = length xs;
wenzelm@18299
    57
    fun rename prem =
wenzelm@18299
    58
      let
wenzelm@18299
    59
        val ps = Logic.strip_params prem;
wenzelm@18299
    60
        val p = length ps;
wenzelm@18299
    61
        val ys =
wenzelm@18299
    62
          if p < n then []
wenzelm@18299
    63
          else map (tune o #1) (Library.take (p - n, ps)) @ xs;
wenzelm@18299
    64
      in Logic.list_rename_params (ys, prem) end;
wenzelm@18299
    65
    fun rename_prems prop =
wenzelm@18299
    66
      let val (As, C) = Logic.strip_horn (Thm.prop_of rule)
wenzelm@18299
    67
      in Logic.list_implies (map rename As, C) end;
wenzelm@18299
    68
  in Thm.equal_elim (Thm.reflexive (Drule.cterm_fun rename_prems (Thm.cprop_of rule))) rule end;
wenzelm@18297
    69
wenzelm@18283
    70
wenzelm@18288
    71
(* nominal_induct_tac *)
wenzelm@18283
    72
wenzelm@18283
    73
fun nominal_induct_tac ctxt def_insts fresh fixing rule facts =
wenzelm@18283
    74
  let
wenzelm@18283
    75
    val thy = ProofContext.theory_of ctxt;
wenzelm@18283
    76
    val cert = Thm.cterm_of thy;
wenzelm@18283
    77
wenzelm@18288
    78
    val ((insts, defs), defs_ctxt) = InductMethod.add_defs def_insts ctxt;
wenzelm@18283
    79
    val atomized_defs = map ObjectLogic.atomize_thm defs;
wenzelm@18283
    80
wenzelm@18297
    81
    val finish_rule =
wenzelm@18297
    82
      split_all_tuples
wenzelm@18299
    83
      #> rename_params_rule true (map (ProofContext.revert_skolem defs_ctxt o #1) fresh);
wenzelm@18299
    84
    fun rule_cases r = RuleCases.make true (SOME (Thm.prop_of r)) (InductMethod.rulified_term r);
wenzelm@18283
    85
  in
wenzelm@18297
    86
    (fn i => fn st =>
wenzelm@18283
    87
      rule
wenzelm@18283
    88
      |> `RuleCases.get
wenzelm@18288
    89
      ||> inst_rule thy insts fresh
wenzelm@18283
    90
      |> RuleCases.consume defs facts
wenzelm@18283
    91
      |> Seq.maps (fn ((cases, (k, more_facts)), r) =>
wenzelm@18283
    92
        (CONJUNCTS (ALLGOALS (fn j =>
wenzelm@18283
    93
            Method.insert_tac (more_facts @ atomized_defs) j
wenzelm@18288
    94
            THEN InductMethod.fix_tac defs_ctxt k (Library.nth_list fixing (j - 1)) j))
wenzelm@18283
    95
          THEN' InductMethod.atomize_tac) i st |> Seq.maps (fn st' =>
wenzelm@18297
    96
            InductMethod.guess_instance (finish_rule r) i st'
wenzelm@18283
    97
            |> Seq.maps (fn r' =>
wenzelm@18283
    98
              CASES (rule_cases r' cases)
wenzelm@18299
    99
                (Tactic.rtac (rename_params_rule false [] r') i THEN
wenzelm@18283
   100
                  PRIMSEQ (ProofContext.exports defs_ctxt ctxt)) st'))))
wenzelm@18283
   101
    THEN_ALL_NEW_CASES InductMethod.rulify_tac
wenzelm@18283
   102
  end;
wenzelm@18283
   103
wenzelm@18283
   104
wenzelm@18288
   105
(* concrete syntax *)
berghofe@17870
   106
berghofe@17870
   107
local
berghofe@17870
   108
wenzelm@18283
   109
val freshN = "fresh";
wenzelm@18283
   110
val fixingN = "fixing";
wenzelm@18283
   111
val ruleN = "rule";
berghofe@17870
   112
wenzelm@18283
   113
val inst = Scan.lift (Args.$$$ "_") >> K NONE || Args.local_term >> SOME;
berghofe@17870
   114
wenzelm@18283
   115
val def_inst =
wenzelm@18283
   116
  ((Scan.lift (Args.name --| (Args.$$$ "\\<equiv>" || Args.$$$ "==")) >> SOME)
wenzelm@18283
   117
      -- Args.local_term) >> SOME ||
wenzelm@18283
   118
    inst >> Option.map (pair NONE);
urbanc@18099
   119
wenzelm@18283
   120
val free = Scan.state -- Args.local_term >> (fn (_, Free v) => v | (ctxt, t) =>
wenzelm@18283
   121
  error ("Bad free variable: " ^ ProofContext.string_of_term ctxt t));
wenzelm@18283
   122
wenzelm@18283
   123
fun unless_more_args scan = Scan.unless (Scan.lift
wenzelm@18283
   124
  ((Args.$$$ freshN || Args.$$$ fixingN || Args.$$$ ruleN) -- Args.colon)) scan;
wenzelm@18283
   125
berghofe@17870
   126
wenzelm@18283
   127
val def_insts = Scan.repeat (unless_more_args def_inst);
berghofe@17870
   128
wenzelm@18283
   129
val fresh = Scan.optional (Scan.lift (Args.$$$ freshN -- Args.colon) |--
wenzelm@18297
   130
  Scan.repeat (unless_more_args free)) [];
berghofe@17870
   131
wenzelm@18283
   132
val fixing = Scan.optional (Scan.lift (Args.$$$ fixingN -- Args.colon) |--
wenzelm@18283
   133
  Args.and_list1 (Scan.repeat (unless_more_args free))) [];
berghofe@17870
   134
berghofe@17870
   135
val rule_spec = Scan.lift (Args.$$$ "rule" -- Args.colon) |-- Attrib.local_thm;
berghofe@17870
   136
berghofe@17870
   137
in
berghofe@17870
   138
wenzelm@18283
   139
fun nominal_induct_method src =
wenzelm@18283
   140
  Method.syntax (def_insts -- fresh -- fixing -- rule_spec) src
wenzelm@18283
   141
  #> (fn (ctxt, (((x, y), z), w)) =>
wenzelm@18283
   142
    Method.RAW_METHOD_CASES (fn facts =>
wenzelm@18283
   143
      HEADGOAL (nominal_induct_tac ctxt x y z w facts)));
berghofe@17870
   144
berghofe@17870
   145
end;
wenzelm@18283
   146
wenzelm@18283
   147
end;