Flow-sensitive typing

From HandWiki

In programming language theory, flow-sensitive typing (also called flow typing or occurrence typing) is a type system where the type of an expression depends on its position in the control flow.

In statically typed languages, a type of an expression is determined by the types of the sub-expressions that compose it. However, in flow-sensitive typing, an expression's type may be updated to a more specific type if it follows an operation that validates its type. Validating operations can include type predicates, imperative updates, and control flow.

Examples

Ceylon

See the following example in Ceylon which illustrates the concept:

// Object? means the variable "name" is of type Object or else null
void hello(Object? name) {
    if (is String name) {
        // "name" now has type String in this block
        print("Hello, ``name``!");
        // and it is possible to call String methods on the variable
        print(" String.size is ``name.size``");
    }
    else if (exists name) {
        // "name" now has type Object in this block
        print("Hello, object ``name``!");
    }
    else {
        print("Hello, world!");
    }
}

          
hello(null);
hello(1);
hello("John Doe");

and which outputs:

Hello, world!
Hello, object 1!
Hello, John Doe!
 String.size is 8

Kotlin

See this example in Kotlin:

fun hello(obj: Any) {
    // A type cast fails if `obj` is not a String
    obj as String

    // Since the type cast did not fail, `obj` must be a String!
    val l = obj.length

    println("'$obj' is a string of length $l")
}
          
hello("Mooooo")

Benefits

This technique coupled with type inference reduces the need for writing type annotations for all variables or to do type casting, like is seen with dynamic languages that use duck typing. It reduces verbosity and makes for terser code, easier to read and modify.

It can also help language implementers provide implementations that execute dynamic languages faster by predicting the type of objects statically.[1]

Finally, it increases type safety and can prevent problems due to null pointers, labeled by C.A.R. Hoare—the null reference inventor—as "the billion dollar mistake"[2]

Implementations

Typed Scheme, a type system for Scheme, was the first type system with this feature.[3] Its successor, Typed Racket (a dialect of Racket), still makes extensive use of occurrence typing.[4] Shortly after Typed Scheme, David J. Pearce independently reinvented flow-typing in Whiley.[5][6]

Typed JavaScript observed that in "scripting" languages, flow-typing depends on more than conditional predicates; it also depends on state and control flow.[7] This style has since been adopted in languages like Ceylon,[8] TypeScript[9] and Facebook Flow.[10]

There are also a few languages that don't have union types but do have nullable types, that have a limited form of this feature that only applies to nullable types, such as C#,[11] Kotlin,[12][13] and Lobster.[14]

Alternatives

Pattern matching reaches the same goals as flow-sensitive typing, namely reducing verbosity and making up for terser code, easier to read and modify. It achieves this is in a different way, it allows to match the type of a structure, extract data out of it at the same time by declaring new variable. As such, it reduces the ceremony around type casting and value extraction. Pattern matching works best when used in conjunction with algebraic data types because all the cases can be enumerated and statically checked by the compiler.

See this example mock for Java:[15]

int eval(Node n) {
    return switch(n) {
        // try to type cast "Node" into "IntNode", and create the variable "i" of type "int".
        // If that works, then return the value of "i"
        case IntNode(int i) -> i;
        // try to type cast "Node" into "NegNode", and create the variable "n" of type "Node".
        // If that works, then return the negation of evaluating the "n" node
        case NegNode(Node n) -> -eval(n);
        // try to type cast "Node" into "AddNode", and create the variables "left" and "right" of type "Node".
        // If that works, then return the addition of evaluating the "left" and "right" nodes
        case AddNode(Node left, Node right) -> eval(left) + eval(right);
        // try to type cast "Node" into "MulNode", and create the variables "left" and "right" of type "Node".
        // If that works, then return the multiplication of evaluating the "left" and "right" nodes
        case MulNode(Node left, Node right) -> eval(left) * eval(right);
        // no "default" because the compiler knows all the possible cases have been enumerated
    };
}

In a statically typed language, the advantage of pattern matching over flow-sensitive typing is that the type of a variable always stays the same: it does not change depending on control flow. When writing down the pattern to be matched, a new variable is declared that will have the new type.

References

  1. Lukas Eder (11 December 2014). "The Inconvenient Truth About Dynamic vs. Static Typing". blog.jooq.org. http://blog.jooq.org/2014/12/11/the-inconvenient-truth-about-dynamic-vs-static-typing. 
  2. Tony Hoare (2009-08-25). "Null References: The Billion Dollar Mistake". InfoQ.com. http://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare. "I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years." 
  3. "The Design and Implementation of Typed Scheme | Lambda the Ultimate, 2008". http://lambda-the-ultimate.org/node/2622. 
  4. "5 Occurrence Typing". https://docs.racket-lang.org/ts-guide/occurrence-typing.html. 
  5. David J. Pearce (22 September 2010). "On Flow-Sensitive Types in Whiley". whiley.org. http://whiley.org/2010/09/22/on-flow-sensitive-types-in-whiley/. 
  6. David J. Pearce (8 April 2012). "Whiley - Flow Typing". whiley.org. http://whiley.org/guide/typing/flow-typing/. 
  7. "Typing Local Control and State Using Flow Analysis". https://cs.brown.edu/~sk/Publications/Papers/Published/gsk-flow-typing-theory/. 
  8. "Ceylon - Quick introduction - Typesafe null and flow-sensitive typing". ceylon-lang.org. http://ceylon-lang.org/documentation/1.2/introduction/#typesafe_null_and_flow_sensitive_typing. 
  9. Ryan Cavanaugh (18 November 2014). "TypeScript 1.4 sneak peek: union types, type guards, and more". blogs.msdn.microsoft.com. https://blogs.msdn.microsoft.com/typescript/2014/11/18/typescript-1-4-sneak-peek-union-types-type-guards-and-more. 
  10. Avik Chaudhuri; Basil Hosmer (18 November 2014). "Flow, a new static type checker for JavaScript". code.facebook.com. https://code.facebook.com/posts/1505962329687926/flow-a-new-static-type-checker-for-javascript. 
  11. "Design with nullable reference types" (in en-us). https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/nullable-reference-types#create-respondents-and-get-answers-to-the-survey. 
  12. "Null Safety". kotlinlang.org. https://kotlinlang.org/docs/reference/null-safety.html. 
  13. "Type Checks and Casts". kotlinlang.org. https://kotlinlang.org/docs/reference/typecasts.html. 
  14. "The Lobster Type System". http://aardappel.github.io/lobster/type_checker.html#the-trouble-with-nil. 
  15. Gavin Bierman and Brian Goetz (19 September 2023). "JEP 441: Pattern Matching for switch" (in en-us). https://openjdk.org/jeps/441.