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