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