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