src/HOL/Tools/inductive_set.ML
author haftmann
Thu, 10 Sep 2015 12:52:24 +0200
changeset 61162 61908914d191
parent 61144 5e94dfead1c2
child 61163 c94c65f35d01
permissions -rw-r--r--
unconceal symbols stemming from inductive_set specifications, which are regular part of user-space specification; also unconceal corresponding primitive definitions, which are official conversions between predicates and sets
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31723
f5cafe803b55 discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 30860
diff changeset
     1
(*  Title:      HOL/Tools/inductive_set.ML
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
     2
    Author:     Stefan Berghofer, TU Muenchen
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
     3
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
     4
Wrapper for defining inductive sets using package for inductive predicates,
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
     5
including infrastructure for converting between predicates and sets.
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
     6
*)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
     7
31723
f5cafe803b55 discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 30860
diff changeset
     8
signature INDUCTIVE_SET =
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
     9
sig
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    10
  val to_set_att: thm list -> attribute
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    11
  val to_pred_att: thm list -> attribute
32306
19f55947d4d5 removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
bulwahn
parents: 32287
diff changeset
    12
  val to_pred : thm list -> Context.generic -> thm -> thm
23764
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:
31723
f5cafe803b55 discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 30860
diff changeset
    15
    Inductive.inductive_flags ->
