src/HOL/Nominal/nominal_induct.ML
author wenzelm
Thu Jan 05 17:16:40 2006 +0100 (2006-01-05)
changeset 18583 96e1ef2f806f
parent 18311 b83b00cbaecf
child 18610 05a5e950d5f1
permissions -rw-r--r--
proper handling of simultaneous goals and mutual rules;
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@18583
     9
  val nominal_induct_tac: Proof.context -> (string option * term) option list list ->
wenzelm@18583
    10
    (string * typ) list -> (string * typ) list list -> thm list ->
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@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@18297
    30
(* prepare rule *)
wenzelm@18288
    31
wenzelm@18583
    32
(*conclusions: ?P avoiding_struct ... insts*)
wenzelm@18583
    33
fun inst_mutual_rule thy insts avoiding rules =
wenzelm@18283
    34
  let
wenzelm@18583
    35
    val (concls, rule) =
wenzelm@18583
    36
      (case RuleCases.mutual_rule rules of
wenzelm@18583
    37
        NONE => error "Failed to join given rules into one mutual rule"
wenzelm@18583
    38
      | SOME res => res);
wenzelm@18583
    39
    val (cases, consumes) = RuleCases.get rule;
wenzelm@18583
    40
wenzelm@18583
    41
    val l = length rules;
wenzelm@18583
    42
    val _ =
wenzelm@18583
    43
      if length insts = l then ()
wenzelm@18583
    44
      else error ("Bad number of instantiations for " ^ string_of_int l ^ " rules");
wenzelm@18288
    45
wenzelm@18583
    46
    fun subst inst rule =
wenzelm@18583
    47
      let
wenzelm@18583
    48
        val vars = InductAttrib.vars_of (Thm.concl_of rule);
wenzelm@18583
    49
        val m = length vars and n = length inst;
wenzelm@18583
    50
        val _ = if m >= n + 2 then () else error "Too few variables in conclusion of rule";
wenzelm@18583
    51
        val P :: x :: ys = vars;
wenzelm@18583
    52
        val zs = Library.drop (m - n - 2, ys);
wenzelm@18583
    53
      in
