Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/rob_thesis/intro.tex

    r0eb18557 rf92aa32  
    1616Therefore, these design principles must be kept in mind throughout the design and development of new language features.
    1717In order to appeal to existing C programmers, great care must be taken to ensure that new features naturally feel like C.
    18 These goals ensure existing C code-bases can be converted to \CFA incrementally with minimal effort, and C programmers can productively generate \CFA code without training beyond the features being used.
    19 Unfortunately, \CC is actively diverging from C, so incremental additions require significant effort and training, coupled with multiple legacy design-choices that cannot be updated.
    20 
    2118The remainder of this section describes some of the important new features that currently exist in \CFA, to give the reader the necessary context in which the new features presented in this thesis must dovetail.
    2219
     
    5653\end{cfacode}
    5754Compound literals create an unnamed object, and result in an lvalue, so it is legal to assign a value into a compound literal or to take its address \cite[p.~86]{C11}.
    58 Syntactically, compound literals look like a cast operator followed by a brace-enclosed initializer, but semantically are different from a C cast, which only applies basic conversions and coercions and is never an lvalue.
     55Syntactically, compound literals look like a cast operator followed by a brace-enclosed initializer, but semantically are different from a C cast, which only applies basic conversions and is never an lvalue.
    5956
    6057\subsection{Overloading}
     
    6259Overloading is the ability to specify multiple entities with the same name.
    6360The most common form of overloading is function overloading, wherein multiple functions can be defined with the same name, but with different signatures.
    64 C provides a small amount of built-in overloading, \eg + is overloaded for the basic types.
    65 Like in \CC, \CFA allows user-defined overloading based both on the number of parameters and on the types of parameters.
     61Like in \CC, \CFA allows overloading based both on the number of parameters and on the types of parameters.
    6662  \begin{cfacode}
    6763  void f(void);  // (1)
     
    9692There are times when a function should logically return multiple values.
    9793Since a function in standard C can only return a single value, a programmer must either take in additional return values by address, or the function's designer must create a wrapper structure to package multiple return-values.
    98 For example, the first approach:
    9994\begin{cfacode}
    10095int f(int * ret) {        // returns a value through parameter ret
     
    106101int res1 = g(&res2);      // explicitly pass storage
    107102\end{cfacode}
    108 is awkward because it requires the caller to explicitly allocate memory for $n$ result variables, even if they are only temporary values used as a subexpression, or even not used at all.
    109 The second approach:
     103The former solution is awkward because it requires the caller to explicitly allocate memory for $n$ result variables, even if they are only temporary values used as a subexpression, or even not used at all.
     104The latter approach:
    110105\begin{cfacode}
    111106struct A {
     
    118113... res3.x ... res3.y ... // use result values
    119114\end{cfacode}
    120 is awkward because the caller has to either learn the field names of the structure or learn the names of helper routines to access the individual return values.
    121 Both approaches are syntactically unnatural.
     115requires the caller to either learn the field names of the structure or learn the names of helper routines to access the individual return values.
     116Both solutions are syntactically unnatural.
    122117
    123118In \CFA, it is possible to directly declare a function returning multiple values.
     
    170165  \begin{cfacode}
    171166  struct A { int i; };
    172   int ?+?(A x, A y);    // '?'s represent operands
     167  int ?+?(A x, A y);
    173168  bool ?<?(A x, A y);
    174169  \end{cfacode}
    175170Notably, the only difference is syntax.
    176171Most of the operators supported by \CC for operator overloading are also supported in \CFA.
    177 Of notable exception are the logical operators (\eg @||@), the sequence operator (\ie @,@), and the member-access operators (\eg @.@ and \lstinline{->}).
     172Of notable exception are the logical operators (e.g. @||@), the sequence operator (i.e. @,@), and the member-access operators (e.g. @.@ and \lstinline{->}).
    178173
    179174Finally, \CFA also permits overloading variable identifiers.
     
    248243  template<typename T>
    249244  T sum(T *arr, int n) {
    250     T t;  // default construct => 0
     245    T t;
    251246    for (; n > 0; n--) t += arr[n-1];
    252247    return t;
     
    266261  \end{cfacode}
    267262The first thing to note here is that immediately following the declaration of @otype T@ is a list of \emph{type assertions} that specify restrictions on acceptable choices of @T@.
    268 In particular, the assertions above specify that there must be an assignment from \zero to @T@ and an addition assignment operator from @T@ to @T@.
     263In particular, the assertions above specify that there must be a an assignment from \zero to @T@ and an addition assignment operator from @T@ to @T@.
    269264The existence of an assignment operator from @T@ to @T@ and the ability to create an object of type @T@ are assumed implicitly by declaring @T@ with the @otype@ type-class.
    270265In addition to @otype@, there are currently two other type-classes.
     
    286281A major difference between the approaches of \CC and \CFA to polymorphism is that the set of assumed properties for a type is \emph{explicit} in \CFA.
    287282One of the major limiting factors of \CC's approach is that templates cannot be separately compiled.
    288 In contrast, the explicit nature of assertions allows \CFA's polymorphic functions to be separately compiled, as the function prototype states all necessary requirements separate from the implementation.
    289 For example, the prototype for the previous sum function is
    290   \begin{cfacode}
    291   forall(otype T | **R**{ T ?=?(T *, zero_t); T ?+=?(T *, T); }**R**)
    292   T sum(T *arr, int n);
    293   \end{cfacode}
    294 With this prototype, a caller in another translation unit knows all of the constraints on @T@, and thus knows all of the operations that need to be made available to @sum@.
     283In contrast, the explicit nature of assertions allows \CFA's polymorphic functions to be separately compiled.
    295284
    296285In \CFA, a set of assertions can be factored into a \emph{trait}.
     
    307296This capability allows specifying the same set of assertions in multiple locations, without the repetition and likelihood of mistakes that come with manually writing them out for each function declaration.
    308297
    309 An interesting application of return-type resolution and polymorphism is a type-safe version of @malloc@.
     298An interesting application of return-type resolution and polymorphism is with type-safe @malloc@.
    310299\begin{cfacode}
    311300forall(dtype T | sized(T))
     
    327316
    328317In object-oriented programming languages, type invariants are typically established in a constructor and maintained throughout the object's lifetime.
    329 These assertions are typically achieved through a combination of access-control modifiers and a restricted interface.
     318These assertions are typically achieved through a combination of access control modifiers and a restricted interface.
    330319Typically, data which requires the maintenance of an invariant is hidden from external sources using the \emph{private} modifier, which restricts reads and writes to a select set of trusted routines, including member functions.
    331320It is these trusted routines that perform all modifications to internal data in a way that is consistent with the invariant, by ensuring that the invariant holds true at the end of the routine call.
     
    399388In other languages, a hybrid situation exists where resources escape the allocation block, but ownership is precisely controlled by the language.
    400389This pattern requires a strict interface and protocol for a data structure, consisting of a pre-initialization and a post-termination call, and all intervening access is done via interface routines.
    401 This kind of encapsulation is popular in object-oriented programming languages, and like the stack, it takes care of a significant portion of resource-management cases.
     390This kind of encapsulation is popular in object-oriented programming languages, and like the stack, it takes care of a significant portion of resource management cases.
    402391
    403392For example, \CC directly supports this pattern through class types and an idiom known as RAII \footnote{Resource Acquisition is Initialization} by means of constructors and destructors.
     
    410399In the context of \CFA, a non-trivial constructor is either a user defined constructor or an auto-generated constructor that calls a non-trivial constructor.
    411400
    412 For the remaining resource ownership cases, a programmer must follow a brittle, explicit protocol for freeing resources or an implicit protocol enforced by the programming language.
     401For the remaining resource ownership cases, programmer must follow a brittle, explicit protocol for freeing resources or an implicit protocol implemented via the programming language.
    413402
    414403In garbage collected languages, such as Java, resources are largely managed by the garbage collector.
    415 Still, garbage collectors typically focus only on memory management.
     404Still, garbage collectors are typically focus only on memory management.
    416405There are many kinds of resources that the garbage collector does not understand, such as sockets, open files, and database connections.
    417406In particular, Java supports \emph{finalizers}, which are similar to destructors.
    418 Unfortunately, finalizers are only guaranteed to be called before an object is reclaimed by the garbage collector \cite[p.~373]{Java8}, which may not happen if memory use is not contentious.
     407Sadly, finalizers are only guaranteed to be called before an object is reclaimed by the garbage collector \cite[p.~373]{Java8}, which may not happen if memory use is not contentious.
    419408Due to operating-system resource-limits, this is unacceptable for many long running programs.
    420409Instead, the paradigm in Java requires programmers to manually keep track of all resources \emph{except} memory, leading many novices and experts alike to forget to close files, etc.
     
    461450\end{javacode}
    462451Variables declared as part of a try-with-resources statement must conform to the @AutoClosable@ interface, and the compiler implicitly calls @close@ on each of the variables at the end of the block.
    463 Depending on when the exception is raised, both @out@ and @log@ are null, @log@ is null, or both are non-null, therefore, the cleanup for these variables at the end is automatically guarded and conditionally executed to prevent null-pointer exceptions.
     452Depending on when the exception is raised, both @out@ and @log@ are null, @log@ is null, or both are non-null, therefore, the cleanup for these variables at the end is appropriately guarded and conditionally executed to prevent null-pointer exceptions.
    464453
    465454While Rust \cite{Rust} does not enforce the use of a garbage collector, it does provide a manual memory management environment, with a strict ownership model that automatically frees allocated memory and prevents common memory management errors.
     
    497486There is no runtime cost imposed on these restrictions, since they are enforced at compile-time.
    498487
    499 Rust provides RAII through the @Drop@ trait, allowing arbitrary code to execute when the object goes out of scope, providing automatic clean up of auxiliary resources, much like a \CC program.
     488Rust provides RAII through the @Drop@ trait, allowing arbitrary code to execute when the object goes out of scope, allowing Rust programs to automatically clean up auxiliary resources much like a \CC program.
    500489\begin{rustcode}
    501490struct S {
     
    504493
    505494impl Drop for S {  // RAII for S
    506   fn drop(&mut self) {  // destructor
     495  fn drop(&mut self) {
    507496    println!("dropped {}", self.name);
    508497  }
     
    569558tuple<int, int, int> triple(10, 20, 30);
    570559auto & [t1, t2, t3] = triple;
    571 t2 = 0; // changes middle element of triple
     560t2 = 0; // changes triple
    572561
    573562struct S { int x; double y; };
     
    575564auto [x, y] = s; // unpack s
    576565\end{cppcode}
    577 Structured bindings allow unpacking any structure with all public non-static data members into fresh local variables.
     566Structured bindings allow unpacking any struct with all public non-static data members into fresh local variables.
    578567The use of @&@ allows declaring new variables as references, which is something that cannot be done with @std::tie@, since \CC references do not support rebinding.
    579568This extension requires the use of @auto@ to infer the types of the new variables, so complicated expressions with a non-obvious type must be documented with some other mechanism.
    580569Furthermore, structured bindings are not a full replacement for @std::tie@, as it always declares new variables.
    581570
    582 Like \CC, D provides tuples through a library variadic-template structure.
     571Like \CC, D provides tuples through a library variadic template struct.
    583572In D, it is possible to name the fields of a tuple type, which creates a distinct type.
    584573% http://dlang.org/phobos/std_typecons.html
     
    611600\end{smlcode}
    612601Here, the function @binco@ appears to take 2 arguments, but it actually takes a single argument which is implicitly decomposed via pattern matching.
    613 Tuples are a foundational tool in SML, allowing the creation of arbitrarily-complex structured data-types.
     602Tuples are a foundational tool in SML, allowing the creation of arbitrarily complex structured data types.
    614603
    615604Scala, like \CC, provides tuple types through the standard library \cite{Scala}.
     
    664653Since the variadic arguments are untyped, it is up to the function to interpret any data that is passed in.
    665654Additionally, the interface to manipulate @va_list@ objects is essentially limited to advancing to the next argument, without any built-in facility to determine when the last argument is read.
    666 This limitation requires the use of an \emph{argument descriptor} to pass information to the function about the structure of the argument list, including the number of arguments and their types.
     655This requires the use of an \emph{argument descriptor} to pass information to the function about the structure of the argument list, including the number of arguments and their types.
    667656The format string in @printf@ is one such example of an argument descriptor.
    668657\begin{cfacode}
Note: See TracChangeset for help on using the changeset viewer.