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