Nominal/activities/tphols09/IDW/simple_inductive_package.ML
changeset 415 f1be8028a4a9
equal deleted inserted replaced
414:05e5d68c9627 415:f1be8028a4a9
       
     1 signature SIMPLE_INDUCTIVE_PACKAGE =
       
     2 sig
       
     3   val add_inductive_i:
       
     4     ((binding * typ) * mixfix) list ->
       
     5     (binding * typ) list ->
       
     6     (Attrib.binding * term) list ->
       
     7     local_theory -> (thm list * thm list) * local_theory
       
     8   val add_inductive:
       
     9     (binding * string option * mixfix) list ->
       
    10     (binding * string option * mixfix) list ->
       
    11     (Attrib.binding * string) list ->
       
    12     local_theory -> (thm list * thm list) * local_theory
       
    13 end;
       
    14 
       
    15 structure SimpleInductivePackage: SIMPLE_INDUCTIVE_PACKAGE =
       
    16 struct
       
    17 
       
    18 fun add_inductive_i preds_syn params intrs lthy =
       
    19   let
       
    20     val params' = map (fn (p, T) => Free (Binding.name_of p, T)) params;
       
    21     val preds = map (fn ((R, T), _) =>
       
    22       list_comb (Free (Binding.name_of R, T), params')) preds_syn;
       
    23     val Tss = map (binder_types o fastype_of) preds;
       
    24 
       
    25     (* making the definition *)
       
    26 
       
    27     val intrs' = map
       
    28       (ObjectLogic.atomize_term (ProofContext.theory_of lthy) o snd) intrs;
       
    29 
       
    30     fun mk_all x P = HOLogic.all_const (fastype_of x) $ lambda x P;
       
    31 
       
    32     val (defs, lthy1) = fold_map (fn ((((R, _), syn), pred), Ts) =>
       
    33       let val zs = map Free (Variable.variant_frees lthy intrs'
       
    34         (map (pair "z") Ts))
       
    35       in
       
    36         LocalTheory.define Thm.internalK
       
    37           ((R, syn), (Attrib.empty_binding, fold_rev lambda (params' @ zs)
       
    38             (fold_rev mk_all preds (fold_rev (curry HOLogic.mk_imp)
       
    39                intrs' (list_comb (pred, zs)))))) #>> snd #>> snd
       
    40        end) (preds_syn ~~ preds ~~ Tss) lthy;
       
    41 
       
    42     val (_, lthy2) = Variable.add_fixes (map (Binding.name_of o fst) params) lthy1;
       
    43 
       
    44 
       
    45     (* proving the induction rules *)
       
    46 
       
    47     val (Pnames, lthy3) =
       
    48       Variable.variant_fixes (replicate (length preds) "P") lthy2;
       
    49     val Ps = map (fn (s, Ts) => Free (s, Ts ---> HOLogic.boolT))
       
    50       (Pnames ~~ Tss);
       
    51     val cPs = map (cterm_of (ProofContext.theory_of lthy3)) Ps;
       
    52     val intrs'' = map (subst_free (preds ~~ Ps) o snd) intrs;
       
    53 
       
    54     fun inst_spec ct = Drule.instantiate'
       
    55       [SOME (ctyp_of_term ct)] [NONE, SOME ct] spec;
       
    56 
       
    57     fun prove_indrule ((R, P), Ts) =
       
    58       let
       
    59         val (znames, lthy4) =
       
    60           Variable.variant_fixes (replicate (length Ts) "z") lthy3;
       
    61         val zs = map Free (znames ~~ Ts)
       
    62       in
       
    63         Goal.prove lthy4 []
       
    64           [HOLogic.mk_Trueprop (list_comb (R, zs))]
       
    65           (Logic.list_implies (intrs'',
       
    66              HOLogic.mk_Trueprop (list_comb (P, zs))))
       
    67           (fn {prems, ...} => EVERY
       
    68              ([ObjectLogic.full_atomize_tac 1,
       
    69                cut_facts_tac prems 1,
       
    70                rewrite_goals_tac defs] @
       
    71               map (fn ct => dtac (inst_spec ct) 1) cPs @
       
    72               [assume_tac 1])) |>
       
    73         singleton (ProofContext.export lthy4 lthy1)
       
    74       end;
       
    75 
       
    76     val indrules = map prove_indrule (preds ~~ Ps ~~ Tss);
       
    77 
       
    78 
       
    79     (* proving the introduction rules *)
       
    80 
       
    81     val all_elims = fold (fn ct => fn th => th RS inst_spec ct);
       
    82     val imp_elims = fold (fn th => fn th' => [th', th] MRS mp);
       
    83 
       
    84     fun prove_intr (i, (_, r)) =
       
    85       Goal.prove lthy2 [] [] r
       
    86         (fn {prems, context = ctxt} => EVERY
       
    87            [ObjectLogic.rulify_tac 1,
       
    88             rewrite_goals_tac defs,
       
    89             REPEAT (resolve_tac [allI, impI] 1),
       
    90             SUBPROOF (fn {params, prems, context = ctxt', ...} =>
       
    91               let
       
    92                 val (prems1, prems2) =
       
    93                   chop (length prems - length intrs) prems;
       
    94                 val (params1, params2) =
       
    95                   chop (length params - length preds) (map snd params)
       
    96               in
       
    97                 rtac (ObjectLogic.rulify
       
    98                   (all_elims params1 (nth prems2 i))) 1 THEN
       
    99                 EVERY (map (fn prem =>
       
   100                   SUBPROOF (fn {prems = prems', concl, ...} =>
       
   101                     let
       
   102                       val prem' = prems' MRS prem;
       
   103                       val prem'' = case prop_of prem' of
       
   104                           _ $ (Const (@{const_name All}, _) $ _) =>
       
   105                             prem' |> all_elims params2 |>
       
   106                             imp_elims prems2
       
   107                         | _ => prem'
       
   108                     in rtac prem'' 1 end) ctxt' 1) prems1)
       
   109               end) ctxt 1]) |>
       
   110       singleton (ProofContext.export lthy2 lthy1);
       
   111 
       
   112     val intr_ths = map_index prove_intr intrs;
       
   113 
       
   114 
       
   115     (* storing the theorems *)
       
   116 
       
   117     val mut_name = space_implode "_" (map (Binding.name_of o fst o fst) preds_syn);
       
   118     val case_names = map (Binding.name_of o fst o fst) intrs
       
   119   in
       
   120     lthy1 |>
       
   121     LocalTheory.notes Thm.theoremK (map (fn (((a, atts), _), th) =>
       
   122       ((Binding.qualify false mut_name a, atts), [([th], [])]))
       
   123         (intrs ~~ intr_ths)) |->
       
   124     (fn intr_thss => LocalTheory.note Thm.theoremK
       
   125        ((Binding.qualify false mut_name (Binding.name "intros"), []), maps snd intr_thss)) |>>
       
   126     snd ||>>
       
   127     (LocalTheory.notes Thm.theoremK (map (fn (((R, _), _), th) =>
       
   128        ((Binding.qualify false (Binding.name_of R) (Binding.name "induct"),
       
   129          [Attrib.internal (K (RuleCases.case_names case_names)),
       
   130           Attrib.internal (K (RuleCases.consumes 1)),
       
   131           Attrib.internal (K (Induct.induct_pred ""))]), [([th], [])]))
       
   132          (preds_syn ~~ indrules)) #>> maps snd)
       
   133   end;
       
   134 
       
   135 fun add_inductive preds_syn params_syn intro_srcs lthy =
       
   136   let
       
   137     val ((vars, intrs), _) = Specification.read_spec
       
   138       (preds_syn @ params_syn) intro_srcs lthy;
       
   139     val (preds_syn', params_syn') = chop (length preds_syn) vars
       
   140   in
       
   141     add_inductive_i preds_syn' (map fst params_syn') intrs lthy
       
   142   end;
       
   143 
       
   144 
       
   145 (* outer syntax *)
       
   146 
       
   147 local structure P = OuterParse and K = OuterKeyword in
       
   148 
       
   149 val ind_decl =
       
   150   P.fixes -- P.for_fixes --
       
   151   Scan.optional (P.$$$ "where" |--
       
   152     P.!!! (P.enum1 "|" (SpecParse.opt_thm_name ":" -- P.prop))) [] >>
       
   153   (fn ((preds, params), specs) =>
       
   154     add_inductive preds params specs #> snd);
       
   155 
       
   156 val _ = OuterSyntax.local_theory "simple_inductive" "define inductive predicates"
       
   157   K.thy_decl ind_decl;
       
   158 
       
   159 end;
       
   160 
       
   161 end;