src/HOL/Set.thy
author wenzelm
Tue Aug 04 09:22:07 1998 +0200 (1998-08-04)
changeset 5236 0cec0b591d4c
parent 5144 7ac22e5a05d7
child 5254 a275d0a3dc08
permissions -rw-r--r--
fixed disjount translation;
clasohm@923
     1
(*  Title:      HOL/Set.thy
clasohm@923
     2
    ID:         $Id$
clasohm@923
     3
    Author:     Tobias Nipkow
clasohm@923
     4
    Copyright   1993  University of Cambridge
clasohm@923
     5
*)
clasohm@923
     6
clasohm@923
     7
Set = Ord +
clasohm@923
     8
wenzelm@2261
     9
wenzelm@2261
    10
(** Core syntax **)
wenzelm@2261
    11
wenzelm@3947
    12
global
wenzelm@3947
    13
clasohm@923
    14
types
clasohm@923
    15
  'a set
clasohm@923
    16
clasohm@923
    17
arities
clasohm@923
    18
  set :: (term) term
clasohm@923
    19
clasohm@923
    20
instance
oheimb@4761
    21
  set :: (term) {ord, minus, power} (* only ('a * 'a) set should be in power! *)
clasohm@923
    22
wenzelm@3820
    23
syntax
wenzelm@3820
    24
  "op :"        :: ['a, 'a set] => bool             ("op :")
wenzelm@3820
    25
clasohm@923
    26
consts
clasohm@1370
    27
  "{}"          :: 'a set                           ("{}")
paulson@4159
    28
  UNIV          :: 'a set
clasohm@1370
    29
  insert        :: ['a, 'a set] => 'a set
