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