src/HOL/Nominal/nominal_induct.ML
author haftmann
Tue Nov 24 17:28:25 2009 +0100 (2009-11-24)
changeset 33955 fff6f11b1f09
parent 33368 b1cf34f1855c
child 33957 e9afca2118d4
permissions -rw-r--r--
curried take/drop
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
haftmann@29581
     8
  val nominal_induct_tac: Proof.context -> (binding option * term) 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@18288
    24
val split_all_tuples =
wenzelm@18288
    25
  Simplifier.full_simplify (HOL_basic_ss addsimps
berghofe@30092
    26
    [split_conv, split_paired_all, 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@33955
    49
        val zs = (uncurry 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@19903
    57
       |> map (pairself (Thm.cterm_of (ProofContext.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@20072
    66
      else fn x => the_default x (try Name.dest_internal x);
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@33955
    74
          else map (tune o #1) ((uncurry take) (p - n, ps)) @ xs;
wenzelm@18299
    75
      in Logic.list_rename_params (ys, prem) end;
wenzelm@18299
    76
    fun rename_prems prop =
wenzelm@18299
    77
      let val (As, C) = Logic.strip_horn (Thm.prop_of rule)
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
wenzelm@18583
    84
fun nominal_induct_tac ctxt def_insts avoiding fixings rules facts =
wenzelm@18283
    85
  let
wenzelm@18283
    86
    val thy = ProofContext.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@23591
    90
    val atomized_defs = map (map (Conv.fconv_rule ObjectLogic.atomize)) defs;
wenzelm@18283
    91
wenzelm@19115
    92
    val finish_rule =
wenzelm@18297
    93
      split_all_tuples
wenzelm@26712
    94
      #> rename_params_rule true
wenzelm@26712
    95
        (map (Name.clean o ProofContext.revert_skolem defs_ctxt o fst) avoiding);
wenzelm@33368
    96
    fun rule_cases r = Rule_Cases.make_nested true (Thm.prop_of r) (Induct.rulified_term r);
wenzelm@18283
    97
  in
wenzelm@18297
    98
    (fn i => fn st =>
wenzelm@18583
    99
      rules
wenzelm@19903
   100
      |> inst_mutual_rule ctxt insts avoiding
wenzelm@33368
   101
      |> Rule_Cases.consume (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
wenzelm@18583
   105
            (Method.insert_tac (more_facts @ nth_list atomized_defs (j - 1))
wenzelm@24830
   106
              THEN' Induct.fix_tac defs_ctxt
wenzelm@18583
   107
                (nth concls (j - 1) + more_consumes)
wenzelm@18583
   108
                (nth_list fixings (j - 1))))
wenzelm@24830
   109
          THEN' Induct.inner_atomize_tac) j))
wenzelm@24830
   110
        THEN' Induct.atomize_tac) i st |> Seq.maps (fn st' =>
wenzelm@26940
   111
            Induct.guess_instance ctxt
wenzelm@24830
   112
              (finish_rule (Induct.internalize more_consumes rule)) i st'
wenzelm@18583
   113
            |> Seq.maps (fn rule' =>
wenzelm@19115
   114
              CASES (rule_cases rule' cases)
wenzelm@18583
   115
                (Tactic.rtac (rename_params_rule false [] rule') i THEN
wenzelm@20288
   116
                  PRIMITIVE (singleton (ProofContext.export defs_ctxt ctxt))) st'))))
wenzelm@24830
   117
    THEN_ALL_NEW_CASES Induct.rulify_tac
wenzelm@18283
   118
  end;
wenzelm@18283
   119
wenzelm@18283
   120
wenzelm@18288
   121
(* concrete syntax *)
berghofe@17870
   122
berghofe@17870
   123
local
berghofe@17870
   124
wenzelm@27809
   125
structure P = OuterParse;
wenzelm@27809
   126
wenzelm@18583
   127
val avoidingN = "avoiding";
urbanc@20998
   128
val fixingN = "arbitrary";  (* to be consistent with induct; hopefully this changes again *)
wenzelm@18283
   129
val ruleN = "rule";
berghofe@17870
   130
berghofe@19036
   131
val inst = Scan.lift (Args.$$$ "_") >> K NONE || Args.term >> SOME;
berghofe@17870
   132
wenzelm@18283
   133
val def_inst =
wenzelm@28083
   134
  ((Scan.lift (Args.binding --| (Args.$$$ "\<equiv>" || Args.$$$ "==")) >> SOME)
berghofe@19036
   135
      -- Args.term) >> SOME ||
wenzelm@18283
   136
    inst >> Option.map (pair NONE);
urbanc@18099
   137
wenzelm@27370
   138
val free = Args.context -- Args.term >> (fn (_, Free v) => v | (ctxt, t) =>
wenzelm@27370
   139
  error ("Bad free variable: " ^ Syntax.string_of_term ctxt t));
wenzelm@18283
   140
wenzelm@18283
   141
fun unless_more_args scan = Scan.unless (Scan.lift
wenzelm@18583
   142
  ((Args.$$$ avoidingN || Args.$$$ fixingN || Args.$$$ ruleN) -- Args.colon)) scan;
wenzelm@18283
   143
berghofe@17870
   144
wenzelm@18583
   145
val avoiding = Scan.optional (Scan.lift (Args.$$$ avoidingN -- Args.colon) |--
wenzelm@18297
   146
  Scan.repeat (unless_more_args free)) [];
berghofe@17870
   147
wenzelm@18283
   148
val fixing = Scan.optional (Scan.lift (Args.$$$ fixingN -- Args.colon) |--
wenzelm@27809
   149
  P.and_list' (Scan.repeat (unless_more_args free))) [];
berghofe@17870
   150
berghofe@19036
   151
val rule_spec = Scan.lift (Args.$$$ "rule" -- Args.colon) |-- Attrib.thms;
berghofe@17870
   152
berghofe@17870
   153
in
berghofe@17870
   154
wenzelm@30549
   155
val nominal_induct_method =
wenzelm@30549
   156
  P.and_list' (Scan.repeat (unless_more_args def_inst)) --
wenzelm@30549
   157
  avoiding -- fixing -- rule_spec >>
wenzelm@30549
   158
  (fn (((x, y), z), w) => fn ctxt =>
wenzelm@30510
   159
    RAW_METHOD_CASES (fn facts =>
wenzelm@18283
   160
      HEADGOAL (nominal_induct_tac ctxt x y z w facts)));
berghofe@17870
   161
berghofe@17870
   162
end;
wenzelm@18283
   163
wenzelm@18283
   164
end;