src/HOL/Nominal/nominal_induct.ML
author wenzelm
Sat Jan 26 20:01:37 2008 +0100 (2008-01-26)
changeset 25985 8d69087f6a4b
parent 24920 2a45e400fdad
child 26712 e2dcda7b0401
permissions -rw-r--r--
avoid redundant escaping of Isabelle symbols;
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@19115
    95
      #> rename_params_rule true (map (ProofContext.revert_skolem defs_ctxt o fst) avoiding);
wenzelm@24830
    96
    fun rule_cases r = RuleCases.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@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@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@24830
   111
            Induct.guess_instance
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@18583
   125
val avoidingN = "avoiding";
urbanc@20998
   126
val fixingN = "arbitrary";  (* to be consistent with induct; hopefully this changes again *)
wenzelm@18283
   127
val ruleN = "rule";
berghofe@17870
   128
berghofe@19036
   129
val inst = Scan.lift (Args.$$$ "_") >> K NONE || Args.term >> SOME;
berghofe@17870
   130
wenzelm@18283
   131
val def_inst =
wenzelm@25985
   132
  ((Scan.lift (Args.name --| (Args.$$$ "\<equiv>" || Args.$$$ "==")) >> SOME)
berghofe@19036
   133
      -- Args.term) >> SOME ||
wenzelm@18283
   134
    inst >> Option.map (pair NONE);
urbanc@18099
   135
berghofe@19036
   136
val free = Scan.state -- Args.term >> (fn (_, Free v) => v | (ctxt, t) =>
wenzelm@24920
   137
  error ("Bad free variable: " ^ Syntax.string_of_term (Context.proof_of ctxt) t));
wenzelm@18283
   138
wenzelm@18283
   139
fun unless_more_args scan = Scan.unless (Scan.lift
wenzelm@18583
   140
  ((Args.$$$ avoidingN || Args.$$$ fixingN || Args.$$$ ruleN) -- Args.colon)) scan;
wenzelm@18283
   141
berghofe@17870
   142
wenzelm@18583
   143
val avoiding = Scan.optional (Scan.lift (Args.$$$ avoidingN -- Args.colon) |--
wenzelm@18297
   144
  Scan.repeat (unless_more_args free)) [];
berghofe@17870
   145
wenzelm@18283
   146
val fixing = Scan.optional (Scan.lift (Args.$$$ fixingN -- Args.colon) |--
wenzelm@18583
   147
  Args.and_list (Scan.repeat (unless_more_args free))) [];
berghofe@17870
   148
berghofe@19036
   149
val rule_spec = Scan.lift (Args.$$$ "rule" -- Args.colon) |-- Attrib.thms;
berghofe@17870
   150
berghofe@17870
   151
in
berghofe@17870
   152
wenzelm@18283
   153
fun nominal_induct_method src =
wenzelm@18583
   154
  Method.syntax
wenzelm@18583
   155
   (Args.and_list (Scan.repeat (unless_more_args def_inst)) --
wenzelm@18583
   156
    avoiding -- fixing -- rule_spec) src
haftmann@21879
   157
  #> (fn ((((x, y), z), w), ctxt) =>
wenzelm@18283
   158
    Method.RAW_METHOD_CASES (fn facts =>
wenzelm@18283
   159
      HEADGOAL (nominal_induct_tac ctxt x y z w facts)));
berghofe@17870
   160
berghofe@17870
   161
end;
wenzelm@18283
   162
wenzelm@18283
   163
end;