src/HOL/Tools/inductive_set_package.ML
author wenzelm
Sat Oct 06 16:50:04 2007 +0200 (2007-10-06)
changeset 24867 e5b55d7be9bb
parent 24815 f7093e90f36c
child 24925 f38dd8d0a30d
permissions -rw-r--r--
simplified interfaces for outer syntax;
     1 (*  Title:      HOL/Tools/inductive_set_package.ML
     2     ID:         $Id$
     3     Author:     Stefan Berghofer, TU Muenchen
     4 
     5 Wrapper for defining inductive sets using package for inductive predicates,
     6 including infrastructure for converting between predicates and sets.
     7 *)
     8 
     9 signature INDUCTIVE_SET_PACKAGE =
    10 sig
    11   val to_set_att: thm list -> attribute
    12   val to_pred_att: thm list -> attribute
    13   val pred_set_conv_att: attribute
    14   val add_inductive_i:
    15     {verbose: bool, kind: string, alt_name: bstring, coind: bool, no_elim: bool, no_ind: bool} ->
    16     ((string * typ) * mixfix) list ->
    17     (string * typ) list -> ((bstring * Attrib.src list) * term) list -> thm list ->
    18       local_theory -> InductivePackage.inductive_result * local_theory
    19   val add_inductive: bool -> bool -> (string * string option * mixfix) list ->
    20     (string * string option * mixfix) list ->
    21     ((bstring * Attrib.src list) * string) list -> (thmref * Attrib.src list) list ->
    22     local_theory -> InductivePackage.inductive_result * local_theory
    23   val setup: theory -> theory
    24 end;
    25 
    26 structure InductiveSetPackage: INDUCTIVE_SET_PACKAGE =
    27 struct
    28 
    29 (**** simplify {(x1, ..., xn). (x1, ..., xn) : S} to S ****)
    30 
    31 val collect_mem_simproc =
    32   Simplifier.simproc (theory "Set") "Collect_mem" ["Collect t"] (fn thy => fn ss =>
    33     fn S as Const ("Collect", Type ("fun", [_, T])) $ t =>
    34          let val (u, Ts, ps) = HOLogic.strip_split t
    35          in case u of
    36            (c as Const ("op :", _)) $ q $ S' =>
    37              (case try (HOLogic.dest_tuple' ps) q of
    38                 NONE => NONE
    39               | SOME ts =>
    40                   if not (loose_bvar (S', 0)) andalso
    41                     ts = map Bound (length ps downto 0)
    42                   then
    43                     let val simp = full_simp_tac (Simplifier.inherit_context ss
    44                       (HOL_basic_ss addsimps [split_paired_all, split_conv])) 1
    45                     in
    46                       SOME (Goal.prove (Simplifier.the_context ss) [] []
    47                         (Const ("==", T --> T --> propT) $ S $ S')
    48                         (K (EVERY
    49                           [rtac eq_reflection 1, rtac @{thm subset_antisym} 1,
    50                            rtac subsetI 1, dtac CollectD 1, simp,
    51                            rtac subsetI 1, rtac CollectI 1, simp])))
    52                     end
    53                   else NONE)
    54          | _ => NONE
    55          end
    56      | _ => NONE);
    57 
    58 (***********************************************************************************)
    59 (* simplifies (%x y. (x, y) : S & P x y) to (%x y. (x, y) : S Int {(x, y). P x y}) *)
    60 (* and        (%x y. (x, y) : S | P x y) to (%x y. (x, y) : S Un {(x, y). P x y})  *)
    61 (* used for converting "strong" (co)induction rules                                *)
    62 (***********************************************************************************)
    63 
    64 val anyt = Free ("t", TFree ("'t", []));
    65 
    66 fun strong_ind_simproc tab =
    67   Simplifier.simproc_i HOL.thy "strong_ind" [anyt] (fn thy => fn ss => fn t =>
    68     let
    69       fun close p t f =
    70         let val vs = Term.add_vars t []
    71         in Drule.instantiate' [] (rev (map (SOME o cterm_of thy o Var) vs))
    72           (p (fold (fn x as (_, T) => fn u => all T $ lambda (Var x) u) vs t) f)
    73         end;
    74       fun mkop "op &" T x = SOME (Const ("op Int", T --> T --> T), x)
    75         | mkop "op |" T x = SOME (Const ("op Un", T --> T --> T), x)
    76         | mkop _ _ _ = NONE;
    77       fun mk_collect p T t =
    78         let val U = HOLogic.dest_setT T
    79         in HOLogic.Collect_const U $
    80           HOLogic.ap_split' (HOLogic.prod_factors p) U HOLogic.boolT t
    81         end;
    82       fun decomp (Const (s, _) $ ((m as Const ("op :",
    83             Type (_, [_, Type (_, [T, _])]))) $ p $ S) $ u) =
    84               mkop s T (m, p, S, mk_collect p T (head_of u))
    85         | decomp (Const (s, _) $ u $ ((m as Const ("op :",
    86             Type (_, [_, Type (_, [T, _])]))) $ p $ S)) =
    87               mkop s T (m, p, mk_collect p T (head_of u), S)
    88         | decomp _ = NONE;
    89       val simp = full_simp_tac (Simplifier.inherit_context ss
    90         (HOL_basic_ss addsimps [mem_Collect_eq, split_conv])) 1;
    91       fun mk_rew t = (case strip_abs_vars t of
    92           [] => NONE
    93         | xs => (case decomp (strip_abs_body t) of
    94             NONE => NONE
    95           | SOME (bop, (m, p, S, S')) =>
    96               SOME (close (Goal.prove (Simplifier.the_context ss) [] [])
    97                 (Logic.mk_equals (t, list_abs (xs, m $ p $ (bop $ S $ S'))))
    98                 (K (EVERY
    99                   [rtac eq_reflection 1, REPEAT (rtac ext 1), rtac iffI 1,
   100                    EVERY [etac conjE 1, rtac IntI 1, simp, simp,
   101                      etac IntE 1, rtac conjI 1, simp, simp] ORELSE
   102                    EVERY [etac disjE 1, rtac UnI1 1, simp, rtac UnI2 1, simp,
   103                      etac UnE 1, rtac disjI1 1, simp, rtac disjI2 1, simp]])))
   104                 handle ERROR _ => NONE))
   105     in
   106       case strip_comb t of
   107         (h as Const (name, _), ts) => (case Symtab.lookup tab name of
   108           SOME _ =>
   109             let val rews = map mk_rew ts
   110             in
   111               if forall is_none rews then NONE
   112               else SOME (fold (fn th1 => fn th2 => combination th2 th1)
   113                 (map2 (fn SOME r => K r | NONE => reflexive o cterm_of thy)
   114                    rews ts) (reflexive (cterm_of thy h)))
   115             end
   116         | NONE => NONE)
   117       | _ => NONE
   118     end);
   119 
   120 (* only eta contract terms occurring as arguments of functions satisfying p *)
   121 fun eta_contract p =
   122   let
   123     fun eta b (Abs (a, T, body)) =
   124           (case eta b body of
   125              body' as (f $ Bound 0) =>
   126                if loose_bvar1 (f, 0) orelse not b then Abs (a, T, body')
   127                else incr_boundvars ~1 f
   128            | body' => Abs (a, T, body'))
   129       | eta b (t $ u) = eta b t $ eta (p (head_of t)) u
   130       | eta b t = t
   131   in eta false end;
   132 
   133 fun eta_contract_thm p =
   134   Conv.fconv_rule (Conv.then_conv (Thm.beta_conversion true, fn ct =>
   135     Thm.transitive (Thm.eta_conversion ct)
   136       (Thm.symmetric (Thm.eta_conversion
   137         (cterm_of (theory_of_cterm ct) (eta_contract p (term_of ct)))))));
   138 
   139 
   140 (***********************************************************)
   141 (* rules for converting between predicate and set notation *)
   142 (*                                                         *)
   143 (* rules for converting predicates to sets have the form   *)
   144 (* P (%x y. (x, y) : s) = (%x y. (x, y) : S s)             *)
   145 (*                                                         *)
   146 (* rules for converting sets to predicates have the form   *)
   147 (* S {(x, y). p x y} = {(x, y). P p x y}                   *)
   148 (*                                                         *)
   149 (* where s and p are parameters                            *)
   150 (***********************************************************)
   151 
   152 structure PredSetConvData = GenericDataFun
   153 (
   154   type T =
   155     {(* rules for converting predicates to sets *)
   156      to_set_simps: thm list,
   157      (* rules for converting sets to predicates *)
   158      to_pred_simps: thm list,
   159      (* arities of functions of type t set => ... => u set *)
   160      set_arities: (typ * (int list list option list * int list list option)) list Symtab.table,
   161      (* arities of functions of type (t => ... => bool) => u => ... => bool *)
   162      pred_arities: (typ * (int list list option list * int list list option)) list Symtab.table};
   163   val empty = {to_set_simps = [], to_pred_simps = [],
   164     set_arities = Symtab.empty, pred_arities = Symtab.empty};
   165   val extend = I;
   166   fun merge _
   167     ({to_set_simps = to_set_simps1, to_pred_simps = to_pred_simps1,
   168       set_arities = set_arities1, pred_arities = pred_arities1},
   169      {to_set_simps = to_set_simps2, to_pred_simps = to_pred_simps2,
   170       set_arities = set_arities2, pred_arities = pred_arities2}) =
   171     {to_set_simps = Thm.merge_thms (to_set_simps1, to_set_simps2),
   172      to_pred_simps = Thm.merge_thms (to_pred_simps1, to_pred_simps2),
   173      set_arities = Symtab.merge_list op = (set_arities1, set_arities2),
   174      pred_arities = Symtab.merge_list op = (pred_arities1, pred_arities2)};
   175 );
   176 
   177 fun name_type_of (Free p) = SOME p
   178   | name_type_of (Const p) = SOME p
   179   | name_type_of _ = NONE;
   180 
   181 fun map_type f (Free (s, T)) = Free (s, f T)
   182   | map_type f (Var (ixn, T)) = Var (ixn, f T)
   183   | map_type f _ = error "map_type";
   184 
   185 fun find_most_specific is_inst f eq xs T =
   186   find_first (fn U => is_inst (T, f U)
   187     andalso forall (fn U' => eq (f U, f U') orelse not
   188       (is_inst (T, f U') andalso is_inst (f U', f U)))
   189         xs) xs;
   190 
   191 fun lookup_arity thy arities (s, T) = case Symtab.lookup arities s of
   192     NONE => NONE
   193   | SOME xs => find_most_specific (Sign.typ_instance thy) fst (op =) xs T;
   194 
   195 fun lookup_rule thy f rules = find_most_specific
   196   (swap #> Pattern.matches thy) (f #> fst) (op aconv) rules;
   197 
   198 fun infer_arities thy arities (optf, t) fs = case strip_comb t of
   199     (Abs (s, T, u), []) => infer_arities thy arities (NONE, u) fs
   200   | (Abs _, _) => infer_arities thy arities (NONE, Envir.beta_norm t) fs
   201   | (u, ts) => (case Option.map (lookup_arity thy arities) (name_type_of u) of
   202       SOME (SOME (_, (arity, _))) =>
   203         (fold (infer_arities thy arities) (arity ~~ List.take (ts, length arity)) fs
   204            handle Subscript => error "infer_arities: bad term")
   205     | _ => fold (infer_arities thy arities) (map (pair NONE) ts)
   206       (case optf of
   207          NONE => fs
   208        | SOME f => AList.update op = (u, the_default f
   209            (Option.map (curry op inter f) (AList.lookup op = fs u))) fs));
   210 
   211 
   212 (**************************************************************)
   213 (*    derive the to_pred equation from the to_set equation    *)
   214 (*                                                            *)
   215 (* 1. instantiate each set parameter with {(x, y). p x y}     *)
   216 (* 2. apply %P. {(x, y). P x y} to both sides of the equation *)
   217 (* 3. simplify                                                *)
   218 (**************************************************************)
   219 
   220 fun mk_to_pred_inst thy fs =
   221   map (fn (x, ps) =>
   222     let
   223       val U = HOLogic.dest_setT (fastype_of x);
   224       val x' = map_type (K (HOLogic.prodT_factors' ps U ---> HOLogic.boolT)) x
   225     in
   226       (cterm_of thy x,
   227        cterm_of thy (HOLogic.Collect_const U $
   228          HOLogic.ap_split' ps U HOLogic.boolT x'))
   229     end) fs;
   230 
   231 fun mk_to_pred_eq p fs optfs' T thm =
   232   let
   233     val thy = theory_of_thm thm;
   234     val insts = mk_to_pred_inst thy fs;
   235     val thm' = Thm.instantiate ([], insts) thm;
   236     val thm'' = (case optfs' of
   237         NONE => thm' RS sym
   238       | SOME fs' =>
   239           let
   240             val U = HOLogic.dest_setT (body_type T);
   241             val Ts = HOLogic.prodT_factors' fs' U;
   242             (* FIXME: should cterm_instantiate increment indexes? *)
   243             val arg_cong' = Thm.incr_indexes (Thm.maxidx_of thm + 1) arg_cong;
   244             val (arg_cong_f, _) = arg_cong' |> cprop_of |> Drule.strip_imp_concl |>
   245               Thm.dest_comb |> snd |> Drule.strip_comb |> snd |> hd |> Thm.dest_comb
   246           in
   247             thm' RS (Drule.cterm_instantiate [(arg_cong_f,
   248               cterm_of thy (Abs ("P", Ts ---> HOLogic.boolT,
   249                 HOLogic.Collect_const U $ HOLogic.ap_split' fs' U
   250                   HOLogic.boolT (Bound 0))))] arg_cong' RS sym)
   251           end)
   252   in
   253     Simplifier.simplify (HOL_basic_ss addsimps [mem_Collect_eq, split_conv]
   254       addsimprocs [collect_mem_simproc]) thm'' |>
   255         zero_var_indexes |> eta_contract_thm (equal p)
   256   end;
   257 
   258 
   259 (**** declare rules for converting predicates to sets ****)
   260 
   261 fun add ctxt thm {to_set_simps, to_pred_simps, set_arities, pred_arities} =
   262   case prop_of thm of
   263     Const ("Trueprop", _) $ (Const ("op =", Type (_, [T, _])) $ lhs $ rhs) =>
   264       (case body_type T of
   265          Type ("bool", []) =>
   266            let
   267              val thy = Context.theory_of ctxt;
   268              fun factors_of t fs = case strip_abs_body t of
   269                  Const ("op :", _) $ u $ S =>
   270                    if is_Free S orelse is_Var S then
   271                      let val ps = HOLogic.prod_factors u
   272                      in (SOME ps, (S, ps) :: fs) end
   273                    else (NONE, fs)
   274                | _ => (NONE, fs);
   275              val (h, ts) = strip_comb lhs
   276              val (pfs, fs) = fold_map factors_of ts [];
   277              val ((h', ts'), fs') = (case rhs of
   278                  Abs _ => (case strip_abs_body rhs of
   279                      Const ("op :", _) $ u $ S =>
   280                        (strip_comb S, SOME (HOLogic.prod_factors u))
   281                    | _ => error "member symbol on right-hand side expected")
   282                | _ => (strip_comb rhs, NONE))
   283            in
   284              case (name_type_of h, name_type_of h') of
   285                (SOME (s, T), SOME (s', T')) =>
   286                  (case Symtab.lookup set_arities s' of
   287                     NONE => ()
   288                   | SOME xs => if exists (fn (U, _) =>
   289                         Sign.typ_instance thy (T', U) andalso
   290                         Sign.typ_instance thy (U, T')) xs
   291                       then
   292                         error ("Clash of conversion rules for operator " ^ s')
   293                       else ();
   294                   {to_set_simps = thm :: to_set_simps,
   295                    to_pred_simps =
   296                      mk_to_pred_eq h fs fs' T' thm :: to_pred_simps,
   297                    set_arities = Symtab.insert_list op = (s',
   298                      (T', (map (AList.lookup op = fs) ts', fs'))) set_arities,
   299                    pred_arities = Symtab.insert_list op = (s,
   300                      (T, (pfs, fs'))) pred_arities})
   301              | _ => error "set / predicate constant expected"
   302            end
   303        | _ => error "equation between predicates expected")
   304   | _ => error "equation expected";
   305 
   306 val pred_set_conv_att = Thm.declaration_attribute
   307   (fn thm => fn ctxt => PredSetConvData.map (add ctxt thm) ctxt);
   308 
   309 
   310 (**** convert theorem in set notation to predicate notation ****)
   311 
   312 fun is_pred tab t =
   313   case Option.map (Symtab.lookup tab o fst) (name_type_of t) of
   314     SOME (SOME _) => true | _ => false;
   315 
   316 fun to_pred_simproc rules =
   317   let val rules' = map mk_meta_eq rules
   318   in
   319     Simplifier.simproc_i HOL.thy "to_pred" [anyt]
   320       (fn thy => K (lookup_rule thy (prop_of #> Logic.dest_equals) rules'))
   321   end;
   322 
   323 fun to_pred_proc thy rules t = case lookup_rule thy I rules t of
   324     NONE => NONE
   325   | SOME (lhs, rhs) =>
   326       SOME (Envir.subst_vars
   327         (Pattern.match thy (lhs, t) (Vartab.empty, Vartab.empty)) rhs);
   328 
   329 fun to_pred thms ctxt thm =
   330   let
   331     val thy = Context.theory_of ctxt;
   332     val {to_pred_simps, set_arities, pred_arities, ...} =
   333       fold (add ctxt) thms (PredSetConvData.get ctxt);
   334     val fs = filter (is_Var o fst)
   335       (infer_arities thy set_arities (NONE, prop_of thm) []);
   336     (* instantiate each set parameter with {(x, y). p x y} *)
   337     val insts = mk_to_pred_inst thy fs
   338   in
   339     thm |>
   340     Thm.instantiate ([], insts) |>
   341     Simplifier.full_simplify (HOL_basic_ss addsimprocs
   342       [to_pred_simproc (mem_Collect_eq :: split_conv :: to_pred_simps)]) |>
   343     eta_contract_thm (is_pred pred_arities)
   344   end;
   345 
   346 val to_pred_att = Thm.rule_attribute o to_pred;
   347     
   348 
   349 (**** convert theorem in predicate notation to set notation ****)
   350 
   351 fun to_set thms ctxt thm =
   352   let
   353     val thy = Context.theory_of ctxt;
   354     val {to_set_simps, pred_arities, ...} =
   355       fold (add ctxt) thms (PredSetConvData.get ctxt);
   356     val fs = filter (is_Var o fst)
   357       (infer_arities thy pred_arities (NONE, prop_of thm) []);
   358     (* instantiate each predicate parameter with %x y. (x, y) : s *)
   359     val insts = map (fn (x, ps) =>
   360       let
   361         val Ts = binder_types (fastype_of x);
   362         val T = HOLogic.mk_tupleT ps Ts;
   363         val x' = map_type (K (HOLogic.mk_setT T)) x
   364       in
   365         (cterm_of thy x,
   366          cterm_of thy (list_abs (map (pair "x") Ts, HOLogic.mk_mem
   367            (HOLogic.mk_tuple' ps T (map Bound (length ps downto 0)), x'))))
   368       end) fs
   369   in
   370     Simplifier.full_simplify (HOL_basic_ss addsimps to_set_simps
   371         addsimprocs [strong_ind_simproc pred_arities])
   372       (Thm.instantiate ([], insts) thm)
   373   end;
   374 
   375 val to_set_att = Thm.rule_attribute o to_set;
   376 
   377 
   378 (**** preprocessor for code generator ****)
   379 
   380 fun codegen_preproc thy =
   381   let
   382     val {to_pred_simps, set_arities, pred_arities, ...} =
   383       PredSetConvData.get (Context.Theory thy);
   384     fun preproc thm =
   385       if exists_Const (fn (s, _) => case Symtab.lookup set_arities s of
   386           NONE => false
   387         | SOME arities => exists (fn (_, (xs, _)) =>
   388             forall is_none xs) arities) (prop_of thm)
   389       then
   390         thm |>
   391         Simplifier.full_simplify (HOL_basic_ss addsimprocs
   392           [to_pred_simproc (mem_Collect_eq :: split_conv :: to_pred_simps)]) |>
   393         eta_contract_thm (is_pred pred_arities)
   394       else thm
   395   in map preproc end;
   396 
   397 fun code_ind_att optmod = to_pred_att [] #> InductiveCodegen.add optmod NONE;
   398 
   399 
   400 (**** definition of inductive sets ****)
   401 
   402 fun add_ind_set_def {verbose, kind, alt_name, coind, no_elim, no_ind}
   403     cs intros monos params cnames_syn ctxt =
   404   let
   405     val thy = ProofContext.theory_of ctxt;
   406     val {set_arities, pred_arities, to_pred_simps, ...} =
   407       PredSetConvData.get (Context.Proof ctxt);
   408     fun infer (Abs (_, _, t)) = infer t
   409       | infer (Const ("op :", _) $ t $ u) =
   410           infer_arities thy set_arities (SOME (HOLogic.prod_factors t), u)
   411       | infer (t $ u) = infer t #> infer u
   412       | infer _ = I;
   413     val new_arities = filter_out
   414       (fn (x as Free (_, Type ("fun", _)), _) => x mem params
   415         | _ => false) (fold (snd #> infer) intros []);
   416     val params' = map (fn x => (case AList.lookup op = new_arities x of
   417         SOME fs =>
   418           let
   419             val T = HOLogic.dest_setT (fastype_of x);
   420             val Ts = HOLogic.prodT_factors' fs T;
   421             val x' = map_type (K (Ts ---> HOLogic.boolT)) x
   422           in
   423             (x, (x',
   424               (HOLogic.Collect_const T $
   425                  HOLogic.ap_split' fs T HOLogic.boolT x',
   426                list_abs (map (pair "x") Ts, HOLogic.mk_mem
   427                  (HOLogic.mk_tuple' fs T (map Bound (length fs downto 0)),
   428                   x)))))
   429           end
   430        | NONE => (x, (x, (x, x))))) params;
   431     val (params1, (params2, params3)) =
   432       params' |> map snd |> split_list ||> split_list;
   433 
   434     (* equations for converting sets to predicates *)
   435     val ((cs', cs_info), eqns) = cs |> map (fn c as Free (s, T) =>
   436       let
   437         val fs = the_default [] (AList.lookup op = new_arities c);
   438         val U = HOLogic.dest_setT (body_type T);
   439         val Ts = HOLogic.prodT_factors' fs U;
   440         val c' = Free (s ^ "p",
   441           map fastype_of params1 @ Ts ---> HOLogic.boolT)
   442       in
   443         ((c', (fs, U, Ts)),
   444          (list_comb (c, params2),
   445           HOLogic.Collect_const U $ HOLogic.ap_split' fs U HOLogic.boolT
   446             (list_comb (c', params1))))
   447       end) |> split_list |>> split_list;
   448     val eqns' = eqns @
   449       map (prop_of #> HOLogic.dest_Trueprop #> HOLogic.dest_eq)
   450         (mem_Collect_eq :: split_conv :: to_pred_simps);
   451 
   452     (* predicate version of the introduction rules *)
   453     val intros' =
   454       map (fn (name_atts, t) => (name_atts,
   455         t |>
   456         map_aterms (fn u =>
   457           (case AList.lookup op = params' u of
   458              SOME (_, (u', _)) => u'
   459            | NONE => u)) |>
   460         Pattern.rewrite_term thy [] [to_pred_proc thy eqns'] |>
   461         eta_contract (member op = cs' orf is_pred pred_arities))) intros;
   462     val cnames_syn' = map (fn (s, _) => (s ^ "p", NoSyn)) cnames_syn;
   463     val monos' = map (to_pred [] (Context.Proof ctxt)) monos;
   464     val ({preds, intrs, elims, raw_induct, ...}, ctxt1) =
   465       InductivePackage.add_ind_def {verbose = verbose, kind = kind,
   466           alt_name = "",  (* FIXME pass alt_name (!?) *)
   467           coind = coind, no_elim = no_elim, no_ind = no_ind}
   468         cs' intros' monos' params1 cnames_syn' ctxt;
   469 
   470     (* define inductive sets using previously defined predicates *)
   471     val (defs, ctxt2) = LocalTheory.defs Thm.internalK
   472       (map (fn ((c_syn, (fs, U, _)), p) => (c_syn, (("", []),
   473          fold_rev lambda params (HOLogic.Collect_const U $
   474            HOLogic.ap_split' fs U HOLogic.boolT (list_comb (p, params3))))))
   475          (cnames_syn ~~ cs_info ~~ preds)) ctxt1;
   476 
   477     (* prove theorems for converting predicate to set notation *)
   478     val ctxt3 = fold
   479       (fn (((p, c as Free (s, _)), (fs, U, Ts)), (_, (_, def))) => fn ctxt =>
   480         let val conv_thm =
   481           Goal.prove ctxt (map (fst o dest_Free) params) []
   482             (HOLogic.mk_Trueprop (HOLogic.mk_eq
   483               (list_comb (p, params3),
   484                list_abs (map (pair "x") Ts, HOLogic.mk_mem
   485                  (HOLogic.mk_tuple' fs U (map Bound (length fs downto 0)),
   486                   list_comb (c, params))))))
   487             (K (REPEAT (rtac ext 1) THEN simp_tac (HOL_basic_ss addsimps
   488               [def, mem_Collect_eq, split_conv]) 1))
   489         in
   490           ctxt |> LocalTheory.note kind ((s ^ "p_" ^ s ^ "_eq",
   491             [Attrib.internal (K pred_set_conv_att)]),
   492               [conv_thm]) |> snd
   493         end) (preds ~~ cs ~~ cs_info ~~ defs) ctxt2;
   494 
   495     (* convert theorems to set notation *)
   496     val rec_name = if alt_name = "" then
   497       space_implode "_" (map fst cnames_syn) else alt_name;
   498     val cnames = map (Sign.full_name (ProofContext.theory_of ctxt3) o #1) cnames_syn;  (* FIXME *)
   499     val (intr_names, intr_atts) = split_list (map fst intros);
   500     val raw_induct' = to_set [] (Context.Proof ctxt3) raw_induct;
   501     val (intrs', elims', induct, ctxt4) =
   502       InductivePackage.declare_rules kind rec_name coind no_ind cnames
   503       (map (to_set [] (Context.Proof ctxt3)) intrs) intr_names intr_atts
   504       (map (fn th => (to_set [] (Context.Proof ctxt3) th,
   505          map fst (fst (RuleCases.get th)))) elims)
   506       raw_induct' ctxt3
   507   in
   508     ({intrs = intrs', elims = elims', induct = induct,
   509       raw_induct = raw_induct', preds = map fst defs},
   510      ctxt4)
   511   end;
   512 
   513 val add_inductive_i = InductivePackage.gen_add_inductive_i add_ind_set_def;
   514 val add_inductive = InductivePackage.gen_add_inductive add_ind_set_def;
   515 
   516 val mono_add_att = to_pred_att [] #> InductivePackage.mono_add;
   517 val mono_del_att = to_pred_att [] #> InductivePackage.mono_del;
   518 
   519 
   520 (** package setup **)
   521 
   522 (* setup theory *)
   523 
   524 val setup =
   525   Attrib.add_attributes
   526     [("pred_set_conv", Attrib.no_args pred_set_conv_att,
   527       "declare rules for converting between predicate and set notation"),
   528      ("to_set", Attrib.syntax (Attrib.thms >> to_set_att),
   529       "convert rule to set notation"),
   530      ("to_pred", Attrib.syntax (Attrib.thms >> to_pred_att),
   531       "convert rule to predicate notation")] #>
   532   Code.add_attribute ("ind_set",
   533     Scan.option (Args.$$$ "target" |-- Args.colon |-- Args.name) >> code_ind_att) #>
   534   Codegen.add_preprocessor codegen_preproc #>
   535   Attrib.add_attributes [("mono_set", Attrib.add_del_args mono_add_att mono_del_att,
   536     "declaration of monotonicity rule for set operators")] #>
   537   Context.theory_map (Simplifier.map_ss (fn ss =>
   538     ss addsimprocs [collect_mem_simproc]));
   539 
   540 (* outer syntax *)
   541 
   542 local structure P = OuterParse and K = OuterKeyword in
   543 
   544 val ind_set_decl = InductivePackage.gen_ind_decl add_ind_set_def;
   545 
   546 val _ =
   547   OuterSyntax.command "inductive_set" "define inductive sets" K.thy_decl (ind_set_decl false);
   548 
   549 val _ =
   550   OuterSyntax.command "coinductive_set" "define coinductive sets" K.thy_decl (ind_set_decl true);
   551 
   552 end;
   553 
   554 end;