1 2% Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd. 3% All rights reserved. 4% 5% Redistribution and use in source and binary forms, with or without 6% modification, are permitted provided that the following conditions are 7% met: 8% 9% - Redistributions of source code must retain the above copyright 10% notice, this list of conditions and the following disclaimer. 11% 12% - Redistributions in binary form must reproduce the above copyright 13% notice, this list of conditions and the following disclaimer in 14% the documentation and/or other materials provided with the 15% distribution. 16% 17% - Neither the name of The Numerical ALgorithms Group Ltd. nor the 18% names of its contributors may be used to endorse or promote products 19% derived from this software without specific prior written permission. 20% 21% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 22% IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23% TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 24% PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 25% OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 26% EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 27% PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES-- LOSS OF USE, DATA, OR 28% PROFITS-- OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 29% LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30% NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 31% SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 33% ********************************************************************* 34\head{chapter}{Using Types and Modes}{ugTypes} 35% ********************************************************************* 36 37In this chapter we look at the key notion of \spadgloss{type} and its 38generalization \spadgloss{mode}. 39We show that every \Language{} object has a type that 40determines what you can do with the object. 41In particular, we explain how to use types to call specific functions 42from particular parts of the library and how types and modes can be used 43to create new objects from old. 44We also look at \pspadtype{Record} and \pspadtype{Union} types and 45the special type \spadtype{Any}. 46Finally, we give you an idea of how \Language{} manipulates types and 47modes internally to resolve ambiguities. 48 49% ********************************************************************* 50\head{section}{The Basic Idea}{ugTypesBasic} 51% ********************************************************************* 52 53The \Language{} world deals with many kinds of objects. 54There are mathematical objects such as numbers and polynomials, 55data structure objects such as lists and arrays, and graphics 56objects such as points and graphic images. 57Functions are objects too. 58 59\Language{} organizes objects using the notion of \spadglossSee{domain of 60computation}{domain}, or simply \spadgloss{domain}. 61Each domain denotes a class of objects. 62The class of objects it denotes is usually given by the name of the 63domain: \spadtype{Integer} for the integers, \spadtype{Float} for 64floating-point numbers, and so on. 65The convention is that the first letter of a domain name is capitalized. 66Similarly, the domain \spadtype{Polynomial(Integer)} denotes ``polynomials 67with integer coefficients.'' 68Also, \spadtype{Matrix(Float)} denotes ``matrices with floating-point 69entries.'' 70 71Every basic \Language{} object belongs to a unique domain. 72The integer \spad{3} belongs to the domain \spadtype{Integer} and 73the polynomial \spad{x + 3} belongs to the domain 74\spadtype{Polynomial(Integer)}. 75The domain of an object is also called its \spadgloss{type}. 76Thus we speak of ``the type \spadtype{Integer}'' 77and ``the type \spadtype{Polynomial(Integer)}.'' 78 79\xtc{ 80After an \Language{} computation, the type is displayed toward the 81right-hand side of the page (or screen). 82}{ 83\spadcommand{-3} 84} 85\xtc{ 86Here we create a rational number but it looks like the last result. 87The type however tells you it is different. 88You cannot identify the type of an object by how \Language{} 89displays the object. 90}{ 91\spadcommand{-3/1} 92} 93\xtc{ 94When a computation produces a result of a simpler type, \Language{} leaves 95the type unsimplified. 96Thus no information is lost. 97}{ 98\spadcommand{x + 3 - x \bound{three}} 99} 100\xtc{ 101This seldom matters since \Language{} retracts the answer to the 102simpler type if it is necessary. 103}{ 104\spadcommand{factorial(\%) \free{three}} 105} 106\xtc{ 107When you issue a positive number, the type \spadtype{PositiveInteger} is 108printed. 109Surely, \spad{3} also has type \spadtype{Integer}! 110The curious reader may now have two questions. 111First, is the type of an object not unique? 112Second, how is \spadtype{PositiveInteger} related to \spadtype{Integer}? 113Read on! 114}{ 115\spadcommand{3} 116} 117 118Any domain can be refined to a \spadgloss{subdomain} by a membership 119\spadgloss{predicate}.\footnote{A predicate is a function that, 120when applied to an object of the domain, returns either 121\spad{true} or \spad{false}.} 122For example, the domain \spadtype{Integer} can be refined to the 123subdomain \spadtype{PositiveInteger}, the set of integers 124\spad{x} such that \spad{x > 0}, by giving the \Language{} 125predicate \spad{x +-> x > 0}. 126Similarly, \Language{} can define subdomains such as ``the 127subdomain of diagonal matrices,'' ``the subdomain of lists of length 128two,'' ``the subdomain of monic irreducible polynomials in 129\spad{x},'' and so on. 130Trivially, any domain is a subdomain of itself. 131 132While an object belongs to a unique domain, it can belong to any 133number of subdomains. 134Any subdomain of the domain of an object can be used as the 135{\it type} of that object. 136The type of \spad{3} is indeed both \spadtype{Integer} and 137\spadtype{PositiveInteger} as well as any other subdomain of 138integer whose predicate is satisfied, such as ``the prime 139integers,'' ``the odd positive integers between 3 and 17,'' and so 140on. 141 142% ********************************************************************* 143\head{subsection}{Domain Constructors}{ugTypesBasicDomainCons} 144% ********************************************************************* 145 146In \Language{}, domains are objects. 147You can create them, pass them to functions, and, as we'll see later, test 148them for certain properties. 149 150In \Language{}, you ask for a value of a function by applying its name 151to a set of arguments. 152 153\xtc{ 154To ask for ``the factorial of 7'' you enter this expression to 155\Language{}. 156This applies the function \spad{factorial} to the value \spad{7} 157to compute the result. 158}{ 159\spadcommand{factorial(7)} 160} 161\xtc{ 162Enter the type \spadtype{Polynomial (Integer)} as an expression to 163\Language{}. 164This looks much like a function call as well. 165It is! 166The result is stated to be of type 167\spadtype{Type}, which 168according to our usual convention, denotes the class of all domains. 169}{ 170\spadcommand{Polynomial(Integer)} 171} 172 173The most basic operation involving domains is that of building a new 174domain from a given one. 175To create the domain of ``polynomials over the integers,'' \Language{} 176applies the function \spadtype{Polynomial} to the domain 177\spadtype{Integer}. 178A function like \spadtype{Polynomial} is called a \spadgloss{domain 179constructor} or, 180\index{constructor!domain} 181more simply, a 182\spadgloss{constructor}. 183A domain constructor is a function that creates a domain. 184An argument to a domain constructor can be another domain or, in general, 185an arbitrary kind of object. 186\spadtype{Polynomial} takes a single domain argument while 187\spadtype{SquareMatrix} takes a positive integer as an argument 188to give its dimension and 189a domain argument to give the type of its components. 190 191What kinds of domains can you use as the argument to 192\spadtype{Polynomial} or \spadtype{SquareMatrix} or 193\spadtype{List}? 194Well, the first two are mathematical in nature. 195You want to be able to perform algebraic operations like 196\spadop{+} and \spadop{*} on polynomials and square matrices, 197and operations such as \spadfun{determinant} on square matrices. 198So you want to allow polynomials of integers {\it and} polynomials 199of square matrices with complex number coefficients and, in 200general, anything that ``makes sense.'' At the same time, you 201don't want \Language{} to be able to build nonsense domains such 202as ``polynomials of strings!'' 203 204In contrast to algebraic structures, data structures can hold any 205kind of object. 206Operations on lists such as \spadfunFrom{insert}{List}, 207\spadfunFrom{delete}{List}, and \spadfunFrom{concat}{List} just 208manipulate the list itself without changing or operating on its 209elements. 210Thus you can build \spadtype{List} over almost any datatype, 211including itself. 212\xtc{ 213Create a complicated algebraic domain. 214}{ 215\spadcommand{List (List (Matrix (Polynomial (Complex (Fraction (Integer))))))} 216} 217\begin{inputonly} 218)set message test off 219\end{inputonly} 220\xtc{ 221Try to create a meaningless domain. 222}{ 223\spadcommand{Polynomial(String)} 224} 225\begin{inputonly} 226)set message test on 227\end{inputonly} 228Evidently from our last example, \Language{} has some mechanism 229that tells what a constructor can use as an argument. 230This brings us to the notion of \spadgloss{category}. 231As domains are objects, they too have a domain. 232The domain of a domain is a category. 233A category is simply a type whose members are domains. 234 235A common algebraic category is \spadtype{Ring}, the class of all domains 236that are ``rings.'' 237A ring is an algebraic structure with constants \spad{0} and \spad{1} and 238operations \spadopFrom{+}{Ring}, \spadopFrom{-}{Ring}, and 239\spadopFrom{*}{Ring}. 240These operations are assumed ``closed'' with respect to the domain, 241meaning that they take two objects of the domain and produce a result 242object also in the domain. 243The operations are understood to satisfy certain ``axioms,'' certain 244mathematical principles providing the algebraic foundation for rings. 245For example, the {\it additive inverse axiom} for rings states: 246\begin{center} 247Every element \spad{x} has an additive inverse \spad{y} such 248that \spad{x + y = 0}. 249\end{center} 250The prototypical example of a domain that is a ring is the integers. 251Keep them in mind whenever we mention \spadtype{Ring}. 252 253Many algebraic domain constructors such as \spadtype{Complex}, 254\spadtype{Polynomial}, \spadtype{Fraction}, take rings as 255arguments and return rings as values. 256You can use the infix operator ``\spad{has}'' 257\spadkey{has} 258to ask a domain if it belongs to a particular category. 259 260\xtc{ 261All numerical types are rings. 262Domain constructor \spadtype{Polynomial} builds ``the ring of polynomials 263over any other ring.'' 264}{ 265\spadcommand{Polynomial(Integer) has Ring} 266} 267\xtc{ 268Constructor \spadtype{List} never produces a ring. 269}{ 270\spadcommand{List(Integer) has Ring} 271} 272\xtc{ 273The constructor \spadtype{Matrix(R)} builds ``the domain of all matrices 274over the ring \spad{R}.'' This domain is never a ring since the operations 275\spadSyntax{+}, \spadSyntax{-}, and \spadSyntax{*} on matrices of arbitrary 276shapes are undefined. 277}{ 278\spadcommand{Matrix(Integer) has Ring} 279} 280\begin{inputonly} 281)set message test off 282\end{inputonly} 283\xtc{ 284Thus you can never build polynomials over matrices. 285}{ 286\spadcommand{Polynomial(Matrix(Integer))} 287} 288\begin{inputonly} 289)set message test on 290\end{inputonly} 291\xtc{ 292Use \spadtype{SquareMatrix(n,R)} instead. 293For any positive integer \spad{n}, it builds ``the ring of \spad{n} by 294\spad{n} matrices over \spad{R}.'' 295}{ 296\spadcommand{Polynomial(SquareMatrix(7,Complex(Integer)))} 297} 298 299Another common category is \spadtype{Field}, the class of all fields. 300\index{field} 301A field is a ring with additional operations. 302For example, a field has commutative multiplication and 303a closed operation \spadopFrom{/}{Field} for the 304division of two elements. 305\spadtype{Integer} is not a field since, for example, \spad{3/2} does not 306have an integer result. 307The prototypical example of a field is the rational numbers, that is, the 308domain \spadtype{Fraction(Integer)}. 309In general, the constructor \spadtype{Fraction} takes a ring as an 310argument and returns a field.\footnote{Actually, 311the argument domain must have some additional 312properties so as to belong to category \spadtype{IntegralDomain}.} 313Other domain constructors, such as \spadtype{Complex}, build fields only if 314their argument domain is a field. 315 316\xtc{ 317The complex integers (often called the ``Gaussian integers'') do not form 318a field. 319}{ 320\spadcommand{Complex(Integer) has Field} 321} 322\xtc{ 323But fractions of complex integers do. 324}{ 325\spadcommand{Fraction(Complex(Integer)) has Field} 326} 327\xtc{ 328The algebraically equivalent domain of complex rational numbers is a field 329since domain constructor \spadtype{Complex} produces a field whenever its 330argument is a field. 331}{ 332\spadcommand{Complex(Fraction(Integer)) has Field} 333} 334 335The most basic category is \spadtype{Type}. 336\exptypeindex{Type} 337It denotes the class of all domains and 338subdomains.\footnote{\spadtype{Type} does not denote the class of 339all types. 340The type of all categories is \spadtype{Category}. 341The type of \spadtype{Type} itself is undefined.} 342Domain constructor \spadtype{List} is able to build ``lists of 343elements from domain \spad{D}'' for arbitrary \spad{D} simply by 344requiring that \spad{D} belong to category \spadtype{Type}. 345 346Now, you may ask, what exactly is a category? 347\index{category} 348Like domains, categories can be defined in the \Language{} language. 349A category is defined by three components: 350% 351\begin{enumerate} 352\item a name (for example, \spadtype{Ring}), 353used to refer to the class of domains that the category represents; 354\item a set of operations, used to refer to the operations that 355the domains of this class support 356(for example, \spadop{+}, \spadop{-}, and \spadop{*} for rings); and 357\item an optional list of other categories that this category extends. 358\end{enumerate} 359% 360This last component is a new idea. 361And it is key to the design of \Language{}! 362Because categories can extend one another, they form hierarchies. 363\begin{texonly} 364Detailed charts showing the category hierarchies in \Language{} are 365displayed in the endpages of this book. 366There you see that all categories are extensions of \spadtype{Type} and that 367\spadtype{Field} is an extension of \spadtype{Ring}. 368\end{texonly} 369 370The operations supported by the domains of a category are called the 371\spadglossSee{exports}{export} of that category because these are the 372operations made available for system-wide use. 373The exports of a domain of a given category are not only the ones 374explicitly mentioned by the category. 375Since a category extends other categories, the operations of these other 376categories---and all categories these other categories extend---are also 377exported by the domains. 378 379For example, polynomial domains belong to \spadtype{PolynomialCategory}. 380This category explicitly mentions some twenty-nine 381operations on polynomials, but it 382extends eleven other categories (including \spadtype{Ring}). 383As a result, the current system has over one hundred operations on polynomials. 384 385If a domain belongs to a category that extends, say, \spadtype{Ring}, it 386is convenient to say that the domain exports \spadtype{Ring}. 387The name of the category thus provides a convenient shorthand for the list 388of operations exported by the category. 389Rather than listing operations such as \spadopFrom{+}{Ring} and 390\spadopFrom{*}{Ring} of \spadtype{Ring} each time they are needed, the 391definition of a type simply asserts that it exports category 392\spadtype{Ring}. 393 394The category name, however, is more than a shorthand. 395The name \spadtype{Ring}, in fact, implies that the operations exported by 396rings are required to satisfy a set of ``axioms'' associated with the name 397\spadtype{Ring}.\footnote{This subtle 398but important feature distinguishes \Language{} from 399other abstract datatype designs.} 400 401Why is it not correct to assume that some type is a ring if it exports all 402of the operations of \spadtype{Ring}? 403Here is why. 404Some languages such as {\bf APL} 405\index{APL} 406denote the \spadtype{Boolean} constants \spad{true} and 407\spad{false} by the integers \spad{1} and \spad{0} 408respectively, then use \spadop{+} and \spadop{*} to denote the 409logical operators \spadfun{or} and \spadfun{and}. 410But with these definitions 411\spadtype{Boolean} is not a ring since the additive inverse 412axiom is violated.\footnote{There is no inverse element \spad{a} 413such that \spad{1 + a = 0}, or, in the usual terms: 414\spad{true or a = false}.} 415This alternative definition of \spadtype{Boolean} can be easily 416and correctly implemented in \Language{}, since 417\spadtype{Boolean} simply does not assert that it is of category 418\spadtype{Ring}. 419This prevents the system from building meaningless domains such as 420\spadtype{Polynomial(Boolean)} and then wrongfully applying 421algorithms that presume that the ring axioms hold. 422 423 424Enough on categories. To learn more about them, see 425\chapref{ugCategories}. 426We now return to our discussion of domains. 427 428Domains \spadgloss{export} a set of operations to make them 429available for system-wide use. 430\spadtype{Integer}, for example, exports the operations 431\spadopFrom{+}{Integer} and \spadopFrom{=}{Integer} given by 432the \spadglossSee{signatures}{signature} 433\spadopFrom{+}{Integer}: \spad{(Integer,Integer)->Integer} and 434\spadopFrom{=}{Integer}: \spad{(Integer,Integer)->Boolean}, 435respectively. 436Each of these operations takes two \spadtype{Integer} arguments. 437The \spadopFrom{+}{Integer} operation also returns an \spadtype{Integer} but 438\spadopFrom{=}{Integer} returns a \spadtype{Boolean}: \spad{true} or 439\spad{false}. 440The operations exported by a domain usually manipulate objects of 441the domain---but not always. 442 443The operations of a domain may actually take as arguments, and return as 444values, objects from any domain. 445For example, \spadtype{Fraction (Integer)} exports the operations 446\spadopFrom{/}{Fraction}: \spad{(Integer,Integer)->Fraction(Integer)} 447and \spadfunFrom{characteristic}{Fraction}: 448\spad{->NonNegativeInteger}. 449 450Suppose all operations of a domain take as arguments and return 451as values, only objects from {\it other} domains. 452\index{package} 453This kind of domain 454\index{constructor!package} 455is what \Language{} calls a \spadgloss{package}. 456 457A package does not designate a class of objects at all. 458Rather, a package is just a collection of operations. 459Actually the bulk of the \Language{} library of algorithms consists 460of packages. 461The facilities for factorization; integration; solution of linear, 462polynomial, and differential equations; computation of limits; and so 463on, are all defined in packages. 464Domains needed by algorithms can be passed to a package as arguments or 465used by name if they are not ``variable.'' 466Packages are useful for defining operations that convert objects of one 467type to another, particularly when these types have different 468parameterizations. 469As an example, the package \spadtype{PolynomialFunction2(R,S)} defines 470operations that convert polynomials over a domain \spad{R} to polynomials 471over \spad{S}. 472To convert an object from \spadtype{Polynomial(Integer)} to 473\spadtype{Polynomial(Float)}, \Language{} builds the package 474\spadtype{PolynomialFunctions2(Integer,Float)} in order to create the 475required conversion function. 476(This happens ``behind the scenes'' for you: see \spadref{ugTypesConvert} 477for details on how to convert objects.) 478 479\Language{} categories, domains and packages and all their contained 480functions are written in the \Language{} programming language and have 481been compiled into machine code. 482This is what comprises the \Language{} \spadgloss{library}. 483In the rest of this book we show you how to use these domains and 484their functions and how to write your own functions. 485 486% ********************************************************************* 487\head{section}{Writing Types and Modes}{ugTypesWriting} 488% ********************************************************************* 489% 490 491We have already seen in 492\texht{the last section}{\spadref{ugTypesBasic}} 493several examples of types. 494Most of these examples had either no arguments (for example, 495\spadtype{Integer}) or one argument (for example, 496\spadtype{Polynomial (Integer)}). 497In this section we give details about writing arbitrary types. 498We then define \spadglossSee{modes}{mode} and discuss how to write 499them. 500We conclude the section with a discussion on constructor 501abbreviations. 502 503\xtc{ 504When might you need to write a type or mode? 505You need to do so when you declare variables. 506}{ 507\spadcommand{a : PositiveInteger} 508} 509\xtc{ 510You need to do so when you declare functions 511(\spadref{ugTypesDeclare}), 512}{ 513\spadcommand{f : Integer -> String} 514} 515\xtc{ 516You need to do so when you convert an object from one type to another 517(\spadref{ugTypesConvert}). 518}{ 519\spadcommand{factor(2 :: Complex(Integer))} 520} 521\xtc{ 522}{ 523\spadcommand{(2 = 3)\$Integer} 524} 525\xtc{ 526You need to do so when you give computation target type information 527(\spadref{ugTypesPkgCall}). 528}{ 529\spadcommand{(2 = 3)@Boolean} 530} 531 532% ********************************************************************* 533\head{subsection}{Types with No Arguments}{ugTypesWritingZero} 534% ********************************************************************* 535 536A constructor with no arguments can be written either 537\index{type!using parentheses} 538with or without 539\index{parentheses!using with types} 540trailing opening and closing parentheses (\spadSyntax{()}). 541\begin{texonly} 542\begin{center} 543\begin{tabular}{ccc} 544\spadtype{Boolean()} is the same as \spadtype{Boolean} & \quad & 545\spadtype{Integer()} is the same as \spadtype{Integer} \\ 546\spadtype{String()} is the same as \spadtype{String} & \quad & 547\spadtype{Void()} is the same as \spadtype{Void} \\ 548\end{tabular} 549\end{center} 550\end{texonly} 551\begin{htonly} 552\begin{center} 553\spadtype{Boolean()} is the same as \spadtype{Boolean} \\ 554\spadtype{Integer()} is the same as \spadtype{Integer} \\ 555\spadtype{String()} is the same as \spadtype{String} \\ 556\spadtype{Void()} is the same as \spadtype{Void} \\ 557\end{center} 558\end{htonly} 559and so on. 560It is customary to omit the parentheses. 561 562% ********************************************************************* 563\head{subsection}{Types with One Argument}{ugTypesWritingOne} 564% ********************************************************************* 565 566A constructor with one argument can frequently be 567\index{type!using parentheses} 568written with no 569\index{parentheses!using with types} 570parentheses. 571Types nest from right to left so that 572\spadtype{Complex Fraction Polynomial Integer} is the same as 573\spadtype{Complex (Fraction (Polynomial (Integer)))}. 574You need to use parentheses to force the application of a constructor 575to the correct argument, but you need not use any more than is 576necessary to remove ambiguities. 577 578Here are some guidelines for using parentheses (they are possibly slightly 579more restrictive than they need to be). 580\xtc{ 581If the argument is an expression like \spad{2 + 3} 582then you must enclose the argument in parentheses. 583}{ 584\spadcommand{e : PrimeField(2 + 3)} 585} 586% 587\xtc{ 588If the type is to be used with package calling 589then you must enclose the argument in parentheses. 590}{ 591\spadcommand{content(2)\$Polynomial(Integer)} 592} 593\xtc{ 594Alternatively, you can write the type without parentheses 595then enclose the whole type expression with parentheses. 596}{ 597\spadcommand{content(2)\$(Polynomial Complex Fraction Integer)} 598} 599\xtc{ 600If you supply computation target type information 601(\spadref{ugTypesPkgCall}) 602then you should enclose the argument in parentheses. 603}{ 604\spadcommand{(2/3)@Fraction(Polynomial(Integer))} 605} 606% 607\xtc{ 608If the type itself has parentheses around it and we are 609not in the case of the first example above, 610then the parentheses can usually be omitted. 611}{ 612\spadcommand{(2/3)@Fraction(Polynomial Integer)} 613} 614% 615\xtc{ 616If the type is used in a declaration and the argument is a single-word 617type, integer or symbol, 618then the parentheses can usually be omitted. 619}{ 620\spadcommand{(d,f,g) : Complex Polynomial Integer} 621} 622 623% ********************************************************************* 624\head{subsection}{Types with More Than One Argument}{ugTypesWritingMore} 625% ********************************************************************* 626 627If a constructor 628\index{type!using parentheses} 629has more than 630\index{parentheses!using with types} 631one argument, you must use parentheses. 632Some examples are 633\begin{center} 634\spadtype{UnivariatePolynomial(x, Float)} \\ 635\spadtype{MultivariatePolynomial([z,w,r], Complex Float)} \\ 636\spadtype{SquareMatrix(3, Integer)} \\ 637\spadtype{FactoredFunctions2(Integer,Fraction Integer)} 638\end{center} 639 640% ********************************************************************* 641\head{subsection}{Modes}{ugTypesWritingModes} 642% ********************************************************************* 643 644A \spadgloss{mode} is a type that possibly is a 645question mark (\spadSyntax{?}) or contains one in an argument 646position. 647For example, the following are all modes. 648\begin{texonly} 649\begin{center} 650\begin{tabular}{ccc} 651\spadtype{?} & \quad & 652\spadtype{Polynomial ?} \\ 653\spadtype{Matrix Polynomial ?} & \quad & 654\spadtype{SquareMatrix(3,?)} \\ 655\spadtype{Integer} & \quad & 656\spadtype{OneDimensionalArray(Float)} 657\end{tabular} 658\end{center} 659\end{texonly} 660\begin{htonly} 661\begin{center} 662\spadtype{?} \\ 663\spadtype{Polynomial ?} \\ 664\spadtype{Matrix Polynomial ?} \\ 665\spadtype{SquareMatrix(3,?)} \\ 666\spadtype{Integer} \\ 667\spadtype{OneDimensionalArray(Float)} 668\end{center} 669\end{htonly} 670 671As is evident from these examples, a mode is a type with a 672part that is not specified (indicated by a question mark). 673Only one \spadSyntax{?} is allowed per mode and it must appear in the 674most deeply nested argument that is a type. Thus 675\nonLibAxiomType{?(Integer)}, 676\nonLibAxiomType{Matrix(? (Polynomial))}, 677\nonLibAxiomType{SquareMatrix(?, Integer)} and 678\nonLibAxiomType{SquareMatrix(?, ?)} are all invalid. 679The question mark must take the place of a domain, not data (for example, 680the integer that is the dimension of a square matrix). 681This rules out, for example, the two \spadtype{SquareMatrix} 682expressions. 683 684Modes can be used for declarations 685(\spadref{ugTypesDeclare}) 686and conversions 687(\spadref{ugTypesConvert}). 688However, you cannot use a mode for package calling or giving target 689type information. 690 691% ********************************************************************* 692\head{subsection}{Abbreviations}{ugTypesWritingAbbr} 693% ********************************************************************* 694 695Every constructor has an abbreviation that 696\index{abbreviation!constructor} 697you can freely 698\index{constructor!abbreviation} 699substitute for the constructor name. 700In some cases, the abbreviation is nothing more than the 701capitalized version of the constructor name. 702 703\beginImportant 704Aside from allowing types to be written more concisely, 705abbreviations are used by \Language{} to name various system 706files for constructors (such as library filenames, test input 707files and example files). 708Here are some common abbreviations. 709\begin{texonly} 710\begin{center} 711\begin{tabular}{ll} 712\small\spadtype{COMPLEX} abbreviates \spadtype{Complex} & 713\small\spadtype{DFLOAT} abbreviates \spadtype{DoubleFloat} \\ 714\small\spadtype{EXPR} abbreviates \spadtype{Expression} & 715\small\spadtype{FLOAT} abbreviates \spadtype{Float} \\ 716\small\spadtype{FRAC} abbreviates \spadtype{Fraction} & 717\small\spadtype{INT} abbreviates \spadtype{Integer} \\ 718\small\spadtype{MATRIX} abbreviates \spadtype{Matrix} & 719\small\spadtype{NNI} abbreviates \spadtype{NonNegativeInteger} \\ 720\small\spadtype{PI} abbreviates \spadtype{PositiveInteger} & 721\small\spadtype{POLY} abbreviates \spadtype{Polynomial} \\ 722\small\spadtype{STRING} abbreviates \spadtype{String} & 723\small\spadtype{UP} abbreviates \spadtype{UnivariatePolynomial}\\ 724\end{tabular} 725\end{center} 726\end{texonly} 727\begin{htonly} 728\table{ 729{\spadtype{COMPLEX} abbreviates \spadtype{Complex} } 730{\spadtype{DFLOAT} abbreviates \spadtype{DoubleFloat} } 731{\spadtype{EXPR} abbreviates \spadtype{Expression} } 732{\spadtype{FLOAT} abbreviates \spadtype{Float} } 733{\spadtype{FRAC} abbreviates \spadtype{Fraction} } 734{\spadtype{INT} abbreviates \spadtype{Integer} } 735{\spadtype{MATRIX} abbreviates \spadtype{Matrix} } 736{\spadtype{NNI} abbreviates \spadtype{NonNegativeInteger} } 737{\spadtype{PI} abbreviates \spadtype{PositiveInteger} } 738{\spadtype{POLY} abbreviates \spadtype{Polynomial} } 739{\spadtype{STRING} abbreviates \spadtype{String} } 740{\spadtype{UP} abbreviates \spadtype{UnivariatePolynomial} } 741} 742\end{htonly} 743\endImportant 744 745You can combine both full constructor names and abbreviations 746in a type expression. 747Here are some types using abbreviations. 748\begin{center} 749\spadtype{POLY INT} is the same as \spadtype{Polynomial(INT)} \\ 750\spadtype{POLY(Integer)} is the same as \spadtype{Polynomial(Integer)} \\ 751\spadtype{POLY(Integer)} is the same as \spadtype{Polynomial(INT)} \\ 752\spadtype{FRAC(COMPLEX(INT))} is the same as \spadtype{Fraction Complex Integer} \\ 753\spadtype{FRAC(COMPLEX(INT))} is the same as \spadtype{FRAC(Complex Integer)} \\ 754\end{center} 755 756There are several ways of finding the names of constructors and 757their abbreviations. 758For a specific constructor, use \spadsys{)abbreviation query}. 759\syscmdindex{abbreviation} 760You can also use the \spadsys{)what} system command to see the names 761and abbreviations of constructors. 762\syscmdindex{what} 763For more information about \spadsys{)what}, see 764\spadref{ugSysCmdwhat}. 765\xtc{ 766\spadsys{)abbreviation query} can be 767abbreviated (no pun intended) to \spadsys{)abb q}. 768}{ 769\spadcommand{)abb q Integer} 770} 771\xtc{ 772The \spadsys{)abbreviation query} command lists 773the constructor name if you give the abbreviation. 774Issue \spadsys{)abb q} if you want to see the names and abbreviations 775of all \Language{} constructors. 776}{ 777\spadcommand{)abb q DMP} 778} 779\xtc{ 780Issue this to see all packages whose names contain the string ``ode''. 781\syscmdindex{what packages} 782}{ 783\spadcommand{)what packages ode} 784} 785 786% ********************************************************************* 787\head{section}{Declarations}{ugTypesDeclare} 788% ********************************************************************* 789% 790A \spadgloss{declaration} is an expression used 791to restrict the type of values that can be assigned to variables. 792A colon (\spadSyntax{:}) is always used after a variable or 793list of variables to be declared. 794 795\beginImportant 796For a single variable, the syntax for declaration is 797\begin{center} 798{\it variableName \spad{:} typeOrMode} 799\end{center} 800For multiple variables, the syntax is 801\begin{center} 802{\tt (\subscriptIt{variableName}{1}, \subscriptIt{variableName}{2}, \ldots \subscriptIt{variableName}{N}): {\it typeOrMode}} 803\end{center} 804\endImportant 805 806You can always combine a declaration with an assignment. 807When you do, it is equivalent to first giving a declaration statement, 808then giving an assignment. 809For more information on assignment, see 810\spadref{ugIntroAssign} and 811\spadref{ugLangAssign}. 812To see how to declare your own functions, see 813\spadref{ugUserDeclare}. 814 815\xtc{ 816This declares one variable to have a type. 817}{ 818\spadcommand{a : Integer \bound{a}} 819} 820\xtc{ 821This declares several variables to have a type. 822}{ 823\spadcommand{(b,c) : Integer \bound{b c}} 824} 825\xtc{ 826\spad{a, b} and \spad{c} can only hold integer values. 827}{ 828\spadcommand{a := 45 \free{a}} 829} 830\begin{inputonly} 831)set message test off 832\end{inputonly} 833\xtc{ 834If a value cannot be converted to a declared type, 835an error message is displayed. 836}{ 837\spadcommand{b := 4/5 \free{b}} 838} 839\begin{inputonly} 840)set message test on 841\end{inputonly} 842\xtc{ 843This declares a variable with a mode. 844}{ 845\spadcommand{n : Complex ? \bound{n}} 846} 847\xtc{ 848This declares several variables with a mode. 849}{ 850\spadcommand{(p,q,r) : Matrix Polynomial ? \bound{p q r}} 851} 852\xtc{ 853This complex object has integer real and imaginary parts. 854}{ 855\spadcommand{n := -36 + 9 * \%i \free{n}} 856} 857\xtc{ 858This complex object has fractional symbolic real and imaginary parts. 859}{ 860\spadcommand{n := complex(4/(x + y),y/x) \free{n}} 861} 862\xtc{ 863This matrix has entries that are polynomials with integer 864coefficients. 865}{ 866\spadcommand{p := [[1,2],[3,4],[5,6]] \free{p}} 867} 868\xtc{ 869This matrix has a single entry that is a polynomial with 870rational number coefficients. 871}{ 872\spadcommand{q := [[x - 2/3]] \free{q}} 873} 874\xtc{ 875This matrix has entries that are polynomials with complex integer 876coefficients. 877}{ 878\spadcommand{r := [[1-\%i*x,7*y+4*\%i]] \free{r}} 879} 880% 881\xtc{ 882Note the difference between this and the next example. 883This is a complex object with polynomial real and imaginary parts. 884}{ 885\spadcommand{f : COMPLEX POLY ? := (x + y*\%i)^2} 886} 887\xtc{ 888This is a polynomial with complex integer coefficients. 889The objects are convertible from one to the other. 890See \spadref{ugTypesConvert} for more information. 891}{ 892\spadcommand{g : POLY COMPLEX ? := (x + y*\%i)^2} 893} 894 895% ********************************************************************* 896\head{section}{Records}{ugTypesRecords} 897% ********************************************************************* 898% 899A \pspadtype{Record} is an object composed of one or more other objects, 900\index{Record@\protect\nonLibAxiomType{Record}} 901each of which is referenced 902\index{selector!record} 903with 904\index{record!selector} 905a \spadgloss{selector}. 906Components can all belong to the same type or each can have a different type. 907 908% ---------------------------------------------------------------------- 909\beginImportant 910The syntax for writing a \pspadtype{Record} type is 911\begin{center} 912{\tt Record(\subscriptIt{selector}{1}:\subscriptIt{type}{1}, \subscriptIt{selector}{2}:\subscriptIt{type}{2}, \ldots, \subscriptIt{selector}{N}:\subscriptIt{type}{N})} 913\end{center} 914You must be careful if a selector has the same name as a variable in the 915workspace. 916If this occurs, precede the selector name by a single 917\index{quote} 918quote. 919\endImportant 920% ---------------------------------------------------------------------- 921 922Record components are implicitly ordered. 923All the components of a record can 924be set at once by assigning the record a 925bracketed \spadgloss{tuple} of values of the proper length 926(for example, \spad{r : Record(a: Integer, b: String) := [1, "two"]}). 927To access a component of a record \spad{r}, 928write the name \spad{r}, followed by a period, followed by a selector. 929 930% 931\xtc{ 932The object returned by this computation is a record with two components: a 933\spad{quotient} part and a \spad{remainder} part. 934}{ 935\spadcommand{u := divide(5,2) \bound{u}} 936} 937% 938\xtc{ 939This is the quotient part. 940}{ 941\spadcommand{u.quotient \free{u}} 942} 943\xtc{ 944This is the remainder part. 945}{ 946\spadcommand{u.remainder \free{u}} 947} 948% 949\xtc{ 950You can use selector expressions on the left-hand side of an assignment 951to change destructively the components of a record. 952}{ 953\spadcommand{u.quotient := 8978 \free{u}\bound{u1}} 954} 955\xtc{ 956The selected component \spad{quotient} has the value \spad{8978}, 957which is what is returned by the assignment. 958Check that the value of \spad{u} was modified. 959}{ 960\spadcommand{u \free{u}\free{u1}} 961} 962\xtc{ 963Selectors are evaluated. 964Thus you can use variables that evaluate to selectors instead of the 965selectors themselves. 966}{ 967\spadcommand{s := 'quotient \bound{s}} 968} 969\xtc{ 970Be careful! 971A selector could have the same name as a variable in the workspace. 972If this occurs, precede the selector name by a single quote, as in 973\spad{u.'quotient}. 974\index{selector!quoting} 975}{ 976\spadcommand{divide(5,2).s \free{s}} 977} 978\xtc{ 979Here we declare that the value of \spad{bd} 980has two components: a string, 981to be accessed via \spad{name}, and an integer, 982to be accessed via \spad{birthdayMonth}. 983}{ 984\spadcommand{bd : Record(name : String, birthdayMonth : Integer) \bound{bddec}} 985} 986\xtc{ 987You must initially set the value of the entire \pspadtype{Record} 988at once. 989}{ 990\spadcommand{bd := ["Judith", 3] \free{bddec}\bound{bd}} 991} 992\xtc{ 993Once set, you can change any of the individual components. 994}{ 995\spadcommand{bd.name := "Katie" \free{bd}} 996} 997\xtc{ 998Records may be nested and the selector names can be shared at 999different levels. 1000}{ 1001\spadcommand{r : Record(a : Record(b: Integer, c: Integer), b: Integer) \bound{rdec}} 1002} 1003\xtc{ 1004The record \spad{r} has a \spad{b} selector at two different levels. 1005Here is an initial value for \spad{r}. 1006}{ 1007\spadcommand{r := [[1,2],3] \bound{r}\free{rdec}} 1008} 1009\xtc{ 1010This extracts the \spad{b} component from the \spad{a} component of \spad{r}. 1011}{ 1012\spadcommand{r.a.b \free{r}} 1013} 1014\xtc{ 1015This extracts the \spad{b} component from \spad{r}. 1016}{ 1017\spadcommand{r.b \free{r}} 1018} 1019% 1020\xtc{ 1021You can also use spaces or parentheses to refer to \pspadtype{Record} 1022components. 1023This is the same as \spad{r.a}. 1024}{ 1025\spadcommand{r(a) \free{r}} 1026} 1027\xtc{ 1028This is the same as \spad{r.b}. 1029}{ 1030\spadcommand{r b \free{r}} 1031} 1032\xtc{ 1033This is the same as \spad{r.b := 10}. 1034}{ 1035\spadcommand{r(b) := 10 \free{r}\bound{r1}} 1036} 1037\xtc{ 1038Look at \spad{r} to make sure it was modified. 1039}{ 1040\spadcommand{r \free{r1}} 1041} 1042 1043% ********************************************************************* 1044\head{section}{Unions}{ugTypesUnions} 1045% ********************************************************************* 1046% 1047Type \pspadtype{Union} is used for objects that 1048can be of any of a specific finite set of types. 1049\index{Union@\protect\nonLibAxiomType{Union}} 1050Two versions of unions are available, 1051one with selectors (like records) and one without. 1052\index{union} 1053 1054% ********************************************************************* 1055\head{subsection}{Unions Without Selectors}{ugTypesUnionsWOSel} 1056% ********************************************************************* 1057 1058The declaration \spad{x : Union(Integer, String, Float)} 1059states that \spad{x} can have values that are integers, 1060strings or ``big'' floats. 1061If, for example, the \pspadtype{Union} object is an integer, the object is 1062said to belong to the \spadtype{Integer} {\it branch} 1063of the \pspadtype{Union}.\footnote{ 1064Note that we are being a bit careless with the language here. 1065Technically, the type of \spad{x} is always 1066\pspadtype{Union(Integer, String, Float)}. 1067If it belongs to the \spadtype{Integer} branch, \spad{x} 1068may be converted to an object of type \spadtype{Integer}.} 1069 1070% ---------------------------------------------------------------------- 1071\beginImportant 1072The syntax for writing a \pspadtype{Union} type without selectors is 1073\begin{center} 1074{\tt Union(\subscriptIt{type}{1}, \subscriptIt{type}{2}, \ldots, \subscriptIt{type}{N})} 1075\end{center} 1076The types in a union without selectors must be distinct. 1077\endImportant 1078% ---------------------------------------------------------------------- 1079 1080It is possible to create unions like 1081\pspadtype{Union(Integer, PositiveInteger)} but they are 1082difficult to work with because of the overlap in the branch 1083types. 1084See below for the rules \Language{} uses for converting something 1085into a union object. 1086 1087The \spad{case} infix 1088\spadkey{case} 1089operator returns a \spadtype{Boolean} 1090and can be used to determine the branch in which an object lies. 1091 1092\xtc{ 1093This function displays a message stating in which 1094branch of the \pspadtype{Union} the object (defined as \spad{x} 1095above) lies. 1096}{ 1097\begin{spadsrc}[\bound{sayBranch}] 1098sayBranch(x : Union(Integer,String,Float)) : Void == 1099 output 1100 x case Integer => "Integer branch" 1101 x case String => "String branch" 1102 "Float branch" 1103\end{spadsrc} 1104} 1105% 1106\xtc{ 1107This tries \userfun{sayBranch} with an integer. 1108}{ 1109\spadcommand{sayBranch 1 \free{sayBranch}} 1110} 1111\xtc{ 1112This tries \userfun{sayBranch} with a string. 1113}{ 1114\spadcommand{sayBranch "hello" \free{sayBranch}} 1115} 1116\xtc{ 1117This tries \userfun{sayBranch} with a floating-point number. 1118}{ 1119\spadcommand{sayBranch 2.718281828 \free{sayBranch}} 1120} 1121% 1122 1123There are two things of interest about this particular 1124example to which we would like to draw your attention. 1125\begin{enumerate} 1126% 1127\item \Language{} normally converts a result to the target value 1128before passing it to the function. 1129If we left the declaration information out of this function definition 1130then the \spad{sayBranch} call would have been attempted with an 1131\spadtype{Integer} rather than a \pspadtype{Union}, and an error would have 1132resulted. 1133% 1134\item The types in a \pspadtype{Union} are searched in the order given. 1135So if the type were given as 1136 1137\noindent 1138{\small\spad{sayBranch(x: Union(String,Integer,Float,Any)): Void}} 1139 1140\noindent 1141then the result would have been ``String branch'' because there 1142is a conversion from \spadtype{Integer} to \spadtype{String}. 1143\end{enumerate} 1144 1145Sometimes \pspadtype{Union} types can have extremely 1146long names. 1147\Language{} therefore abbreviates the names of unions by printing 1148the type of the branch first within the \pspadtype{Union} and then 1149eliding the remaining types with an ellipsis (\spadSyntax{...}). 1150 1151\xtc{ 1152Here the \spadtype{Integer} branch is displayed first. 1153Use \spadSyntax{::} to create a \pspadtype{Union} object from an object. 1154}{ 1155\spadcommand{78 :: Union(Integer,String)} 1156} 1157\xtc{ 1158Here the \spadtype{String} branch is displayed first. 1159}{ 1160\spadcommand{s := "string" :: Union(Integer,String) \bound{s}} 1161} 1162\xtc{ 1163Use \spad{typeOf} to see the full and actual \pspadtype{Union} type. 1164\spadkey{typeOf} 1165}{ 1166\spadcommand{typeOf s} 1167} 1168\xtc{ 1169A common operation that returns a union is \spadfunFrom{exquo}{Integer} 1170which returns the ``exact quotient'' if the quotient is exact,... 1171}{ 1172\spadcommand{three := exquo(6,2) \bound{three}} 1173} 1174\xtc{ 1175and \spad{"failed"} if the quotient is not exact. 1176}{ 1177\spadcommand{exquo(5,2)} 1178} 1179\xtc{ 1180A union with a \spad{"failed"} is frequently used to indicate the failure 1181or lack of applicability of an object. 1182As another example, assign an integer a variable \spad{r} declared to be a 1183rational number. 1184}{ 1185\spadcommand{r: FRAC INT := 3 \bound{r}\bound{rdec}} 1186} 1187\xtc{ 1188The operation \spadfunFrom{retractIfCan}{Fraction} tries to retract the 1189fraction to the underlying domain \spadtype{Integer}. 1190It produces a union object. 1191Here it succeeds. 1192}{ 1193\spadcommand{retractIfCan(r) \free{r}} 1194} 1195\xtc{ 1196Assign it a rational number. 1197}{ 1198\spadcommand{r := 3/2 \bound{r1}\free{rdec}} 1199} 1200\xtc{ 1201Here the retraction fails. 1202}{ 1203\spadcommand{retractIfCan(r) \free{r1}} 1204} 1205 1206% ********************************************************************* 1207\head{subsection}{Unions With Selectors}{ugTypesUnionsWSel} 1208% ********************************************************************* 1209 1210Like records (\spadref{ugTypesRecords}), 1211you can write \pspadtype{Union} types 1212\index{selector!union} 1213with selectors. 1214\index{union!selector} 1215 1216% ---------------------------------------------------------------------- 1217\beginImportant 1218The syntax for writing a \pspadtype{Union} type with selectors is 1219\begin{center} 1220{\tt Union(\subscriptIt{selector}{1}:\subscriptIt{type}{1}, \subscriptIt{selector}{2}:\subscriptIt{type}{2}, \ldots, \subscriptIt{selector}{N}:\subscriptIt{type}{N})} 1221\end{center} 1222You must be careful if a selector has the same name as a variable in the 1223workspace. 1224If this occurs, precede the selector name by a single 1225\index{quote} 1226quote. 1227\index{selector!quoting} 1228It is an error to use a selector that does not correspond to the branch of 1229the \pspadtype{Union} in which the element actually lies. 1230\endImportant 1231% ---------------------------------------------------------------------- 1232 1233Be sure to understand the difference between records and unions 1234with selectors. 1235\index{union!difference from record} 1236Records can have more than one component and the selectors are 1237used to refer to the components. 1238\index{record!difference from union} 1239Unions always have one component but the type of that one 1240component can vary. 1241An object of type \pspadtype{Record(a: Integer, b: Float, c: String)} 1242contains an integer {\it and} a float {\it and} a 1243string. 1244An object of type \pspadtype{Union(a: Integer, b: Float, c: String)} 1245contains an integer {\it or} a float {\it or} a 1246string. 1247 1248Here is a version of the \userfun{sayBranch} function (cf. 1249\spadref{ugTypesUnionsWOSel}) that works with a union with selectors. 1250It displays a message stating in which branch of the \pspadtype{Union} the 1251object lies. 1252\begin{verbatim} 1253sayBranch(x:Union(i:Integer,s:String,f:Float)):Void== 1254 output 1255 x case i => "Integer branch" 1256 x case s => "String branch" 1257 "Float branch" 1258\end{verbatim} 1259Note that \spad{case} uses the selector name as its right-hand argument. 1260\spadkey{case} 1261 1262\xtc{ 1263Declare variable \spad{u} to have a union type with selectors. 1264}{ 1265\spadcommand{u : Union(i : Integer, s : String) \bound{undec}} 1266} 1267\xtc{ 1268Give an initial value to \spad{u}. 1269}{ 1270\spadcommand{u := "good morning" \bound{u}\free{undec}} 1271} 1272\xtc{ 1273Use \spad{case} to determine in which 1274branch of a \pspadtype{Union} an object lies. 1275}{ 1276\spadcommand{u case i \free{u}} 1277} 1278\xtc{ 1279}{ 1280\spadcommand{u case s \free{u}} 1281} 1282\xtc{ 1283To access the element in a particular branch, use the selector. 1284}{ 1285\spadcommand{u.s \free{u}} 1286} 1287 1288% ********************************************************************* 1289\head{section}{The ``Any'' Domain}{ugTypesAnyNone} 1290% ********************************************************************* 1291 1292With the exception of objects of type \pspadtype{Record}, all \Language{} 1293data structures are homogeneous, that is, they hold objects all of the same 1294type. 1295\exptypeindex{Any} 1296If you need to get around this, you can use type \spadtype{Any}. 1297Using \spadtype{Any}, for example, you can create lists whose 1298elements are integers, rational numbers, strings, and even other lists. 1299 1300\xtc{ 1301Declare \spad{u} to have type \spadtype{Any}. 1302}{ 1303\spadcommand{u: Any\bound{uany}} 1304} 1305\xtc{ 1306Assign a list of mixed type values to \spad{u} 1307}{ 1308\spadcommand{u := [1, 7.2, 3/2, x^2, "wally"]\free{uany}\bound{u}} 1309} 1310\xtc{ 1311When we ask for the elements, \Language{} displays these types. 1312}{ 1313\spadcommand{u.1 \free{u}} 1314} 1315\xtc{ 1316Actually, these objects belong to \spadtype{Any} but \Language{} 1317automatically converts them to their natural types for you. 1318}{ 1319\spadcommand{u.3 \free{u}} 1320} 1321\begin{inputonly} 1322)set message test off 1323\end{inputonly} 1324\xtc{ 1325Since type \spadtype{Any} can be anything, 1326it can only belong to type \spadtype{Type}. 1327Therefore it cannot be used in algebraic domains. 1328}{ 1329\spadcommand{v : Matrix(Any)} 1330} 1331\begin{inputonly} 1332)set message test on 1333\end{inputonly} 1334 1335Perhaps you are wondering how \Language{} internally represents 1336objects of type \spadtype{Any}. 1337An object of type \spadtype{Any} consists not only a data part 1338representing its normal value, but also a type part (a {\it badge}) giving 1339\index{badge} 1340its type. 1341For example, the value \spad{1} of type \spadtype{PositiveInteger} as an 1342object of type \spadtype{Any} internally looks like 1343\spad{[1,PositiveInteger()]}. 1344 1345%When should you use \spadtype{Any} instead of a \pspadtype{Union} type? 1346%Can you plan ahead? 1347%For a \pspadtype{Union}, you must know in advance exactly which types you 1348%are 1349%\index{union!vs. Any@{vs. \protect\nonLibAxiomType{Any}}} 1350%going to allow. 1351%For \spadtype{Any}, anything that comes along can be accommodated. 1352 1353% ********************************************************************* 1354\head{section}{Conversion}{ugTypesConvert} 1355% ********************************************************************* 1356% 1357\beginImportant 1358\spadglossSee{Conversion}{conversion} 1359is the process of changing an object of one type 1360into an object of another type. 1361The syntax for conversion is: 1362\begin{center} 1363{\it object} {\tt ::} {\it newType} 1364\end{center} 1365\endImportant 1366 1367\xtc{ 1368By default, \spad{3} has the type \spadtype{PositiveInteger}. 1369}{ 1370\spadcommand{3} 1371} 1372\xtc{ 1373We can change this into an object of type \spadtype{Fraction Integer} 1374by using \spadSyntax{::}. 1375}{ 1376\spadcommand{3 :: Fraction Integer} 1377} 1378 1379A \spadgloss{coercion} is a special kind of conversion that \Language{} is 1380allowed to do automatically when you enter an expression. 1381Coercions are usually somewhat safer than more general conversions. 1382The \Language{} library contains operations called 1383\spadfun{coerce} and \spadfun{convert}. 1384Only the \spadfun{coerce} operations can be used by the 1385interpreter to change an object into an object of another type unless 1386you explicitly use a \spadSyntax{::}. 1387 1388By now you will be quite familiar with what types and modes look like. 1389It is useful to think of a type or mode as a pattern 1390for what you want the result to be. 1391\xtc{ 1392Let's start with a square matrix of polynomials with complex rational number 1393coefficients. 1394\exptypeindex{SquareMatrix} 1395}{ 1396\spadcommand{m : SquareMatrix(2,POLY COMPLEX FRAC INT) \bound{mdec}} 1397} 1398\xtc{ 1399}{ 1400\spadcommand{m := matrix [[x-3/4*\%i,z*y^2+1/2],[3/7*\%i*y^4 - x,12-\%i*9/5]] \bound{m}\free{mdec}} 1401} 1402\xtc{ 1403We first want to interchange the \spadtype{Complex} and 1404\spadtype{Fraction} layers. 1405We do the conversion by doing the interchange in the type expression. 1406}{ 1407\spadcommand{m1 := m :: SquareMatrix(2,POLY FRAC COMPLEX INT) \free{m}\bound{m1}} 1408} 1409\xtc{ 1410Interchange the \spadtype{Polynomial} and the 1411\spadtype{Fraction} levels. 1412}{ 1413\spadcommand{m2 := m1 :: SquareMatrix(2,FRAC POLY COMPLEX INT) \free{m1}\bound{m2}} 1414} 1415\xtc{ 1416Interchange the \spadtype{Polynomial} and the 1417\spadtype{Complex} levels. 1418}{ 1419\spadcommand{m3 := m2 :: SquareMatrix(2,FRAC COMPLEX POLY INT) \free{m2}\bound{m3}} 1420} 1421 1422All the entries have changed types, although in comparing the 1423last two results only the entry in the lower left corner looks different. 1424We did all the intermediate steps to show you what \Language{} can do. 1425 1426\xtc{ 1427In fact, we could have combined all these into one conversion. 1428}{ 1429\spadcommand{m :: SquareMatrix(2,FRAC COMPLEX POLY INT) \free{m}} 1430} 1431 1432There are times when \Language{} is not be able to do the conversion 1433in one step. 1434You may need to break up the transformation into several conversions 1435in order to get an object of the desired type. 1436 1437We cannot move either \spadtype{Fraction} or \spadtype{Complex} 1438above (or to the left of, depending on how you look at it) 1439\spadtype{SquareMatrix} because each of these levels requires that its 1440argument type have commutative multiplication, whereas 1441\spadtype{SquareMatrix} does not.\footnote{\spadtype{Fraction} requires 1442that its argument belong to the category \spadtype{IntegralDomain} and 1443\index{category} 1444\spadtype{Complex} requires that its argument belong to 1445\spadtype{CommutativeRing}. See 1446\spadref{ugTypesBasic} 1447for a brief discussion of categories.} 1448The \spadtype{Integer} level did not move anywhere 1449because it does not allow any arguments. 1450We also did not move the \spadtype{SquareMatrix} part anywhere, but 1451we could have. 1452\xtc{ 1453Recall that \spad{m} looks like this. 1454}{ 1455\spadcommand{m \free{m}} 1456} 1457\xtc{ 1458If we want a polynomial with matrix coefficients rather than a matrix 1459with polynomial entries, we can just do the conversion. 1460}{ 1461\spadcommand{m :: POLY SquareMatrix(2,COMPLEX FRAC INT) \free{m}} 1462} 1463\xtc{ 1464We have not yet used modes for any conversions. 1465Modes are a great shorthand for indicating the type of the 1466object you want. 1467Instead of using the long type expression in the 1468last example, we could have simply said this. 1469}{ 1470\spadcommand{m :: POLY ? \free{m}} 1471} 1472\xtc{ 1473We can also indicate more structure if we want the entries 1474of the matrices to be fractions. 1475}{ 1476\spadcommand{m :: POLY SquareMatrix(2,FRAC ?) \free{m}} 1477} 1478 1479% ********************************************************************* 1480\head{section}{Subdomains Again}{ugTypesSubdomains} 1481% ********************************************************************* 1482 1483A \spadgloss{subdomain} \spad{S} of a domain \spad{D} is a domain 1484consisting of 1485\begin{enumerate} 1486\item those elements of \spad{D} that satisfy some 1487\spadgloss{predicate} (that is, a test that returns \spad{true} or 1488\spad{false}) and 1489\item a subset of the operations of \spad{D}. 1490\end{enumerate} 1491Every domain is a subdomain of itself, trivially satisfying the 1492membership test: \spad{true}. 1493 1494Currently, there are only two system-defined subdomains in \Language{} that receive 1495substantial use. 1496\spadtype{PositiveInteger} and 1497\spadtype{NonNegativeInteger} are subdomains of \spadtype{Integer}. 1498An element \spad{x} of \spadtype{NonNegativeInteger} is an integer 1499that is greater than or equal to zero, that is, satisfies 1500\spad{x >= 0.} 1501An element \spad{x} of \spadtype{PositiveInteger} is a nonnegative integer 1502that is, in fact, greater than zero, that is, satisfies \spad{x > 0.} 1503Not all operations from \spadtype{Integer} are available for these 1504subdomains. 1505For example, negation and subtraction are not provided since the subdomains 1506are not closed under those operations. 1507When you use an integer in an expression, \Language{} assigns to it the 1508type that is the most specific subdomain whose predicate is satisfied. 1509\xtc{ 1510This is a positive integer. 1511}{ 1512\spadcommand{5} 1513} 1514\xtc{ 1515This is a nonnegative integer. 1516}{ 1517\spadcommand{0} 1518} 1519\xtc{ 1520This is neither of the above. 1521}{ 1522\spadcommand{-5} 1523} 1524\xtc{ 1525Furthermore, unless you are assigning an integer to a declared variable 1526or using a conversion, any integer result has as type the most 1527specific subdomain. 1528}{ 1529\spadcommand{(-2) - (-3)} 1530} 1531\xtc{ 1532}{ 1533\spadcommand{0 :: Integer} 1534} 1535\xtc{ 1536}{ 1537\spadcommand{x : NonNegativeInteger := 5} 1538} 1539 1540When necessary, \Language{} converts an integer object into one belonging 1541to a less specific subdomain. 1542For example, in \spad{3-2}, the arguments to \spadopFrom{-}{Integer} are both 1543elements of \spadtype{PositiveInteger}, but this type does not provide 1544a subtraction operation. 1545Neither does \spadtype{NonNegativeInteger}, so \spad{3} and \spad{2} 1546are viewed as elements of \spadtype{Integer}, where their difference 1547can be calculated. 1548The result is \spad{1}, which \Language{} then automatically assigns 1549the type \spadtype{PositiveInteger}. 1550 1551\xtc{ 1552Certain operations are very sensitive to the subdomains to which their 1553arguments belong. 1554This is an element of \spadtype{PositiveInteger}. 1555}{ 1556\spadcommand{2 ^ 2} 1557} 1558\xtc{ 1559This is an element of \spadtype{Fraction Integer}. 1560}{ 1561\spadcommand{2 ^ (-2)} 1562} 1563\xtc{ 1564It makes sense then that this 1565is a list of elements of \spadtype{PositiveInteger}. 1566}{ 1567\spadcommand{[10^i for i in 2..5]} 1568} 1569What should the type of \spad{[10^(i-1) for i in 2..5]} be? 1570On one hand, \spad{i-1} is always an integer greater than zero 1571as \spad{i} ranges from \spad{2} to \spad{5} and so \spad{10^i} 1572is also always a positive integer. 1573On the other, \spad{i-1} is a very simple function of \spad{i}. 1574\Language{} does not try to analyze every such function over the 1575index's range of values to determine whether it is always positive 1576or nowhere negative. 1577For an arbitrary \Language{} function, this analysis is not possible. 1578 1579\xtc{ 1580So, to be consistent no such analysis is done and we get this. 1581}{ 1582\spadcommand{[10^(i-1) for i in 2..5]} 1583} 1584\xtc{ 1585To get a list of elements of \spadtype{PositiveInteger} instead, you 1586have two choices. 1587You can use a conversion. 1588}{ 1589\spadcommand{[10^((i-1) :: PI) for i in 2..5]} 1590} 1591\xtc{ 1592Or you can use \spad{pretend}. 1593\spadkey{pretend} 1594}{ 1595\spadcommand{[10^((i-1) pretend PI) for i in 2..5]} 1596} 1597 1598The operation \spad{pretend} is used to defeat the \Language{} 1599type system. 1600The expression \spad{object pretend D} means ``make a new object 1601(without copying) of type \spad{D} from \spad{object}.'' 1602If \spad{object} were an integer and you told \Language{} 1603to pretend it was a list, you would probably see a message about a 1604fatal error being caught and memory possibly being damaged. 1605Lists do not have the same internal representation as integers! 1606 1607You use \spad{pretend} at your peril. 1608\index{peril} 1609 1610\xtc{ 1611Use \spad{pretend} with great care! 1612\Language{} trusts you that the value is of the specified type. 1613}{ 1614\spadcommand{(2/3) pretend Complex Integer} 1615} 1616 1617% ********************************************************************* 1618\head{section}{Package Calling and Target Types}{ugTypesPkgCall} 1619% ********************************************************************* 1620 1621\Language{} works hard to figure out what you mean by an 1622expression without your having to qualify it with type 1623information. 1624Nevertheless, there are times when you need to help it along by 1625providing hints (or even orders!) to get \Language{} to do what 1626you want. 1627 1628We saw in \spadref{ugTypesDeclare} that declarations using types 1629and modes control the type of the results produced. 1630For example, we can either produce a complex object with 1631polynomial real and imaginary parts or a polynomial with complex 1632integer coefficients, depending on the declaration. 1633 1634\spadglossSee{Package calling}{package call} is how you tell 1635\Language{} to use a particular function from a particular part of 1636the library. 1637 1638\xtc{ 1639Use the \spadopFrom{/}{Fraction} from \spadtype{Fraction Integer} 1640to create a fraction of two integers. 1641}{ 1642\spadcommand{2/3} 1643} 1644\xtc{ 1645If we wanted a floating point number, we can say ``use the 1646\spadopFrom{/}{Float} in \spadtype{Float}.'' 1647}{ 1648\spadcommand{(2/3)\$Float} 1649} 1650\xtc{ 1651Perhaps we actually wanted a fraction of complex integers. 1652}{ 1653\spadcommand{(2/3)\$Fraction(Complex Integer)} 1654} 1655 1656In each case, \Language{} used the indicated operations, sometimes 1657first needing to convert the two integers into objects of an 1658appropriate type. 1659In these examples, \spadopFrom{/}{Fraction} is written as an 1660infix operator. 1661 1662\beginImportant 1663To use package calling with an infix operator, use the 1664following syntax: 1665\begin{center} 1666{\tt ( \subscriptIt{arg}{1} {\it op} \subscriptIt{arg}{1} )\${\it type} } 1667\end{center} 1668\endImportant 1669 1670We used, for example, \spad{(2/3)$Float}. 1671The expression \spad{2 + 3 + 4} is equivalent to \spad{(2+3) + 4.} 1672Therefore in the expression 1673\spad{(2 + 3 + 4)$Float} the second 1674\spadop{+} comes from the \spadtype{Float} domain. 1675Can you guess whether the first \spadop{+} comes from 1676\spadtype{Integer} or \spadtype{Float}?\footnote{\spadtype{Float}, 1677because the package call causes \Language{} to convert 1678\spad{(2 + 3)} and \spad{4} to type \spadtype{Float}. 1679Before the sum is converted, it is given a target type (see below) of 1680\spadtype{Float} by \Language{} and then evaluated. 1681The target type causes the \spadop{+} from \spadtype{Float} to be used.} 1682 1683\beginImportant 1684For an operator written before its arguments, you must use 1685parentheses around the arguments (even if there is only one), 1686and follow the closing parenthesis by a \spadSyntax{$} 1687and then the type. 1688\begin{center} 1689{\tt {\it fun} ( \subscriptIt{arg}{1}, \subscriptIt{arg}{1}, \ldots, \subscriptIt{arg}{N} )\${\it type}} 1690\end{center} 1691\endImportant 1692 1693For example, to call the ``minimum'' function from \spadtype{DoubleFloat} 1694on two integers, you could write \spad{min(4,89)$DoubleFloat}. 1695Another use of package calling is to tell \Language{} to use a library 1696function rather than a function you defined. 1697We discuss this in \spadref{ugUserUse}. 1698 1699Sometimes rather than specifying where an operation comes from, you just 1700want to say what type the result should be. 1701We say that you provide 1702\index{type!target} 1703a 1704\spadglossSee{target type}{target} for the expression. 1705\index{target type} 1706Instead of using a \spadSyntax{$}, use a \spadSyntax{@} to specify 1707the requested target type. 1708Otherwise, the syntax is the same. 1709Note that giving a target type is not the same as explicitly doing a 1710conversion. 1711The first says ``try to pick operations so that the result has 1712such-and-such a type.'' 1713The second says ``compute the result and then convert to an object of 1714such-and-such a type.'' 1715 1716\xtc{ 1717Sometimes it makes sense, as in this expression, 1718to say ``choose the operations in this expression so that 1719the final result is a \spadtype{Float}.'' 1720}{ 1721\spadcommand{(2/3)@Float} 1722} 1723 1724Here we used \spadSyntax{@} to say that the target type of the 1725left-hand side was \spadtype{Float}. 1726In this simple case, there was no real difference 1727between using \spadSyntax{$} and \spadSyntax{@}. 1728You can see the difference if you try the following. 1729\begin{inputonly} 1730)set message test off 1731\end{inputonly} 1732\xtc{ 1733This says to try to choose \spadop{+} so that the result is 1734a string. 1735\Language{} cannot do this. 1736}{ 1737\spadcommand{(2 + 3)@String} 1738} 1739\xtc{ 1740This says to get the \spadop{+} from \spadtype{String} and apply 1741it to the two integers. 1742\Language{} also cannot do this because there is no \spadop{+} 1743exported by \spadtype{String}. 1744}{ 1745\spadcommand{(2 + 3)\$String} 1746} 1747\begin{inputonly} 1748)set message test on 1749\end{inputonly} 1750(By the way, the operation \spadfunFrom{concat}{String} or juxtaposition 1751is used to concatenate two strings.) 1752\exptypeindex{String} 1753 1754When we have more than one operation in an expression, the 1755difference is even more evident. 1756The following two expressions show that \Language{} uses the 1757target type to create different objects. 1758The \spadop{+}, \spadop{*} and \spadop{^} operations are all 1759chosen so that an object of the correct final type is created. 1760 1761\xtc{ 1762This says that the operations should be chosen so 1763that the result is a \spadtype{Complex} object. 1764}{ 1765\spadcommand{((x + y * \%i)^2)@(Complex Polynomial Integer)} 1766} 1767\xtc{ 1768This says that the operations should be chosen so 1769that the result is a \spadtype{Polynomial} object. 1770}{ 1771\spadcommand{((x + y * \%i)^2)@(Polynomial Complex Integer)} 1772} 1773\xtc{ 1774What do you think might happen if we left off all 1775target type and package call information in this last example? 1776}{ 1777\spadcommand{(x + y * \%i)^2 \bound{prevC}} 1778} 1779\xtc{ 1780We can convert it to \spadtype{Complex} as an afterthought. 1781But this is more work than just saying making what we want in the first 1782place. 1783}{ 1784\spadcommand{\% :: Complex ? \free{prevC}} 1785} 1786 1787Finally, another use of package calling is to qualify fully an 1788operation that is passed as an argument to a function. 1789 1790\xtc{ 1791Start with a small matrix of integers. 1792}{ 1793\spadcommand{h := matrix [[8,6],[-4,9]] \bound{h}} 1794} 1795% 1796\xtc{ 1797We want to produce a new matrix that has for entries the multiplicative 1798inverses of the entries of \spad{h}. 1799One way to do this is by calling 1800\spadfunFrom{map}{MatrixCategoryFunctions2} with the 1801\spadfunFrom{inv}{Fraction} function from \spadtype{Fraction (Integer)}. 1802}{ 1803\spadcommand{map(inv\$Fraction(Integer),h) \free{h}} 1804} 1805\xtc{ 1806We could have been a bit less verbose and used abbreviations. 1807}{ 1808\spadcommand{map(inv\$FRAC(INT),h) \free{h}\bound{h1}} 1809} 1810% 1811\xtc{ 1812As it turns out, \Language{} is smart enough to know what we mean 1813anyway. 1814We can just say this. 1815}{ 1816\spadcommand{map(inv,h) \free{h}} 1817} 1818 1819% ********************************************************************* 1820\head{section}{Resolving Types}{ugTypesResolve} 1821% ********************************************************************* 1822 1823In this section we briefly describe an internal process by which 1824\index{resolve} 1825\Language{} determines a type to which two objects of possibly 1826different types can be converted. 1827We do this to give you further insight into how \Language{} takes 1828your input, analyzes it, and produces a result. 1829 1830What happens when you enter \spad{x + 1} to \Language{}? 1831Let's look at what you get from the two terms of this expression. 1832 1833\xtc{ 1834This is a symbolic object whose type indicates the name. 1835}{ 1836\spadcommand{x} 1837} 1838\xtc{ 1839This is a positive integer. 1840}{ 1841\spadcommand{1} 1842} 1843 1844There are no operations in \spadtype{PositiveInteger} that add 1845positive integers to objects of type \spadtype{Variable(x)} nor 1846are there any in \spadtype{Variable(x)}. 1847Before it can add the two parts, \Language{} must come up with 1848a common type to which both \spad{x} and \spad{1} can be 1849converted. 1850We say that \Language{} must {\it resolve} the two types 1851into a common type. 1852In this example, the common type is \spadtype{Polynomial(Integer)}. 1853 1854\xtc{ 1855Once this is determined, both parts are converted into polynomials, 1856and the addition operation from \spadtype{Polynomial(Integer)} is used to 1857get the answer. 1858}{ 1859\spadcommand{x + 1} 1860} 1861\xtc{ 1862\Language{} can always resolve two types: if nothing resembling 1863the original types can be found, then \spadtype{Any} is be used. 1864\exptypeindex{Any} 1865This is fine and useful in some cases. 1866}{ 1867\spadcommand{["string",3.14159]} 1868} 1869\begin{inputonly} 1870)set message test off 1871\end{inputonly} 1872\xtc{ 1873In other cases objects of type \spadtype{Any} can't be used 1874by the operations you specified. 1875}{ 1876\spadcommand{"string" + 3.14159} 1877} 1878\begin{inputonly} 1879)set message test on 1880\end{inputonly} 1881Although this example was contrived, your expressions may need 1882to be qualified slightly to help \Language{} resolve the 1883types involved. 1884You may need to declare a few variables, do some package calling, 1885provide some target type information or do some explicit 1886conversions. 1887 1888We suggest that you just enter the expression you want evaluated and 1889see what \Language{} does. 1890We think you will be impressed with its ability to ``do what I 1891mean.'' 1892If \Language{} is still being obtuse, give it some hints. 1893As you work with \Language{}, you will learn where it needs a 1894little help to analyze quickly and perform your computations. 1895 1896% ********************************************************************* 1897\head{section}{Exposing Domains and Packages}{ugTypesExpose} 1898% ********************************************************************* 1899 1900In this section we discuss how \Language{} makes some operations 1901available to you while hiding others that are meant to be used by 1902developers or only in rare cases. 1903If you are a new user of \Language{}, it is likely that everything 1904you need is available by default and you may want 1905to skip over this section on first reading. 1906 1907Every 1908\index{constructor!exposed} 1909domain and package in the \Language{} library 1910\index{constructor!hidden} 1911is 1912\index{exposed!constructor} 1913either 1914\spadglossSee{exposed}{expose} (meaning that you can use its operations without doing 1915anything special) or it is {\it hidden} (meaning you have to either 1916package call 1917(see \spadref{ugTypesPkgCall}) 1918the operations it contains or explicitly expose it to use the 1919operations). 1920The initial exposure status for a constructor is set in the 1921file {\bf exposed.lsp} (see the {\it Installer's Note} 1922\index{exposed.lsp @{\bf exposed.lsp}} 1923for \Language{} 1924\index{file!exposed.lsp @{\bf exposed.lsp}} 1925if you need to know the location of this file). 1926Constructors are collected together in 1927\index{group!exposure} 1928{\it exposure groups}. 1929\index{exposure!group} 1930Categories are all in the exposure group ``categories'' and the 1931bulk of the basic set of packages and domains that are exposed 1932are in the exposure group ``basic.'' 1933Here is an abbreviated sample of the file (without the Lisp parentheses): 1934\begin{verbatim} 1935basic 1936 AlgebraicNumber AN 1937 AlgebraGivenByStructuralConstants ALGSC 1938 Any ANY 1939 AnyFunctions1 ANY1 1940 BinaryExpansion BINARY 1941 Boolean BOOLEAN 1942 CardinalNumber CARD 1943 CartesianTensor CARTEN 1944 Character CHAR 1945 CharacterClass CCLASS 1946 CliffordAlgebra CLIF 1947 Color COLOR 1948 Complex COMPLEX 1949 ContinuedFraction CONTFRAC 1950 DecimalExpansion DECIMAL 1951 ... 1952\end{verbatim} 1953\begin{verbatim} 1954categories 1955 AbelianGroup ABELGRP 1956 AbelianMonoid ABELMON 1957 AbelianMonoidRing AMR 1958 AbelianSemiGroup ABELSG 1959 Aggregate AGG 1960 Algebra ALGEBRA 1961 AlgebraicallyClosedField ACF 1962 AlgebraicallyClosedFunctionSpace ACFS 1963 ArcHyperbolicFunctionCategory AHYP 1964 ... 1965\end{verbatim} 1966For each constructor in a group, the full name and the abbreviation 1967is given. 1968There are other groups in {\bf exposed.lsp} but initially only the 1969constructors in exposure groups ``basic'' and ``categories'' are exposed. 1970 1971As an interactive user of \Language{}, you do not need to modify 1972this file. 1973Instead, use \spadsys{)set expose} to expose, hide or query the exposure 1974status of an individual constructor or exposure group. 1975\syscmdindex{set expose} 1976The reason for having exposure groups is to be able to expose or hide 1977multiple constructors with a single command. 1978For example, you might group together into exposure group ``quantum'' a 1979number of domains and packages useful for quantum mechanical computations. 1980These probably should not be available to every user, but you want an easy 1981way to make the whole collection visible to \Language{} when it is looking 1982for operations to apply. 1983 1984If you wanted to hide all the basic constructors available by default, you 1985would issue \spadsys{)set expose drop group basic}. 1986\syscmdindex{set expose drop group} We do not recommend that you do this. 1987If, however, you discover that you have hidden all the basic constructors, 1988you should issue \spadsys{)set expose add group basic} to restore your 1989default environment. 1990\syscmdindex{set expose add group} 1991 1992It is more likely that you would want to expose or hide individual 1993constructors. 1994In \spadref{ugUserTriangle} we use several operations from 1995\spadtype{OutputForm}, a domain usually hidden. 1996To avoid package calling every operation from \spadtype{OutputForm}, we 1997expose the domain and let \Language{} conclude that those operations should 1998be used. 1999Use \spadsys{)set expose add constructor} and \spadsys{)set expose drop 2000constructor} to expose and hide a constructor, respectively. 2001\syscmdindex{set expose drop constructor} 2002You should use the constructor name, not the abbreviation. 2003The \spadsys{)set expose} command guides you through these options. 2004\syscmdindex{set expose add constructor} 2005 2006If you expose a previously hidden constructor, \Language{} 2007exhibits new behavior (that was your intention) though you might not 2008expect the results that you get. 2009\spadtype{OutputForm} is, in fact, one of the worst offenders in this 2010regard. 2011\exptypeindex{OutputForm} 2012This domain is meant to be used by other domains for creating a 2013structure that \Language{} knows how to display. 2014It has functions like \spadopFrom{+}{OutputForm} that form output 2015representations rather than do mathematical calculations. 2016Because of the order in which \Language{} looks at constructors 2017when it is deciding what operation to apply, \spadtype{OutputForm} 2018might be used instead of what you expect. 2019\xtc{ 2020This is a polynomial. 2021}{ 2022\spadcommand{x + x} 2023} 2024\xtc{ 2025Expose \spadtype{OutputForm}. 2026}{ 2027\spadcommand{)set expose add constructor OutputForm \bound{setexposeadd}} 2028} 2029\xtc{ 2030This is what we get when \spadtype{OutputForm} is automatically 2031available. 2032}{ 2033\spadcommand{x + x \free{setexposeadd}} 2034} 2035\xtc{ 2036Hide \spadtype{OutputForm} so we don't run into problems 2037with any later examples! 2038}{ 2039\spadcommand{)set expose drop constructor OutputForm \bound{setexposedrop}} 2040} 2041 2042Finally, exposure is done on a frame-by-frame basis. 2043A \spadgloss{frame} (see \spadref{ugSysCmdframe}) 2044\index{frame!exposure and} 2045is one of possibly several 2046logical \Language{} workspaces within a physical one, each having 2047its own environment (for example, variables and function definitions). 2048If you have several \Language{} workspace windows on your screen, they 2049are all different frames, automatically created for you by \HyperName{}. 2050Frames can be manually created, made active and destroyed by the 2051\spadsys{)frame} system command. 2052\syscmdindex{frame} 2053They do not share exposure information, so you need to use 2054\spadsys{)set expose} in each one to add or drop constructors from view. 2055 2056% ********************************************************************* 2057\head{section}{Commands for Snooping}{ugAvailSnoop} 2058% ********************************************************************* 2059 2060To conclude this chapter, we introduce you to some system commands 2061that you can use for getting more information about domains, 2062packages, categories, and operations. 2063The most powerful \Language{} facility for getting information about 2064constructors and operations is the \Browse{} component of \HyperName{}. 2065This is discussed in \chapref{ugBrowse}. 2066 2067Use the \spadsys{)what} system command to see lists of system objects 2068whose name contain a particular substring (uppercase or lowercase is 2069not significant). 2070\syscmdindex{what} 2071 2072\xtc{ 2073Issue this to see a list of all operations with 2074``{\tt complex}'' in their names. 2075\syscmdindex{what operation} 2076}{ 2077\spadcommand{)what operation complex} 2078} 2079\xtc{ 2080If you want to see all domains with ``{\tt matrix}'' in their names, issue 2081this. 2082\syscmdindex{what domain} 2083}{ 2084\spadcommand{)what domain matrix} 2085} 2086\xtc{ 2087Similarly, if you wish to see all packages whose names contain 2088``{\tt gauss}'', enter this. 2089\syscmdindex{what packages} 2090}{ 2091\spadcommand{)what package gauss} 2092} 2093\xtc{ 2094This command shows all 2095the operations that \spadtype{Any} provides. 2096Wherever \spadSyntax{%} appears, it means ``\spadtype{Any}''. 2097\syscmdindex{show} 2098}{ 2099\spadcommand{)show Any} 2100} 2101\xtc{ 2102This displays all operations with the name \spadfun{complex}. 2103\syscmdindex{display operation} 2104}{ 2105\spadcommand{)display operation complex} 2106} 2107Let's analyze this output. 2108\xtc{ 2109First we find out what some of the abbreviations mean. 2110}{ 2111\spadcommand{)abbreviation query COMPCAT} 2112} 2113\xtc{ 2114}{ 2115\spadcommand{)abbreviation query COMRING} 2116} 2117 2118So if \spad{D1} is a commutative ring (such as the integers or 2119floats) and \spad{D} belongs to 2120\spadtype{ComplexCategory D1}, 2121then there is an operation called \spadfun{complex} that 2122takes two elements of \spad{D1} and creates an element of 2123\spad{D}. 2124The primary example of a constructor implementing domains 2125belonging to \spadtype{ComplexCategory} is \spadtype{Complex}. 2126See \xmpref{Complex} for more information on that and see 2127\spadref{ugUserDeclare} for more information on function types. 2128