src/HOL/Tools/inductive_set.ML
author wenzelm
Sat, 13 Jul 2013 00:50:49 +0200
changeset 52641 c56b6fa636e8
parent 51717 9e7d1c139569
child 54895 515630483010
permissions -rw-r--r--
hybrid "auto" tool setup, for TTY (within theory) and PIDE (global print function);
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 ->
28084
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 ->
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
28723
c4fcffe0fe48 exported codegen_preproc
haftmann
parents: 28084
diff changeset
    27
  val codegen_preproc: theory -> thm list -> thm list
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    28
  val setup: theory -> theory
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    29
end;
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    30
31723
f5cafe803b55 discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 30860
diff changeset
    31
structure Inductive_Set: INDUCTIVE_SET =
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    32
struct
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
(**** simplify {(x1, ..., xn). (x1, ..., xn) : S} to S ****)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    35
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    36
val collect_mem_simproc =
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
    37
  Simplifier.simproc_global @{theory Set} "Collect_mem" ["Collect t"] (fn ctxt =>
35364
b8c62d60195c more antiquotations;
wenzelm
parents: 34986
diff changeset
    38
    fn S as Const (@{const_name Collect}, Type ("fun", [_, T])) $ t =>
34903
d75704c60768 dropped unused binding
haftmann
parents: 33766
diff changeset
    39
         let val (u, _, ps) = HOLogic.strip_psplits t
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    40
         in case u of
37677
c5a8b612e571 qualified constants Set.member and Set.Collect
haftmann
parents: 37390
diff changeset
    41
           (c as Const (@{const_name Set.member}, _)) $ q $ S' =>
32342
3fabf5b5fc83 path-sensitive tuple combinators carry a "p"(ath) prefix; combinators for standard right-fold tuples
haftmann
parents: 32287
diff changeset
    42
             (case try (HOLogic.strip_ptuple ps) q of
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    43
                NONE => NONE
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    44
              | SOME ts =>
42083
e1209fc7ecdc added Term.is_open and Term.is_dependent convenience, to cover common situations of loose bounds;
wenzelm
parents: 41489
diff changeset
    45
                  if not (Term.is_open S') andalso
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    46
                    ts = map Bound (length ps downto 0)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    47
                  then
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
    48
                    let val simp =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
    49
                      full_simp_tac (put_simpset HOL_basic_ss ctxt
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
    50
                        addsimps [@{thm split_paired_all}, @{thm split_conv}]) 1
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    51
                    in
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
    52
                      SOME (Goal.prove ctxt [] []
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    53
                        (Const ("==", T --> T --> propT) $ S $ S')
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    54
                        (K (EVERY
24815
f7093e90f36c tuned internal interfaces: flags record, added kind for results;
wenzelm
parents: 24745
diff changeset
    55
                          [rtac eq_reflection 1, rtac @{thm subset_antisym} 1,
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    56
                           rtac subsetI 1, dtac CollectD 1, simp,
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    57
                           rtac subsetI 1, rtac CollectI 1, simp])))
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
                  else NONE)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    60
         | _ => NONE
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    61
         end
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    62
     | _ => NONE);
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    63
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    64
(***********************************************************************************)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    65
(* 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
    66
(* 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
    67
(* used for converting "strong" (co)induction rules                                *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    68
(***********************************************************************************)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    69
23849
2a0e24c74593 strong_ind_simproc now only rewrites arguments of inductive predicates.
berghofe
parents: 23764
diff changeset
    70
val anyt = Free ("t", TFree ("'t", []));
2a0e24c74593 strong_ind_simproc now only rewrites arguments of inductive predicates.
berghofe
parents: 23764
diff changeset
    71
2a0e24c74593 strong_ind_simproc now only rewrites arguments of inductive predicates.
berghofe
parents: 23764
diff changeset
    72
fun strong_ind_simproc tab =
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
    73
  Simplifier.simproc_global_i @{theory HOL} "strong_ind" [anyt] (fn ctxt => fn t =>
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    74
    let
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
    75
      val thy = Proof_Context.theory_of ctxt;
23849
2a0e24c74593 strong_ind_simproc now only rewrites arguments of inductive predicates.
berghofe
parents: 23764
diff changeset
    76
      fun close p t f =
2a0e24c74593 strong_ind_simproc now only rewrites arguments of inductive predicates.
berghofe
parents: 23764
diff changeset
    77
        let val vs = Term.add_vars t []
2a0e24c74593 strong_ind_simproc now only rewrites arguments of inductive predicates.
berghofe
parents: 23764
diff changeset
    78
        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
    79
          (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
    80
        end;
38795
848be46708dc formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents: 38715
diff changeset
    81
      fun mkop @{const_name HOL.conj} T x =
35364
b8c62d60195c more antiquotations;
wenzelm
parents: 34986
diff changeset
    82
            SOME (Const (@{const_name Lattices.inf}, T --> T --> T), x)
38795
848be46708dc formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents: 38715
diff changeset
    83
        | mkop @{const_name HOL.disj} T x =
35364
b8c62d60195c more antiquotations;
wenzelm
parents: 34986
diff changeset
    84
            SOME (Const (@{const_name Lattices.sup}, T --> T --> T), x)
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    85
        | mkop _ _ _ = NONE;
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    86
      fun mk_collect p T t =
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    87
        let val U = HOLogic.dest_setT T
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    88
        in HOLogic.Collect_const U $
32342
3fabf5b5fc83 path-sensitive tuple combinators carry a "p"(ath) prefix; combinators for standard right-fold tuples
haftmann
parents: 32287
diff changeset
    89
          HOLogic.mk_psplits (HOLogic.flat_tuple_paths p) U HOLogic.boolT t
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    90
        end;
37677
c5a8b612e571 qualified constants Set.member and Set.Collect
haftmann
parents: 37390
diff changeset
    91
      fun decomp (Const (s, _) $ ((m as Const (@{const_name Set.member},
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    92
            Type (_, [_, Type (_, [T, _])]))) $ p $ S) $ u) =
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    93
              mkop s T (m, p, S, mk_collect p T (head_of u))
37677
c5a8b612e571 qualified constants Set.member and Set.Collect
haftmann
parents: 37390
diff changeset
    94
        | decomp (Const (s, _) $ u $ ((m as Const (@{const_name Set.member},
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    95
            Type (_, [_, Type (_, [T, _])]))) $ p $ S)) =
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
    96
              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
    97
        | decomp _ = NONE;
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
    98
      val simp =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
    99
        full_simp_tac
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   100
          (put_simpset HOL_basic_ss ctxt addsimps [mem_Collect_eq, @{thm split_conv}]) 1;
23849
2a0e24c74593 strong_ind_simproc now only rewrites arguments of inductive predicates.
berghofe
parents: 23764
diff changeset
   101
      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
   102
          [] => NONE
2a0e24c74593 strong_ind_simproc now only rewrites arguments of inductive predicates.
berghofe
parents: 23764
diff changeset
   103
        | xs => (case decomp (strip_abs_body t) of
2a0e24c74593 strong_ind_simproc now only rewrites arguments of inductive predicates.
berghofe
parents: 23764
diff changeset
   104
            NONE => NONE
2a0e24c74593 strong_ind_simproc now only rewrites arguments of inductive predicates.
berghofe
parents: 23764
diff changeset
   105
          | SOME (bop, (m, p, S, S')) =>
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   106
              SOME (close (Goal.prove ctxt [] [])
46219
426ed18eba43 discontinued old-style Term.list_abs in favour of plain Term.abs;
wenzelm
parents: 45979
diff changeset
   107
                (Logic.mk_equals (t, fold_rev Term.abs xs (m $ p $ (bop $ S $ S'))))
23849
2a0e24c74593 strong_ind_simproc now only rewrites arguments of inductive predicates.
berghofe
parents: 23764
diff changeset
   108
                (K (EVERY
2a0e24c74593 strong_ind_simproc now only rewrites arguments of inductive predicates.
berghofe
parents: 23764
diff changeset
   109
                  [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
   110
                   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
   111
                     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
   112
                   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
   113
                     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
   114
                handle ERROR _ => NONE))
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   115
    in
23849
2a0e24c74593 strong_ind_simproc now only rewrites arguments of inductive predicates.
berghofe
parents: 23764
diff changeset
   116
      case strip_comb t of
2a0e24c74593 strong_ind_simproc now only rewrites arguments of inductive predicates.
berghofe
parents: 23764
diff changeset
   117
        (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
   118
          SOME _ =>
2a0e24c74593 strong_ind_simproc now only rewrites arguments of inductive predicates.
berghofe
parents: 23764
diff changeset
   119
            let val rews = map mk_rew ts
2a0e24c74593 strong_ind_simproc now only rewrites arguments of inductive predicates.
berghofe
parents: 23764
diff changeset
   120
            in
2a0e24c74593 strong_ind_simproc now only rewrites arguments of inductive predicates.
berghofe
parents: 23764
diff changeset
   121
              if forall is_none rews then NONE
36945
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 36692
diff changeset
   122
              else SOME (fold (fn th1 => fn th2 => Thm.combination th2 th1)
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 36692
diff changeset
   123
                (map2 (fn SOME r => K r | NONE => Thm.reflexive o cterm_of thy)
9bec62c10714 less pervasive names from structure Thm;
wenzelm
parents: 36692
diff changeset
   124
                   rews ts) (Thm.reflexive (cterm_of thy h)))
23849
2a0e24c74593 strong_ind_simproc now only rewrites arguments of inductive predicates.
berghofe
parents: 23764
diff changeset
   125
            end
2a0e24c74593 strong_ind_simproc now only rewrites arguments of inductive predicates.
berghofe
parents: 23764
diff changeset
   126
        | NONE => NONE)
2a0e24c74593 strong_ind_simproc now only rewrites arguments of inductive predicates.
berghofe
parents: 23764
diff changeset
   127
      | _ => NONE
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   128
    end);
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   129
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   130
(* 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
   131
fun eta_contract p =
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   132
  let
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   133
    fun eta b (Abs (a, T, body)) =
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   134
          (case eta b body of
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   135
             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
   136
               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
   137
               else incr_boundvars ~1 f
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   138
           | body' => Abs (a, T, body'))
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   139
      | 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
   140
      | eta b t = t
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   141
  in eta false end;
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
fun eta_contract_thm p =
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   144
  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
   145
    Thm.transitive (Thm.eta_conversion ct)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   146
      (Thm.symmetric (Thm.eta_conversion
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   147
        (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
   148
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   149
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   150
(***********************************************************)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   151
(* rules for converting between predicate and set notation *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   152
(*                                                         *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   153
(* rules for converting predicates to sets have the form   *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   154
(* 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
   155
(*                                                         *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   156
(* rules for converting sets to predicates have the form   *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   157
(* 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
   158
(*                                                         *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   159
(* where s and p are parameters                            *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   160
(***********************************************************)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   161
50774
ac53370dfae1 more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
wenzelm
parents: 49324
diff changeset
   162
structure Data = Generic_Data
23764
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
  type T =
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   165
    {(* rules for converting predicates to sets *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   166
     to_set_simps: thm list,
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   167
     (* rules for converting sets to predicates *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   168
     to_pred_simps: thm list,
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   169
     (* arities of functions of type t set => ... => u set *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   170
     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
   171
     (* arities of functions of type (t => ... => bool) => u => ... => bool *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   172
     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
   173
  val empty = {to_set_simps = [], to_pred_simps = [],
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   174
    set_arities = Symtab.empty, pred_arities = Symtab.empty};
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   175
  val extend = I;
33519
e31a85f92ce9 adapted Generic_Data, Proof_Data;
wenzelm
parents: 33459
diff changeset
   176
  fun merge
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   177
    ({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
   178
      set_arities = set_arities1, pred_arities = pred_arities1},
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   179
     {to_set_simps = to_set_simps2, to_pred_simps = to_pred_simps2,
29288
253bcf2a5854 avoid polymorphic equality;
wenzelm
parents: 29064
diff changeset
   180
      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
   181
    {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
   182
     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
   183
     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
   184
     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
   185
);
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   186
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   187
fun name_type_of (Free p) = SOME p
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   188
  | name_type_of (Const p) = SOME p
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   189
  | name_type_of _ = NONE;
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   190
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   191
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
   192
  | map_type f (Var (ixn, T)) = Var (ixn, f T)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   193
  | map_type f _ = error "map_type";
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
fun find_most_specific is_inst f eq xs T =
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   196
  find_first (fn U => is_inst (T, f U)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   197
    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
   198
      (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
   199
        xs) xs;
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 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
   202
    NONE => NONE
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   203
  | 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
   204
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   205
fun lookup_rule thy f rules = find_most_specific
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   206
  (swap #> Pattern.matches thy) (f #> fst) (op aconv) rules;
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   207
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   208
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
   209
    (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
   210
  | (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
   211
  | (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
   212
      SOME (SOME (_, (arity, _))) =>
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   213
        (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
   214
           handle General.Subscript => error "infer_arities: bad term")
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   215
    | _ => fold (infer_arities thy arities) (map (pair NONE) ts)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   216
      (case optf of
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   217
         NONE => fs
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   218
       | 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
   219
           (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
   220
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
(*    derive the to_pred equation from the to_set equation    *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   224
(*                                                            *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   225
(* 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
   226
(* 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
   227
(* 3. simplify                                                *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   228
(**************************************************************)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   229
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   230
fun mk_to_pred_inst thy fs =
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   231
  map (fn (x, ps) =>
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   232
    let
46828
b1d15637381a to_pred/set attributes now properly handle variables of type "... => T set"
berghofe
parents: 46219
diff changeset
   233
      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
   234
      val U = HOLogic.dest_setT T;
b1d15637381a to_pred/set attributes now properly handle variables of type "... => T set"
berghofe
parents: 46219
diff changeset
   235
      val x' = map_type
b1d15637381a to_pred/set attributes now properly handle variables of type "... => T set"
berghofe
parents: 46219
diff changeset
   236
        (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
   237
    in
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   238
      (cterm_of thy x,
46828
b1d15637381a to_pred/set attributes now properly handle variables of type "... => T set"
berghofe
parents: 46219
diff changeset
   239
       cterm_of thy (fold_rev (Term.abs o pair "x") Ts
b1d15637381a to_pred/set attributes now properly handle variables of type "... => T set"
berghofe
parents: 46219
diff changeset
   240
         (HOLogic.Collect_const U $
b1d15637381a to_pred/set attributes now properly handle variables of type "... => T set"
berghofe
parents: 46219
diff changeset
   241
            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
   242
              (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
   243
    end) fs;
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   244
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   245
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
   246
  let
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   247
    val thy = theory_of_thm thm;
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   248
    val insts = mk_to_pred_inst thy fs;
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   249
    val thm' = Thm.instantiate ([], insts) thm;
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   250
    val thm'' =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   251
      (case optfs' of
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   252
        NONE => thm' RS sym
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   253
      | SOME fs' =>
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   254
          let
45979
296d9a9c8d24 treatment of type constructor `set`
haftmann
parents: 45384
diff changeset
   255
            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
   256
            val Ts = HOLogic.strip_ptupleT fs' U;
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   257
            (* FIXME: should cterm_instantiate increment indexes? *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   258
            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
   259
            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
   260
              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
   261
          in
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   262
            thm' RS (Drule.cterm_instantiate [(arg_cong_f,
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   263
              cterm_of thy (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
   264
                HOLogic.Collect_const U $ HOLogic.mk_psplits fs' U
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   265
                  HOLogic.boolT (Bound 0))))] arg_cong' RS sym)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   266
          end)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   267
  in
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   268
    Simplifier.simplify (put_simpset HOL_basic_ss ctxt addsimps [mem_Collect_eq, @{thm split_conv}]
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   269
      addsimprocs [collect_mem_simproc]) thm'' |>
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   270
        zero_var_indexes |> eta_contract_thm (equal p)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   271
  end;
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   272
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   273
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   274
(**** declare rules for converting predicates to sets ****)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   275
50774
ac53370dfae1 more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
wenzelm
parents: 49324
diff changeset
   276
exception Malformed of string;
ac53370dfae1 more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
wenzelm
parents: 49324
diff changeset
   277
ac53370dfae1 more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
wenzelm
parents: 49324
diff changeset
   278
fun add context thm (tab as {to_set_simps, to_pred_simps, set_arities, pred_arities}) =
ac53370dfae1 more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
wenzelm
parents: 49324
diff changeset
   279
  (case prop_of thm of
38864
4abe644fcea5 formerly unnamed infix equality now named HOL.eq
haftmann
parents: 38795
diff changeset
   280
    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
   281
      (case body_type T of
35364
b8c62d60195c more antiquotations;
wenzelm
parents: 34986
diff changeset
   282
         @{typ bool} =>
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   283
           let
50774
ac53370dfae1 more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
wenzelm
parents: 49324
diff changeset
   284
             val thy = Context.theory_of context;
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   285
             val ctxt = Context.proof_of context;
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   286
             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
   287
                 Const (@{const_name Set.member}, _) $ u $ S =>
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   288
                   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
   289
                     let val ps = HOLogic.flat_tuple_paths u
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   290
                     in (SOME ps, (S, ps) :: fs) end
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   291
                   else (NONE, fs)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   292
               | _ => (NONE, fs);
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   293
             val (h, ts) = strip_comb lhs
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   294
             val (pfs, fs) = fold_map factors_of ts [];
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   295
             val ((h', ts'), fs') = (case rhs of
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   296
                 Abs _ => (case strip_abs_body rhs of
37677
c5a8b612e571 qualified constants Set.member and Set.Collect
haftmann
parents: 37390
diff changeset
   297
                     Const (@{const_name Set.member}, _) $ u $ S =>
32287
65d5c5b30747 cleaned up abstract tuple operations and named them consistently
haftmann
parents: 32135
diff changeset
   298
                       (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
   299
                   | _ => raise Malformed "member symbol on right-hand side expected")
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   300
               | _ => (strip_comb rhs, NONE))
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   301
           in
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   302
             case (name_type_of h, name_type_of h') of
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   303
               (SOME (s, T), SOME (s', T')) =>
26047
d27b89c95b29 Instead of raising an exception, pred_set_conv now ignores conversion
berghofe
parents: 25978
diff changeset
   304
                 if exists (fn (U, _) =>
d27b89c95b29 Instead of raising an exception, pred_set_conv now ignores conversion
berghofe
parents: 25978
diff changeset
   305
                   Sign.typ_instance thy (T', U) andalso
d27b89c95b29 Instead of raising an exception, pred_set_conv now ignores conversion
berghofe
parents: 25978
diff changeset
   306
                   Sign.typ_instance thy (U, T'))
d27b89c95b29 Instead of raising an exception, pred_set_conv now ignores conversion
berghofe
parents: 25978
diff changeset
   307
                     (Symtab.lookup_list set_arities s')
d27b89c95b29 Instead of raising an exception, pred_set_conv now ignores conversion
berghofe
parents: 25978
diff changeset
   308
                 then
d27b89c95b29 Instead of raising an exception, pred_set_conv now ignores conversion
berghofe
parents: 25978
diff changeset
   309
                   (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
   310
                 else
d27b89c95b29 Instead of raising an exception, pred_set_conv now ignores conversion
berghofe
parents: 25978
diff changeset
   311
                   {to_set_simps = thm :: to_set_simps,
d27b89c95b29 Instead of raising an exception, pred_set_conv now ignores conversion
berghofe
parents: 25978
diff changeset
   312
                    to_pred_simps =
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   313
                      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
   314
                    set_arities = Symtab.insert_list op = (s',
d27b89c95b29 Instead of raising an exception, pred_set_conv now ignores conversion
berghofe
parents: 25978
diff changeset
   315
                      (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
   316
                    pred_arities = Symtab.insert_list op = (s,
d27b89c95b29 Instead of raising an exception, pred_set_conv now ignores conversion
berghofe
parents: 25978
diff changeset
   317
                      (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
   318
             | _ => raise Malformed "set / predicate constant expected"
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   319
           end
50774
ac53370dfae1 more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
wenzelm
parents: 49324
diff changeset
   320
       | _ => 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
   321
  | _ => raise Malformed "equation expected")
ac53370dfae1 more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
wenzelm
parents: 49324
diff changeset
   322
  handle Malformed msg =>
ac53370dfae1 more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
wenzelm
parents: 49324
diff changeset
   323
    (warning ("Ignoring malformed set / predicate conversion rule: " ^ msg ^
ac53370dfae1 more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
wenzelm
parents: 49324
diff changeset
   324
      "\n" ^ Display.string_of_thm (Context.proof_of context) thm); tab);
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   325
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   326
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
   327
  (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
   328
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   329
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   330
(**** convert theorem in set notation to predicate notation ****)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   331
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   332
fun is_pred tab t =
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   333
  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
   334
    SOME (SOME _) => true | _ => false;
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   335
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   336
fun to_pred_simproc rules =
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   337
  let val rules' = map mk_meta_eq rules
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   338
  in
38715
6513ea67d95d renamed Simplifier.simproc(_i) to Simplifier.simproc_global(_i) to emphasize that this is not the real thing;
wenzelm
parents: 38665
diff changeset
   339
    Simplifier.simproc_global_i @{theory HOL} "to_pred" [anyt]
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   340
      (fn ctxt => (lookup_rule (Proof_Context.theory_of ctxt) (prop_of #> Logic.dest_equals) rules'))
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   341
  end;
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   342
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   343
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
   344
    NONE => NONE
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   345
  | SOME (lhs, rhs) =>
32035
8e77b6a250d5 tuned/modernized Envir.subst_XXX;
wenzelm
parents: 31998
diff changeset
   346
      SOME (Envir.subst_term
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   347
        (Pattern.match thy (lhs, t) (Vartab.empty, Vartab.empty)) rhs);
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   348
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   349
fun to_pred thms context thm =
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   350
  let
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   351
    val thy = Context.theory_of context;
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   352
    val ctxt = Context.proof_of context;
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   353
    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
   354
      fold (add context) thms (Data.get context);
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   355
    val fs = filter (is_Var o fst)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   356
      (infer_arities thy set_arities (NONE, prop_of thm) []);
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   357
    (* instantiate each set parameter with {(x, y). p x y} *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   358
    val insts = mk_to_pred_inst thy fs
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   359
  in
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   360
    thm |>
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   361
    Thm.instantiate ([], insts) |>
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   362
    Simplifier.full_simplify (put_simpset HOL_basic_ss ctxt addsimprocs
37136
e0c9d3e49e15 dropped legacy theorem bindings
haftmann
parents: 36960
diff changeset
   363
      [to_pred_simproc (mem_Collect_eq :: @{thm split_conv} :: to_pred_simps)]) |>
25416
1d8ebaf5f211 to_pred and to_set now save induction and case rule tags.
berghofe
parents: 25016
diff changeset
   364
    eta_contract_thm (is_pred pred_arities) |>
33368
b1cf34f1855c modernized structure Rule_Cases;
wenzelm
parents: 33278
diff changeset
   365
    Rule_Cases.save thm
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   366
  end;
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   367
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   368
val to_pred_att = Thm.rule_attribute o to_pred;
45979
296d9a9c8d24 treatment of type constructor `set`
haftmann
parents: 45384
diff changeset
   369
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   370
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   371
(**** convert theorem in predicate notation to set notation ****)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   372
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   373
fun to_set thms context thm =
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   374
  let
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   375
    val thy = Context.theory_of context;
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   376
    val ctxt = Context.proof_of context;
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   377
    val {to_set_simps, pred_arities, ...} =
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   378
      fold (add context) thms (Data.get context);
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   379
    val fs = filter (is_Var o fst)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   380
      (infer_arities thy pred_arities (NONE, prop_of thm) []);
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   381
    (* instantiate each predicate parameter with %x y. (x, y) : s *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   382
    val insts = map (fn (x, ps) =>
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   383
      let
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   384
        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
   385
        val l = length Ts;
b1d15637381a to_pred/set attributes now properly handle variables of type "... => T set"
berghofe
parents: 46219
diff changeset
   386
        val k = length ps;
b1d15637381a to_pred/set attributes now properly handle variables of type "... => T set"
berghofe
parents: 46219
diff changeset
   387
        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
   388
        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
   389
        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
   390
      in
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   391
        (cterm_of thy x,
46219
426ed18eba43 discontinued old-style Term.list_abs in favour of plain Term.abs;
wenzelm
parents: 45979
diff changeset
   392
         cterm_of thy (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
   393
          (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
   394
             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
   395
      end) fs;
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   396
  in
25416
1d8ebaf5f211 to_pred and to_set now save induction and case rule tags.
berghofe
parents: 25016
diff changeset
   397
    thm |>
1d8ebaf5f211 to_pred and to_set now save induction and case rule tags.
berghofe
parents: 25016
diff changeset
   398
    Thm.instantiate ([], insts) |>
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   399
    Simplifier.full_simplify (put_simpset HOL_basic_ss ctxt addsimps to_set_simps
25487
d96d5808d926 to_set now applies collect_mem_simproc as well.
berghofe
parents: 25416
diff changeset
   400
        addsimprocs [strong_ind_simproc pred_arities, collect_mem_simproc]) |>
33368
b1cf34f1855c modernized structure Rule_Cases;
wenzelm
parents: 33278
diff changeset
   401
    Rule_Cases.save thm
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   402
  end;
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
val to_set_att = Thm.rule_attribute o to_set;
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   405
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   406
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   407
(**** preprocessor for code generator ****)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   408
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   409
fun codegen_preproc thy =
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   410
  let
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   411
    val {to_pred_simps, set_arities, pred_arities, ...} =
50774
ac53370dfae1 more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
wenzelm
parents: 49324
diff changeset
   412
      Data.get (Context.Theory thy);
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   413
    fun preproc thm =
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   414
      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
   415
          NONE => false
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   416
        | SOME arities => exists (fn (_, (xs, _)) =>
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   417
            forall is_none xs) arities) (prop_of thm)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   418
      then
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   419
        thm |>
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50774
diff changeset
   420
        Simplifier.full_simplify (Simplifier.global_context thy HOL_basic_ss addsimprocs
37136
e0c9d3e49e15 dropped legacy theorem bindings
haftmann
parents: 36960
diff changeset
   421
          [to_pred_simproc (mem_Collect_eq :: @{thm split_conv} :: to_pred_simps)]) |>
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   422
        eta_contract_thm (is_pred pred_arities)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   423
      else thm
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   424
  in map preproc end;
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   425
45177
189c81779a68 removing old code generator setup for inductive sets in the inductive set package
bulwahn
parents: 44045
diff changeset
   426
fun code_ind_att optmod = to_pred_att [];
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
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   429
(**** definition of inductive sets ****)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   430
29389
0a49f940d729 inductive: added fork_mono flag;
wenzelm
parents: 29288
diff changeset
   431
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
   432
    {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
   433
    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
   434
  let
42361
23f352990944 modernized structure Proof_Context;
wenzelm
parents: 42083
diff changeset
   435
    val thy = Proof_Context.theory_of lthy;
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   436
    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
   437
      Data.get (Context.Proof lthy);
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   438
    fun infer (Abs (_, _, t)) = infer t
37677
c5a8b612e571 qualified constants Set.member and Set.Collect
haftmann
parents: 37390
diff changeset
   439
      | infer (Const (@{const_name Set.member}, _) $ t $ u) =
32287
65d5c5b30747 cleaned up abstract tuple operations and named them consistently
haftmann
parents: 32135
diff changeset
   440
          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
   441
      | infer (t $ u) = infer t #> infer u
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   442
      | infer _ = I;
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   443
    val new_arities = filter_out
45979
296d9a9c8d24 treatment of type constructor `set`
haftmann
parents: 45384
diff changeset
   444
      (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
   445
        | _ => false) (fold (snd #> infer) intros []);
33278
ba9f52f56356 conceal internal bindings;
wenzelm
parents: 33049
diff changeset
   446
    val params' = map (fn x =>
ba9f52f56356 conceal internal bindings;
wenzelm
parents: 33049
diff changeset
   447
      (case AList.lookup op = new_arities x of
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   448
        SOME fs =>
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   449
          let
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   450
            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
   451
            val Ts = HOLogic.strip_ptupleT fs T;
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   452
            val x' = map_type (K (Ts ---> HOLogic.boolT)) x
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   453
          in
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   454
            (x, (x',
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   455
              (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
   456
                 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
   457
               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
   458
                 (HOLogic.mk_mem
426ed18eba43 discontinued old-style Term.list_abs in favour of plain Term.abs;
wenzelm
parents: 45979
diff changeset
   459
                   (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
   460
          end
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   461
       | NONE => (x, (x, (x, x))))) params;
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   462
    val (params1, (params2, params3)) =
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   463
      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
   464
    val paramTs = map fastype_of params;
23764
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   465
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   466
    (* equations for converting sets to predicates *)
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   467
    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
   468
      let
15f81c5d5330 New wrapper for defining inductive sets with new inductive
berghofe
parents:
diff changeset
   469
        val fs = the_default [] (AList.lookup op = new_arities c);
45979
296d9a9c8d24 treatment of type constructor `set`
haftmann
parents: 45384
diff changeset
   470
        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
   471
        val _ = Us = paramTs orelse error (Pretty.string_of (Pretty.chunks