src/HOL/Nominal/nominal_induct.ML
author wenzelm
Sun Nov 09 17:04:14 2014 +0100 (2014-11-09)
changeset 58957 c9e744ea8a38
parent 58002 0ed1e999a0fb
child 59058 a78612c67ec0
permissions -rw-r--r--
proper context for match_tac etc.;
berghofe@30092
     1
(*  Author:     Christian Urban and Makarius
wenzelm@18283
     2
wenzelm@18288
     3
The nominal induct proof method.
wenzelm@18283
     4
*)
wenzelm@18283
     5
wenzelm@18283
     6
structure NominalInduct:
wenzelm@18283
     7
sig
berghofe@34907
     8
  val nominal_induct_tac: Proof.context -> bool -> (binding option * (term * bool)) option list list ->
wenzelm@18583
     9
    (string * typ) list -> (string * typ) list list -> thm list ->
wenzelm@33368
    10
    thm list -> int -> Rule_Cases.cases_tactic
wenzelm@30549
    11
  val nominal_induct_method: (Proof.context -> Proof.method) context_parser
wenzelm@18283
    12
end =
wenzelm@18283
    13
struct
wenzelm@18283
    14
wenzelm@18288
    15
(* proper tuples -- nested left *)
wenzelm@18283
    16
wenzelm@18288
    17
fun tupleT Ts = HOLogic.unitT |> fold (fn T => fn U => HOLogic.mk_prodT (U, T)) Ts;
wenzelm@18288
    18
fun tuple ts = HOLogic.unit |> fold (fn t => fn u => HOLogic.mk_prod (u, t)) ts;
wenzelm@18288
    19
wenzelm@18288
    20
fun tuple_fun Ts (xi, T) =
wenzelm@18288
    21
  Library.funpow (length Ts) HOLogic.mk_split
wenzelm@18288
    22
    (Var (xi, (HOLogic.unitT :: Ts) ---> Term.range_type T));
wenzelm@18283
    23
wenzelm@51717
    24
fun split_all_tuples ctxt =
wenzelm@51717
    25
  Simplifier.full_simplify (put_simpset HOL_basic_ss ctxt addsimps
haftmann@37137
    26
    [@{thm split_conv}, @{thm split_paired_all}, @{thm unit_all_eq1}, @{thm fresh_unit_elim}, @{thm fresh_prod_elim}] @
berghofe@30092
    27
    @{thms fresh_star_unit_elim} @ @{thms fresh_star_prod_elim});
wenzelm@18283
    28
wenzelm@18288
    29
wenzelm@18297
    30
(* prepare rule *)
wenzelm@18288
    31
wenzelm@19903
    32
fun inst_mutual_rule ctxt insts avoiding rules =
wenzelm@18283
    33
  let
wenzelm@33368
    34
    val (nconcls, joined_rule) = Rule_Cases.strict_mutual_rule ctxt rules;
urbanc@22072
    35
    val concls = Logic.dest_conjunctions (Thm.concl_of joined_rule);
wenzelm@33368
    36
    val (cases, consumes) = Rule_Cases.get joined_rule;
wenzelm@18583
    37
wenzelm@18583
    38
    val l = length rules;
wenzelm@18583
    39
    val _ =
wenzelm@18583
    40
      if length insts = l then ()
wenzelm@18583
    41
      else error ("Bad number of instantiations for " ^ string_of_int l ^ " rules");
wenzelm@18288
    42
urbanc@22072
    43
    fun subst inst concl =
wenzelm@18583
    44
      let
wenzelm@24830
    45
        val vars = Induct.vars_of concl;
wenzelm@18583
    46
        val m = length vars and n = length inst;
wenzelm@18583
    47
        val _ = if m >= n + 2 then () else error "Too few variables in conclusion of rule";
wenzelm@18583
    48
        val P :: x :: ys = vars;
haftmann@33957
    49
        val zs = drop (m - n - 2) ys;
wenzelm@18583
    50
      in
