Changeset e6f1a4b for doc


Ignore:
Timestamp:
May 19, 2024, 8:07:22 PM (5 months ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
bfcd3af
Parents:
31f4837 (diff), fbc84ca (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

Location:
doc
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/jiada_liang_MMath/background.tex

    r31f4837 re6f1a4b  
    4545Statically initialized identifiers may appear in any constant-expression context, \eg @case@.
    4646Dynamically initialized identifiers may appear as array dimensions in @g++@, which allows variable-sized arrays on the stack.
    47 Again, this form of aliasing to primary constant is not an enumeration.
     47Again, this form of aliasing is not an enumeration.
    4848
    4949
  • doc/theses/jiada_liang_MMath/intro.tex

    r31f4837 re6f1a4b  
    11\chapter{Introduction}
    22
    3 All types in a programming language must have a set of constants, and these constants have \newterm{primary names}, \eg integral types have constants @-1@, @17@, @0xff@, floating-point types have constants @5.3@, @2.3E-5@, @0xff.ffp0@, character types have constants @'a'@, @"abc\n"@, \mbox{\lstinline{u8"}\texttt{\guillemotleft{na\"{i}ve}\guillemotright}\lstinline{"}}, \etc.
    4 Con\-stants can be overloaded among types, \eg @0@ is a null pointer for all pointer types, and the value zero for integral and floating-point types.
    5 (In \CFA, the primary constants @0@ and @1@ can be overloaded for any type.)
    6 Hence, each primary constant has a symbolic name referring to its internal representation, and these names are dictated by language syntax related to types.
    7 In theory, there are an infinite set of primary constant names per type.
    8 
    9 \newterm{Secondary naming} is a common practice in mathematics, engineering and computer science, \eg $\pi$, $\tau$ (2$\pi$), $\phi$ (golden ratio), MB (megabyte, 1E6), and in general situations, \eg specific times (noon, New Years), cities (Big Apple), flowers (Lily), \etc.
    10 Many programming languages capture this important software-engineering capability through a mechanism called \newterm{constant} or \newterm{literal} naming, where a secondary name is aliased to a primary name.
    11 Its purpose is for readability and to eliminate duplication of the primary constant throughout a program.
    12 For example, a meaningful secondary name replaces a primary name throughout a program;
    13 thereafter, changing the binding of the secondary to primary name automatically distributes the rebinding, preventing errors.
    14 In some cases, secondary naming is \newterm{opaque}, where the matching internal representation can be chosen arbitrarily, and only equality operations are available, \eg @O_RDONLY@, @O_WRONLY@, @O_CREAT@, @O_TRUNC@, @O_APPEND@.
    15 Because a secondary name is a constant, it cannot appear in a mutable context, \eg \mbox{$\pi$ \lstinline{= 42}} is meaningless, and a constant has no address, \ie it is an \newterm{rvalue}\footnote{
     3All types in a programming language have a set of constants (symbols), and these constants represent values, \eg integer types have constants @-1@, @17@, @0xff@ representing whole numbers, floating-point types have constants @5.3@, @2.3E-5@, @0xff.ffp0@ representing  real numbers, character types have constants @'a'@, @"abc\n"@, \mbox{\lstinline{u8"}\texttt{\guillemotleft{na\"{i}ve}\guillemotright}\lstinline{"}} representing (human readable) text, \etc.
     4Constants can be overloaded among types, \eg @0@ is a null pointer for all pointer types, and the value zero for integer and floating-point types.
     5(In \CFA, the constants @0@ and @1@ can be overloaded for any type.)
     6A constant's symbolic name is dictated by language syntax related to types.
     7In general, the representation of a constant's value is \newterm{opaque}, so the internal representation can be chosen arbitrarily.
     8In theory, there are an infinite set of constant names per type representing an infinite set of values.
     9
     10It is common in mathematics, engineering and computer science to alias new constants to existing constants so they have the same value, \eg $\pi$, $\tau$ (2$\pi$), $\phi$ (golden ratio), K(k), M, G, T for powers of 2\footnote{Overloaded with SI powers of 10.} often prefixing bits (b) or bytes (B), \eg Gb, MB, and in general situations, \eg specific times (noon, New Years), cities (Big Apple), flowers (Lily), \etc.
     11An alias can bind to another alias, which transitively binds it to the specified constant.
     12Multiple aliases can represent the same value, \eg eighth note and quaver, giving synonyms.
     13
     14Many programming languages capture this important software-engineering capability through a mechanism called \newterm{constant} or \newterm{literal} naming, where a new constant is aliased to an existing constant.
     15Its purpose is for readability, replacing a constant name that directly represents a value with a name that is more symbolic and meaningful in the context of the program.
     16Thereafter, changing the aliasing of the new constant to another constant automatically distributes the rebinding, preventing errors.
     17% and only equality operations are available, \eg @O_RDONLY@, @O_WRONLY@, @O_CREAT@, @O_TRUNC@, @O_APPEND@.
     18Because an aliased name is a constant, it cannot appear in a mutable context, \eg \mbox{$\pi$ \lstinline{= 42}} is meaningless, and a constant has no address, \ie it is an \newterm{rvalue}\footnote{
    1619The term rvalue defines an expression that can only appear on the right-hand side of an assignment expression.}.
    17 
    18 Secondary names can form an (ordered) set, \eg days of a week, months of a year, floors of a building (basement, ground, 1st), colours in a rainbow, \etc.
    19 Many programming languages capture these groupings through a mechanism called an \newterm{enumeration}.
     20In theory, there are an infinite set of possible aliasing, in practice, the number of aliasing per program is finite and small.
     21
     22Aliased constants can form an (ordered) set, \eg days of a week, months of a year, floors of a building (basement, ground, 1st), colours in a rainbow, \etc.
     23In this case, the binding between a constant name and value can be implicit, where the values are chosen to support any set operations.
     24Many programming languages capture the aliasing and ordering through a mechanism called an \newterm{enumeration}.
    2025\begin{quote}
    2126enumerate (verb, transitive).
     
    2429to specify as in a list or catalogue.~\cite{OEDenumerate}
    2530\end{quote}
    26 Within an enumeration set, the enumeration names must be unique, and instances of an enumerated type are \emph{often} restricted to hold only the secondary names.
    27 It is possible to enumerate among set names without having an ordering among the set elements.
     31Within an enumeration set, the enumeration names (aliases) must be unique, and instances of an enumerated type are \emph{often} restricted to hold only these names.
     32
     33It is possible to enumerate among set names without having an ordering among the set values.
    2834For example, the week, the weekdays, the weekend, and every second day of the week.
    2935\begin{cfa}[morekeywords={in}]
     
    3339for ( cursor in Mon, Wed, Fri, Sun } ...                $\C{// every second day of week}\CRT$
    3440\end{cfa}
    35 This independence from internal representation allows multiple names to have the same representation (eighth note, quaver), giving synonyms.
    3641A set can have a partial or total ordering, making it possible to compare set elements, \eg Monday is before Friday and Friday is after.
    3742Ordering allows iterating among the enumeration set using relational operators and advancement, \eg:
     
    3944for ( cursor = Monday; cursor @<=@ Friday; cursor = @succ@( cursor ) ) ...
    4045\end{cfa}
    41 Here the internal representation for the secondary names are logically \emph{generated} rather than listing a subset of names.
     46Here the values for the set names are logically \emph{generated} rather than listing a subset of names.
    4247
    4348Hence, the fundamental aspects of an enumeration are:
     
    4550\item
    4651\begin{sloppypar}
    47 It provides a finite set of secondary names, which become its primary constants.
    48 This differentiates an enumeration from general types with an infinite set
    49 of primary constants.
     52It provides a finite set of new constants, which are implicitly or explicitly assigned values that must be appropriate for any set operations.
     53This aspect differentiates an enumeration from general types with an infinite set of constants.
    5054\end{sloppypar}
    5155\item
    52 The secondary names are constants, which follows transitively from their binding (aliasing) to primary names, which are constants.
     56The alias names are constants, which follows transitively from their binding to other constants.
    5357\item
    5458Defines a type for generating instants (variables).
    5559\item
    56 For safety, an enumeration instance should be restricted to hold only its type's secondary names.
    57 \item
    58 There is a mechanism for \emph{enumerating} over the secondary names, where the ordering can be implicit from the type, explicitly listed, or generated arithmetically.
     60For safety, an enumeration instance should be restricted to hold only its constant names.
     61\item
     62There is a mechanism for \emph{enumerating} over the enumeration names, where the ordering can be implicit from the type, explicitly listed, or generated arithmetically.
    5963\end{enumerate}
    6064
     
    6367\label{s:Terminology}
    6468
    65 The term \newterm{enumeration} defines a type with a set of secondary names, and the term \newterm{enumerator} represents an arbitrary secondary name \see{\VRef{s:CEnumeration} for the name derivation}.
     69The term \newterm{enumeration} defines a type with a set of new constants, and the term \newterm{enumerator} represents an arbitrary alias name \see{\VRef{s:CEnumeration} for the name derivation}.
    6670As well, an enumerated type can have three fundamental properties, \newterm{label}, \newterm{order}, and \newterm{value}.
    6771\begin{cquote}
     
    7680\end{tabular}
    7781\end{cquote}
    78 Here, the enumeration @Week@ defines the enumerator labels @Mon@, @Tue@, @Wed@, @Thu@, @Fri@, @Sat@ and @Sun@.
     82Here, the enumeration @Week@ defines the enumerator constant @Mon@, @Tue@, @Wed@, @Thu@, @Fri@, @Sat@ and @Sun@.
    7983The implicit ordering implies the successor of @Tue@ is @Mon@ and the predecessor of @Tue@ is @Wed@, independent of any associated enumerator values.
    80 The value is the constant represented by the secondary name, which can be implicitly or explicitly set.
     84The value is the implicitly/explicitly assigned constant to support any enumeration operations;
     85the value may be hidden (opaque) or visible.
    8186
    8287Specifying complex ordering is possible:
     
    9499Many programming languages provide an enumeration-like mechanism, which may or may not cover the previous five fundamental enumeration aspects.
    95100Hence, the term \emph{enumeration} can be confusing and misunderstood.
    96 Furthermore, some languages conjoin the enumeration with other type features, making it difficult to tease apart which featuring is being used.
     101Furthermore, some languages conjoin the enumeration with other type features, making it difficult to tease apart which feature is being used.
    97102This section discusses some language features that are sometimes called an enumeration but do not provide all enumeration aspects.
    98103
     
    101106\label{s:Aliasing}
    102107
    103 Some languages provide simple secondary aliasing (renaming), \eg:
     108Some languages provide simple aliasing (renaming), \eg:
    104109\begin{cfa}
    105110const Size = 20, Pi = 3.14159, Name = "Jane";
    106111\end{cfa}
    107 The secondary name is logically replaced in the program text by its corresponding primary name.
    108 Therefore, it is possible to compare the secondary names, \eg @Size < Pi@, only because the primary constants allow it, whereas \eg @Pi < Name@ might be disallowed depending on the language.
     112The alias name is logically replaced in the program text by its matching constant.
     113It is possible to compare aliases, if the constants allow it, \eg @Size < Pi@;
     114whereas \eg @Pi < Name@ might be disallowed depending on the language.
    109115
    110116Aliasing is not macro substitution, \eg @#define Size 20@, where a name is replaced by its value \emph{before} compilation, so the name is invisible to the programming language.
    111 With aliasing, each secondary name is part of the language, and hence, participates fully, such as name overloading in the type system.
     117With aliasing, each new name is part of the language, and hence, participates fully, such as name overloading in the type system.
    112118Aliasing is not an immutable variable, \eg:
    113119\begin{cfa}
     
    121127
    122128Aliasing does provide readability and automatic resubstitution.
    123 It also provides simple enumeration properties, but with extra effort.
     129It also provides simple enumeration properties, but with effort.
    124130\begin{cfa}
    125131const Mon = 1, Tue = 2, Wed = 3, Thu = 4, Fri = 5, Sat = 6, Sun = 7;
     
    148154the ADT has three variants (constructors), @A@, @B@, @C@ with associated types @Int@, @Double@, and @S@.
    149155The constructors create an initialized value of the specific type that is bound to the immutable variables @foo@, @bar@, and @baz@.
    150 Hence, the ADT @Foo@ is like a union containing values of the associated types, and a constructor name is used to access the value using dynamic pattern-matching.
     156Hence, the ADT @Foo@ is like a union containing values of the associated types, and a constructor name is used to intialize and access the value using dynamic pattern-matching.
    151157\begin{cquote}
    152158\setlength{\tabcolsep}{15pt}
     
    175181\end{tabular}
    176182\end{cquote}
    177 For safety, most languages require all assocaited types to be listed or a default case with no field accesses.
     183For safety, most languages require all associated types to be listed or a default case with no field accesses.
    178184
    179185A less frequent case is multiple constructors with the same type.
     
    188194Note, the term \newterm{variant} is often associated with ADTs.
    189195However, there are multiple languages with a @variant@ type that is not an ADT \see{Algol68~\cite{Algol68} or \CC \lstinline{variant}}.
    190 In these languages, the variant is often a union using RTTI tags, which cannot be used to simulate an enumeration.
     196In these languages, the variant is often a union using RTTI tags for discrimination, which cannot be used to simulate an enumeration.
    191197Hence, in this work the term variant is not a synonym for ADT.
    192198
     
    208214data Week = Mon | Tue | Wed | Thu | Fri | Sat | Sun deriving(Enum, Eq, Show)
    209215\end{haskell}
    210 the default type for each constructor is the unit type, and deriving from @Enum@ enforces no other type, @Eq@ allows equality comparison, and @Show@ is for printing.
     216the default type for each constructor is the unit type, and deriving from @Enum@ enforces no other associated types, @Eq@ allows equality comparison, and @Show@ is for printing.
    211217The nullary constructors for the unit types are numbered left-to-right from $0$ to @maxBound@$- 1$, and provides enumerating operations @succ@, @pred@, @enumFrom@ @enumFromTo@.
    212218\VRef[Figure]{f:HaskellEnumeration} shows enumeration comparison and iterating (enumerating).
     
    246252
    247253The key observation is the dichotomy between an ADT and enumeration: the ADT uses the associated type resulting in a union-like data structure, and the enumeration does not use the associated type, and hence, is not a union.
    248 While the enumeration is constructed using the ADT mechanism, it is so restricted it is not really an ADT.
     254While the enumeration is constructed using the ADT mechanism, it is so restricted it is not an ADT.
    249255Furthermore, a general ADT cannot be an enumeration because the constructors generate different values making enumerating meaningless.
    250256While functional programming languages regularly repurpose the ADT type into an enumeration type, this process seems contrived and confusing.
  • doc/theses/jiada_liang_MMath/relatedwork.tex

    r31f4837 re6f1a4b  
    423423
    424424\section{Golang}
     425\label{s:Golang}
    425426
    426427Golang has a no enumeration.
     
    10681069
    10691070Python is a dynamically-typed reflexive programming language with multiple versions, and hence, it is possible to extend existing or build new language features within the language.
    1070 As a result, discussing Python enumerations is a moving target, because if a features does not exist, if can often be created with varying levels of complexity.
    1071 Nevertheless, an attempt has been made to discuss core enumeration features that come with Python 3.13.
     1071As a result, discussing Python enumerations is a moving target, because if a features does not exist, it can often be created with varying levels of complexity within the language.
     1072Nevertheless, the following is a discuss of the core enumeration features that come with Python 3.13.
    10721073
    10731074A Python enumeration type is a set of ordered scoped identifiers (enumerators) bound to \emph{unique} values.
     
    10811082class Week(Enum): Mon = 1; Tue = 2; Wed = 3; Thu = 10; Fri = @auto()@; Sat = 4; Sun = @auto()@
    10821083\end{python}
    1083 where @auto@ increments by 1 from the previous enumerator value.
     1084where @auto@ increments by 1 from the previous enumerator value \see{Golang \lstinline[language=Go]{iota}, \VRef{s:Golang}}.
    10841085Object initialization and assignment are restricted to the enumerators of this type.
    10851086An enumerator initialized with same value is an alias and invisible at the enumeration level, \ie the alias it substituted for its aliasee.
     
    11081109\end{cquote}
    11091110
    1110 As an enumeration is a \lstinline[language=python]{class}, its own methods.
     1111An enumeration \lstinline[language=python]{class} can have methods.
    11111112\begin{python}
    11121113class Week(Enum):
Note: See TracChangeset for help on using the changeset viewer.