src/HOL/Nominal/nominal_induct.ML
author wenzelm
Thu Jun 26 15:06:25 2008 +0200 (2008-06-26)
changeset 27370 8e8f96dfaf63
parent 26940 80df961f7900
child 27809 a1e409db516b
permissions -rw-r--r--
Args.context;
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@19903
    32
fun inst_mutual_rule ctxt insts avoiding rules =
wenzelm@18283
    33
  let
urbanc@22072
    34
urbanc@22072
    35
    val (nconcls, joined_rule) = RuleCases.strict_mutual_rule ctxt rules;
urbanc@22072
    36
    val concls = Logic.dest_conjunctions (Thm.concl_of joined_rule);
urbanc@22072
    37
    val (cases, consumes) = RuleCases.get joined_rule;
wenzelm@18583
    38
wenzelm@18583
    39
    val l = length rules;
wenzelm@18583
    40
    val _ =
wenzelm@18583
    41
      if length insts = l then ()
wenzelm@18583
    42
      else error ("Bad number of instantiations for " ^ string_of_int l ^ " rules");
wenzelm@18288
    43
urbanc@22072
    44
    fun subst inst concl =
wenzelm@18583
    45
      let
wenzelm@24830
    46
        val vars = Induct.vars_of concl;
wenzelm@18583
    47
        val m = length vars and n = length inst;
wenzelm@18583
    48
        val _ = if m >= n + 2 then () else error "Too few variables in conclusion of rule";
wenzelm@18583
    49
        val P :: x :: ys = vars;
wenzelm@18583
    50
        val zs = Library.drop (m - n - 2, ys);
wenzelm@18583
    51
      in