wenzelm@18583
    54
        (P, tuple_fun (map #2 avoiding) (Term.dest_Var P)) ::
wenzelm@18583
    55
        (x, tuple (map Free avoiding)) ::
wenzelm@18583
    56
        List.mapPartial (fn (z, SOME t) => SOME (z, t) | _ => NONE) (zs ~~ inst)
wenzelm@18583
    57
      end;
wenzelm@18583
    58
     val substs =
wenzelm@18583
    59
       map2 subst insts rules |> List.concat |> distinct
wenzelm@18583
    60
       |> map (pairself (Thm.cterm_of thy));
wenzelm@18583
    61
  in (((cases, concls), consumes), Drule.cterm_instantiate substs rule) end;
wenzelm@18283
    62
wenzelm@18299
    63
fun rename_params_rule internal xs rule =
wenzelm@18297
    64
  let
wenzelm@18299
    65
    val tune =
wenzelm@18299
    66
      if internal then Syntax.internal
wenzelm@18299
    67
      else fn x => the_default x (try Syntax.dest_internal x);
wenzelm@18299
    68
    val n = length xs;
wenzelm@18299
    69
    fun rename prem =
wenzelm@18299
    70
      let
wenzelm@18299
    71
        val ps = Logic.strip_params prem;
wenzelm@18299
    72
        val p = length ps;
wenzelm@18299
    73
        val ys =
wenzelm@18299
    74
          if p < n then []
wenzelm@18299
    75
          else map (tune o #1) (Library.take (p - n, ps)) @ xs;
wenzelm@18299
    76
      in Logic.list_rename_params (ys, prem) end;
wenzelm@18299
    77
    fun rename_prems prop =
wenzelm@18299
    78
      let val (As, C) = Logic.strip_horn (Thm.prop_of rule)
wenzelm@18299
    79
      in Logic.list_implies (map rename As, C) end;
wenzelm@18299
    80
  in Thm.equal_elim (Thm.reflexive (Drule.cterm_fun rename_prems (Thm.cprop_of rule))) rule end;
wenzelm@18297
    81
wenzelm@18283
    82
wenzelm@18288
    83
(* nominal_induct_tac *)
wenzelm@18283
    84
wenzelm@18583
    85
fun nominal_induct_tac ctxt def_insts avoiding fixings rules facts =
wenzelm@18283
    86
  let
wenzelm@18283
    87
    val thy = ProofContext.theory_of ctxt;
wenzelm@18283
    88
    val cert = Thm.cterm_of thy;
wenzelm@18283
    89
wenzelm@18583
    90
    val ((insts, defs), defs_ctxt) = fold_map InductMethod.add_defs def_insts ctxt |>> split_list;
wenzelm@18583
    91
    val atomized_defs = map (map ObjectLogic.atomize_thm) defs;
wenzelm@18283
    92
wenzelm@18583
    93
    val finish_rule = PolyML.print #>
wenzelm@18297
    94
      split_all_tuples
wenzelm@18583
    95
      #> rename_params_rule true (map (ProofContext.revert_skolem defs_ctxt o fst) avoiding) #> PolyML.print;
wenzelm@18299
    96
    fun rule_cases r = RuleCases.make true (SOME (Thm.prop_of r)) (InductMethod.rulified_term r);
wenzelm@18283
    97
  in
wenzelm@18297
    98
    (fn i => fn st =>
wenzelm@18583
    99
      rules
wenzelm@18583
   100
      |> inst_mutual_rule thy insts avoiding
wenzelm@18583
   101
      |> RuleCases.consume (List.concat defs) facts
wenzelm@18583
   102
      |> Seq.maps (fn (((cases, concls), (more_consumes, more_facts)), rule) =>
wenzelm@18583
   103
        (PRECISE_CONJUNCTS (length concls) (ALLGOALS (fn j =>
wenzelm@18583
   104
          (CONJUNCTS (ALLGOALS
wenzelm@18583
   105
            (Method.insert_tac (more_facts @ nth_list atomized_defs (j - 1))
wenzelm@18583
   106
              THEN' InductMethod.fix_tac defs_ctxt
wenzelm@18583
   107
                (nth concls (j - 1) + more_consumes)
wenzelm@18583
   108
                (nth_list fixings (j - 1))))
wenzelm@18583
   109
          THEN' InductMethod.inner_atomize_tac) j))
wenzelm@18583
   110
        THEN' InductMethod.atomize_tac) i st |> Seq.maps (fn st' =>
wenzelm@18583
   111
            InductMethod.guess_instance (finish_rule (InductMethod.internalize more_consumes rule)) i (PolyML.print st')
wenzelm@18583
   112
            |> Seq.maps (fn rule' =>
wenzelm@18583
   113
              CASES (rule_cases (PolyML.print rule') cases)
wenzelm@18583
   114
                (Tactic.rtac (rename_params_rule false [] rule') i THEN
wenzelm@18283
   115
                  PRIMSEQ (ProofContext.exports defs_ctxt ctxt)) st'))))
wenzelm@18283
   116
    THEN_ALL_NEW_CASES InductMethod.rulify_tac
wenzelm@18283
   117
  end;
wenzelm@18283
   118
wenzelm@18283
   119
wenzelm@18288
   120
(* concrete syntax *)
berghofe@17870
   121
berghofe@17870
   122
local
berghofe@17870
   123
wenzelm@18583
   124
val avoidingN = "avoiding";
wenzelm@18283
   125
val fixingN = "fixing";
wenzelm@18283
   126
val ruleN = "rule";
berghofe@17870
   127
wenzelm@18283
   128
val inst = Scan.lift (Args.$$$ "_") >> K NONE || Args.local_term >> SOME;
berghofe@17870
   129
wenzelm@18283
   130
val def_inst =
wenzelm@18283
   131
  ((Scan.lift (Args.name --| (Args.$$$ "\\<equiv>" || Args.$$$ "==")) >> SOME)
wenzelm@18283
   132
      -- Args.local_term) >> SOME ||
wenzelm@18283
   133
    inst >> Option.map (pair NONE);
urbanc@18099
   134
wenzelm@18283
   135
val free = Scan.state -- Args.local_term >> (fn (_, Free v) => v | (ctxt, t) =>
wenzelm@18283
   136
  error ("Bad free variable: " ^ ProofContext.string_of_term ctxt t));
wenzelm@18283
   137
wenzelm@18283
   138
fun unless_more_args scan = Scan.unless (Scan.lift
wenzelm@18583
   139
  ((Args.$$$ avoidingN || Args.$$$ fixingN || Args.$$$ ruleN) -- Args.colon)) scan;
wenzelm@18283
   140
berghofe@17870
   141
wenzelm@18583
   142
val avoiding = Scan.optional (Scan.lift (Args.$$$ avoidingN -- Args.colon) |--
wenzelm@18297
   143
  Scan.repeat (unless_more_args free)) [];
berghofe@17870
   144
wenzelm@18283
   145
val fixing = Scan.optional (Scan.lift (Args.$$$ fixingN -- Args.colon) |--
wenzelm@18583
   146
  Args.and_list (Scan.repeat (unless_more_args free))) [];
berghofe@17870
   147
wenzelm@18583
   148
val rule_spec = Scan.lift (Args.$$$ "rule" -- Args.colon) |-- Attrib.local_thmss;
berghofe@17870
   149
berghofe@17870
   150
in
berghofe@17870
   151
wenzelm@18283
   152
fun nominal_induct_method src =
wenzelm@18583
   153
  Method.syntax
wenzelm@18583
   154
   (Args.and_list (Scan.repeat (unless_more_args def_inst)) --
wenzelm@18583
   155
    avoiding -- fixing -- rule_spec) src
wenzelm@18283
   156
  #> (fn (ctxt, (((x, y), z), w)) =>
wenzelm@18283
   157
    Method.RAW_METHOD_CASES (fn facts =>
wenzelm@18283
   158
      HEADGOAL (nominal_induct_tac ctxt x y z w facts)));
berghofe@17870
   159
berghofe@17870
   160
end;
wenzelm@18283
   161
wenzelm@18283
   162
end;