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