clasohm@1370
    30
  Collect       :: ('a => bool) => 'a set               (*comprehension*)
clasohm@1370
    31
  Compl         :: ('a set) => 'a set                   (*complement*)
clasohm@1370
    32
  Int           :: ['a set, 'a set] => 'a set       (infixl 70)
clasohm@1370
    33
  Un            :: ['a set, 'a set] => 'a set       (infixl 65)
clasohm@1370
    34
  UNION, INTER  :: ['a set, 'a => 'b set] => 'b set     (*general*)
wenzelm@2261
    35
  Union, Inter  :: (('a set) set) => 'a set             (*of a set*)
clasohm@1370
    36
  Pow           :: 'a set => 'a set set                 (*powerset*)
clasohm@1370
    37
  range         :: ('a => 'b) => 'b set                 (*of function*)
clasohm@1370
    38
  Ball, Bex     :: ['a set, 'a => bool] => bool         (*bounded quantifiers*)
paulson@1962
    39
  "``"          :: ['a => 'b, 'a set] => ('b set)   (infixr 90)
wenzelm@2261
    40
  (*membership*)
wenzelm@2261
    41
  "op :"        :: ['a, 'a set] => bool             ("(_/ : _)" [50, 51] 50)
clasohm@923
    42
clasohm@923
    43
wenzelm@2261
    44
wenzelm@2261
    45
(** Additional concrete syntax **)
wenzelm@2261
    46
clasohm@923
    47
syntax
clasohm@923
    48
wenzelm@2261
    49
  (* Infix syntax for non-membership *)
clasohm@923
    50
wenzelm@3820
    51
  "op ~:"       :: ['a, 'a set] => bool               ("op ~:")
wenzelm@2261
    52
  "op ~:"       :: ['a, 'a set] => bool               ("(_/ ~: _)" [50, 51] 50)
clasohm@923
    53
wenzelm@2261
    54
  "@Finset"     :: args => 'a set                     ("{(_)}")
wenzelm@2261
    55
wenzelm@2261
    56
  "@Coll"       :: [pttrn, bool] => 'a set            ("(1{_./ _})")
wenzelm@2261
    57
  "@SetCompr"   :: ['a, idts, bool] => 'a set         ("(1{_ |/_./ _})")
clasohm@923
    58
clasohm@923
    59
  (* Big Intersection / Union *)
clasohm@923
    60
paulson@4159
    61
  INTER1        :: [pttrns, 'a => 'b set] => 'b set   ("(3INT _./ _)" 10)
paulson@4159
    62
  UNION1        :: [pttrns, 'a => 'b set] => 'b set   ("(3UN _./ _)" 10)
paulson@4159
    63
clasohm@1370
    64
  "@INTER"      :: [pttrn, 'a set, 'b set] => 'b set  ("(3INT _:_./ _)" 10)
clasohm@1370
    65
  "@UNION"      :: [pttrn, 'a set, 'b set] => 'b set  ("(3UN _:_./ _)" 10)
clasohm@923
    66
clasohm@923
    67
  (* Bounded Quantifiers *)
clasohm@923
    68
wenzelm@2368
    69
  "@Ball"       :: [pttrn, 'a set, bool] => bool      ("(3! _:_./ _)" [0, 0, 10] 10)
wenzelm@2368
    70
  "@Bex"        :: [pttrn, 'a set, bool] => bool      ("(3? _:_./ _)" [0, 0, 10] 10)
wenzelm@2368
    71
  "*Ball"       :: [pttrn, 'a set, bool] => bool      ("(3ALL _:_./ _)" [0, 0, 10] 10)
wenzelm@2368
    72
  "*Bex"        :: [pttrn, 'a set, bool] => bool      ("(3EX _:_./ _)" [0, 0, 10] 10)
clasohm@923
    73
nipkow@5144
    74
  disjoint      :: 'a set => 'a set => bool
nipkow@5144
    75
clasohm@923
    76
translations
wenzelm@2261
    77
  "range f"     == "f``UNIV"
clasohm@923
    78
  "x ~: y"      == "~ (x : y)"
clasohm@923
    79
  "{x, xs}"     == "insert x {xs}"
clasohm@923
    80
  "{x}"         == "insert x {}"
clasohm@923
    81
  "{x. P}"      == "Collect (%x. P)"
paulson@4159
    82
  "UN x y. B"   == "UN x. UN y. B"
paulson@4159
    83
  "UN x. B"     == "UNION UNIV (%x. B)"
paulson@4159
    84
  "INT x y. B"   == "INT x. INT y. B"
paulson@4159
    85
  "INT x. B"    == "INTER UNIV (%x. B)"
paulson@4159
    86
  "UN x:A. B"   == "UNION A (%x. B)"
clasohm@923
    87
  "INT x:A. B"  == "INTER A (%x. B)"
clasohm@923
    88
  "! x:A. P"    == "Ball A (%x. P)"
clasohm@923
    89
  "? x:A. P"    == "Bex A (%x. P)"
clasohm@923
    90
  "ALL x:A. P"  => "Ball A (%x. P)"
clasohm@923
    91
  "EX x:A. P"   => "Bex A (%x. P)"
clasohm@923
    92
wenzelm@2388
    93
syntax ("" output)
wenzelm@3820
    94
  "_setle"      :: ['a set, 'a set] => bool           ("op <=")
wenzelm@2388
    95
  "_setle"      :: ['a set, 'a set] => bool           ("(_/ <= _)" [50, 51] 50)
wenzelm@3820
    96
  "_setless"    :: ['a set, 'a set] => bool           ("op <")
wenzelm@2684
    97
  "_setless"    :: ['a set, 'a set] => bool           ("(_/ < _)" [50, 51] 50)
clasohm@923
    98
wenzelm@2261
    99
syntax (symbols)
wenzelm@3820
   100
  "_setle"      :: ['a set, 'a set] => bool           ("op \\<subseteq>")
wenzelm@2388
   101
  "_setle"      :: ['a set, 'a set] => bool           ("(_/ \\<subseteq> _)" [50, 51] 50)
wenzelm@3820
   102
  "_setless"    :: ['a set, 'a set] => bool           ("op \\<subset>")
wenzelm@2684
   103
  "_setless"    :: ['a set, 'a set] => bool           ("(_/ \\<subset> _)" [50, 51] 50)
wenzelm@2261
   104
  "op Int"      :: ['a set, 'a set] => 'a set         (infixl "\\<inter>" 70)
wenzelm@2261
   105
  "op Un"       :: ['a set, 'a set] => 'a set         (infixl "\\<union>" 65)
wenzelm@3820
   106
  "op :"        :: ['a, 'a set] => bool               ("op \\<in>")
wenzelm@2261
   107
  "op :"        :: ['a, 'a set] => bool               ("(_/ \\<in> _)" [50, 51] 50)
wenzelm@3820
   108
  "op ~:"       :: ['a, 'a set] => bool               ("op \\<notin>")
wenzelm@2261
   109
  "op ~:"       :: ['a, 'a set] => bool               ("(_/ \\<notin> _)" [50, 51] 50)
wenzelm@2261
   110
  "UN "         :: [idts, bool] => bool               ("(3\\<Union> _./ _)" 10)
wenzelm@2261
   111
  "INT "        :: [idts, bool] => bool               ("(3\\<Inter> _./ _)" 10)
paulson@4159
   112
  "UNION1"      :: [pttrn, 'b set] => 'b set          ("(3\\<Union> _./ _)" 10)
paulson@4159
   113
  "INTER1"      :: [pttrn, 'b set] => 'b set          ("(3\\<Inter> _./ _)" 10)
wenzelm@2261
   114
  "@UNION"      :: [pttrn, 'a set, 'b set] => 'b set  ("(3\\<Union> _\\<in>_./ _)" 10)
wenzelm@2261
   115
  "@INTER"      :: [pttrn, 'a set, 'b set] => 'b set  ("(3\\<Inter> _\\<in>_./ _)" 10)
wenzelm@2261
   116
  Union         :: (('a set) set) => 'a set           ("\\<Union> _" [90] 90)
wenzelm@2261
   117
  Inter         :: (('a set) set) => 'a set           ("\\<Inter> _" [90] 90)
wenzelm@2368
   118
  "@Ball"       :: [pttrn, 'a set, bool] => bool      ("(3\\<forall> _\\<in>_./ _)" [0, 0, 10] 10)
wenzelm@2368
   119
  "@Bex"        :: [pttrn, 'a set, bool] => bool      ("(3\\<exists> _\\<in>_./ _)" [0, 0, 10] 10)
wenzelm@2372
   120
wenzelm@2372
   121
syntax (symbols output)
wenzelm@2368
   122
  "*Ball"       :: [pttrn, 'a set, bool] => bool      ("(3\\<forall> _\\<in>_./ _)" [0, 0, 10] 10)
wenzelm@2368
   123
  "*Bex"        :: [pttrn, 'a set, bool] => bool      ("(3\\<exists> _\\<in>_./ _)" [0, 0, 10] 10)
wenzelm@2261
   124
wenzelm@2412
   125
translations
wenzelm@2965
   126
  "op \\<subseteq>" => "op <= :: [_ set, _ set] => bool"
wenzelm@2965
   127
  "op \\<subset>" => "op <  :: [_ set, _ set] => bool"
wenzelm@5236
   128
  "disjoint A B" == "_setle A (Compl B)"
wenzelm@2412
   129
wenzelm@2261
   130
wenzelm@2261
   131
wenzelm@2261
   132
(** Rules and definitions **)
wenzelm@2261
   133
wenzelm@3947
   134
local
wenzelm@3947
   135
clasohm@923
   136
rules
clasohm@923
   137
clasohm@923
   138
  (* Isomorphisms between Predicates and Sets *)
clasohm@923
   139
wenzelm@3842
   140
  mem_Collect_eq    "(a : {x. P(x)}) = P(a)"
wenzelm@3842
   141
  Collect_mem_eq    "{x. x:A} = A"
clasohm@923
   142
clasohm@923
   143
clasohm@923
   144
defs
wenzelm@2261
   145
clasohm@923
   146
  Ball_def      "Ball A P       == ! x. x:A --> P(x)"
clasohm@923
   147
  Bex_def       "Bex A P        == ? x. x:A & P(x)"
clasohm@923
   148
  subset_def    "A <= B         == ! x:A. x:B"
nipkow@3222
   149
  psubset_def   "A < B          == (A::'a set) <= B & ~ A=B"
oheimb@2393
   150
  Compl_def     "Compl A        == {x. ~x:A}"
wenzelm@3842
   151
  Un_def        "A Un B         == {x. x:A | x:B}"
wenzelm@3842
   152
  Int_def       "A Int B        == {x. x:A & x:B}"
clasohm@923
   153
  set_diff_def  "A - B          == {x. x:A & ~x:B}"
clasohm@923
   154
  INTER_def     "INTER A B      == {y. ! x:A. y: B(x)}"
clasohm@923
   155
  UNION_def     "UNION A B      == {y. ? x:A. y: B(x)}"
oheimb@2393
   156
  Inter_def     "Inter S        == (INT x:S. x)"
oheimb@2393
   157
  Union_def     "Union S        == (UN x:S. x)"
oheimb@2393
   158
  Pow_def       "Pow A          == {B. B <= A}"
clasohm@923
   159
  empty_def     "{}             == {x. False}"
paulson@4159
   160
  UNIV_def      "UNIV           == {x. True}"
wenzelm@3842
   161
  insert_def    "insert a B     == {x. x=a} Un B"
clasohm@923
   162
  image_def     "f``A           == {y. ? x:A. y=f(x)}"
regensbu@1273
   163
clasohm@923
   164
end
clasohm@923
   165
wenzelm@2261
   166
clasohm@923
   167
ML
clasohm@923
   168
clasohm@923
   169
local
clasohm@923
   170
wenzelm@2388
   171
(* Set inclusion *)
wenzelm@2388
   172
wenzelm@4151
   173
fun le_tr' _ (*op <=*) (Type ("fun", (Type ("set", _) :: _))) ts =
wenzelm@2388
   174
      list_comb (Syntax.const "_setle", ts)
wenzelm@4151
   175
  | le_tr' _ (*op <=*) _ _ = raise Match;
wenzelm@2388
   176
wenzelm@4151
   177
fun less_tr' _ (*op <*) (Type ("fun", (Type ("set", _) :: _))) ts =
wenzelm@2684
   178
      list_comb (Syntax.const "_setless", ts)
wenzelm@4151
   179
  | less_tr' _ (*op <*) _ _ = raise Match;
wenzelm@2684
   180
wenzelm@2388
   181
clasohm@923
   182
(* Translates between { e | x1..xn. P} and {u. ? x1..xn. u=e & P}      *)
clasohm@923
   183
(* {y. ? x1..xn. y = e & P} is only translated if [0..n] subset bvs(e) *)
clasohm@923
   184
clasohm@923
   185
val ex_tr = snd(mk_binder_tr("? ","Ex"));
clasohm@923
   186
clasohm@923
   187
fun nvars(Const("_idts",_) $ _ $ idts) = nvars(idts)+1
clasohm@923
   188
  | nvars(_) = 1;
clasohm@923
   189
clasohm@923
   190
fun setcompr_tr[e,idts,b] =
clasohm@923
   191
  let val eq = Syntax.const("op =") $ Bound(nvars(idts)) $ e
clasohm@923
   192
      val P = Syntax.const("op &") $ eq $ b
clasohm@923
   193
      val exP = ex_tr [idts,P]
clasohm@923
   194
  in Syntax.const("Collect") $ Abs("",dummyT,exP) end;
clasohm@923
   195
clasohm@923
   196
val ex_tr' = snd(mk_binder_tr' ("Ex","DUMMY"));
clasohm@923
   197
clasohm@923
   198
fun setcompr_tr'[Abs(_,_,P)] =
clasohm@923
   199
  let fun ok(Const("Ex",_)$Abs(_,_,P),n) = ok(P,n+1)
clasohm@923
   200
        | ok(Const("op &",_) $ (Const("op =",_) $ Bound(m) $ e) $ _, n) =
clasohm@923
   201
            if n>0 andalso m=n andalso
clasohm@923
   202
              ((0 upto (n-1)) subset add_loose_bnos(e,0,[]))
clasohm@923
   203
            then () else raise Match
clasohm@923
   204
clasohm@923
   205
      fun tr'(_ $ abs) =
clasohm@923
   206
        let val _ $ idts $ (_ $ (_ $ _ $ e) $ Q) = ex_tr'[abs]
clasohm@923
   207
        in Syntax.const("@SetCompr") $ e $ idts $ Q end
clasohm@923
   208
  in ok(P,0); tr'(P) end;
clasohm@923
   209
clasohm@923
   210
in
clasohm@923
   211
clasohm@923
   212
val parse_translation = [("@SetCompr", setcompr_tr)];
clasohm@923
   213
val print_translation = [("Collect", setcompr_tr')];
wenzelm@2684
   214
val typed_print_translation = [("op <=", le_tr'), ("op <", less_tr')];
clasohm@923
   215
val print_ast_translation =
clasohm@923
   216
  map HOL.alt_ast_tr' [("@Ball", "*Ball"), ("@Bex", "*Bex")];
clasohm@923
   217
clasohm@923
   218
end;