Changeset 8b3109b
- Timestamp:
- May 19, 2025, 11:23:15 AM (4 weeks ago)
- Branches:
- master
- Children:
- 206f4cf
- Parents:
- 4d542db
- Location:
- doc/theses/fangren_yu_MMath
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/fangren_yu_MMath/background.tex
r4d542db r8b3109b 4 4 5 5 ML~\cite{ML} was the first language to support parametric polymorphism. 6 Like \CFA, it supports universal type parameters, but not the use of assertions and traits to constrain type arguments. 7 Haskell~\cite{Haskell10} combines ML-style polymorphism, polymorphic data types, and type inference with the notion of type classes, collections of overloadable methods that correspond in intent to traits in \CFA. 8 Unlike \CFA, Haskell requires an explicit association between types and their classes that specifies the implementation of operations. 9 These associations determine the functions that are assertion arguments for particular combinations of class and type, in contrast to \CFA where the assertion arguments are selected at function call sites based upon the set of operations in scope at that point. 10 Haskell also severely restricts the use of overloading: an overloaded name can only be associated with a single class, and methods with overloaded names can only be defined as part of instance declarations. 6 Like \CFA, it supports unconstrained, universal, type parameters. 7 \CFA differs by adding assertions and traits to constrain type arguments. 8 Haskell~\cite{Haskell10} combines ML-style polymorphism, polymorphic data types, and type inference with the notion of type classes, collections of overloadable methods. 9 The class/type-class association constrain type arguments by indicating the set of functions that become implicit assertion arguments and specify the implementation of these operations. 10 As pointed out \see{\VRef[Figure]{f:ImplicitExplicitTraitInferencing}}, Haskell requires an explicit association between types and constrains (type-class). 11 Otherwise, Haskell does not provide general overloading. 12 \CFA differs by allowing general overloading and constraining type arguments with traits. 13 Most importantly, \CFA automates selection of the assertion arguments at the function call-sites based on the set of operations in scope at that point to generalize reuse. 11 14 12 15 \CC provides three disjoint polymorphic extensions to C: overloading, inheritance, and templates. 13 The overloading is restricted because resolution does not use the return type, inheritance requires learning object-oriented programming and coping with a restricted nominal-inheritance hierarchy, templates cannot be separately compiled resulting in compilation/code bloat and poor error messages, and determining how these mechanisms interact and which to use is confusing. 14 In contrast, \CFA has a single facility for polymorphic code supporting type-safe separate compilation of polymorphic functions and generic (opaque) types, which uniformly leverage the C procedural paradigm. 15 The key mechanism to support separate compilation is \CFA's \emph{explicit} use of assumed type properties. 16 Until \CC concepts~\cite{C++Concepts} are standardized (anticipated for \CCtwenty), \CC provides no way of specifying the requirements of a generic function beyond compilation errors during template expansion; 17 furthermore, \CC concepts are restricted to template polymorphism. 16 Selecting among them and understanding how they interact is part of the challenge in \CC program development. 17 General overloading is available, subtyping inheritance can be single or multiple, templates are typed macro-expansion over a universal type, which precludes separate compilation. 18 Universal template types are constrained using @concept@s~\cite{C++Concepts}, but only as a guideline, as the template expansion can still discover additional constrains. 19 Template expansion can result in code bloat and poor error messages. 20 Type inferencing is available using \lstinline[language=C++]{auto}, precluding using the return type for overload selection. 21 \CFA differs by providing a simplified, uniform facility for polymorphic code, eliminating subtyping polymorphic, and encompassing overloading among parametric functions and universal (generic) types, all of which are separately compilable. 22 Overload resolution uses the return type and arithmetic conversions to make precise function selections versus generating ambiguities, at the cost of type inferencing. 23 Both \CFA call-site inferencing and \CC template expansion search in the local environment to satisfy explicit assertions or to find named functions to complete the template, respectively. 18 24 19 25 Cyclone~\cite{Grossman06} also provides capabilities for polymorphic functions and existential types, similar to \CFA's @forall@ functions and generic types. 20 Cyclone existential types can include function pointers in a construct similar to a virtual function table, but these pointers must be explicitly initialized at some point in the code, which is a tedious and potentially error-prone process.26 Cyclone's existential types can include function pointers in a construct similar to a virtual function table, but these pointers must be explicitly initialized at some point in the code, which is potentially error prone. 21 27 Furthermore, Cyclone's polymorphic functions and types are restricted to abstraction over types with the same layout and calling convention as @void *@, \ie only pointer types and @int@. 22 In \CFA terms, all Cyclone polymorphism must be dtype-static. 23 While the Cyclone design provides the efficiency benefits discussed in~\VRef{s:GenericImplementation} for dtype-static polymorphism, it is more restrictive than \CFA's general model. 24 Smith and Volpano~\cite{Smith98} present Polymorphic C, an ML dialect with polymorphic functions, C-like syntax, and pointer types; 25 it lacks many of C's features, most notably structure types, and hence, is not a practical C replacement. 28 In \CFA terms, all Cyclone polymorphism must be an incomplete data-type @forall( T * )@ \see{\VRef{s:PolymorphicFunction}}, which provides the efficiency benefits of fixed-size types \see{\VPageref{s:GenericImplementation}}. 29 \CFA differs by adding object and variadic kinds of polymorphism, which provide more expressive reuse forms. 30 Smith and Volpano~\cite{Smith98} present Polymorphic C, an ML dialect with polymorphic functions, C-like syntax, and pointer types. 31 While these language purport to be C replacements, they are significantly different from C, and hence, not a practical C replacement. 32 \CFA differs in providing better imperative-style polymorphism, while retaining backwards syntax and semantic compatibility with C. 26 33 27 Objective-C~\cite{obj-c-book} is an industrially successful extension to C. 28 However, Objective-C is a radical departure from C, using an object-oriented model with message passing. 29 Objective-C did not support type-checked generics until recently \cite{xcode7}, historically using less-efficient runtime checking of object types. 30 The GObject~\cite{GObject} framework also adds object-oriented programming with runtime type-checking and reference-counting garbage collection to C; 31 these features are more intrusive additions than those provided by \CFA, in addition to the runtime overhead of reference counting. 32 Vala~\cite{Vala} compiles to GObject-based C, adding the burden of learning a separate language syntax to the aforementioned demerits of GObject as a modernization path for existing C code bases. 33 Java~\cite{Java8} included generic types in Java~5, which are type checked at compilation and type erased at runtime, similar to \CFA's. 34 However, in Java, each object carries its own table of method pointers, whereas \CFA passes the method pointers separately to maintain a C-compatible layout. 35 Java is also a garbage-collected, object-oriented language, with the associated resource usage and C-interoperability burdens. 34 Objective-C~\cite{obj-c-book} and its successor Swift~\cite{Swift} are used in iOS phone applications. 35 Swift has polymorphic functions and generics, object subtyping, traits via @extensions@, general function/method overloading, including parameter names and return type. 36 Objective-C communication is via message passing rather than function call, while swift uses function call unless interacting with Objective-C. 37 Swift and \CFA's type-systems are very similar, minus the object-oriented subtyping in \CFA, which is felt to be unnecessary. 38 The GObject~\cite{GObject} framework adds object-oriented programming to C with runtime type-checking and reference-counting garbage collection, as a modernization path for existing C code-bases. 39 Vala~\cite{Vala} compiles to GObject-based C, but requires additional language syntax over GObject. 40 \CFA's path for modernizing works with the existing C type system and runtime, \ie not adding object-oriented types or garbage collection. 41 Java~\cite{Java8} has object-oriented subtyping, generic @interface@s that act like traits, which are type checked at compilation and type erased at runtime similar to \CFA's, and general overloading on methods. 42 However, in Java, each object carries its own table of method pointers, whereas \CFA passes trait pointers at call-site maintaining a C-compatible layout. 43 Java is also garbage-collected. 36 44 37 D~\cite{D}, Go, and Rust~\cite{Rust} are modern compiled languages with abstraction features similar to \CFA traits, \emph{interfaces} in D and Go, and \emph{traits} in Rust. 38 However, each language represents a significant departure from C in terms of language model, and none has the same level of compatibility with C as \CFA. 39 D and Go are garbage-collected languages, imposing the associated runtime overhead. 40 The necessity of accounting for data transfer between managed runtimes and the unmanaged C runtime complicates foreign-function interfaces to C. 41 Furthermore, while generic types and functions are available in Go, they are limited to a small fixed set provided by the compiler, with no language facility to define more. 42 D restricts garbage collection to its own heap by default, whereas Rust is not garbage collected and, thus, has a lighter-weight runtime more interoperable with C. 43 Rust also possesses much more powerful abstraction capabilities for writing generic code than Go. 44 On the other hand, Rust's borrow checker provides strong safety guarantees but is complex and difficult to learn and imposes a distinctly idiomatic programming style. 45 \CFA, with its more modest safety features, allows direct ports of C code while maintaining the idiomatic style of the original source. 45 D~\cite{D}, Go, and Rust~\cite{Rust} are compiled languages with generic functions and types using traits similar to \CFA: \emph{interfaces} in D and Go, and \emph{traits} in Rust. 46 D and Go are garbage-collected languages; 47 Rust is not garbage collected. 48 Go's generic types and functions are limited to a small fixed-set provided by the compiler, with no language facility to define more. 49 Rust also possesses more powerful abstraction capabilities for writing generic code than Go. 50 While Rust's borrow checker provides strong safety guarantees, it is complex and difficult to learn and imposes a distinctly idiomatic programming style different than C. 51 \CFA, with its modest safety features, has a comparable type-system to Rust's, while maintaining C backwards compatibility, providing a modernization path for existing C code-bases. 46 52 47 53 48 \section{Tuples/ variadics}54 \section{Tuples/Variadics} 49 55 50 \vspace*{-5pt}51 56 Many programming languages have some form of tuple construct and/or variadic functions, \eg SETL, C, KW-C, \CC, D, Go, Java, ML, and Scala. 52 57 SETL~\cite{SETL} is a high-level mathematical programming language, with tuples being one of the primary data types. 53 58 Tuples in SETL allow subscripting, dynamic expansion, and multiple assignment. 59 KW-C~\cite{Buhr94a}, a predecessor of \CFA, introduced tuples to C as an extension of the C syntax, taking much of its inspiration from SETL. 60 This work added multiple return value functions (MRVF), tuple mass and multiple assignment, and record-member access, giving unstructured tuples. 61 Structured tuples (tuple variables) are also introduced including multiple coercions between structured and unstructured tuples. 62 Like \CC, D provides tuples through a library variadic-template structure. 63 Go does not have tuples but supports MRVF. 64 Tuples are a fundamental abstraction in most functional programming languages, such as Standard ML~\cite{sml}, Haskell, and Scala~\cite{Scala}, which decompose tuples using pattern matching. 65 From KW-C unstructured tuples, \CFA took MRVF, mass and multiple assignment, and record-member access. 66 While \CFA has some structured-tuple capabilities, \VRef{s:TupleImplementation}, my analysis suggests this feature might be removed. 67 68 An alternative to a tuple type is of variadic (variable argument) functions or type. 54 69 C provides variadic functions through @va_list@ objects, but the programmer is responsible for managing the number of arguments and their types; 55 70 thus, the mechanism is type unsafe. 56 KW-C~\cite{Buhr94a}, a predecessor of \CFA, introduced tuples to C as an extension of the C syntax, taking much of its inspiration from SETL. 57 The main contributions of that work were adding MRVF, tuple mass and multiple assignment, and record-member access. 58 \CCeleven introduced @std::tuple@ as a library variadic-template structure. 59 Tuples are a generalization of @std::pair@, in that they allow for arbitrary length, fixed-size aggregation of heterogeneous values. 71 \CC{11} introduced @std::tuple@ as a library variadic-template structure. 72 Tuples are a generalization of @std::pair@ allowing for arbitrary length, fixed-size aggregation of heterogeneous values. 60 73 Operations include @std::get<N>@ to extract values, @std::tie@ to create a tuple of references used for assignment, and lexicographic comparisons. 61 \CC seventeenproposes \emph{structured bindings}~\cite{Sutter15} to eliminate predeclaring variables and the use of @std::tie@ for binding the results.74 \CC{17} proposes \emph{structured bindings}~\cite{Sutter15} to eliminate predeclaring variables and the use of @std::tie@ for binding the results. 62 75 This extension requires the use of @auto@ to infer the types of the new variables; hence, complicated expressions with a nonobvious type must be documented with some other mechanism. 63 76 Furthermore, structured bindings are not a full replacement for @std::tie@, as it always declares new variables. 64 Like \CC, D provides tuples through a library variadic-template structure. 65 Go does not have tuples but supports MRVF. 66 Java's variadic functions appear similar to C's but are type safe using homogeneous arrays, which are less useful than \CFA's heterogeneously typed variadic functions. 67 Tuples are a fundamental abstraction in most functional programming languages, such as Standard ML~\cite{sml}, Haskell, and Scala~\cite{Scala}, which decompose tuples using pattern matching. 77 Java's variadic functions appear similar to C's but are type safe using homogeneous arrays. 78 \CFA's heterogeneous variadic-functions \see{\VPageref{p:VariadicFunctions}} provide a type-safe version of C variadic, although limited in features, which fits into the overall \CFA type-system design. 68 79 69 80 70 81 \section{Type Resolution} 71 82 72 \CFA expression resolution must deal with extensive overloading and inference of polymorphic types with assertions.73 The goal is to keep the base algorithm used in type unification simple enough so resolving a complicated expression can still be done reasonably fast.74 The following is work that handles the aforementioned bidirectional subtyping relations concisely.83 % \CFA expression resolution must deal with extensive overloading and inference of polymorphic types with assertions. 84 % The goal is to keep the base algorithm used in type unification simple enough so resolving a complicated expression can still be done reasonably fast. 85 % The following is work that handles the aforementioned bidirectional subtyping relations concisely. 75 86 76 Melo~\etal~\cite{Melo17} developed PsycheC,a tool built for inferencing missing type and variable declarations of incomplete C programs, which can also be viewed as a dialect of C with type inferencing.87 Psyche-C~\cite{Melo17} is a tool built for inferencing missing type and variable declarations of incomplete C programs, which can also be viewed as a dialect of C with type inferencing. 77 88 As PsycheC is built for analyzing standard C programs, it does not have any kind of overloading or polymorphism. 78 89 Instead, all top-level variables and function parameters may have indeterminate types. -
doc/theses/fangren_yu_MMath/features.tex
r4d542db r8b3109b 303 303 304 304 \section{Tuple Implementation} 305 \label{s:TupleImplementation} 305 306 306 307 As noted, traditional languages manipulate multiple values by in/out parameters and/or structures. … … 504 505 \end{comment} 505 506 507 \label{p:VariadicFunctions} 506 508 Finally, a type-safe variadic argument signature was added by Robert Schluntz~\cite[\S~4.1.2]{Schluntz17} using @forall@ and a new tuple parameter-type, denoted by the keyword @ttype@ in Schluntz's implementation, but changed to the ellipsis syntax similar to \CC's template parameter pack. 507 509 For C variadics, \eg @va_list@, the number and types of the arguments must be conveyed in some way, \eg @printf@ uses a format string indicating the number and types of the arguments. -
doc/theses/fangren_yu_MMath/intro.tex
r4d542db r8b3109b 711 711 712 712 \subsection{Polymorphic Function} 713 \label{s:PolymorphicFunction} 713 714 714 715 The signature feature of the \CFA type-system is parametric-polymorphic functions~\cite{forceone:impl,Cormack90,Duggan96}, generalized using a @forall@ clause (giving the language its name).
Note: See TracChangeset
for help on using the changeset viewer.