src/HOL/Nominal/nominal_induct.ML
author wenzelm
Wed Nov 30 00:46:08 2005 +0100 (2005-11-30)
changeset 18288 feb79a6b274b
parent 18283 f8a49f09a202
child 18297 116fe71fad51
permissions -rw-r--r--
proper treatment of tuple/tuple_fun -- nest to the left!
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@18283
    10
    term list -> (string * typ) list list -> thm -> thm list -> int -> RuleCases.cases_tactic
wenzelm@18283
    11
  val nominal_induct_method: Method.src -> Proof.context -> Method.method
wenzelm@18283
    12
end =
wenzelm@18283
    13
struct
wenzelm@18283
    14
wenzelm@18283
    15
wenzelm@18288
    16
(* proper tuples -- nested left *)
wenzelm@18283
    17
wenzelm@18288
    18
fun tupleT Ts = HOLogic.unitT |> fold (fn T => fn U => HOLogic.mk_prodT (U, T)) Ts;
wenzelm@18288
    19
fun tuple ts = HOLogic.unit |> fold (fn t => fn u => HOLogic.mk_prod (u, t)) ts;
wenzelm@18288
    20
wenzelm@18288
    21
fun tuple_fun Ts (xi, T) =
wenzelm@18288
    22
  Library.funpow (length Ts) HOLogic.mk_split
wenzelm@18288
    23
    (Var (xi, (HOLogic.unitT :: Ts) ---> Term.range_type T));
wenzelm@18283
    24
wenzelm@18288
    25
val split_all_tuples =
wenzelm@18288
    26
  Simplifier.full_simplify (HOL_basic_ss addsimps
wenzelm@18288
    27
    [split_conv, split_paired_all, unit_all_eq1, thm "fresh_unit_elim", thm "fresh_prod_elim"]);
wenzelm@18283
    28
wenzelm@18288
    29
wenzelm@18288
    30
(* inst_rule conclusion: ?P fresh_struct ... insts *)
wenzelm@18288
    31
wenzelm@18288
    32
fun inst_rule thy insts fresh rule =
wenzelm@18283
    33
  let
wenzelm@18288
    34
    val vars = InductAttrib.vars_of (Thm.concl_of rule);
wenzelm@18288
    35
    val m = length vars and n = length insts;
wenzelm@18288
    36
    val _ = if m >= n + 2 then () else error "Too few variables in conclusion of rule";
wenzelm@18288
    37
    val P :: x :: ys = vars;
wenzelm@18288
    38
    val zs = Library.drop (m - n - 2, ys);
wenzelm@18288
    39
wenzelm@18288
    40
    val subst =
wenzelm@18288
    41
      (P, tuple_fun (map Term.fastype_of fresh) (Term.dest_Var P)) ::
wenzelm@18288
    42
      (x, tuple fresh) ::
wenzelm@18288
    43
      List.mapPartial (fn (z, SOME t) => SOME (z, t) | _ => NONE) (zs ~~ insts);
wenzelm@18283
    44
  in
wenzelm@18288
    45
    rule
wenzelm@18288
    46
    |> Drule.cterm_instantiate (PolyML.print (map (pairself (Thm.cterm_of thy)) subst))
wenzelm@18288
    47
    |> PolyML.print
wenzelm@18283
    48
  end;
wenzelm@18283
    49
wenzelm@18283
    50
wenzelm@18288
    51
(* nominal_induct_tac *)
wenzelm@18283
    52
wenzelm@18283
    53
fun nominal_induct_tac ctxt def_insts fresh fixing rule facts =
wenzelm@18283
    54
  let
wenzelm@18283
    55
    val thy = ProofContext.theory_of ctxt;
wenzelm@18283
    56
    val cert = Thm.cterm_of thy;
wenzelm@18283
    57
wenzelm@18288
    58
    val ((insts, defs), defs_ctxt) = InductMethod.add_defs def_insts ctxt;
wenzelm@18283
    59
    val atomized_defs = map ObjectLogic.atomize_thm defs;
wenzelm@18283
    60
wenzelm@18283
    61
    fun rule_cases r = RuleCases.make false (SOME (Thm.prop_of r)) (InductMethod.rulified_term r);
wenzelm@18283
    62
  in
