Ignore:
Timestamp:
Apr 7, 2017, 6:25:23 PM (7 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
2ccb93c
Parents:
c51b5a3
Message:

thesis conclusions and editting pass

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/rob_thesis/tuples.tex

    rc51b5a3 rf92aa32  
    22\chapter{Tuples}
    33%======================================================================
    4 
    5 \section{Introduction}
    6 % TODO: no passing input parameters by assignment, instead will have reference types => this is not a very C-like model and greatly complicates syntax for likely little gain (and would cause confusion with already supported return-by-reference)
    7 % TODO: benefits (conclusion) by Till: reduced number of variables and statements; no specified order of execution for multiple assignment (more optimzation freedom); can store parameter lists in variable; MRV routines (natural code); more convenient assignment statements; simple and efficient access of record fields; named return values more legible and efficient in use of storage
    84
    95\section{Multiple-Return-Value Functions}
     
    128This restriction results in code which emulates functions with multiple return values by \emph{aggregation} or by \emph{aliasing}.
    139In the former situation, the function designer creates a record type that combines all of the return values into a single type.
    14 For example, consider a function returning the most frequently occuring letter in a string, and its frequency.
    15 % TODO: consider simplifying the example!
    16 %   Two things I like about this example:
    17 %   * it uses different types to illustrate why an array is insufficient (this is not necessary, but is nice)
    18 %   * it's complicated enough to show the uninitialized pitfall that exists in the aliasing example.
    19 %   Still, it may be a touch too complicated. Is there a simpler example with these two properties?
     10For example, consider a function returning the most frequently occurring letter in a string, and its frequency.
     11This example is complex enough to illustrate that an array is insufficient, since arrays are homogeneous, and demonstrates a potential pitfall that exists with aliasing.
    2012\begin{cfacode}
    2113struct mf_ret {
     
    8779The expression resolution phase of the \CFA translator ensures that the correct form is used depending on the values being returned and the return type of the current function.
    8880A multiple-returning function with return type @T@ can return any expression that is implicitly convertible to @T@.
    89 Using the running example, the @most_frequent@ function can be written in using multiple return values as such,
     81Using the running example, the @most_frequent@ function can be written using multiple return values as such,
    9082\begin{cfacode}
    9183[int, char] most_frequent(const char * str) {
     
    282274These semantics allow cascading tuple assignment to work out naturally in any context where a tuple is permitted.
    283275These semantics are a change from the original tuple design in \KWC \cite{Till89}, wherein tuple assignment was a statement that allows cascading assignments as a special case.
    284 The \KWC semantics fix what was seen as a problem with assignment, wherein it can be used in many different locations, such as in function-call argument position. % TODO: remove??
     276Restricting tuple assignment to statements was an attempt to to fix what was seen as a problem with assignment, wherein it can be used in many different locations, such as in function-call argument position.
    285277While permitting assignment as an expression does introduce the potential for subtle complexities, it is impossible to remove assignment expressions from \CFA without affecting backwards compatibility.
    286278Furthermore, there are situations where permitting assignment as an expression improves readability by keeping code succinct and reducing repetition, and complicating the definition of tuple assignment puts a greater cognitive burden on the user.
     
    313305[S, S] z = x.0;        // uses (4), (4), copy constructor
    314306\end{cfacode}
    315 In this example, @x@ is initialized by the multiple constructor calls @?{}(&x.0, 3)@ and @?{}(&x.1, 6.28)@, while @y@ is initilaized by two default constructor calls @?{}(&y.0)@ and @?{}(&y.1)@.
     307In this example, @x@ is initialized by the multiple constructor calls @?{}(&x.0, 3)@ and @?{}(&x.1, 6.28)@, while @y@ is initialized by two default constructor calls @?{}(&y.0)@ and @?{}(&y.1)@.
    316308@z@ is initialized by mass copy constructor calls @?{}(&z.0, x.0)@ and @?{}(&z.1, x.0)@.
    317309Finally, @x@, @y@, and @z@ are destructed, i.e. the calls @^?{}(&x.0)@, @^?{}(&x.1)@, @^?{}(&y.0)@, @^?{}(&y.1)@, @^?{}(&z.0)@, and @^?{}(&z.1)@.
     
    392384z.y;  // ???
    393385\end{cfacode}
    394 One possiblity is for @s.1@ to select the second member of @s@.
     386One possibility is for @s.1@ to select the second member of @s@.
    395387Under this interpretation, it becomes possible to not only access members of a struct by name, but also by position.
    396388Likewise, it seems natural to open this mechanism to enumerations as well, wherein the left side would be a type, rather than an expression.
    397 One benefit of this interpretation is familiar, since it is extremely reminiscent of tuple-index expressions.
     389One benefit of this interpretation is familiarity, since it is extremely reminiscent of tuple-index expressions.
    398390On the other hand, it could be argued that this interpretation is brittle in that changing the order of members or adding new members to a structure becomes a brittle operation.
    399391This problem is less of a concern with tuples, since modifying a tuple affects only the code that directly uses the tuple, whereas modifying a structure has far reaching consequences for every instance of the structure.
    400392
    401 As for @z.y@, a one interpretation is to extend the meaning of member tuple expressions.
     393As for @z.y@, one interpretation is to extend the meaning of member tuple expressions.
    402394That is, currently the tuple must occur as the member, i.e. to the right of the dot.
    403395Allowing tuples to the left of the dot could distribute the member across the elements of the tuple, in much the same way that member tuple expressions distribute the aggregate across the member tuple.
     
    430422p1.0 + p1.1 + p2.0 + p2.1;  // equivalent
    431423\end{cfacode}
    432 In this simpler interpretation, a named tuple type carries with it a list of possibly empty identifiers.
     424In this simpler interpretation, a tuple type carries with it a list of possibly empty identifiers.
    433425This approach fits naturally with the named return-value feature, and would likely go a long way towards implementing it.
    434426
    435 Ultimately, the first two extensions introduce complexity into the model, with relatively little peceived benefit, and so were dropped from consideration.
     427Ultimately, the first two extensions introduce complexity into the model, with relatively little perceived benefit, and so were dropped from consideration.
    436428Named tuples are a potentially useful addition to the language, provided they can be parsed with a reasonable syntax.
    437429
     
    439431\section{Casting}
    440432In C, the cast operator is used to explicitly convert between types.
    441 In \CFA, the cast operator has a secondary use, which is type ascription.
     433In \CFA, the cast operator has a secondary use, which is type ascription, since it force the expression resolution algorithm to choose the lowest cost conversion to the target type.
    442434That is, a cast can be used to select the type of an expression when it is ambiguous, as in the call to an overloaded function.
    443435\begin{cfacode}
     
    515507\end{cfacode}
    516508Note that due to the implicit tuple conversions, this function is not restricted to the addition of two triples.
     509A call to this plus operator type checks as long as a total of 6 non-tuple arguments are passed after flattening, and all of the arguments have a common type that can bind to @T@, with a pairwise @?+?@ over @T@.
    517510For example, these expressions also succeed and produce the same value.
    518 A call to this plus operator type checks as long as a total of 6 non-tuple arguments are passed after flattening, and all of the arguments have a common type that can bind to @T@, with a pairwise @?+?@ over @T@.
    519511\begin{cfacode}
    520512([x.0, x.1]) + ([x.2, 10, 20, 30]);  // x + ([10, 20, 30])
     
    522514\end{cfacode}
    523515This presents a potential problem if structure is important, as these three expressions look like they should have different meanings.
    524 Furthermore, these calls can be made ambiguous by adding seemingly different functions.
     516Furthermore, these calls can be made ambiguous by introducing seemingly different functions.
    525517\begin{cfacode}
    526518forall(otype T | { T ?+?(T, T); })
     
    630622g(h());
    631623\end{cfacode}
    632 Interally, this is converted to psuedo-\CFA
     624Internally, this is converted to pseudo-\CFA
    633625\begin{cfacode}
    634626void g(int, double);
    635627[int, double] h();
    636 lazy [int, double] unq<0> = h();
    637 g(unq<0>.0, unq<0>.1);
     628lazy [int, double] unq0 = h(); // deferred execution
     629g(unq0.0, unq0.1);             // execute h() once
    638630\end{cfacode}
    639631That is, the function @h@ is evaluated lazily and its result is stored for subsequent accesses.
     
    654646Every subsequent evaluation of the unique expression then results in an access to the stored result of the actual expression.
    655647
    656 Currently, the \CFA translator has a very broad, imprecise definition of impurity (side-effects), where any function call is assumed to be impure.
    657 This notion could be made more precise for certain intrinsic, autogenerated, and builtin functions, and could analyze function bodies, when they are available, to recursively detect impurity, to eliminate some unique expressions.
     648Currently, the \CFA translator has a very broad, imprecise definition of impurity (side-effects), where every function call is assumed to be impure.
     649This notion could be made more precise for certain intrinsic, auto-generated, and built-in functions, and could analyze function bodies, when they are available, to recursively detect impurity, to eliminate some unique expressions.
    658650It is possible that lazy evaluation could be exposed to the user through a lazy keyword with little additional effort.
    659651
Note: See TracChangeset for help on using the changeset viewer.