Soft typing

Robert Cartwright, Mike Fagan
2004 SIGPLAN notices  
During the 80's and early 90's, high-level programming languages supported two different type-checking philosophies: static typing, and dynamic typing. Statically typed programming languages ensured that all programs conformed to a set of typing rules, which implied that programs could not "go wrong". Non-conforming programs were rejected. Dynamically typed programming languages, in contrast, did not reject programs, but rather checked for "type errors" during program execution. ML and Scheme
more » ... re the prototypical respresentatives of the static and dynamic approachs to typing. Each typing philosophy had distinct advantages and disadvantages. Static typing does not require as much run-time type checking, and the detection of non-conformance supplies programmers with potentially valuable debugging information. The disadvantage of static typing, however, is that are some well-written, meaningful programs that never generate type errors at run time but do not conform to a given system of static typing rules. Semantic typecorrectness is undecidable. As a result, a programmer must rewrite such a program to conform to the type rules. In contrast, dynamic typing permits all programs to be executed, but fails to detect obvious type errors at compile time. Dynamic typing is more flexible at the cost of failing to detect type errors statically. Soft typing seeks to combine the advantages of static typing with the flexibility of dynamic typing. In other words, it provides the expressiveness of a dynamically typed language but it detects and flags potential type errors statically. In our paper, we focused on a core functional language similar to the functional core of both ML and Scheme, presuming that the non-functional features could be handled by extensions to our type system similar to those used in ML. Our key observation was that a type inference engine could be used to insert dynamic run-time checks where needed and inform a programmer that such checks were necessary. The inference engine, however, did not reject programs. At this stage, we noted that the standard ML Hindley-Milner type inference algorithm could be used as a soft type engine for a Scheme-like functional language. After trying the idea on some typical example programs, however, we noted that many typical Scheme programs would need unnecessary run-time checks. The troublesome programs we considered used union types and recursive types. Consequently, we next investigated ways to include true 20 union types (not discriminated union types as in ML) and recursive types in our soft typing system. The second phase of our soft typing research concerned our effort to add union types and recursive types to a Hindley-Milner type inference engine. Recursive types could be added by using circular unification instead of classical unification. Union types, however, have a subtype relation that seems to be inconsistent with the disjoint class assumption of traditional unification methods. Using a classical mathematical technique called slack variables, we could convert subtype inequality constraints into equality constraints more amenable to traditional unification. In addition, work by Rémy[6] on record types for ML suggested an implementation technique for our slack variable method. WORK THAT INFLUENCED SOFT TYPING The major academic work that influenced our our soft typing work were the classic static typing work on ML of Milner[4], Damas[2], and Tofte[8]. In addition, Rémy[6] extended the ML system to include records by using a slack variable method. The concept of using slack variables for inequalities is one of the fundamental mathematical techniques used in the field of optimization[7]. Finally, our information about circular unification for recursive types came from Colmerauer[1]. CONTRIBUTIONS OF THE PAPER Our 1991 PLDI paper made 2 contributions to the design and implementation of programming languages: 1. The paper introduced the concept of soft typing as an alternative to purely static typing or purely dynamic typing. Soft typing enables static error detection and removal of run-time type checks that pure static typing enjoys. At the same time, soft typing also retains the flexibility and expressiveness of dynamic typing. 2. The paper develops a soft typing algorithm suitable for functional programming languages that is an extension of Hindley-Milner unification-based typing. The soft algorithm we developed extends Hindley-Milner to include union types and recursive types. ACM SIGPLAN 412 Best of PLDI 1979PLDI -1999
doi:10.1145/989393.989435 fatcat:ozhvn4jyffblba5iiwj4j6z3lq