=pod =encoding utf8 =head1 NAME Muldis::D::Core::Routines - Muldis D general purpose routines =head1 VERSION This document is Muldis::D::Core::Routines version 0.23.0. =head1 PREFACE This document is part of the Muldis D language specification, whose root document is L; you should read that root document before you read this one, which provides subservient details. Moreover, you should read the L document before this current document, as that forms its own tree beneath a root document branch. =head1 DESCRIPTION This document contains one or more sections that were moved here from L so that that other document would not be too large. =head1 SYSTEM-DEFINED GENERIC UNIVERSAL FUNCTIONS These functions are applicable to values of any data type at all. =over =item C This function results in C iff its 2 arguments are exactly the same value, and C otherwise. This function's arguments must be of compatible declared types; in this case, 2 declared types are compatible iff at least one of the following is true: 1. they are both subtypes of a common scalar root type; 2. they are both subtypes of a common non-incomplete tuple or relation type, that is they essentially have the same headings; 3. at least one type is a generic (eg-C) or incomplete (eg-C) type, and it is a supertype of the other. This function's 2 parameters are mutually commutative. =item C This function is exactly the same as C except that it results in the opposite boolean value when given the same arguments. =item C This function results in C iff the value of its C argument is a member of the data type whose name is given in the C argument, and C otherwise. As trivial cases, this function always results in C if the named type is C, and C if it is C. This function will fail if the named type doesn't exist in the virtual machine. =item C This function results in the value of its C argument, but that the declared type of the result is the not-C data type whose name is given in the C argument. This function will fail if the named type doesn't exist in the virtual machine, or if C isn't a member of the named type. The purpose of C is to permit taking values from a context having a more generic declared type, and using them in a context having a more specific declared type; such an action would otherwise be blocked at compile time due to a type-mismatch error; C causes the type-mismatch validation, and possible failure, to happen at runtime instead, on the actual value rather than declared value. For example, if you are storing an C value in a C-typed variable, using C will cause the compiler to let you use that variable as an argument to C, which it otherwise wouldn't. =item C This function results in the default value of the not-C data type whose name is given in the C argument, and the declared type of the result is that same type. This function will fail if the named type doesn't exist in the virtual machine, either at compile or runtime depending whether the type is in the system or user namespace. This function is conceptually implicitly used to provide default values for variables, so they always hold valid values of their declared type. =back =head1 SYSTEM-DEFINED BOOLEAN-CONCERNING FUNCTIONS These functions select values of the C enumeration. =over =item C This selector function results in the C value. =back These functions implement commonly used boolean operations. =over =item C This function results in the logical I of its argument. =item C This function is a reduction operator that recursively takes each pair of its N input element values and does a logical I (which is both commutative and associative) on them until just one is left, which is the function's result. If C has zero values, then C results in C, which is the identity value for logical I. =item C This function is a reduction operator that recursively takes each pair of its N input element values and does a logical inclusive-or (which is both commutative and associative) on them until just one is left, which is the function's result. If C has zero values, then C results in C, which is the identity value for logical inclusive-or. =item C This function is a reduction operator that recursively takes each pair of its N input element values and does a logical exclusive-or (which is both commutative and associative) on them until just one is left, which is the function's result. If C has zero values, then C results in C, which is the identity value for logical exclusive-or. =back =head1 SYSTEM-DEFINED GENERIC TUPLE-CONCERNING FUNCTIONS These functions are applicable to mainly tuple types, but are generic in that they typically work with any tuple types. =over =item C This function results in the scalar or nonscalar value of the attribute of C whose name is given by C. This function will fail if C specifies an attribute name that C doesn't have. =item C This function results in its C argument but that its attribute whose name is C has been updated with a new scalar or nonscalar value given by C. This function will fail if C specifies an attribute name that C doesn't have, or if the declared type of C isn't a subtype of the declared type of the attribute. =item C This function is like C except that it handles N tuple attributes at once rather than just 1. The heading of the C argument must be a subset of the heading of the C argument; this function's result is C with all the attribute values of C substituted into it. This function could alternately be named I. =item C This function results in a C value that is the same as its C argument but that some of its attributes have different names. Each tuple of the argument C specifies how to rename one C attribute, with the C and C attributes of a C tuple representing the old and new names of a C attribute, respectively. As a trivial case, this function's result is C if C has no tuples. This function supports renaming attributes to each others' names. This function will fail if C specifies any old names that C doesn't have, or any new names that are the same as C attributes that aren't being renamed. =item C This function results in the projection of its C argument that has just the subset of attributes of C which are named in its C argument. As a trivial case, this function's result is C if C lists all attributes of C; or, it is the nullary tuple if C is empty. This function will fail if C specifies any attribute names that C doesn't have. =item C This function is the same as C but that it results in the complementary subset of attributes of C when given the same arguments. =item C This function results in a C value that is the same as its C argument but that some of its attributes have been wrapped up into a new C-typed attribute, which exists in place of the original attributes. The C argument specifies which C attributes are to be removed and wrapped up, and the C argument specifies the name of their replacement attribute. As a trivial case, if C is empty, then the result has all the same attributes as before plus a new tuple-typed attribute of degree zero; or, if C lists all attributes of C, then the result has a single attribute whose value is the same as C. This function supports the new attribute having the same name as an old one being wrapped into it. This function will fail if C specifies any attribute names that C doesn't have, or if C is the same as C attributes that aren't being wrapped. =item C This function is the same as C but that it wraps the complementary subset of attributes of C to those specified by C. =item C This function is the inverse of C, such that it will unwrap a C-type attribute into its member attributes. This function will fail if C specifies any attribute name that C doesn't have, or if an attribute of C is the same as another C attribute. =item C This function is similar to C but that it works with tuples rather than relations. This function is mainly intended for use in connecting tuples that have all disjoint headings, such as for extending one tuple with additional attributes. =back =head1 SYSTEM-DEFINED GENERIC SINGLE INPUT RELATION FUNCTIONS These functions are applicable to mainly relation types, but are generic in that they typically work with any relation types. =over =item C This function results in the cardinality of its argument (that is, the count of tuples its body has). =item C This function results in C iff its C argument matches a tuple of its C argument (that is, iff conceptually C is a member of C), and C otherwise. This function is like C except that the tuple being looked for doesn't have to be wrapped in a relation. This function will fail if the 2 arguments don't have the same heading. =item C This function is exactly the same as C except that it results in the opposite boolean value when given the same arguments. =item C This function results in the C that is the sole member tuple of its argument. This function will fail if its argument does not have exactly one tuple. =item C This function results in the C value those body has just the one C that is its argument. =item C This function results in a C that is the relational union of C and a relation whose sole tuple is C; that is, conceptually the result is C inserted into C. As a trivial case, if C already exists in C, then the result is just C. =item C This function is exactly the same as C except that it will fail if C already exists in C. =item C This function results in a C that is the relational difference from C of a relation whose sole tuple is C; that is, conceptually the result is C deleted from C. As a trivial case, if C already doesn't exist in C, then the result is just C. =item C This function is the same as C but that it operates on and results in a C rather than a C. =item C This function is the same as C but that it operates on and results in a C rather than a C. But note that the result relation will have fewer tuples than C if any C tuples were non-distinct for just the projected attributes. =item C This function is the same as C but that it operates on and results in a C rather than a C. =item C This function is the same as C but that it operates on and results in a C rather than a C, where each of its member tuples was transformed as per C. =item C This function is the same as C but that it operates on and results in a C rather than a C, where each of its member tuples was transformed as per C. =item C This function is the inverse of C as C is to C. But unlike the C variant of C, this current function requires the extra C argument to prevent ambiguity in the general case where C might have zero tuples, because in that situation the most-specific-type of C would be C, and the names of the attributes to add to C in place of C are not known. This function will fail if C has at least 1 tuple and C does not match the names of the attributes of C. =item C This function is similar to C but that the C attribute-wrapping transformations result in new C-typed attributes rather than new C-typed attributes, and moreover multiple C tuples may be combined into fewer tuples whose new C-typed attributes have multiple tuples. This function takes a relation of N tuples and divides the tuples into M groups where all the tuples in a group have the same values in the attributes which aren't being grouped (and distinct values in the attributes that are being grouped); it then results in a new relation of M tuples where the new relation-valued attribute of the result has the tuples of the M groups. A grouped relation contains all of the information in the original relation, but it has less redundancy due to redundant non-grouped attributes now just being represented in one tuple per the multiple tuples whose grouped attributes had them in common. A relation having relation-valued attributes like this is a common way to group so-called child tuples under their parents. As a trivial case, if C is empty, then the result has all the same tuples and attributes as before plus a new relation-typed attribute of degree zero whose value per tuple is of cardinality one; or, if C lists all attributes of C, then the result has a single tuple of a single attribute whose value is the same as C. This function supports the new attribute having the same name as an old one being grouped into it. This function will fail if C specifies any attribute names that C doesn't have, or if C is the same as C attributes that aren't being grouped. =item C This function is the same as C but that it groups the complementary subset of attributes of C to those specified by C. =item C This function is the inverse of C as C is to C; it will ungroup a C-type attribute into its member attributes and tuples. A relation can be first grouped and then that result ungrouped to produce the original relation, with no data loss. However, the ungroup of a relation on a relation-valued attribute will lose the information in any outer relation tuples whose inner relation value has zero tuples; a group on this result won't bring them back. This function will fail if C specifies any attribute name that C doesn't have, or if an attribute of C is the same as another C attribute. =item C This function results in the relational restriction of its C argument as determined by applying the C-resulting function named in its C argument when the latter function is curried by its C argument. The result relation has the same heading as C, and its body contains the subset of C tuples where, for each tuple, the function named by C results in C when passed the tuple as its C argument and C as its C argument. As a trivial case, if C is defined to unconditionally result in C, then this function results simply in C; or, for an unconditional C, this function results in the empty relation with the same heading. Note that this operation is also legitimately known as I. See also the C function, which is a simpler-syntax alternative for C in its typical usage where restrictions are composed simply of anded or ored tests for attribute value equality. =item C This function is the same as C but that it results in the complementary subset of tuples of C when given the same arguments. See also the C function. =item C This function results in the relational extension of its C argument as determined by applying the C-resulting function named in its C argument when the latter function is curried by its C argument. The result relation has a heading that is a superset of that of C, and its body contains the same number of tuples, with all attribute values of C retained, and possibly extra present, determined as follows; for each C tuple, the function named by C results in a second tuple when passed the first tuple as its C argument and C as its C argument; the first and second tuples must have no attribute names in common, and the result tuple is derived by joining (cross-product) the tuples together. As a trivial case, if C is defined to unconditionally result in the degree-zero tuple, then this function results simply in C. =item C This function is a simpler-syntax alternative to both C and C in the typical scenario of extending a relation, given in the C argument, such that every tuple has mutually identical values for each of the new attributes; the new attribute names and common values are given in the C argument. =item C This function provides a convenient context for using aggregate functions to derive a per-group summary relation, which is its result, from another relation, which is its C argument. This function first performs a C on C using C to specify which attributes get grouped into a new relation-valued attribute and which don't; those that don't instead get wrapped into a tuple-valued attribute. Then, per tuple in the main relation, this function applies the C-resulting function named in its C argument when the latter function is curried by its C argument (passed to it as just C); the curried function has, rather than the typical 1 C varying parameter, 2 varying parameters named C and C, which are valued with the relation-valued attribute and tuple-valued attribute, respectively. As per a function that C applies, the function named by C effectively takes a whole post-grouping input tuple and results in a whole tuple; the applied function would directly invoke any N-ary / aggregate operators, and extract their inputs from (or calculate) C as it sees fit. Note that C is not intended to be used to summarize an entire C relation at once (except by chance of it resolving to 1 group); you should instead invoke your summarize-all C directly, or inline it, rather than by way of C, especially if you want a single-tuple result on an empty C (which C) won't do. =back =head1 SYSTEM-DEFINED GENERIC MULTIPLE INPUT RELATION FUNCTIONS These functions are applicable to mainly relation types, but are generic in that they typically work with any relation types. =over =item C This function results in C iff the set of tuples comprising C is a subset of the set of tuples comprising C, and C otherwise. This function will fail if the 2 arguments don't have the same heading. =item C This function is exactly the same as C except that it results in the opposite boolean value when given the same arguments. =item C This function results in the relational union/inclusive-or of the N element values of its argument; it is a reduction operator that recursively takes each pair of input values and relationally unions (which is both commutative and associative) them together until just one is left, which is the result. The result relation has the same heading as all of its inputs, and its body contains every tuple that is in any of the input relations. If C has zero values, then C results in the empty relation with the same heading, which is the per-distinct-heading identity value for relational union. =item C This function is exactly the same as C except that it will fail if any 2 input values have a tuple in common. =item C This function results in the relational intersection/and of the N element values of its argument; it is a reduction operator that recursively takes each pair of input values and relationally intersects (which is both commutative and associative) them together until just one is left, which is the result. The result relation has the same heading as all of its inputs, and its body contains only the tuples that are in every one of the input relations. If C has zero values, then C results in the universal relation with the same heading (that is, the relation having all the tuples that could ever exist in a relation with that heading), which is the per-distinct-heading identity value for relational intersection. Note that this I operator is conceptually a special case of C, applicable when the headings of the inputs are the same, and the other will produce the same result as this when given the same inputs, but with the exception that I has a different identity value when given zero inputs. This function will fail on a C of zero values if the result type's universal relation is impossible or impractically large to represent, such as when any attributes are of infinite types. =item C This function results in the relational difference when its C argument is subtracted from its C argument. The result relation has the same heading as both of its arguments, and its body contains only the tuples that are in C and are not in C. This function will fail if its 2 arguments do not have the same heading. Note that this I operator is conceptually a special case of I, applicable when the headings of the inputs are the same. =item C This function is the same as C but that it results in the complementary subset of tuples of C when given the same arguments. Note that this operation is also legitimately known as I or I. =item C This function results in the relational semijoin of its C and C arguments. The result relation has the same heading as C, and its body contains the subset of C tuples that match those of C as per C. Note that relational semijoin is conceptually a short-hand for first doing an ordinary relational join between its 2 arguments, and then performing a relational projection on all of the attributes that just C has. This function will fail any time that C would fail on the same 2 input relations. =item C This function results in the relational join of the N element values of its argument; it is a reduction operator that recursively takes each pair of input values and relationally joins (which is both commutative and associative) them together until just one is left, which is the result. The result relation has a heading that is a union of all of the headings of its inputs, and its body is the result of first pairwise-matching every tuple of each input relation with every tuple of each other input relation, then where each member of a tuple pair has attribute names in common, eliminating pairs where the values of those attributes differ and unioning the remaining said tuple pairs, then eliminating any result tuples that duplicate others. If C has zero values, then C results in the nullary relation with one tuple, which is the identity value for relational join. As a trivial case, if any input relation has zero tuples, then the function's result will too; or, if any input is the nullary relation with one tuple, that input can be ignored (see identity value); or, if any 2 inputs have no attribute names in common, then the join of just those 2 is a cartesian product; or, if any 2 inputs have all attribute names in common, then the join of just those 2 is an intersection; or, if for 2 inputs, one's set of attribute names is a proper subset of another's, then the join of just those to is a semijoin with the former filtering the latter. This function will fail if any input relations have attributes with common names but different/incompatible declared types. Note that this operation is also legitimately known as I. =item C This function results in the relational cartesian/cross product of the N element values of its argument; it is conceptually a special case of C where all input relations have mutually distinct attribute names; unlike C, C will fail if any inputs have attribute names in common. Note that this operation is also legitimately known as I. =item C This function results in the quotient when its C argument is divided by its C argument using relational division. Speaking informally, say the relations C and C are called C and C, and their attribute sets are respectively named C<{X,Y}> and C<{Y}>, then the result relation has a heading composed of attributes C<{X}> (so the result and C headings are both complementary subsets of the C heading); the result has all tuples C<{X}> such that a tuple C<{X,Y}> appears in C for all tuples C<{Y}> appearing in C; that is, C is shorthand for C. =back =head1 SYSTEM-DEFINED CORE GENERIC QUASI- FUNCTIONS I =head1 SYSTEM-DEFINED CORE CATALOG FUNCTIONS I =head1 SYSTEM-DEFINED CORE UPDATERS =head2 Generic Universal These update operators are applicable to values of any data type at all. =over =item C This update operator will update the variable supplied as its C argument so that it holds the value supplied as its C argument. This update operator's arguments must be of compatible declared types; in this case, C must be a subtype of C. =back =head1 SYSTEM-DEFINED CORE SYSTEM SERVICES These system services are applicable to just one or more specific system-defined core scalar data type. I =head1 SYSTEM-DEFINED CORE PROCEDURES =head2 Generic Control-Flow Procedures These procedures are applicable to use in all kinds of procedures. =over =item C This procedure will throw the exception given as its argument; this results in the call stack unwinding, and transaction rollbacks, until it is caught. =item C This procedure invokes the procedure named in its C argument, giving it the arguments C and C as its C and C arguments, respectively. If the C procedure throws an exception, then any state changes it made roll back (but changes made before that don't), and the call stack unwinds to the C itself; then the procedure named by C is invoked similarly to C was, with corresponding arguments, but with the extra read-only argument C whose value is a C; if the C procedure also throws an exception (such as to say its not handling the thrown one), then that one is not caught and the call stack unwinding plus applicable transaction rollback carries on to the caller of the C. If the C procedure succeeds (doesn't throw an exception), then the C procedure is not called. =back =head1 SYSTEM-DEFINED CORE BOOTLOADER EXCLUSIVES These system services may only be invoked directly by a C routine. =over =item C This system service starts a new child-most transaction. =item C This system service commits the child-most transaction; it will fail if there isn't one. =item C This system service rolls back the child-most transaction; it will fail if there isn't one. =back =head1 SEE ALSO Go to L for the majority of distribution-internal references, and L for the majority of distribution-external references. =head1 AUTHOR Darren Duncan (C) =head1 LICENSE AND COPYRIGHT This file is part of the formal specification of the Muldis D language. Muldis D is Copyright © 2002-2008, Darren Duncan. See the LICENSE AND COPYRIGHT of L for details. =head1 TRADEMARK POLICY The TRADEMARK POLICY in L applies to this file too. =head1 ACKNOWLEDGEMENTS The ACKNOWLEDGEMENTS in L apply to this file too. =cut