wenzelm@18583
    51
        (P, tuple_fun (map #2 avoiding) (Term.dest_Var P)) ::
wenzelm@18583
    52
        (x, tuple (map Free avoiding)) ::
wenzelm@32952
    53
        map_filter (fn (z, SOME t) => SOME (z, t) | _ => NONE) (zs ~~ inst)
wenzelm@18583
    54
      end;
wenzelm@18583
    55
     val substs =
wenzelm@32952
    56
       map2 subst insts concls |> flat |> distinct (op =)
wenzelm@42361
    57
       |> map (pairself (Thm.cterm_of (Proof_Context.theory_of ctxt)));
urbanc@22072
    58
  in 
urbanc@22072
    59
    (((cases, nconcls), consumes), Drule.cterm_instantiate substs joined_rule) 
urbanc@22072
    60
  end;
wenzelm@18283
    61
wenzelm@18299
    62
fun rename_params_rule internal xs rule =
wenzelm@18297
    63
  let
wenzelm@18299
    64
    val tune =
wenzelm@20072
    65
      if internal then Name.internal
wenzelm@55947
    66
      else perhaps (try Name.dest_internal);
wenzelm@18299
    67
    val n = length xs;
wenzelm@18299
    68
    fun rename prem =
wenzelm@18299
    69
      let
wenzelm@18299
    70
        val ps = Logic.strip_params prem;
wenzelm@18299
    71
        val p = length ps;
wenzelm@18299
    72
        val ys =
wenzelm@18299
    73
          if p < n then []
haftmann@33957
    74
          else map (tune o #1) (take (p - n) ps) @ xs;
wenzelm@45328
    75
      in Logic.list_rename_params ys prem end;
wenzelm@18299
    76
    fun rename_prems prop =
berghofe@34907
    77
      let val (As, C) = Logic.strip_horn prop
wenzelm@18299
    78
      in Logic.list_implies (map rename As, C) end;
wenzelm@18299
    79
  in Thm.equal_elim (Thm.reflexive (Drule.cterm_fun rename_prems (Thm.cprop_of rule))) rule end;
wenzelm@18297
    80
wenzelm@18283
    81
wenzelm@18288
    82
(* nominal_induct_tac *)
wenzelm@18283
    83
berghofe@34907
    84
fun nominal_induct_tac ctxt simp def_insts avoiding fixings rules facts =
wenzelm@18283
    85
  let
wenzelm@42361
    86
    val thy = Proof_Context.theory_of ctxt;
wenzelm@18283
    87
    val cert = Thm.cterm_of thy;
wenzelm@18283
    88
wenzelm@24830
    89
    val ((insts, defs), defs_ctxt) = fold_map Induct.add_defs def_insts ctxt |>> split_list;
wenzelm@54742
    90
    val atomized_defs = map (map (Conv.fconv_rule (Induct.atomize_cterm ctxt))) defs;
wenzelm@18283
    91
wenzelm@19115
    92
    val finish_rule =
wenzelm@51717
    93
      split_all_tuples defs_ctxt
wenzelm@26712
    94
      #> rename_params_rule true
wenzelm@42488
    95
        (map (Name.clean o Variable.revert_fixed defs_ctxt o fst) avoiding);
berghofe@34907
    96
berghofe@34907
    97
    fun rule_cases ctxt r =
berghofe@34907
    98
      let val r' = if simp then Induct.simplified_rule ctxt r else r
berghofe@34907
    99
      in Rule_Cases.make_nested (Thm.prop_of r') (Induct.rulified_term r') end;
wenzelm@18283
   100
  in
wenzelm@18297
   101
    (fn i => fn st =>
wenzelm@18583
   102
      rules
wenzelm@19903
   103
      |> inst_mutual_rule ctxt insts avoiding
wenzelm@54742
   104
      |> Rule_Cases.consume ctxt (flat defs) facts
wenzelm@18583
   105
      |> Seq.maps (fn (((cases, concls), (more_consumes, more_facts)), rule) =>
wenzelm@18583
   106
        (PRECISE_CONJUNCTS (length concls) (ALLGOALS (fn j =>
wenzelm@18583
   107
          (CONJUNCTS (ALLGOALS
berghofe@34907
   108
            let
berghofe@34907
   109
              val adefs = nth_list atomized_defs (j - 1);
berghofe@34907
   110
              val frees = fold (Term.add_frees o prop_of) adefs [];
berghofe@34907
   111
              val xs = nth_list fixings (j - 1);
berghofe@34907
   112
              val k = nth concls (j - 1) + more_consumes
berghofe@34907
   113
            in
berghofe@34907
   114
              Method.insert_tac (more_facts @ adefs) THEN'
berghofe@34907
   115
                (if simp then
berghofe@34907
   116
                   Induct.rotate_tac k (length adefs) THEN'
wenzelm@45132
   117
                   Induct.arbitrary_tac defs_ctxt k (List.partition (member op = frees) xs |> op @)
berghofe@34907
   118
                 else
wenzelm@45132
   119
                   Induct.arbitrary_tac defs_ctxt k xs)
berghofe@34907
   120
            end)
wenzelm@54742
   121
          THEN' Induct.inner_atomize_tac defs_ctxt) j))
wenzelm@54742
   122
        THEN' Induct.atomize_tac ctxt) i st |> Seq.maps (fn st' =>
wenzelm@26940
   123
            Induct.guess_instance ctxt
wenzelm@54742
   124
              (finish_rule (Induct.internalize ctxt more_consumes rule)) i st'
wenzelm@18583
   125
            |> Seq.maps (fn rule' =>
berghofe@34907
   126
              CASES (rule_cases ctxt rule' cases)
wenzelm@52732
   127
                (rtac (rename_params_rule false [] rule') i THEN
wenzelm@42361
   128
                  PRIMITIVE (singleton (Proof_Context.export defs_ctxt ctxt))) st'))))
berghofe@34907
   129
    THEN_ALL_NEW_CASES
wenzelm@58957
   130
      ((if simp then Induct.simplify_tac ctxt THEN' (TRY o Induct.trivial_tac ctxt)
berghofe@34907
   131
        else K all_tac)
wenzelm@54742
   132
       THEN_ALL_NEW Induct.rulify_tac ctxt)
wenzelm@18283
   133
  end;
wenzelm@18283
   134
wenzelm@18283
   135
wenzelm@18288
   136
(* concrete syntax *)
berghofe@17870
   137
berghofe@17870
   138
local
berghofe@17870
   139
wenzelm@18583
   140
val avoidingN = "avoiding";
urbanc@20998
   141
val fixingN = "arbitrary";  (* to be consistent with induct; hopefully this changes again *)
wenzelm@18283
   142
val ruleN = "rule";
berghofe@17870
   143
berghofe@34907
   144
val inst = Scan.lift (Args.$$$ "_") >> K NONE ||
berghofe@34907
   145
  Args.term >> (SOME o rpair false) ||
berghofe@34907
   146
  Scan.lift (Args.$$$ "(") |-- (Args.term >> (SOME o rpair true)) --|
berghofe@34907
   147
    Scan.lift (Args.$$$ ")");
berghofe@17870
   148
wenzelm@18283
   149
val def_inst =
wenzelm@28083
   150
  ((Scan.lift (Args.binding --| (Args.$$$ "\<equiv>" || Args.$$$ "==")) >> SOME)
berghofe@34907
   151
      -- (Args.term >> rpair false)) >> SOME ||
wenzelm@18283
   152
    inst >> Option.map (pair NONE);
urbanc@18099
   153
wenzelm@27370
   154
val free = Args.context -- Args.term >> (fn (_, Free v) => v | (ctxt, t) =>
wenzelm@27370
   155
  error ("Bad free variable: " ^ Syntax.string_of_term ctxt t));
wenzelm@18283
   156
wenzelm@18283
   157
fun unless_more_args scan = Scan.unless (Scan.lift
wenzelm@18583
   158
  ((Args.$$$ avoidingN || Args.$$$ fixingN || Args.$$$ ruleN) -- Args.colon)) scan;
wenzelm@18283
   159
berghofe@17870
   160
wenzelm@18583
   161
val avoiding = Scan.optional (Scan.lift (Args.$$$ avoidingN -- Args.colon) |--
wenzelm@18297
   162
  Scan.repeat (unless_more_args free)) [];
berghofe@17870
   163
wenzelm@18283
   164
val fixing = Scan.optional (Scan.lift (Args.$$$ fixingN -- Args.colon) |--
wenzelm@36960
   165
  Parse.and_list' (Scan.repeat (unless_more_args free))) [];
berghofe@17870
   166
berghofe@19036
   167
val rule_spec = Scan.lift (Args.$$$ "rule" -- Args.colon) |-- Attrib.thms;
berghofe@17870
   168
berghofe@17870
   169
in
berghofe@17870
   170
wenzelm@30549
   171
val nominal_induct_method =
wenzelm@53168
   172
  Scan.lift (Args.mode Induct.no_simpN) --
wenzelm@53168
   173
  (Parse.and_list' (Scan.repeat (unless_more_args def_inst)) --
wenzelm@53168
   174
    avoiding -- fixing -- rule_spec) >>
wenzelm@58002
   175
  (fn (no_simp, (((x, y), z), w)) => fn ctxt => fn facts =>
wenzelm@58002
   176
    HEADGOAL (nominal_induct_tac ctxt (not no_simp) x y z w facts));
berghofe@17870
   177
berghofe@17870
   178
end;
wenzelm@18283
   179
wenzelm@18283
   180
end;