src/HOL/Nominal/nominal_induct.ML
author wenzelm
Tue Sep 26 20:54:40 2017 +0200 (23 months ago)
changeset 66695 91500c024c7f
parent 62178 c3c98ed94b0f
permissions -rw-r--r--
tuned;
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
wenzelm@61841
     8
  val nominal_induct_tac: bool -> (binding option * (term * bool)) option list list ->
wenzelm@18583
     9
    (string * typ) list -> (string * typ) list list -> thm list ->
wenzelm@61841
    10
    thm list -> int -> context_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) =
haftmann@61424
    21
  Library.funpow (length Ts) HOLogic.mk_case_prod
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@60787
    57
       |> map (fn (t, u) => (#1 (dest_Var t), Thm.cterm_of ctxt u));
urbanc@22072
    58
  in 
wenzelm@60787
    59
    (((cases, nconcls), consumes), infer_instantiate ctxt 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@60313
    79
  in Thm.renamed_prop (rename_prems (Thm.prop_of rule)) rule end;
wenzelm@18297
    80
wenzelm@18283
    81
wenzelm@18288
    82
(* nominal_induct_tac *)
wenzelm@18283
    83
wenzelm@61841
    84
fun nominal_induct_tac simp def_insts avoiding fixings rules facts i (ctxt, st) =
wenzelm@18283
    85
  let
wenzelm@24830
    86
    val ((insts, defs), defs_ctxt) = fold_map Induct.add_defs def_insts ctxt |>> split_list;
wenzelm@54742
    87
    val atomized_defs = map (map (Conv.fconv_rule (Induct.atomize_cterm ctxt))) defs;
wenzelm@18283
    88
wenzelm@19115
    89
    val finish_rule =
wenzelm@51717
    90
      split_all_tuples defs_ctxt
wenzelm@26712
    91
      #> rename_params_rule true
wenzelm@42488
    92
        (map (Name.clean o Variable.revert_fixed defs_ctxt o fst) avoiding);
berghofe@34907
    93
berghofe@34907
    94
    fun rule_cases ctxt r =
berghofe@34907
    95
      let val r' = if simp then Induct.simplified_rule ctxt r else r
wenzelm@59970
    96
      in Rule_Cases.make_nested ctxt (Thm.prop_of r') (Induct.rulified_term ctxt r') end;
wenzelm@61841
    97
wenzelm@61841
    98
    fun context_tac _ _ =
wenzelm@18583
    99
      rules
wenzelm@19903
   100
      |> inst_mutual_rule ctxt insts avoiding
wenzelm@54742
   101
      |> Rule_Cases.consume ctxt (flat 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
berghofe@34907
   105
            let
berghofe@34907
   106
              val adefs = nth_list atomized_defs (j - 1);
wenzelm@59582
   107
              val frees = fold (Term.add_frees o Thm.prop_of) adefs [];
berghofe@34907
   108
              val xs = nth_list fixings (j - 1);
berghofe@34907
   109
              val k = nth concls (j - 1) + more_consumes
berghofe@34907
   110
            in
wenzelm@61841
   111
              Method.insert_tac ctxt (more_facts @ adefs) THEN'
berghofe@34907
   112
                (if simp then
berghofe@34907
   113
                   Induct.rotate_tac k (length adefs) THEN'
wenzelm@45132
   114
                   Induct.arbitrary_tac defs_ctxt k (List.partition (member op = frees) xs |> op @)
berghofe@34907
   115
                 else
wenzelm@45132
   116
                   Induct.arbitrary_tac defs_ctxt k xs)
berghofe@34907
   117
            end)
wenzelm@54742
   118
          THEN' Induct.inner_atomize_tac defs_ctxt) j))
wenzelm@54742
   119
        THEN' Induct.atomize_tac ctxt) i st |> Seq.maps (fn st' =>
wenzelm@26940
   120
            Induct.guess_instance ctxt
wenzelm@54742
   121
              (finish_rule (Induct.internalize ctxt more_consumes rule)) i st'
wenzelm@18583
   122
            |> Seq.maps (fn rule' =>
wenzelm@61841
   123
              CONTEXT_CASES (rule_cases ctxt rule' cases)
wenzelm@60754
   124
                (resolve_tac ctxt [rename_params_rule false [] rule'] i THEN
wenzelm@61841
   125
                  PRIMITIVE (singleton (Proof_Context.export defs_ctxt ctxt))) (ctxt, st'))));
wenzelm@61841
   126
  in
wenzelm@61841
   127
    (context_tac CONTEXT_THEN_ALL_NEW
wenzelm@58957
   128
      ((if simp then Induct.simplify_tac ctxt THEN' (TRY o Induct.trivial_tac ctxt)
wenzelm@61841
   129
        else K all_tac) THEN_ALL_NEW Induct.rulify_tac ctxt)) i (ctxt, st)
wenzelm@18283
   130
  end;
wenzelm@18283
   131
wenzelm@18283
   132
wenzelm@18288
   133
(* concrete syntax *)
berghofe@17870
   134
berghofe@17870
   135
local
berghofe@17870
   136
wenzelm@18583
   137
val avoidingN = "avoiding";
urbanc@20998
   138
val fixingN = "arbitrary";  (* to be consistent with induct; hopefully this changes again *)
wenzelm@18283
   139
val ruleN = "rule";
berghofe@17870
   140
berghofe@34907
   141
val inst = Scan.lift (Args.$$$ "_") >> K NONE ||
berghofe@34907
   142
  Args.term >> (SOME o rpair false) ||
berghofe@34907
   143
  Scan.lift (Args.$$$ "(") |-- (Args.term >> (SOME o rpair true)) --|
berghofe@34907
   144
    Scan.lift (Args.$$$ ")");
berghofe@17870
   145
wenzelm@18283
   146
val def_inst =
wenzelm@28083
   147
  ((Scan.lift (Args.binding --| (Args.$$$ "\<equiv>" || Args.$$$ "==")) >> SOME)
berghofe@34907
   148
      -- (Args.term >> rpair false)) >> SOME ||
wenzelm@18283
   149
    inst >> Option.map (pair NONE);
urbanc@18099
   150
wenzelm@27370
   151
val free = Args.context -- Args.term >> (fn (_, Free v) => v | (ctxt, t) =>
wenzelm@27370
   152
  error ("Bad free variable: " ^ Syntax.string_of_term ctxt t));
wenzelm@18283
   153
wenzelm@18283
   154
fun unless_more_args scan = Scan.unless (Scan.lift
wenzelm@18583
   155
  ((Args.$$$ avoidingN || Args.$$$ fixingN || Args.$$$ ruleN) -- Args.colon)) scan;
wenzelm@18283
   156
berghofe@17870
   157
wenzelm@18583
   158
val avoiding = Scan.optional (Scan.lift (Args.$$$ avoidingN -- Args.colon) |--
wenzelm@18297
   159
  Scan.repeat (unless_more_args free)) [];
berghofe@17870
   160
wenzelm@18283
   161
val fixing = Scan.optional (Scan.lift (Args.$$$ fixingN -- Args.colon) |--
wenzelm@36960
   162
  Parse.and_list' (Scan.repeat (unless_more_args free))) [];
berghofe@17870
   163
berghofe@19036
   164
val rule_spec = Scan.lift (Args.$$$ "rule" -- Args.colon) |-- Attrib.thms;
berghofe@17870
   165
berghofe@17870
   166
in
berghofe@17870
   167
wenzelm@62178
   168
val nominal_induct_method : (Proof.context -> Proof.method) context_parser =
wenzelm@53168
   169
  Scan.lift (Args.mode Induct.no_simpN) --
wenzelm@53168
   170
  (Parse.and_list' (Scan.repeat (unless_more_args def_inst)) --
wenzelm@53168
   171
    avoiding -- fixing -- rule_spec) >>
wenzelm@61841
   172
  (fn (no_simp, (((x, y), z), w)) => fn _ => fn facts =>
wenzelm@61841
   173
    (nominal_induct_tac (not no_simp) x y z w facts 1));
berghofe@17870
   174
berghofe@17870
   175
end;
wenzelm@18283
   176
wenzelm@18283
   177
end;