wenzelm@18283
    63
    SUBGOAL_CASES (fn (goal, i) => fn st =>
wenzelm@18283
    64
      rule
wenzelm@18283
    65
      |> `RuleCases.get
wenzelm@18288
    66
      ||> inst_rule thy insts fresh
wenzelm@18283
    67
      |> RuleCases.consume defs facts
wenzelm@18283
    68
      |> Seq.maps (fn ((cases, (k, more_facts)), r) =>
wenzelm@18283
    69
        (CONJUNCTS (ALLGOALS (fn j =>
wenzelm@18283
    70
            Method.insert_tac (more_facts @ atomized_defs) j
wenzelm@18288
    71
            THEN InductMethod.fix_tac defs_ctxt k (Library.nth_list fixing (j - 1)) j))
wenzelm@18283
    72
          THEN' InductMethod.atomize_tac) i st |> Seq.maps (fn st' =>
wenzelm@18288
    73
            InductMethod.guess_instance (split_all_tuples r) i st'
wenzelm@18283
    74
            |> Seq.maps (fn r' =>
wenzelm@18283
    75
              CASES (rule_cases r' cases)
wenzelm@18283
    76
                (Tactic.rtac r' i THEN
wenzelm@18283
    77
                  PRIMSEQ (ProofContext.exports defs_ctxt ctxt)) st'))))
wenzelm@18283
    78
    THEN_ALL_NEW_CASES InductMethod.rulify_tac
wenzelm@18283
    79
  end;
wenzelm@18283
    80
wenzelm@18283
    81
wenzelm@18288
    82
(* concrete syntax *)
berghofe@17870
    83
berghofe@17870
    84
local
berghofe@17870
    85
wenzelm@18283
    86
val freshN = "fresh";
wenzelm@18283
    87
val fixingN = "fixing";
wenzelm@18283
    88
val ruleN = "rule";
berghofe@17870
    89
wenzelm@18283
    90
val inst = Scan.lift (Args.$$$ "_") >> K NONE || Args.local_term >> SOME;
berghofe@17870
    91
wenzelm@18283
    92
val def_inst =
wenzelm@18283
    93
  ((Scan.lift (Args.name --| (Args.$$$ "\\<equiv>" || Args.$$$ "==")) >> SOME)
wenzelm@18283
    94
      -- Args.local_term) >> SOME ||
wenzelm@18283
    95
    inst >> Option.map (pair NONE);
urbanc@18099
    96
wenzelm@18283
    97
val free = Scan.state -- Args.local_term >> (fn (_, Free v) => v | (ctxt, t) =>
wenzelm@18283
    98
  error ("Bad free variable: " ^ ProofContext.string_of_term ctxt t));
wenzelm@18283
    99
wenzelm@18283
   100
fun unless_more_args scan = Scan.unless (Scan.lift
wenzelm@18283
   101
  ((Args.$$$ freshN || Args.$$$ fixingN || Args.$$$ ruleN) -- Args.colon)) scan;
wenzelm@18283
   102
berghofe@17870
   103
wenzelm@18283
   104
val def_insts = Scan.repeat (unless_more_args def_inst);
berghofe@17870
   105
wenzelm@18283
   106
val fresh = Scan.optional (Scan.lift (Args.$$$ freshN -- Args.colon) |--
wenzelm@18283
   107
  Scan.repeat (unless_more_args Args.local_term)) [];
berghofe@17870
   108
wenzelm@18283
   109
val fixing = Scan.optional (Scan.lift (Args.$$$ fixingN -- Args.colon) |--
wenzelm@18283
   110
  Args.and_list1 (Scan.repeat (unless_more_args free))) [];
berghofe@17870
   111
berghofe@17870
   112
val rule_spec = Scan.lift (Args.$$$ "rule" -- Args.colon) |-- Attrib.local_thm;
berghofe@17870
   113
berghofe@17870
   114
in
berghofe@17870
   115
wenzelm@18283
   116
fun nominal_induct_method src =
wenzelm@18283
   117
  Method.syntax (def_insts -- fresh -- fixing -- rule_spec) src
wenzelm@18283
   118
  #> (fn (ctxt, (((x, y), z), w)) =>
wenzelm@18283
   119
    Method.RAW_METHOD_CASES (fn facts =>
wenzelm@18283
   120
      HEADGOAL (nominal_induct_tac ctxt x y z w facts)));
berghofe@17870
   121
berghofe@17870
   122
end;
wenzelm@18283
   123
wenzelm@18283
   124
end;