29581
b3b33e0298eb binding is alias for Binding.T
haftmann
parents: 29389
diff changeset
    16
    ((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 ->
31723
f5cafe803b55 discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 30860
diff changeset
    19
    local_theory -> Inductive.inductive_result * local_theory
28084
a05ca48ef263 type Attrib.binding abbreviates Name.binding without attributes;
wenzelm
parents: 28083
diff changeset
    20
  val add_inductive: bool -> bool ->
29581
b3b33e0298eb binding is alias for Binding.T
haftmann
parents: 29389
diff changeset
    21
    (binding * string option * mixfix) list ->
b3b33e0298eb binding is alias for Binding.T
haftmann
parents: 29389
diff changeset
    22
    (binding * string option * mixfix) list ->
58011
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57870
diff changeset
    23
    (Attrib.binding * string) list -> (Facts.ref * Token.src list) list ->
49324
4f28543ae7fa removed obsolete argument "int" and thus made SML/NJ happy (cf. 03bee3a6a1b7);
wenzelm
parents: 49170
diff changeset
    24
    local_theory -> Inductive.inductive_result * local_theory
45384
dffa657f0aa2 clarified attribute "mono_set": pure declaration, proper export in ML;
wenzelm
parents: 45375
diff changeset
    25
  val mono_add: attribute
dffa657f0aa2 clarified attribute "mono_set": pure declaration, proper export in ML;
wenzelm
parents: 45375
diff changeset
    26
  val mono_del: attribute
23764
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
31723
f5cafe803b55 discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 30860
diff changeset
    29
structure Inductive_Set: INDUCTIVE_SET =
23764
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
(***********************************************************************************)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    33
(* 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
    34
(* 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
    35
(* used for converting "strong" (co)induction rules                                *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    36
(***********************************************************************************)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    37
23849
2a0e24c74593 strong_ind_simproc now only rewrites arguments of inductive predicates.
berghofe
parents: 23764
diff changeset
    38
val anyt = Free ("t", TFree ("'t", []));
2a0e24c74593 strong_ind_simproc now only rewrites arguments of inductive predicates.
berghofe
parents: 23764
diff changeset
    39
2a0e24c74593 strong_ind_simproc now only rewrites arguments of inductive predicates.
berghofe
parents: 23764
diff changeset
    40
fun strong_ind_simproc tab =
61144
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    41
  Simplifier.make_simproc @{context} "strong_ind"
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    42
   {lhss = [@{term "x::'a::{}"}],
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    43
    proc = fn _ => fn ctxt => fn ct =>
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    44
      let
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    45
        fun close p t f =
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    46
          let val vs = Term.add_vars t []
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    47
          in Thm.instantiate' [] (rev (map (SOME o Thm.cterm_of ctxt o Var) vs))
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    48
            (p (fold (Logic.all o Var) vs t) f)
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    49
          end;
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    50
        fun mkop @{const_name HOL.conj} T x =
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    51
              SOME (Const (@{const_name Lattices.inf}, T --> T --> T), x)
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    52
          | mkop @{const_name HOL.disj} T x =
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    53
              SOME (Const (@{const_name Lattices.sup}, T --> T --> T), x)
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    54
          | mkop _ _ _ = NONE;
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    55
        fun mk_collect p T t =
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    56
          let val U = HOLogic.dest_setT T
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    57
          in HOLogic.Collect_const U $
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    58
            HOLogic.mk_psplits (HOLogic.flat_tuple_paths p) U HOLogic.boolT t
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    59
          end;
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    60
        fun decomp (Const (s, _) $ ((m as Const (@{const_name Set.member},
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    61
              Type (_, [_, Type (_, [T, _])]))) $ p $ S) $ u) =
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    62
                mkop s T (m, p, S, mk_collect p T (head_of u))
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    63
          | decomp (Const (s, _) $ u $ ((m as Const (@{const_name Set.member},
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    64
              Type (_, [_, Type (_, [T, _])]))) $ p $ S)) =
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    65
                mkop s T (m, p, mk_collect p T (head_of u), S)
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    66
          | decomp _ = NONE;
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    67
        val simp =
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    68
          full_simp_tac
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    69
            (put_simpset HOL_basic_ss ctxt addsimps [mem_Collect_eq, @{thm split_conv}]) 1;
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    70
        fun mk_rew t = (case strip_abs_vars t of
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    71
            [] => NONE
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    72
          | xs => (case decomp (strip_abs_body t) of
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    73
              NONE => NONE
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    74
            | SOME (bop, (m, p, S, S')) =>
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    75
                SOME (close (Goal.prove ctxt [] [])
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    76
                  (Logic.mk_equals (t, fold_rev Term.abs xs (m $ p $ (bop $ S $ S'))))
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    77
                  (K (EVERY
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    78
                    [resolve_tac ctxt [eq_reflection] 1,
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    79
                     REPEAT (resolve_tac ctxt @{thms ext} 1),
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    80
                     resolve_tac ctxt [iffI] 1,
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    81
                     EVERY [eresolve_tac ctxt [conjE] 1, resolve_tac ctxt [IntI] 1, simp, simp,
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    82
                       eresolve_tac ctxt [IntE] 1, resolve_tac ctxt [conjI] 1, simp, simp] ORELSE
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    83
                     EVERY [eresolve_tac ctxt [disjE] 1, resolve_tac ctxt [UnI1] 1, simp,
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    84
                       resolve_tac ctxt [UnI2] 1, simp,
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    85
                       eresolve_tac ctxt [UnE] 1, resolve_tac ctxt [disjI1] 1, simp,
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    86
                       resolve_tac ctxt [disjI2] 1, simp]])))
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    87
                  handle ERROR _ => NONE))
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    88
      in
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    89
        (case strip_comb (Thm.term_of ct) of
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    90
          (h as Const (name, _), ts) =>
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    91
            if Symtab.defined tab name then
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    92
              let val rews = map mk_rew ts
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    93
              in
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    94
                if forall is_none rews then NONE
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    95
                else SOME (fold (fn th1 => fn th2 => Thm.combination th2 th1)
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    96
                  (map2 (fn SOME r => K r | NONE => Thm.reflexive o Thm.cterm_of ctxt)
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    97
                     rews ts) (Thm.reflexive (Thm.cterm_of ctxt h)))
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    98
              end
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
    99
            else NONE
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
   100
        | _ => NONE)
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
   101
      end,
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
   102
    identifier = []};
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   103
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   104
(* 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
   105
fun eta_contract p =
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   106
  let
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   107
    fun eta b (Abs (a, T, body)) =
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   108
          (case eta b body of
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   109
             body' as (f $ Bound 0) =>
42083
e1209fc7ecdc added Term.is_open and Term.is_dependent convenience, to cover common situations of loose bounds;
wenzelm
parents: 41489
diff changeset
   110
               if Term.is_dependent f orelse not b then Abs (a, T, body')
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   111
               else incr_boundvars ~1 f
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   112
           | body' => Abs (a, T, body'))
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   113
      | 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
   114
      | eta b t = t
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   115
  in eta false end;
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   116
60328
9c94e6a30d29 clarified context;
wenzelm
parents: 59936
diff changeset
   117
fun eta_contract_thm ctxt p =
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   118
  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
   119
    Thm.transitive (Thm.eta_conversion ct)
60328
9c94e6a30d29 clarified context;
wenzelm
parents: 59936
diff changeset
   120
      (Thm.symmetric (Thm.eta_conversion (Thm.cterm_of ctxt (eta_contract p (Thm.term_of ct)))))));
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   121
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
(***********************************************************)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   124
(* rules for converting between predicate and set notation *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   125
(*                                                         *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   126
(* rules for converting predicates to sets have the form   *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   127
(* 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
   128
(*                                                         *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   129
(* rules for converting sets to predicates have the form   *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   130
(* 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
   131
(*                                                         *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   132
(* where s and p are parameters                            *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   133
(***********************************************************)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   134
50774
ac53370dfae1 more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
wenzelm
parents: 49324
diff changeset
   135
structure Data = Generic_Data
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   136
(
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   137
  type T =
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   138
    {(* rules for converting predicates to sets *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   139
     to_set_simps: thm list,
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   140
     (* rules for converting sets to predicates *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   141
     to_pred_simps: thm list,
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   142
     (* arities of functions of type t set => ... => u set *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   143
     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
   144
     (* arities of functions of type (t => ... => bool) => u => ... => bool *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   145
     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
   146
  val empty = {to_set_simps = [], to_pred_simps = [],
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   147
    set_arities = Symtab.empty, pred_arities = Symtab.empty};
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   148
  val extend = I;
33519
e31a85f92ce9 adapted Generic_Data, Proof_Data;
wenzelm
parents: 33459
diff changeset
   149
  fun merge
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   150
    ({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
   151
      set_arities = set_arities1, pred_arities = pred_arities1},
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   152
     {to_set_simps = to_set_simps2, to_pred_simps = to_pred_simps2,
29288
253bcf2a5854 avoid polymorphic equality;
wenzelm
parents: 29064
diff changeset
   153
      set_arities = set_arities2, pred_arities = pred_arities2}) : T =
24039
273698405054 renamed Drule.add/del/merge_rules to Thm.add/del/merge_thms;
wenzelm
parents: 23849
diff changeset
   154
    {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
   155
     to_pred_simps = Thm.merge_thms (to_pred_simps1, to_pred_simps2),
41472
f6ab14e61604 misc tuning and comments based on review of Theory_Data, Proof_Data, Generic_Data usage;
wenzelm
parents: 38864
diff changeset
   156
     set_arities = Symtab.merge_list (op =) (set_arities1, set_arities2),
f6ab14e61604 misc tuning and comments based on review of Theory_Data, Proof_Data, Generic_Data usage;
wenzelm
parents: 38864
diff changeset
   157
     pred_arities = Symtab.merge_list (op =) (pred_arities1, pred_arities2)};
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   158
);
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   159
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   160
fun name_type_of (Free p) = SOME p
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   161
  | name_type_of (Const p) = SOME p
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   162
  | name_type_of _ = NONE;
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   163
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   164
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
   165
  | map_type f (Var (ixn, T)) = Var (ixn, f T)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   166
  | map_type f _ = error "map_type";
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   167
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   168
fun find_most_specific is_inst f eq xs T =
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   169
  find_first (fn U => is_inst (T, f U)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   170
    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
   171
      (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
   172
        xs) xs;
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   173
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   174
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
   175
    NONE => NONE
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   176
  | 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
   177
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   178
fun lookup_rule thy f rules = find_most_specific
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   179
  (swap #> Pattern.matches thy) (f #> fst) (op aconv) rules;
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   180
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   181
fun infer_arities thy arities (optf, t) fs = case strip_comb t of
56512
9276da80f7c3 modernized simproc_setup;
wenzelm
parents: 56245
diff changeset
   182
    (Abs (_, _, u), []) => infer_arities thy arities (NONE, u) fs
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   183
  | (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
   184
  | (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
   185
      SOME (SOME (_, (arity, _))) =>
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   186
        (fold (infer_arities thy arities) (arity ~~ List.take (ts, length arity)) fs
43278
1fbdcebb364b more robust exception pattern General.Subscript;
wenzelm
parents: 42795
diff changeset
   187
           handle General.Subscript => error "infer_arities: bad term")
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   188
    | _ => fold (infer_arities thy arities) (map (pair NONE) ts)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   189
      (case optf of
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   190
         NONE => fs
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   191
       | SOME f => AList.update op = (u, the_default f
33049
c38f02fdf35d curried inter as canonical list operation (beware of argument order)
haftmann
parents: 33038
diff changeset
   192
           (Option.map (fn g => inter (op =) g f) (AList.lookup op = fs u))) fs));
23764
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
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   195
(**************************************************************)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   196
(*    derive the to_pred equation from the to_set equation    *)
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
(* 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
   199
(* 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
   200
(* 3. simplify                                                *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   201
(**************************************************************)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   202
59642
929984c529d3 clarified context;
wenzelm
parents: 59621
diff changeset
   203
fun mk_to_pred_inst ctxt fs =
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   204
  map (fn (x, ps) =>
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   205
    let
46828
b1d15637381a to_pred/set attributes now properly handle variables of type "... => T set"
berghofe
parents: 46219
diff changeset
   206
      val (Ts, T) = strip_type (fastype_of x);
b1d15637381a to_pred/set attributes now properly handle variables of type "... => T set"
berghofe
parents: 46219
diff changeset
   207
      val U = HOLogic.dest_setT T;
b1d15637381a to_pred/set attributes now properly handle variables of type "... => T set"
berghofe
parents: 46219
diff changeset
   208
      val x' = map_type
b1d15637381a to_pred/set attributes now properly handle variables of type "... => T set"
berghofe
parents: 46219
diff changeset
   209
        (K (Ts @ HOLogic.strip_ptupleT ps U ---> HOLogic.boolT)) x;
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   210
    in
60642
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60330
diff changeset
   211
      (dest_Var x,
59642
929984c529d3 clarified context;
wenzelm
parents: 59621
diff changeset
   212
       Thm.cterm_of ctxt (fold_rev (Term.abs o pair "x") Ts
46828
b1d15637381a to_pred/set attributes now properly handle variables of type "... => T set"
berghofe
parents: 46219
diff changeset
   213
         (HOLogic.Collect_const U $
b1d15637381a to_pred/set attributes now properly handle variables of type "... => T set"
berghofe
parents: 46219
diff changeset
   214
            HOLogic.mk_psplits ps U HOLogic.boolT
b1d15637381a to_pred/set attributes now properly handle variables of type "... => T set"
berghofe
parents: 46219
diff changeset
   215
              (list_comb (x', map Bound (length Ts - 1 downto 0))))))
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   216
    end) fs;
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   217
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   218
fun mk_to_pred_eq ctxt p fs optfs' T thm =
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   219
  let
59642
929984c529d3 clarified context;
wenzelm
parents: 59621
diff changeset
   220
    val insts = mk_to_pred_inst ctxt fs;
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   221
    val thm' = Thm.instantiate ([], insts) thm;
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   222
    val thm'' =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   223
      (case optfs' of
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   224
        NONE => thm' RS sym
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   225
      | SOME fs' =>
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   226
          let
45979
296d9a9c8d24 treatment of type constructor `set`
haftmann
parents: 45384
diff changeset
   227
            val U = HOLogic.dest_setT (body_type T);
32342
3fabf5b5fc83 path-sensitive tuple combinators carry a "p"(ath) prefix; combinators for standard right-fold tuples
haftmann
parents: 32287
diff changeset
   228
            val Ts = HOLogic.strip_ptupleT fs' U;
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   229
            val arg_cong' = Thm.incr_indexes (Thm.maxidx_of thm + 1) arg_cong;
60781
2da59cdf531c updated to infer_instantiate;
wenzelm
parents: 60642
diff changeset
   230
            val (Var (arg_cong_f, _), _) = arg_cong' |> Thm.concl_of |>
2da59cdf531c updated to infer_instantiate;
wenzelm
parents: 60642
diff changeset
   231
              dest_comb |> snd |> strip_comb |> snd |> hd |> dest_comb;
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   232
          in
60781
2da59cdf531c updated to infer_instantiate;
wenzelm
parents: 60642
diff changeset
   233
            thm' RS (infer_instantiate ctxt [(arg_cong_f,
59642
929984c529d3 clarified context;
wenzelm
parents: 59621
diff changeset
   234
              Thm.cterm_of ctxt (Abs ("P", Ts ---> HOLogic.boolT,
32342
3fabf5b5fc83 path-sensitive tuple combinators carry a "p"(ath) prefix; combinators for standard right-fold tuples
haftmann
parents: 32287
diff changeset
   235
                HOLogic.Collect_const U $ HOLogic.mk_psplits fs' U
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   236
                  HOLogic.boolT (Bound 0))))] arg_cong' RS sym)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   237
          end)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   238
  in
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   239
    Simplifier.simplify (put_simpset HOL_basic_ss ctxt addsimps [mem_Collect_eq, @{thm split_conv}]
56512
9276da80f7c3 modernized simproc_setup;
wenzelm
parents: 56245
diff changeset
   240
      addsimprocs [@{simproc Collect_mem}]) thm'' |>
60328
9c94e6a30d29 clarified context;
wenzelm
parents: 59936
diff changeset
   241
        zero_var_indexes |> eta_contract_thm ctxt (equal p)
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   242
  end;
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   243
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   244
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   245
(**** declare rules for converting predicates to sets ****)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   246
50774
ac53370dfae1 more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
wenzelm
parents: 49324
diff changeset
   247
exception Malformed of string;
ac53370dfae1 more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
wenzelm
parents: 49324
diff changeset
   248
ac53370dfae1 more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
wenzelm
parents: 49324
diff changeset
   249
fun add context thm (tab as {to_set_simps, to_pred_simps, set_arities, pred_arities}) =
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   250
  (case Thm.prop_of thm of
38864
4abe644fcea5 formerly unnamed infix equality now named HOL.eq
haftmann
parents: 38795
diff changeset
   251
    Const (@{const_name Trueprop}, _) $ (Const (@{const_name HOL.eq}, Type (_, [T, _])) $ lhs $ rhs) =>
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   252
      (case body_type T of
35364
b8c62d60195c more antiquotations;
wenzelm
parents: 34986
diff changeset
   253
         @{typ bool} =>
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   254
           let
50774
ac53370dfae1 more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
wenzelm
parents: 49324
diff changeset
   255
             val thy = Context.theory_of context;
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   256
             val ctxt = Context.proof_of context;
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   257
             fun factors_of t fs = case strip_abs_body t of
37677
c5a8b612e571 qualified constants Set.member and Set.Collect
haftmann
parents: 37390
diff changeset
   258
                 Const (@{const_name Set.member}, _) $ u $ S =>
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   259
                   if is_Free S orelse is_Var S then
32287
65d5c5b30747 cleaned up abstract tuple operations and named them consistently
haftmann
parents: 32135
diff changeset
   260
                     let val ps = HOLogic.flat_tuple_paths u
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   261
                     in (SOME ps, (S, ps) :: fs) end
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   262
                   else (NONE, fs)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   263
               | _ => (NONE, fs);
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   264
             val (h, ts) = strip_comb lhs
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   265
             val (pfs, fs) = fold_map factors_of ts [];
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   266
             val ((h', ts'), fs') = (case rhs of
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   267
                 Abs _ => (case strip_abs_body rhs of
37677
c5a8b612e571 qualified constants Set.member and Set.Collect
haftmann
parents: 37390
diff changeset
   268
                     Const (@{const_name Set.member}, _) $ u $ S =>
32287
65d5c5b30747 cleaned up abstract tuple operations and named them consistently
haftmann
parents: 32135
diff changeset
   269
                       (strip_comb S, SOME (HOLogic.flat_tuple_paths u))
50774
ac53370dfae1 more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
wenzelm
parents: 49324
diff changeset
   270
                   | _ => raise Malformed "member symbol on right-hand side expected")
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   271
               | _ => (strip_comb rhs, NONE))
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   272
           in
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   273
             case (name_type_of h, name_type_of h') of
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   274
               (SOME (s, T), SOME (s', T')) =>
26047
d27b89c95b29 Instead of raising an exception, pred_set_conv now ignores conversion
berghofe
parents: 25978
diff changeset
   275
                 if exists (fn (U, _) =>
d27b89c95b29 Instead of raising an exception, pred_set_conv now ignores conversion
berghofe
parents: 25978
diff changeset
   276
                   Sign.typ_instance thy (T', U) andalso
d27b89c95b29 Instead of raising an exception, pred_set_conv now ignores conversion
berghofe
parents: 25978
diff changeset
   277
                   Sign.typ_instance thy (U, T'))
d27b89c95b29 Instead of raising an exception, pred_set_conv now ignores conversion
berghofe
parents: 25978
diff changeset
   278
                     (Symtab.lookup_list set_arities s')
d27b89c95b29 Instead of raising an exception, pred_set_conv now ignores conversion
berghofe
parents: 25978
diff changeset
   279
                 then
57870
561680651364 observe context visibility -- less redundant warnings;
wenzelm
parents: 56512
diff changeset
   280
                   (if Context_Position.is_really_visible ctxt then
561680651364 observe context visibility -- less redundant warnings;
wenzelm
parents: 56512
diff changeset
   281
                     warning ("Ignoring conversion rule for operator " ^ s')
561680651364 observe context visibility -- less redundant warnings;
wenzelm
parents: 56512
diff changeset
   282
                    else (); tab)
26047
d27b89c95b29 Instead of raising an exception, pred_set_conv now ignores conversion
berghofe
parents: 25978
diff changeset
   283
                 else
d27b89c95b29 Instead of raising an exception, pred_set_conv now ignores conversion
berghofe
parents: 25978
diff changeset
   284
                   {to_set_simps = thm :: to_set_simps,
d27b89c95b29 Instead of raising an exception, pred_set_conv now ignores conversion
berghofe
parents: 25978
diff changeset
   285
                    to_pred_simps =
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   286
                      mk_to_pred_eq ctxt h fs fs' T' thm :: to_pred_simps,
26047
d27b89c95b29 Instead of raising an exception, pred_set_conv now ignores conversion
berghofe
parents: 25978
diff changeset
   287
                    set_arities = Symtab.insert_list op = (s',
d27b89c95b29 Instead of raising an exception, pred_set_conv now ignores conversion
berghofe
parents: 25978
diff changeset
   288
                      (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
   289
                    pred_arities = Symtab.insert_list op = (s,
d27b89c95b29 Instead of raising an exception, pred_set_conv now ignores conversion
berghofe
parents: 25978
diff changeset
   290
                      (T, (pfs, fs'))) pred_arities}
50774
ac53370dfae1 more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
wenzelm
parents: 49324
diff changeset
   291
             | _ => raise Malformed "set / predicate constant expected"
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   292
           end
50774
ac53370dfae1 more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
wenzelm
parents: 49324
diff changeset
   293
       | _ => raise Malformed "equation between predicates expected")
ac53370dfae1 more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
wenzelm
parents: 49324
diff changeset
   294
  | _ => raise Malformed "equation expected")
ac53370dfae1 more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
wenzelm
parents: 49324
diff changeset
   295
  handle Malformed msg =>
57870
561680651364 observe context visibility -- less redundant warnings;
wenzelm
parents: 56512
diff changeset
   296
    let
561680651364 observe context visibility -- less redundant warnings;
wenzelm
parents: 56512
diff changeset
   297
      val ctxt = Context.proof_of context
561680651364 observe context visibility -- less redundant warnings;
wenzelm
parents: 56512
diff changeset
   298
      val _ =
561680651364 observe context visibility -- less redundant warnings;
wenzelm
parents: 56512
diff changeset
   299
        if Context_Position.is_really_visible ctxt then
561680651364 observe context visibility -- less redundant warnings;
wenzelm
parents: 56512
diff changeset
   300
          warning ("Ignoring malformed set / predicate conversion rule: " ^ msg ^
561680651364 observe context visibility -- less redundant warnings;
wenzelm
parents: 56512
diff changeset
   301
            "\n" ^ Display.string_of_thm ctxt thm)
561680651364 observe context visibility -- less redundant warnings;
wenzelm
parents: 56512
diff changeset
   302
        else ();
561680651364 observe context visibility -- less redundant warnings;
wenzelm
parents: 56512
diff changeset
   303
    in tab end;
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   304
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   305
val pred_set_conv_att = Thm.declaration_attribute
50774
ac53370dfae1 more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
wenzelm
parents: 49324
diff changeset
   306
  (fn thm => fn ctxt => Data.map (add ctxt thm) ctxt);
23764
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
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   309
(**** convert theorem in set notation to predicate notation ****)
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
fun is_pred tab t =
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   312
  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
   313
    SOME (SOME _) => true | _ => false;
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   314
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   315
fun to_pred_simproc rules =
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   316
  let val rules' = map mk_meta_eq rules
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   317
  in
61144
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
   318
    Simplifier.make_simproc @{context} "to_pred"
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
   319
      {lhss = [anyt],
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
   320
       proc = fn _ => fn ctxt => fn ct =>
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
   321
        lookup_rule (Proof_Context.theory_of ctxt)
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
   322
          (Thm.prop_of #> Logic.dest_equals) rules' (Thm.term_of ct),
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
   323
       identifier = []}
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   324
  end;
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   325
59642
929984c529d3 clarified context;
wenzelm
parents: 59621
diff changeset
   326
fun to_pred_proc thy rules t =
929984c529d3 clarified context;
wenzelm
parents: 59621
diff changeset
   327
  case lookup_rule thy I rules t of
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   328
    NONE => NONE
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   329
  | SOME (lhs, rhs) =>
32035
8e77b6a250d5 tuned/modernized Envir.subst_XXX;
wenzelm
parents: 31998
diff changeset
   330
      SOME (Envir.subst_term
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   331
        (Pattern.match thy (lhs, t) (Vartab.empty, Vartab.empty)) rhs);
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   332
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   333
fun to_pred thms context thm =
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   334
  let
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   335
    val thy = Context.theory_of context;
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   336
    val ctxt = Context.proof_of context;
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   337
    val {to_pred_simps, set_arities, pred_arities, ...} =
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   338
      fold (add context) thms (Data.get context);
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   339
    val fs = filter (is_Var o fst)
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   340
      (infer_arities thy set_arities (NONE, Thm.prop_of thm) []);
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   341
    (* instantiate each set parameter with {(x, y). p x y} *)
59642
929984c529d3 clarified context;
wenzelm
parents: 59621
diff changeset
   342
    val insts = mk_to_pred_inst ctxt fs
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   343
  in
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   344
    thm |>
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   345
    Thm.instantiate ([], insts) |>
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   346
    Simplifier.full_simplify (put_simpset HOL_basic_ss ctxt addsimprocs
37136
e0c9d3e49e15 dropped legacy theorem bindings
haftmann
parents: 36960
diff changeset
   347
      [to_pred_simproc (mem_Collect_eq :: @{thm split_conv} :: to_pred_simps)]) |>
60328
9c94e6a30d29 clarified context;
wenzelm
parents: 59936
diff changeset
   348
    eta_contract_thm ctxt (is_pred pred_arities) |>
33368
b1cf34f1855c modernized structure Rule_Cases;
wenzelm
parents: 33278
diff changeset
   349
    Rule_Cases.save thm
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   350
  end;
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
val to_pred_att = Thm.rule_attribute o to_pred;
45979
296d9a9c8d24 treatment of type constructor `set`
haftmann
parents: 45384
diff changeset
   353
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   354
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   355
(**** convert theorem in predicate notation to set notation ****)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   356
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   357
fun to_set thms context thm =
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   358
  let
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   359
    val thy = Context.theory_of context;
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   360
    val ctxt = Context.proof_of context;
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   361
    val {to_set_simps, pred_arities, ...} =
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   362
      fold (add context) thms (Data.get context);
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   363
    val fs = filter (is_Var o fst)
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   364
      (infer_arities thy pred_arities (NONE, Thm.prop_of thm) []);
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   365
    (* instantiate each predicate parameter with %x y. (x, y) : s *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   366
    val insts = map (fn (x, ps) =>
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   367
      let
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   368
        val Ts = binder_types (fastype_of x);
46828
b1d15637381a to_pred/set attributes now properly handle variables of type "... => T set"
berghofe
parents: 46219
diff changeset
   369
        val l = length Ts;
b1d15637381a to_pred/set attributes now properly handle variables of type "... => T set"
berghofe
parents: 46219
diff changeset
   370
        val k = length ps;
b1d15637381a to_pred/set attributes now properly handle variables of type "... => T set"
berghofe
parents: 46219
diff changeset
   371
        val (Rs, Us) = chop (l - k - 1) Ts;
b1d15637381a to_pred/set attributes now properly handle variables of type "... => T set"
berghofe
parents: 46219
diff changeset
   372
        val T = HOLogic.mk_ptupleT ps Us;
b1d15637381a to_pred/set attributes now properly handle variables of type "... => T set"
berghofe
parents: 46219
diff changeset
   373
        val x' = map_type (K (Rs ---> HOLogic.mk_setT T)) x
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   374
      in
60642
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60330
diff changeset
   375
        (dest_Var x,
59642
929984c529d3 clarified context;
wenzelm
parents: 59621
diff changeset
   376
         Thm.cterm_of ctxt (fold_rev (Term.abs o pair "x") Ts
46828
b1d15637381a to_pred/set attributes now properly handle variables of type "... => T set"
berghofe
parents: 46219
diff changeset
   377
          (HOLogic.mk_mem (HOLogic.mk_ptuple ps T (map Bound (k downto 0)),
b1d15637381a to_pred/set attributes now properly handle variables of type "... => T set"
berghofe
parents: 46219
diff changeset
   378
             list_comb (x', map Bound (l - 1 downto k + 1))))))
46219
426ed18eba43 discontinued old-style Term.list_abs in favour of plain Term.abs;
wenzelm
parents: 45979
diff changeset
   379
      end) fs;
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   380
  in
25416
1d8ebaf5f211 to_pred and to_set now save induction and case rule tags.
berghofe
parents: 25016
diff changeset
   381
    thm |>
1d8ebaf5f211 to_pred and to_set now save induction and case rule tags.
berghofe
parents: 25016
diff changeset
   382
    Thm.instantiate ([], insts) |>
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   383
    Simplifier.full_simplify (put_simpset HOL_basic_ss ctxt addsimps to_set_simps
56512
9276da80f7c3 modernized simproc_setup;
wenzelm
parents: 56245
diff changeset
   384
        addsimprocs [strong_ind_simproc pred_arities, @{simproc Collect_mem}]) |>
33368
b1cf34f1855c modernized structure Rule_Cases;
wenzelm
parents: 33278
diff changeset
   385
    Rule_Cases.save thm
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   386
  end;
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   387
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   388
val to_set_att = Thm.rule_attribute o to_set;
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   389
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   390
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   391
(**** definition of inductive sets ****)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   392
29389
0a49f940d729 inductive: added fork_mono flag;
wenzelm
parents: 29288
diff changeset
   393
fun add_ind_set_def
49170
03bee3a6a1b7 discontinued obsolete fork_mono to loosen some brakes -- NB: TTY interaction has Goal.future_proofs disabled due to missing Future.worker_task;
wenzelm
parents: 46961
diff changeset
   394
    {quiet_mode, verbose, alt_name, coind, no_elim, no_ind, skip_mono}
33458
ae1f5d89b082 proper naming convention lthy: local_theory, but ctxt: Proof.context for arbitrary context;
wenzelm
parents: 33368
diff changeset
   395
    cs intros monos params cnames_syn lthy =
ae1f5d89b082 proper naming convention lthy: local_theory, but ctxt: Proof.context for arbitrary context;
wenzelm
parents: 33368
diff changeset
   396
  let
42361
23f352990944 modernized structure Proof_Context;
wenzelm
parents: 42083
diff changeset
   397
    val thy = Proof_Context.theory_of lthy;
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   398
    val {set_arities, pred_arities, to_pred_simps, ...} =
50774
ac53370dfae1 more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
wenzelm
parents: 49324
diff changeset
   399
      Data.get (Context.Proof lthy);
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   400
    fun infer (Abs (_, _, t)) = infer t
37677
c5a8b612e571 qualified constants Set.member and Set.Collect
haftmann
parents: 37390
diff changeset
   401
      | infer (Const (@{const_name Set.member}, _) $ t $ u) =
32287
65d5c5b30747 cleaned up abstract tuple operations and named them consistently
haftmann
parents: 32135
diff changeset
   402
          infer_arities thy set_arities (SOME (HOLogic.flat_tuple_paths t), u)
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   403
      | infer (t $ u) = infer t #> infer u
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   404
      | infer _ = I;
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   405
    val new_arities = filter_out
45979
296d9a9c8d24 treatment of type constructor `set`
haftmann
parents: 45384
diff changeset
   406
      (fn (x as Free (_, T), _) => member (op =) params x andalso length (binder_types T) > 0
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   407
        | _ => false) (fold (snd #> infer) intros []);
33278
ba9f52f56356 conceal internal bindings;
wenzelm
parents: 33049
diff changeset
   408
    val params' = map (fn x =>
ba9f52f56356 conceal internal bindings;
wenzelm
parents: 33049
diff changeset
   409
      (case AList.lookup op = new_arities x of
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   410
        SOME fs =>
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   411
          let
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   412
            val T = HOLogic.dest_setT (fastype_of x);
32342
3fabf5b5fc83 path-sensitive tuple combinators carry a "p"(ath) prefix; combinators for standard right-fold tuples
haftmann
parents: 32287
diff changeset
   413
            val Ts = HOLogic.strip_ptupleT fs T;
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   414
            val x' = map_type (K (Ts ---> HOLogic.boolT)) x
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   415
          in
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   416
            (x, (x',
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   417
              (HOLogic.Collect_const T $
32342
3fabf5b5fc83 path-sensitive tuple combinators carry a "p"(ath) prefix; combinators for standard right-fold tuples
haftmann
parents: 32287
diff changeset
   418
                 HOLogic.mk_psplits fs T HOLogic.boolT x',
46219
426ed18eba43 discontinued old-style Term.list_abs in favour of plain Term.abs;
wenzelm
parents: 45979
diff changeset
   419
               fold_rev (Term.abs o pair "x") Ts
426ed18eba43 discontinued old-style Term.list_abs in favour of plain Term.abs;
wenzelm
parents: 45979
diff changeset
   420
                 (HOLogic.mk_mem
426ed18eba43 discontinued old-style Term.list_abs in favour of plain Term.abs;
wenzelm
parents: 45979
diff changeset
   421
                   (HOLogic.mk_ptuple fs T (map Bound (length fs downto 0)), x)))))
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   422
          end
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   423
       | NONE => (x, (x, (x, x))))) params;
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   424
    val (params1, (params2, params3)) =
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   425
      params' |> map snd |> split_list ||> split_list;
30860
e5f9477aed50 Added check whether argument types of inductive set agree with types of declared
berghofe
parents: 30528
diff changeset
   426
    val paramTs = map fastype_of params;
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   427
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   428
    (* equations for converting sets to predicates *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   429
    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
   430
      let
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   431
        val fs = the_default [] (AList.lookup op = new_arities c);
45979
296d9a9c8d24 treatment of type constructor `set`
haftmann
parents: 45384
diff changeset
   432
        val (Us, U) = strip_type T |> apsnd HOLogic.dest_setT;
30860
e5f9477aed50 Added check whether argument types of inductive set agree with types of declared
berghofe
parents: 30528
diff changeset
   433
        val _ = Us = paramTs orelse error (Pretty.string_of (Pretty.chunks
e5f9477aed50 Added check whether argument types of inductive set agree with types of declared
berghofe
parents: 30528
diff changeset
   434
          [Pretty.str "Argument types",
33458
ae1f5d89b082 proper naming convention lthy: local_theory, but ctxt: Proof.context for arbitrary context;
wenzelm
parents: 33368
diff changeset
   435
           Pretty.block (Pretty.commas (map (Syntax.pretty_typ lthy) Us)),
30860
e5f9477aed50 Added check whether argument types of inductive set agree with types of declared
berghofe
parents: 30528
diff changeset
   436
           Pretty.str ("of " ^ s ^ " do not agree with types"),
33458
ae1f5d89b082 proper naming convention lthy: local_theory, but ctxt: Proof.context for arbitrary context;
wenzelm
parents: 33368
diff changeset
   437
           Pretty.block (Pretty.commas (map (Syntax.pretty_typ lthy) paramTs)),
30860
e5f9477aed50 Added check whether argument types of inductive set agree with types of declared
berghofe
parents: 30528
diff changeset
   438
           Pretty.str "of declared parameters"]));
32342
3fabf5b5fc83 path-sensitive tuple combinators carry a "p"(ath) prefix; combinators for standard right-fold tuples
haftmann
parents: 32287
diff changeset
   439
        val Ts = HOLogic.strip_ptupleT fs U;
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   440
        val c' = Free (s ^ "p",
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   441
          map fastype_of params1 @ Ts ---> HOLogic.boolT)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   442
      in
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   443
        ((c', (fs, U, Ts)),
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   444
         (list_comb (c, params2),
32342
3fabf5b5fc83 path-sensitive tuple combinators carry a "p"(ath) prefix; combinators for standard right-fold tuples
haftmann
parents: 32287
diff changeset
   445
          HOLogic.Collect_const U $ HOLogic.mk_psplits fs U HOLogic.boolT
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   446
            (list_comb (c', params1))))
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   447
      end) |> split_list |>> split_list;
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   448
    val eqns' = eqns @
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   449
      map (Thm.prop_of #> HOLogic.dest_Trueprop #> HOLogic.dest_eq)
37136
e0c9d3e49e15 dropped legacy theorem bindings
haftmann
parents: 36960
diff changeset
   450
        (mem_Collect_eq :: @{thm split_conv} :: to_pred_simps);
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   451
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   452
    (* predicate version of the introduction rules *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   453
    val intros' =
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   454
      map (fn (name_atts, t) => (name_atts,
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   455
        t |>
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   456
        map_aterms (fn u =>
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   457
          (case AList.lookup op = params' u of
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   458
             SOME (_, (u', _)) => u'
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   459
           | NONE => u)) |>
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   460
        Pattern.rewrite_term thy [] [to_pred_proc thy eqns'] |>
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   461
        eta_contract (member op = cs' orf is_pred pred_arities))) intros;
30345
76fd85bbf139 more uniform handling of binding in packages;
wenzelm
parents: 30305
diff changeset
   462
    val cnames_syn' = map (fn (b, _) => (Binding.suffix_name "p" b, NoSyn)) cnames_syn;
33458
ae1f5d89b082 proper naming convention lthy: local_theory, but ctxt: Proof.context for arbitrary context;
wenzelm
parents: 33368
diff changeset
   463
    val monos' = map (to_pred [] (Context.Proof lthy)) monos;
38665
e92223c886f8 introducing simplification equations for inductive sets; added data structure for storing equations; rewriting retrieval of simplification equation for inductive predicates and sets
bulwahn
parents: 37863
diff changeset
   464
    val ({preds, intrs, elims, raw_induct, eqs, ...}, lthy1) =
31723
f5cafe803b55 discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 30860
diff changeset
   465
      Inductive.add_ind_def
33669
ae9a2ea9a989 inductive: eliminated obsolete kind;
wenzelm
parents: 33643
diff changeset
   466
        {quiet_mode = quiet_mode, verbose = verbose, alt_name = Binding.empty,
49170
03bee3a6a1b7 discontinued obsolete fork_mono to loosen some brakes -- NB: TTY interaction has Goal.future_proofs disabled due to missing Future.worker_task;
wenzelm
parents: 46961
diff changeset
   467
          coind = coind, no_elim = no_elim, no_ind = no_ind, skip_mono = skip_mono}
33458
ae1f5d89b082 proper naming convention lthy: local_theory, but ctxt: Proof.context for arbitrary context;
wenzelm
parents: 33368
diff changeset
   468
        cs' intros' monos' params1 cnames_syn' lthy;
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   469
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   470
    (* define inductive sets using previously defined predicates *)
33458
ae1f5d89b082 proper naming convention lthy: local_theory, but ctxt: Proof.context for arbitrary context;
wenzelm
parents: 33368
diff changeset
   471
    val (defs, lthy2) = lthy1
33766
c679f05600cd adapted Local_Theory.define -- eliminated odd thm kind;
wenzelm
parents: 33671
diff changeset
   472
      |> fold_map Local_Theory.define
46909
3c73a121a387 more explicit indication of def names;
wenzelm
parents: 46828
diff changeset
   473
        (map (fn (((c, syn), (fs, U, _)), p) => ((c, syn), ((Thm.def_binding c, []),
33278
ba9f52f56356 conceal internal bindings;
wenzelm
parents: 33049
diff changeset
   474
           fold_rev lambda params (HOLogic.Collect_const U $
ba9f52f56356 conceal internal bindings;
wenzelm
parents: 33049
diff changeset
   475
             HOLogic.mk_psplits fs U HOLogic.boolT (list_comb (p, params3))))))
ba9f52f56356 conceal internal bindings;
wenzelm
parents: 33049
diff changeset
   476
           (cnames_syn ~~ cs_info ~~ preds))
59880
30687c3f2b10 clarified role of naming for background theory: transform_binding (e.g. for "concealed" flag) uses naming of hypothetical context;
wenzelm
parents: 59859
diff changeset
   477
      ||> Proof_Context.restore_naming lthy1;
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   478
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   479
    (* prove theorems for converting predicate to set notation *)
33458
ae1f5d89b082 proper naming convention lthy: local_theory, but ctxt: Proof.context for arbitrary context;
wenzelm
parents: 33368
diff changeset
   480
    val lthy3 = fold
ae1f5d89b082 proper naming convention lthy: local_theory, but ctxt: Proof.context for arbitrary context;
wenzelm
parents: 33368
diff changeset
   481
      (fn (((p, c as Free (s, _)), (fs, U, Ts)), (_, (_, def))) => fn lthy =>
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   482
        let val conv_thm =
33458
ae1f5d89b082 proper naming convention lthy: local_theory, but ctxt: Proof.context for arbitrary context;
wenzelm
parents: 33368
diff changeset
   483
          Goal.prove lthy (map (fst o dest_Free) params) []
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   484
            (HOLogic.mk_Trueprop (HOLogic.mk_eq
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   485
              (list_comb (p, params3),
46219
426ed18eba43 discontinued old-style Term.list_abs in favour of plain Term.abs;
wenzelm
parents: 45979
diff changeset
   486
               fold_rev (Term.abs o pair "x") Ts
426ed18eba43 discontinued old-style Term.list_abs in favour of plain Term.abs;
wenzelm
parents: 45979
diff changeset
   487
                (HOLogic.mk_mem (HOLogic.mk_ptuple fs U (map Bound (length fs downto 0)),
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   488
                  list_comb (c, params))))))
59498
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 58839
diff changeset
   489
            (K (REPEAT (resolve_tac lthy @{thms ext} 1) THEN
58839
ccda99401bc8 eliminated aliases;
wenzelm
parents: 58011
diff changeset
   490
              simp_tac (put_simpset HOL_basic_ss lthy addsimps
ccda99401bc8 eliminated aliases;
wenzelm
parents: 58011
diff changeset
   491
                [def, mem_Collect_eq, @{thm split_conv}]) 1))
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   492
        in
33671
4b0f2599ed48 modernized structure Local_Theory;
wenzelm
parents: 33670
diff changeset
   493
          lthy |> Local_Theory.note ((Binding.name (s ^ "p_" ^ s ^ "_eq"),
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   494
            [Attrib.internal (K pred_set_conv_att)]),
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   495
              [conv_thm]) |> snd
33458
ae1f5d89b082 proper naming convention lthy: local_theory, but ctxt: Proof.context for arbitrary context;
wenzelm
parents: 33368
diff changeset
   496
        end) (preds ~~ cs ~~ cs_info ~~ defs) lthy2;
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   497
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   498
    (* convert theorems to set notation *)
28083
103d9282a946 explicit type Name.binding for higher-specification elements;
wenzelm
parents: 27330
diff changeset
   499
    val rec_name =
28965
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
   500
      if Binding.is_empty alt_name then
30223
24d975352879 renamed Binding.name_pos to Binding.make, renamed Binding.base_name to Binding.name_of, renamed Binding.map_base to Binding.map_name, added mandatory flag to Binding.qualify;
wenzelm
parents: 30089
diff changeset
   501
        Binding.name (space_implode "_" (map (Binding.name_of o fst) cnames_syn))
28083
103d9282a946 explicit type Name.binding for higher-specification elements;
wenzelm
parents: 27330
diff changeset
   502
      else alt_name;
33671
4b0f2599ed48 modernized structure Local_Theory;
wenzelm
parents: 33670
diff changeset
   503
    val cnames = map (Local_Theory.full_name lthy3 o #1) cnames_syn;  (* FIXME *)
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   504
    val (intr_names, intr_atts) = split_list (map fst intros);
33458
ae1f5d89b082 proper naming convention lthy: local_theory, but ctxt: Proof.context for arbitrary context;
wenzelm
parents: 33368
diff changeset
   505
    val raw_induct' = to_set [] (Context.Proof lthy3) raw_induct;
37734
489ac1ecb9f1 added the new command inductive_cases to derive simplification equations for inductive predicates; added binding simps for general simplification equation
bulwahn
parents: 37677
diff changeset
   506
    val (intrs', elims', eqs', induct, inducts, lthy4) =
35757
c2884bec5463 adding Spec_Rules to definitional package inductive and inductive_set
bulwahn
parents: 35646
diff changeset
   507
      Inductive.declare_rules rec_name coind no_ind cnames (map fst defs)
33459
wenzelm
parents: 33458
diff changeset
   508
        (map (to_set [] (Context.Proof lthy3)) intrs) intr_names intr_atts
wenzelm
parents: 33458
diff changeset
   509
        (map (fn th => (to_set [] (Context.Proof lthy3) th,
44045
2814ff2a6e3e infrastructure for attaching names to hypothesis in cases; realised via the same tag mechanism as case names
nipkow
parents: 43278
diff changeset
   510
           map (fst o fst) (fst (Rule_Cases.get th)),
34986
7f7939c9370f Added "constraints" tag / attribute for specifying the number of equality
berghofe
parents: 34903
diff changeset
   511
           Rule_Cases.get_constraints th)) elims)
38665
e92223c886f8 introducing simplification equations for inductive sets; added data structure for storing equations; rewriting retrieval of simplification equation for inductive predicates and sets
bulwahn
parents: 37863
diff changeset
   512
        (map (to_set [] (Context.Proof lthy3)) eqs) raw_induct' lthy3;
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   513
  in
35646
b32d6c1bdb4d Added inducts field to inductive_result.
berghofe
parents: 35364
diff changeset
   514
    ({intrs = intrs', elims = elims', induct = induct, inducts = inducts,
37734
489ac1ecb9f1 added the new command inductive_cases to derive simplification equations for inductive predicates; added binding simps for general simplification equation
bulwahn
parents: 37677
diff changeset
   515
      raw_induct = raw_induct', preds = map fst defs, eqs = eqs'},
33458
ae1f5d89b082 proper naming convention lthy: local_theory, but ctxt: Proof.context for arbitrary context;
wenzelm
parents: 33368
diff changeset
   516
     lthy4)
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   517
  end;
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   518
31723
f5cafe803b55 discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 30860
diff changeset
   519
val add_inductive_i = Inductive.gen_add_inductive_i add_ind_set_def;
f5cafe803b55 discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 30860
diff changeset
   520
val add_inductive = Inductive.gen_add_inductive add_ind_set_def;
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   521
45384
dffa657f0aa2 clarified attribute "mono_set": pure declaration, proper export in ML;
wenzelm
parents: 45375
diff changeset
   522
fun mono_att att =
dffa657f0aa2 clarified attribute "mono_set": pure declaration, proper export in ML;
wenzelm
parents: 45375
diff changeset
   523
  Thm.declaration_attribute (fn thm => fn context =>
dffa657f0aa2 clarified attribute "mono_set": pure declaration, proper export in ML;
wenzelm
parents: 45375
diff changeset
   524
    Thm.attribute_declaration att (to_pred [] context thm) context);
45375
7fe19930dfc9 more explicit representation of rule_attribute vs. declaration_attribute vs. mixed_attribute;
wenzelm
parents: 45177
diff changeset
   525
45384
dffa657f0aa2 clarified attribute "mono_set": pure declaration, proper export in ML;
wenzelm
parents: 45375
diff changeset
   526
val mono_add = mono_att Inductive.mono_add;
dffa657f0aa2 clarified attribute "mono_set": pure declaration, proper export in ML;
wenzelm
parents: 45375
diff changeset
   527
val mono_del = mono_att Inductive.mono_del;
23764
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
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   530
(** package setup **)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   531
56512
9276da80f7c3 modernized simproc_setup;
wenzelm
parents: 56245
diff changeset
   532
(* attributes *)
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   533
56512
9276da80f7c3 modernized simproc_setup;
wenzelm
parents: 56245
diff changeset
   534
val _ =
9276da80f7c3 modernized simproc_setup;
wenzelm
parents: 56245
diff changeset
   535
  Theory.setup
9276da80f7c3 modernized simproc_setup;
wenzelm
parents: 56245
diff changeset
   536
   (Attrib.setup @{binding pred_set_conv} (Scan.succeed pred_set_conv_att)
9276da80f7c3 modernized simproc_setup;
wenzelm
parents: 56245
diff changeset
   537
      "declare rules for converting between predicate and set notation" #>
9276da80f7c3 modernized simproc_setup;
wenzelm
parents: 56245
diff changeset
   538
    Attrib.setup @{binding to_set} (Attrib.thms >> to_set_att)
9276da80f7c3 modernized simproc_setup;
wenzelm
parents: 56245
diff changeset
   539
      "convert rule to set notation" #>
9276da80f7c3 modernized simproc_setup;
wenzelm
parents: 56245
diff changeset
   540
    Attrib.setup @{binding to_pred} (Attrib.thms >> to_pred_att)
9276da80f7c3 modernized simproc_setup;
wenzelm
parents: 56245
diff changeset
   541
      "convert rule to predicate notation" #>
9276da80f7c3 modernized simproc_setup;
wenzelm
parents: 56245
diff changeset
   542
    Attrib.setup @{binding mono_set} (Attrib.add_del mono_add mono_del)
9276da80f7c3 modernized simproc_setup;
wenzelm
parents: 56245
diff changeset
   543
      "declare of monotonicity rule for set operators");
30528
7173bf123335 simplified attribute setup;
wenzelm
parents: 30345
diff changeset
   544
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   545
56512
9276da80f7c3 modernized simproc_setup;
wenzelm
parents: 56245
diff changeset
   546
(* commands *)
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   547
31723
f5cafe803b55 discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 30860
diff changeset
   548
val ind_set_decl = Inductive.gen_ind_decl add_ind_set_def;
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   549
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24815
diff changeset
   550
val _ =
59936
b8ffc3dc9e24 @{command_spec} is superseded by @{command_keyword};
wenzelm
parents: 59880
diff changeset
   551
  Outer_Syntax.local_theory @{command_keyword inductive_set} "define inductive sets"
33458
ae1f5d89b082 proper naming convention lthy: local_theory, but ctxt: Proof.context for arbitrary context;
wenzelm
parents: 33368
diff changeset
   552
    (ind_set_decl false);
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   553
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24815
diff changeset
   554
val _ =
59936
b8ffc3dc9e24 @{command_spec} is superseded by @{command_keyword};
wenzelm
parents: 59880
diff changeset
   555
  Outer_Syntax.local_theory @{command_keyword coinductive_set} "define coinductive sets"
33458
ae1f5d89b082 proper naming convention lthy: local_theory, but ctxt: Proof.context for arbitrary context;
wenzelm
parents: 33368
diff changeset
   556
    (ind_set_decl true);
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   557
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   558
end;