src/ZF/Tools/ind_cases.ML
author wenzelm
Sat, 06 Oct 2007 16:50:04 +0200
changeset 24867 e5b55d7be9bb
parent 24508 c8b82fec6447
child 27691 ce171cbd4b93
permissions -rw-r--r--
simplified interfaces for outer syntax;
     1 (*  Title:      ZF/Tools/ind_cases.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, LMU Muenchen
     4 
     5 Generic inductive cases facility for (co)inductive definitions.
     6 *)
     7 
     8 signature IND_CASES =
     9 sig
    10   val declare: string -> (simpset -> cterm -> thm) -> theory -> theory
    11   val inductive_cases: ((bstring * Attrib.src list) * string list) list -> theory -> theory
    12   val setup: theory -> theory
    13 end;
    14 
    15 structure IndCases: IND_CASES =
    16 struct
    17 
    18 
    19 (* theory data *)
    20 
    21 structure IndCasesData = TheoryDataFun
    22 (
    23   type T = (simpset -> cterm -> thm) Symtab.table;
    24   val empty = Symtab.empty;
    25   val copy = I;
    26   val extend = I;
    27   fun merge _ tabs : T = Symtab.merge (K true) tabs;
    28 );
    29 
    30 
    31 fun declare name f = IndCasesData.map (Symtab.update (name, f));
    32 
    33 fun smart_cases thy ss read_prop s =
    34   let
    35     fun err msg = cat_error msg ("Malformed set membership statement: " ^ s);
    36     val A = read_prop s handle ERROR msg => err msg;
    37     val c = #1 (Term.dest_Const (Term.head_of (#2 (Ind_Syntax.dest_mem (FOLogic.dest_Trueprop
    38       (Logic.strip_imp_concl A)))))) handle TERM _ => err "";
    39   in
    40     (case Symtab.lookup (IndCasesData.get thy) c of
    41       NONE => error ("Unknown inductive cases rule for set " ^ quote c)
    42     | SOME f => f ss (Thm.cterm_of thy A))
    43   end;
    44 
    45 
    46 (* inductive_cases command *)
    47 
    48 fun inductive_cases args thy =
    49   let
    50     val read_prop = Syntax.read_prop (ProofContext.init thy);
    51     val mk_cases = smart_cases thy (Simplifier.simpset_of thy) read_prop;
    52     val facts = args |> map (fn ((name, srcs), props) =>
    53       ((name, map (Attrib.attribute thy) srcs),
    54         map (Thm.no_attributes o single o mk_cases) props));
    55   in thy |> PureThy.note_thmss_i "" facts |> snd end;
    56 
    57 
    58 (* ind_cases method *)
    59 
    60 fun mk_cases_meth (props, ctxt) =
    61   props
    62   |> map (smart_cases (ProofContext.theory_of ctxt) (local_simpset_of ctxt) (Syntax.read_prop ctxt))
    63   |> Method.erule 0;
    64 
    65 val mk_cases_args = Method.syntax (Scan.lift (Scan.repeat1 Args.name));
    66 
    67 
    68 (* package setup *)
    69 
    70 val setup =
    71   Method.add_methods
    72     [("ind_cases", mk_cases_meth oo mk_cases_args, "dynamic case analysis on sets")];
    73 
    74 
    75 (* outer syntax *)
    76 
    77 local structure P = OuterParse and K = OuterKeyword in
    78 
    79 val _ =
    80   OuterSyntax.command "inductive_cases"
    81     "create simplified instances of elimination rules (improper)" K.thy_script
    82     (P.and_list1 (SpecParse.opt_thm_name ":" -- Scan.repeat1 P.prop)
    83       >> (Toplevel.theory o inductive_cases));
    84 
    85 end;
    86 
    87 end;
    88