An Approach to Overloading with Polymorphism (original) (raw)
A Type System for Context-dependent Overloading
Electronic Notes in Theoretical Computer Science, 1998
This article presents a type system for context-dependent overloading, based on the notion of constrained types. These are types constrained by the definition of functions or constants of given types. This notion supports both overloading and a form of subtyping, and is related to Haskell type classes[11,2], System O [7] and other systems with constrained types[9,8]. We study an extension
Types, Data Abstraction, and Polymorphism
Our objective is to understand the notion of type in programming languages, present a model of typed, polymorphic programming languages that reflects recent research in type theory, and examine the relevance of recent research to the design of practical programming languages. Object-oriented languages provide both a framework and a motivation for exploring the interaction among the concepts of type, data abstraction, and polymorphism, since they extend the notion of type to data abstraction and since type inheritance is an important form of polymorphism. We develop a X-calculus-based model for type systems that allows us to explore these interactions in a simple setting, unencumbered by complexities of production programming languages. The evolution of languages from untyped universes to monomorphic and then polymorphic type systems is reviewed. Mechanisms for polymorphism such as overloading, coercion, subtyping, and parameterization are examined. A unifying framework for polymorphic type systems is developed in terms of the typed A-calculus augmented to include binding of types by quantification as well as binding of values by abstraction. The typed X-calculus is augmented by universal quantification to model generic functions with type parameters, existential quantification and packaging (information hiding) to model abstract data types, and bounded quantification to model subtypes and type inheritance. In this way we obtain a simple and precise characterization of a powerful type system that includes abstract data types, parametric polymorphism, and multiple inheritance in a single consistent framework. The mechanisms for type checking for the augmented X-calculus are discussed. The augmented typed X-calculus is used as a programming language for a variety of illustrative examples. We christen this language Fun because fun instead of X is the functional abstraction keyword and because it is pleasant to deal with. Fun is mathematically simple and can serve as a basis for the design and implementation of real programming languages with type facilities that are more powerful and expressive than those of existing programming languages. In particular, it provides a basis for the design of strongly typed object-oriented languages.
Function Overloading Implementation in C++ A Static Type of Polymorphism
In this article the function overloading in object oriented programming is elaborated and how they are implemented in C++. The language supports a variety of programming styles. Here we are describing the polymorphism and its types in brief. The main stress is given on the function overloading implementation styles in the language. The polymorphic nature of languages has advantages like that we can add new code without requiring changes to the other classes and interfaces (in Java language) are easily implemented. In this technique, the run-time overhead is also introduced in dynamic binding which increases the execution time of the software. But there are no such types of overheads in this static type of polymorphism because everything is resolved at the time of compile time.
Type Inference for Overloading without Restrictions, Declarations or Annotations
Lecture Notes in Computer Science, 1999
This article presents a type system based on the Damas-Milner system [DM82], that supports overloading. Types of overloaded symbols are constrained polymorphic types. The work is related to Haskell type classes [Wad89,NP93,HHJW96], System O [OWW95] and other type systems with similar forms of constrained types [Kae88,Smi91,Jon94,DCO96]. Restrictions imposed in these systems with respect to overloading are eliminated. User-defined global and local overloading is supported without restrictions. There is no need for declarations or annotations of any sort. No language construct is added in order to cope with overloading. The type system uses a context-dependent overloading policy, which is specified by means of a predicate used in a single inference rule. Overloading of functions defined over different type constructors is supported, as done with Haskell's constructor classes. No monomorphism restriction is required in order to solve ambiguity problems. The system uses an open-world approach, in which new overloaded definitions can be introduced with types automatically reflecting the new definitions. The article also presents a type inference algorithm for the system, which is proved to be sound and to compute principal typings.
First Class Overloading via Insersection Type Parameters⋆
The Hindley-Milner type system imposes the restriction that function parameters must have monomorphic types. Lifting this restric-tion and providing system F "first class" polymorphism is clearly desir-able, but comes with the difficulty that inference of types for system F is undecidable. More practical type systems incorporating types of higher-rank have recently been proposed, that rely on system F but require type annotations for the definition of functions with polymorphic type parameters. However, these type annotations inevitably disallow some possible uses of higher-rank functions. To avoid this problem and to pro-mote code reuse, we explore using intersection types for specifying the types of function parameters that are used polymorphically inside the function body, allowing a flexible use of such functions, on applications to both polymorphic or overloaded arguments.
ACM SIGPLAN Notices, 2002
We present a minimal extension of the Hindley/Milner system to allow for overloading of identifiers. Our approach relies on a combination of the HM(X) type system framework with Constraint Handling Rules (CHRs). CHRs are a declarative language for writing incremental constraint solvers. CHRs allow us to precisely describe the relationships among overloaded identifiers. Under some sufficient conditions on the CHRs
A Calculus for Overload Functions with Subtyping
We present a simple extension of typed -calculus where functions can be overloaded by putting di erent \branches of code" together. When the function is applied, the branch to execute is chosen according to a particular selection rule which depends on the type of the argument. The crucial feature of the present approach is that the branch selection depends on the \run-time type" of the argument, which may di er from its compile-time type, because of the existence of a subtyping relation among types. Hence overloading cannot be eliminated by a static analysis of code, but is an essential feature to be dealt with during computation. We obtain in this way a type-dependent calculus, which di ers from the various -calculi where types do not play any rôle during computation. We prove Con uence and a generalized Subject-Reduction theorem for this calculus. We prove Strong Normalization for a \strati ed" subcalculus. The de nition of this calculus is guided by the understanding of object-oriented features and the connections between our calculus and object-orientedness are extensively stressed. We show that this calculus provides a foundation for typed object-oriented languages which solves some of the problems of the standard record-based approach.
Open and Closed Worlds for Overloading: a Definition and Support for Coexistence
Journal of Universal Computer Science, 2007
The type system of Haskell and some related systems are based on an open world approach for overloading. In an open world, the principal type of each overloaded symbol must be explicitly annotated (in Haskell, annotations occur in type class declarations) and a definition of an overloaded symbol is required to exist only when overloading is resolved. In a closed world, on the other hand, each principal type is determined according to the types of definitions that exist in the relevant context and, furthermore, overloading resolution for an expression considers only the context of the definition of its constituent symbols. In this paper we formally characterize open and closed worlds, and discuss their relative advantages. We present a type system that supports both approaches together, and compare the defined system with Haskell type classes extended with multi-parameter type classes and functional dependencies. We show in particular that functional dependencies are not necessary in order to support multi-parameter type classes, and present an alternative route.
Concept-Controlled Polymorphism
Lecture Notes in Computer Science, 2003
Concepts -sets of abstractions related by common requirementshave a central role in generic programming. This paper proposes a general framework for using concepts to control polymorphism in different ways. First, concepts can be used to constrain parametric polymorphism, as exemplified by type classes in Haskell. Second, concepts can be used to provide fine-grained control of function and operator overloading. Finally, generic functions can be overloaded (specialized) based on concepts, rather than simply on types. We describe a C ++ implementation of a new mechanism, which we call enable if , and its role in concept-controlled polymorphism.
F-Bounded polymorphism for object-oriented programming
Wegner as a means of typing functions that operate uniformly over all subtypes of a given type. They defined a simple "object" model and used bounded quantification to type-check functions that make sense on all objects having a specified set of "attributes." A more realistic presentation of object-oriented languages would allow objects that are elements of recursively-defined types. In this context, bounded quantification no longer serves its intended purpose. It is easy to find functions that makes sense on all objects having a specified set of methods, but which cannot be typed in the Cardelli-Wegner system. To provide a basis for typed polymorphic functions in object-oriented languages, we introduce F-bounded quantification. Some applications of F-bounded quantification are presented and semantic issues are discussed. Although our original motivation was to type polymorphic functions over objects, F-bounded quantification is a general form of polymorphism that seems useful whenever recursive type definitions and subtyping are used.
General Homomorphic Overloading
Lecture Notes in Computer Science, 2005
A general homomorphic overloading in a first-order type system is discussed. Type inference is applied within predefined classes each containing an arbitrary first-order subtyping hierarchy. We propose a computationally efficient type inference algorithm by converting the attendant constraint-satisfaction problem into the algebraic path problem for a constraint graph weighted with elements of a specially constructed non-commutative star semiring. The elements of the semiring are monotonic functions from integers to integers (including ±∞) with pointwise maximum and function composition as semiring operations. The computational efficiency of our method is due to Klene's algebraic path method's cubic complexity. Our algorithm is applicable to type inference in the presence of unknown external types and supports distributed type inference.
Dynamic overloading with copy semantics in object-oriented languages: a formal account
Theoretical Informatics and Applications, 2009
Mainstream object-oriented languages often fail to provide complete powerful features altogether, such as, multiple inheritance, dynamic overloading and copy semantics of inheritance. In this paper we present a core object-oriented imperative language that integrates all these features in a formal framework. We define a static type system and a translation of the language into the meta-language λ object, in order to account for semantic issues and prove type safety of our proposal.
How to make ad-hoc polymorphism less ad hoc
Proceedings of the 16th ACM SIGPLAN-SIGACT symposium on Principles of programming languages - POPL '89, 1989
This paper presents type classes, a new approach to ad-hoc polymorphism. Type classes permit overloading of arithmetic operators such as multiplication, and generalise the "eqtype variables" of Standard ML. Type classes extend the Hindley/Milner polymorphic type system, and provide a new approach to issues that arise in object-oriented programming, bounded type quantification, and abstract data types. This paper provides an informal introduction to type classes, and defines them formally by means of type inference rules.
Objects and polymorphism in system programming languages: a new approach
Periodica Polytechnica Electrical Engineering, 2007
A low-level data structure always has a predefined representation which does not fit into an object of traditional objectoriented languages, where explicit type tag denotes its dynamic type. This is the main reason why the advanced features of object-oriented programming cannot be fully used at the lowest level. On the other hand, the hierarchy of low-level data structures is very similar to class-trees, but instead of an explicit tag-field the value of the object determines its dynamic type. Another peculiar requirement in system programming is that some classes have to be polymorphic by-value with their ancestor: objects must fit into the space of a superclass instance. In our paper we show language constructs which enable the system programmer to handle all data structures as objects, and exploit the advantages of object-oriented programming even at the lowest level. Our solution is based on Predicate Dispatching, but adopted to the special needs of system programming. The techniques we show also allow for some classes to be polymorphic by-value with their super. We also describe how to implement these features without losing modularity.
2000
Overloading allows several function definitions for the same name, distinguished primarily through different argu - ment types; it is typically resolved at compile-time. Inher - itance allows subclasses to define more special versions of the same function; it is typically resolved at run-time. Modern object-oriented languages incorporate both fea- tures, usually in a type-safe manner. However, the combina- tion of
Higher-order+ polymorphic= reusable
1997
This paper explores how certain ideas in object oriented languages have their correspondents in functional languages. In particular we look at the analogue of the iterators of the C++ standard template library. We also give an example of the use of constructor classes which feature in Haskell 1.3 and Gofer.
Code reuse through polymorphic variants
Their support for code reuse has made object-oriented languages popular. However, they do not succeed equally in all areas, particularly when data has a complex structure, making hard to keep the parallel between data and code. On the other hand, functional programming languages, which separate data from code, are better at handling complex structures, but they do not provide direct ways to reuse code for a different datatype. We show here a way to achieve code reuse, even when data and code are separated. The method is illustrated by a detailed example.
A Modular Type System for First-class Composition Inheritance
2000
First-class composition inheritance is a significant improvement over other inheritance techniques with respect to reuse. It allows a class to be used as a component to build other classes, enabling a new kind of reuse. Frequently used characteristics and collaborations, such as bounded values and associations, can be encapsulated in classes and reused conveniently to build other classes. In this
On automatic class insertion with overloading
ACM SIGPLAN Notices, 1996
Several algorithms Cas92, MS89, Run92, DDHL94a, DDHL95, GMM95] have been proposed to automatically insert a class into an inheritance hierarchy. But actual hierarchies all include overriden and overloaded properties that these algorithms handle either very partially or not at all. Partially handled means handled provided there is a separate given function f able to compare overloaded properties DDHL95, GMM95]. In this paper, we describe a new version of our algorithm (named Ares) which handles automatic class insertion more e ciently using such a function f. Although impossible to fully de ne, this function can be computed for a number of well dened cases of overloading and overriding. We give a classi cation of such cases and describe the computation process for a well-de ned set of nontrivial cases. The algorithm preserves these important properties: -preservation of the maximal factorization of properties -preservation of the underlying structure (Galois lattice) of the input hierarchy -conservation of relevant classes of the input hierarchy with their properties.
A first-class approach to genericity
Sigplan Notices, 2003
This paper describes how to add first-class generic typesincluding mixins-to strongly-typed OO languages with nominal subtyping such as Java and C#. A generic type system is "first-class" if generic types can appear in any context where conventional types can appear. In this context, a mixin is simply a generic class that extends one of its type parameters, e.g., a class C that extends T. Although mixins of this form are widely used in C++ (via templates), they are clumsy and error-prone because C++ treats mixins as macros, forcing each mixin instantiation to be separately compiled and type-checked. The abstraction embodied in a mixin is never separately analyzed.