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