SETS.TEX 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. \documentstyle[11pt]{article}
  2. \title{SETS: A Basic Set Theory Package}
  3. \author{Francis J. Wright \\
  4. School of Mathematical Sciences \\
  5. Queen Mary and Westfield College \\
  6. University of London \\
  7. Mile End Road, London E1 4NS, UK. \\
  8. Email: {\tt F.J.Wright@QMW.ac.uk}}
  9. \begin{document}
  10. \maketitle
  11. \begin{abstract}
  12. The SETS package for \REDUCE 3.5 and later versions provides
  13. algebraic-mode support for set operations on lists regarded as sets
  14. (or representing explicit sets) and on implicit sets represented by
  15. identifiers. It provides the set-valued infix operators (with
  16. synonyms) {\tt union}, {\tt intersection} ({\tt intersect}) and {\tt
  17. setdiff} (\verb|\|, {\tt minus}) and the Boolean-valued infix
  18. operators (predicates) {\tt member}, {\tt subset\_eq}, {\tt subset},
  19. {\tt set\_eq}. The union and intersection operators are n-ary and
  20. the rest are binary. A list can be explicitly converted to the
  21. canonical set representation by applying the operator {\tt mkset}.
  22. (The package also provides an operator not specifically related to
  23. set theory called {\tt evalb} that allows the value of any
  24. Boolean-valued expression to be displayed in algebraic mode.)
  25. \end{abstract}
  26. \section{Introduction}
  27. REDUCE has no specific representation for a set, neither in algebraic
  28. mode nor internally, and any object that is mathematically a set is
  29. represented in REDUCE as a list. The difference between a set and a
  30. list is that in a set the ordering of elements is not significant and
  31. duplicate elements are not allowed (or are ignored). Hence a list
  32. provides a perfectly natural and satisfactory representation for a set
  33. (but not vice versa). Some languages, such as Maple, provide
  34. different internal representations for sets and lists, which may allow
  35. sets to be processed more efficiently, but this is not {\em
  36. necessary}.
  37. This package supports set theoretic operations on lists and represents
  38. the results as normal algebraic-mode lists, so that all other REDUCE
  39. facilities that apply to lists can still be applied to lists that have
  40. been constructed by explicit set operations. The algebraic-mode set
  41. operations provided by this package have all been available in
  42. symbolic mode for a long time, and indeed are used internally by the
  43. rest of REDUCE, so in that sense set theory facilities in REDUCE are
  44. far from new. What this package does is make them available in
  45. algebraic mode, generalize their operation by extending the arity of
  46. union and intersection, and allow their arguments to be implicit sets
  47. represented by unbound identifiers. It performs some simplifications
  48. on such symbolic set-valued expressions, but this is currently rather
  49. {\it ad hoc\/} and is probably incomplete.
  50. For examples of the operation of the SETS package see (or run) the
  51. test file {\tt sets.tst}. This package is experimental and
  52. developments are under consideration; if you have suggestions for
  53. improvements (or corrections) then please send them to me (FJW),
  54. preferably by email. The package is intended to be run under
  55. \REDUCE 3.5 and later versions; it may well run correctly under earlier
  56. versions although I cannot provide support for such use.
  57. \section{Infix operator precedence}
  58. The set operators are currently inserted into the standard REDUCE
  59. precedence list (see page 28, \S2.7, of the REDUCE 3.6 manual) as
  60. follows:
  61. \begin{verbatim}
  62. or and not member memq = set_eq neq eq >= > <= < subset_eq
  63. subset freeof + - setdiff union intersection * / ^ .
  64. \end{verbatim}
  65. \section{Explicit set representation and {\tt mkset}}
  66. Explicit sets are represented by lists, and this package does not
  67. require any restrictions at all on the forms of lists that are
  68. regarded as sets. Nevertheless, duplicate elements in a set
  69. correspond by definition to the same element and it is conventional
  70. and convenient to represent them by a single element, i.e.\ to remove
  71. any duplicate elements. I will call this a normal representation.
  72. Since the order of elements in a set is irrelevant it is also
  73. conventional and may be convenient to sort them into some standard
  74. order, and an appropriate ordering of a normal representation gives a
  75. canonical representation. This means that two identical sets have
  76. identical representations, and therefore the standard REDUCE equality
  77. predicate ({\tt =}) correctly determines set equality; without a
  78. canonical representation this is not the case.
  79. Pre-processing of explicit set-valued arguments of the set-valued
  80. operators to remove duplicates is always done because of the obvious
  81. efficiency advantage if there were any duplicates, and hence explicit
  82. sets appearing in the values of such operators will never contain any
  83. duplicate elements. Such sets are also currently sorted, mainly
  84. because the result looks better. The ordering used satisfies the {\tt
  85. ordp} predicate used for most sorting within REDUCE, except that
  86. explicit integers are sorted into increasing numerical order rather
  87. than the decreasing order that satisfies {\tt ordp}.
  88. Hence explicit sets appearing in the result of any set operator are
  89. currently returned in a canonical form. Any explicit set can also be
  90. put into this form by applying the operator {\tt mkset} to the list
  91. representing it. For example
  92. \begin{verbatim}
  93. mkset {1,2,y,x*y,x+y};
  94. {x + y,x*y,y,1,2}
  95. \end{verbatim}
  96. The empty set is represented by the empty list \verb|{}|.
  97. \section{Union and intersection}
  98. The operator {\tt intersection} (the name used internally) has the
  99. shorter synonym {\tt intersect}. These operators will probably most
  100. commonly be used as binary infix operators applied to explicit sets,
  101. e.g.
  102. \begin{verbatim}
  103. {1,2,3} union {2,3,4};
  104. {1,2,3,4}
  105. {1,2,3} intersect {2,3,4};
  106. {2,3}
  107. \end{verbatim}
  108. They can also be used as n-ary operators with any number of arguments,
  109. in which case it saves typing to use them as prefix operators (which
  110. is possible with all REDUCE infix operators), e.g.
  111. \begin{verbatim}
  112. {1,2,3} union {2,3,4} union {3,4,5};
  113. {1,2,3,4,5}
  114. intersect({1,2,3}, {2,3,4}, {3,4,5});
  115. {3}
  116. \end{verbatim}
  117. For completeness, they can currently also be used as unary operators,
  118. in which case they just return their arguments (in canonical form),
  119. and so act as slightly less efficient versions of {\tt mkset} (but
  120. this may change), e.g.
  121. \begin{verbatim}
  122. union {1,5,3,5,1};
  123. {1,3,5}
  124. \end{verbatim}
  125. \section{Symbolic set expressions}
  126. If one or more of the arguments evaluates to an unbound identifier
  127. then it is regarded as representing a symbolic implicit set, and the
  128. union or intersection will evaluate to an expression that still
  129. contains the union or intersection operator. These two operators are
  130. symmetric, and so if they remain symbolic their arguments will be
  131. sorted as for any symmetric operator. Such symbolic set expressions
  132. are simplified, but the simplification may not be complete in
  133. non-trivial cases. For example:
  134. \begin{verbatim}
  135. a union b union {} union b union {7,3};
  136. {3,7} union a union b
  137. a intersect {};
  138. {}
  139. \end{verbatim}
  140. In implementations of REDUCE that provide fancy display using
  141. mathematical notation, such as PSL-REDUCE~3.6 for MS-Windows, the
  142. empty set, union, intersection and set difference are all displayed
  143. using their conventional mathematical symbols, namely $\emptyset$,
  144. $\cup$, $\cap$, $\setminus$.
  145. A symbolic set expression is a valid argument for any other set
  146. operator, e.g.
  147. \begin{verbatim}
  148. a union (b intersect c);
  149. b intersection c union a
  150. \end{verbatim}
  151. Intersection distributes over union, which is not applied by default
  152. but is implemented as a rule list assigned to the variable {\tt
  153. set\_distribution\_rule}, e.g.
  154. \begin{verbatim}
  155. a intersect (b union c);
  156. (b union c) intersection a
  157. a intersect (b union c) where set_distribution_rule;
  158. a intersection b union a intersection c
  159. \end{verbatim}
  160. \section{Set difference}
  161. The set difference operator is represented by the symbol \verb|\| and
  162. is always output using this symbol, although it can also be input using
  163. either of the two names {\tt setdiff} (the name used internally) or
  164. {\tt minus} (as used in Maple). It is a binary operator, its operands
  165. may be any combination of explicit or implicit sets, and it may be
  166. used in an argument of any other set operator. Here are some
  167. examples:
  168. \begin{verbatim}
  169. {1,2,3} \ {2,4};
  170. {1,3}
  171. {1,2,3} \ {};
  172. {1,2,3}
  173. a \ {1,2};
  174. a\{1,2}
  175. a \ a;
  176. {}
  177. a \ {};
  178. a
  179. {} \ a;
  180. {}
  181. \end{verbatim}
  182. \section{Predicates on sets}
  183. These are all binary infix operators. Currently, like all REDUCE
  184. predicates, they can only be used within conditional statements ({\tt
  185. if}, {\tt while}, {\tt repeat}) or within the argument of the {\tt
  186. evalb} operator provided by this package, and they cannot remain
  187. symbolic -- a predicate that cannot be evaluated to a Boolean value
  188. causes a normal REDUCE error.
  189. The {\tt evalb} operator provides a convenient shorthand for an {\tt
  190. if} statement designed purely to display the value of any Boolean
  191. expression (not only predicates defined in this package). It has some
  192. similarity with the {\tt evalb} function in Maple, except that the
  193. values returned by {\tt evalb} in REDUCE (the identifiers {\tt true}
  194. and {\tt false}) have no significance to REDUCE itself. Hence, in
  195. REDUCE, use of {\tt evalb} is {\em never\/} necessary.
  196. \begin{verbatim}
  197. if a = a then true else false;
  198. true
  199. evalb(a = a);
  200. true
  201. if a = b then true else false;
  202. false
  203. evalb(a = b);
  204. false
  205. evalb 1;
  206. true
  207. evalb 0;
  208. false
  209. \end{verbatim}
  210. I will use the {\tt evalb} operator in preference to an explicit {\tt
  211. if} statement for purposes of illustration.
  212. \subsection{Set membership}
  213. Set membership is tested by the predicate {\tt member}. Its left
  214. operand is regarded as a potential set element and its right operand
  215. {\em must\/} evaluate to an explicit set. There is currently no sense
  216. in which the right operand could be an implicit set; this would
  217. require a mechanism for declaring implicit set membership (akin to
  218. implicit variable dependence) which is currently not implemented. Set
  219. membership testing works like this:
  220. \begin{verbatim}
  221. evalb(1 member {1,2,3});
  222. true
  223. evalb(2 member {1,2} intersect {2,3});
  224. true
  225. evalb(a member b);
  226. ***** b invalid as list
  227. \end{verbatim}
  228. \subsection{Set inclusion}
  229. Set inclusion is tested by the predicate {\tt subset\_eq} where {\tt a
  230. subset\_eq b} is true if the set $a$ is either a subset of or equal to
  231. the set $b$; strict inclusion is tested by the predicate {\tt subset}
  232. where {\tt a subset b} is true if the set $a$ is {\em strictly\/} a
  233. subset of the set $b$ and is false is $a$ is equal to $b$. These
  234. predicates provide some support for symbolic set expressions, but this
  235. is not yet correct as indicated below. Here are some examples:
  236. \begin{verbatim}
  237. evalb({1,2} subset_eq {1,2,3});
  238. true
  239. evalb({1,2} subset_eq {1,2});
  240. true
  241. evalb({1,2} subset {1,2});
  242. false
  243. evalb(a subset a union b);
  244. true
  245. evalb(a\b subset a);
  246. true
  247. evalb(a intersect b subset a union b); %%% BUG
  248. false
  249. \end{verbatim}
  250. An undecidable predicate causes a normal REDUCE error, e.g.
  251. \begin{verbatim}
  252. evalb(a subset_eq {b});
  253. ***** Cannot evaluate a subset_eq {b} as Boolean-valued set
  254. expression
  255. evalb(a subset_eq b); %%% BUG
  256. false
  257. \end{verbatim}
  258. \subsection{Set equality}
  259. As explained above, equality of two sets in canonical form can be
  260. reliably tested by the standard REDUCE equality predicate ({\tt =}).
  261. This package also provides the predicate {\tt set\_eq} to test
  262. equality of two sets not represented canonically. The two predicates
  263. behave identically for operands that are symbolic set expressions
  264. because these are always evaluated to canonical form (although
  265. currently this is probably strictly true only in simple cases). Here
  266. are some examples:
  267. \begin{verbatim}
  268. evalb({1,2,3} = {1,2,3});
  269. true
  270. evalb({2,1,3} = {1,3,2});
  271. false
  272. evalb(mkset{2,1,3} = mkset{1,3,2});
  273. true
  274. evalb({2,1,3} set_eq {1,3,2});
  275. true
  276. evalb(a union a = a\{});
  277. true
  278. \end{verbatim}
  279. \section{Installation}
  280. The source file {\tt sets.red} can be read into REDUCE when required
  281. using {\tt IN}. If the ``professional'' version is being used this
  282. should be done with {\tt ON COMP} set, but it is much better to
  283. compile the code as a {\tt FASL} file using {\tt FASLOUT} and then
  284. load it with {\tt LOAD\_PACKAGE} (or {\tt LOAD}). See the REDUCE
  285. manual and implementation-specific guide for further details.
  286. This package has to redefine the REDUCE internal procedure {\tt
  287. mk!*sq} and a warning about this can be expected and ignored. I
  288. believe (and hope!) that this redefinition is safe and will not have
  289. any unexpected consequences for the rest of REDUCE.
  290. \section{Possible future developments}
  291. \begin{itemize}
  292. \item Unary union/intersection to implement repeated
  293. union/intersection on a set of sets.
  294. \item More symbolic set algebra, canonical forms for set expressions,
  295. more complete simplification.
  296. \item Better support for Boolean variables via a version (evalb10?)
  297. of {\tt evalb} that returns 1/0 instead of {\tt true}/{\tt false},
  298. or predicates that return 1/0 directly.
  299. \end{itemize}
  300. \end{document}