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