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