wenzelm@18583
    52
        (P, tuple_fun (map #2 avoiding) (Term.dest_Var P)) ::
wenzelm@18583
    53
        (x, tuple (map Free avoiding)) ::
wenzelm@18583
    54
        List.mapPartial (fn (z, SOME t) => SOME (z, t) | _ => NONE) (zs ~~ inst)
wenzelm@18583
    55
      end;
wenzelm@18583
    56
     val substs =
urbanc@22072
    57
       map2 subst insts concls |> List.concat |> distinct (op =)
wenzelm@19903
    58
       |> map (pairself (Thm.cterm_of (ProofContext.theory_of ctxt)));
urbanc@22072
    59
  in 
urbanc@22072
    60
    (((cases, nconcls), consumes), Drule.cterm_instantiate substs joined_rule) 
urbanc@22072
    61
  end;
wenzelm@18283
    62
wenzelm@18299
    63
fun rename_params_rule internal xs rule =
wenzelm@18297
    64
  let
wenzelm@18299
    65
    val tune =
wenzelm@20072
    66
      if internal then Name.internal
wenzelm@20072
    67
      else fn x => the_default x (try Name.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@24830
    90
    val ((insts, defs), defs_ctxt) = fold_map Induct.add_defs def_insts ctxt |>> split_list;
wenzelm@23591
    91
    val atomized_defs = map (map (Conv.fconv_rule ObjectLogic.atomize)) defs;
wenzelm@18283
    92
wenzelm@19115
    93
    val finish_rule =
wenzelm@18297
    94
      split_all_tuples
wenzelm@26712
    95
      #> rename_params_rule true
wenzelm@26712
    96
        (map (Name.clean o ProofContext.revert_skolem defs_ctxt o fst) avoiding);
wenzelm@24830
    97
    fun rule_cases r = RuleCases.make_nested true (Thm.prop_of r) (Induct.rulified_term r);
wenzelm@18283
    98
  in
wenzelm@18297
    99
    (fn i => fn st =>
wenzelm@18583
   100
      rules
wenzelm@19903
   101
      |> inst_mutual_rule ctxt insts avoiding
wenzelm@18583
   102
      |> RuleCases.consume (List.concat defs) facts
wenzelm@18583
   103
      |> Seq.maps (fn (((cases, concls), (more_consumes, more_facts)), rule) =>
wenzelm@18583
   104
        (PRECISE_CONJUNCTS (length concls) (ALLGOALS (fn j =>
wenzelm@18583
   105
          (CONJUNCTS (ALLGOALS
wenzelm@18583
   106
            (Method.insert_tac (more_facts @ nth_list atomized_defs (j - 1))
wenzelm@24830
   107
              THEN' Induct.fix_tac defs_ctxt
wenzelm@18583
   108
                (nth concls (j - 1) + more_consumes)
wenzelm@18583
   109
                (nth_list fixings (j - 1))))
wenzelm@24830
   110
          THEN' Induct.inner_atomize_tac) j))
wenzelm@24830
   111
        THEN' Induct.atomize_tac) i st |> Seq.maps (fn st' =>
wenzelm@26940
   112
            Induct.guess_instance ctxt
wenzelm@24830
   113
              (finish_rule (Induct.internalize more_consumes rule)) i st'
wenzelm@18583
   114
            |> Seq.maps (fn rule' =>
wenzelm@19115
   115
              CASES (rule_cases rule' cases)
wenzelm@18583
   116
                (Tactic.rtac (rename_params_rule false [] rule') i THEN
wenzelm@20288
   117
                  PRIMITIVE (singleton (ProofContext.export defs_ctxt ctxt))) st'))))
wenzelm@24830
   118
    THEN_ALL_NEW_CASES Induct.rulify_tac
wenzelm@18283
   119
  end;
wenzelm@18283
   120
wenzelm@18283
   121
wenzelm@18288
   122
(* concrete syntax *)
berghofe@17870
   123
berghofe@17870
   124
local
berghofe@17870
   125
wenzelm@18583
   126
val avoidingN = "avoiding";
urbanc@20998
   127
val fixingN = "arbitrary";  (* to be consistent with induct; hopefully this changes again *)
wenzelm@18283
   128
val ruleN = "rule";
berghofe@17870
   129
berghofe@19036
   130
val inst = Scan.lift (Args.$$$ "_") >> K NONE || Args.term >> SOME;
berghofe@17870
   131
wenzelm@18283
   132
val def_inst =
wenzelm@25985
   133
  ((Scan.lift (Args.name --| (Args.$$$ "\<equiv>" || Args.$$$ "==")) >> SOME)
berghofe@19036
   134
      -- Args.term) >> SOME ||
wenzelm@18283
   135
    inst >> Option.map (pair NONE);
urbanc@18099
   136
wenzelm@27370
   137
val free = Args.context -- Args.term >> (fn (_, Free v) => v | (ctxt, t) =>
wenzelm@27370
   138
  error ("Bad free variable: " ^ Syntax.string_of_term ctxt t));
wenzelm@18283
   139
wenzelm@18283
   140
fun unless_more_args scan = Scan.unless (Scan.lift
wenzelm@18583
   141
  ((Args.$$$ avoidingN || Args.$$$ fixingN || Args.$$$ ruleN) -- Args.colon)) scan;
wenzelm@18283
   142
berghofe@17870
   143
wenzelm@18583
   144
val avoiding = Scan.optional (Scan.lift (Args.$$$ avoidingN -- Args.colon) |--
wenzelm@18297
   145
  Scan.repeat (unless_more_args free)) [];
berghofe@17870
   146
wenzelm@18283
   147
val fixing = Scan.optional (Scan.lift (Args.$$$ fixingN -- Args.colon) |--
wenzelm@18583
   148
  Args.and_list (Scan.repeat (unless_more_args free))) [];
berghofe@17870
   149
berghofe@19036
   150
val rule_spec = Scan.lift (Args.$$$ "rule" -- Args.colon) |-- Attrib.thms;
berghofe@17870
   151
berghofe@17870
   152
in
berghofe@17870
   153
wenzelm@18283
   154
fun nominal_induct_method src =
wenzelm@18583
   155
  Method.syntax
wenzelm@18583
   156
   (Args.and_list (Scan.repeat (unless_more_args def_inst)) --
wenzelm@18583
   157
    avoiding -- fixing -- rule_spec) src
haftmann@21879
   158
  #> (fn ((((x, y), z), w), ctxt) =>
wenzelm@18283
   159
    Method.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;