src/HOL/Nominal/nominal_induct.ML
author wenzelm
Wed Nov 30 14:27:50 2005 +0100 (2005-11-30)
changeset 18297 116fe71fad51
parent 18288 feb79a6b274b
child 18299 af72dfc4b9f9
permissions -rw-r--r--
fresh: frees instead of terms, rename corresponding params in rule;
tuned;
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@18297
    51
fun rename_params_prems xs rule =
wenzelm@18297
    52
  let
wenzelm@18297
    53
    val cert = Thm.cterm_of (Thm.theory_of_thm rule);
wenzelm@18297
    54
    val (As, C) = Logic.strip_horn (Thm.prop_of rule);
wenzelm@18297
    55
    val prop = Logic.list_implies (map (curry Logic.list_rename_params xs) As, C);
wenzelm@18297
    56
  in Thm.equal_elim (Thm.reflexive (cert prop)) rule end;
wenzelm@18297
    57
wenzelm@18283
    58
wenzelm@18288
    59
(* nominal_induct_tac *)
wenzelm@18283
    60
wenzelm@18283
    61
fun nominal_induct_tac ctxt def_insts fresh fixing rule facts =
wenzelm@18283
    62
  let
wenzelm@18283
    63
    val thy = ProofContext.theory_of ctxt;
wenzelm@18283
    64
    val cert = Thm.cterm_of thy;
wenzelm@18283
    65
wenzelm@18288
    66
    val ((insts, defs), defs_ctxt) = InductMethod.add_defs def_insts ctxt;
wenzelm@18283
    67
    val atomized_defs = map ObjectLogic.atomize_thm defs;
wenzelm@18283
    68
wenzelm@18297
    69
    val finish_rule =
wenzelm@18297
    70
      split_all_tuples
wenzelm@18297
    71
      #> rename_params_prems (map (ProofContext.revert_skolem defs_ctxt o #1) fresh);
wenzelm@18283
    72
    fun rule_cases r = RuleCases.make false (SOME (Thm.prop_of r)) (InductMethod.rulified_term r);
wenzelm@18283
    73
  in
wenzelm@18297
    74
    (fn i => fn st =>
wenzelm@18283
    75
      rule
wenzelm@18283
    76
      |> `RuleCases.get
wenzelm@18288
    77
      ||> inst_rule thy insts fresh
wenzelm@18283
    78
      |> RuleCases.consume defs facts
wenzelm@18283
    79
      |> Seq.maps (fn ((cases, (k, more_facts)), r) =>
wenzelm@18283
    80
        (CONJUNCTS (ALLGOALS (fn j =>
wenzelm@18283
    81
            Method.insert_tac (more_facts @ atomized_defs) j
wenzelm@18288
    82
            THEN InductMethod.fix_tac defs_ctxt k (Library.nth_list fixing (j - 1)) j))
wenzelm@18283
    83
          THEN' InductMethod.atomize_tac) i st |> Seq.maps (fn st' =>
wenzelm@18297
    84
            InductMethod.guess_instance (finish_rule r) i st'
wenzelm@18283
    85
            |> Seq.maps (fn r' =>
wenzelm@18283
    86
              CASES (rule_cases r' cases)
wenzelm@18283
    87
                (Tactic.rtac r' i THEN
wenzelm@18283
    88
                  PRIMSEQ (ProofContext.exports defs_ctxt ctxt)) st'))))
wenzelm@18283
    89
    THEN_ALL_NEW_CASES InductMethod.rulify_tac
wenzelm@18283
    90
  end;
wenzelm@18283
    91
wenzelm@18283
    92
wenzelm@18288
    93
(* concrete syntax *)
berghofe@17870
    94
berghofe@17870
    95
local
berghofe@17870
    96
wenzelm@18283
    97
val freshN = "fresh";
wenzelm@18283
    98
val fixingN = "fixing";
wenzelm@18283
    99
val ruleN = "rule";
berghofe@17870
   100
wenzelm@18283
   101
val inst = Scan.lift (Args.$$$ "_") >> K NONE || Args.local_term >> SOME;
berghofe@17870
   102
wenzelm@18283
   103
val def_inst =
wenzelm@18283
   104
  ((Scan.lift (Args.name --| (Args.$$$ "\\<equiv>" || Args.$$$ "==")) >> SOME)
wenzelm@18283
   105
      -- Args.local_term) >> SOME ||
wenzelm@18283
   106
    inst >> Option.map (pair NONE);
urbanc@18099
   107
wenzelm@18283
   108
val free = Scan.state -- Args.local_term >> (fn (_, Free v) => v | (ctxt, t) =>
wenzelm@18283
   109
  error ("Bad free variable: " ^ ProofContext.string_of_term ctxt t));
wenzelm@18283
   110
wenzelm@18283
   111
fun unless_more_args scan = Scan.unless (Scan.lift
wenzelm@18283
   112
  ((Args.$$$ freshN || Args.$$$ fixingN || Args.$$$ ruleN) -- Args.colon)) scan;
wenzelm@18283
   113
berghofe@17870
   114
wenzelm@18283
   115
val def_insts = Scan.repeat (unless_more_args def_inst);
berghofe@17870
   116
wenzelm@18283
   117
val fresh = Scan.optional (Scan.lift (Args.$$$ freshN -- Args.colon) |--
wenzelm@18297
   118
  Scan.repeat (unless_more_args free)) [];
berghofe@17870
   119
wenzelm@18283
   120
val fixing = Scan.optional (Scan.lift (Args.$$$ fixingN -- Args.colon) |--
wenzelm@18283
   121
  Args.and_list1 (Scan.repeat (unless_more_args free))) [];
berghofe@17870
   122
berghofe@17870
   123
val rule_spec = Scan.lift (Args.$$$ "rule" -- Args.colon) |-- Attrib.local_thm;
berghofe@17870
   124
berghofe@17870
   125
in
berghofe@17870
   126
wenzelm@18283
   127
fun nominal_induct_method src =
wenzelm@18283
   128
  Method.syntax (def_insts -- fresh -- fixing -- rule_spec) src
wenzelm@18283
   129
  #> (fn (ctxt, (((x, y), z), w)) =>
wenzelm@18283
   130
    Method.RAW_METHOD_CASES (fn facts =>
wenzelm@18283
   131
      HEADGOAL (nominal_induct_tac ctxt x y z w facts)));
berghofe@17870
   132
berghofe@17870
   133
end;
wenzelm@18283
   134
wenzelm@18283
   135
end;