1 / 34

Soft Types for Scheme

Soft Types for Scheme. Donna Malayeri Type Refinements Seminar October 2003. A Practical Soft Type System for Scheme . Scheme is not (statically) typed Types are good Let’s add (soft) types to Scheme! But not static types:

ardara
Download Presentation

Soft Types for Scheme

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Soft Types for Scheme Donna Malayeri Type Refinements Seminar October 2003

  2. A Practical Soft Type System for Scheme • Scheme is not (statically) typed • Types are good • Let’s add (soft) types to Scheme! • But not static types: “Statically typed languages provide all the benefits of types and type checking that we seek, but at the price of rejecting programs that do not satisfy the type system’s arbitrary constraints.”

  3. Soft Types • A soft type system infers types for dynamically typed programs • Main idea: type checker does not reject ill-typed programs, instead inserts explicit run-time checks • Soft Scheme uses inferred types to eliminate run-time checks • Remaining run-time checks are flagged as potential program errors

  4. Soft Scheme • Assigns types to Scheme programs by inserting run-time checks at applications of primitive operations • Has both an internal type language and a presentation type language • Internal type language is mapped to the programmer-friendly presentation type language • Performs a whole-program analysis • Requires no programmer-supplied type annotations

  5. Core Scheme Syntax Const includes basic constants (numbers, #t, etc.) and primitive operations

  6. Checked and Unchecked Operations • Primitive operations are either checked or unchecked • Checked: CHECK-ap • Unchecked: ap (ap 1 2) • meaningless: either core dumps or gives an arbitrary answer (CHECK-ap 1 2) • terminates with an error message

  7. Challenges • Values in dynamically typed programs belong to many different semantic types • Soft type system should include union types • For typing applications: • Need to modify Hindley-Milner typing

  8. Overview of Soft Type System • Combine union types and subtyping with Hindley-Milner polymorphism using record subtyping (Wand 1991, Rémy 1991) • (Many) union types can be expressed as terms in a free algebra (as in HM typing) • Use flag variables to encode subtyping as a subset on union types

  9. Soft Type System • Infers types using a variant of the Hindley-Milner algorithm • Provides the illusion of a polymorphic union type system based on ordinary union types • Uses algorithms for unification, generalization and instantiation of types from Rémy [1992].

  10. Static Types • Partition the domain into disjoint subsets • Primitive operations are (mostly?) closed over these subsets

  11. Static Types • Define pretypes (, ) • is a flag variable and  is a tag (value constructor) • Tags specify partitions of data domain • Soft types are a subset of the pretypes

  12. Tags and Flags num true false nil cons ! • Flag indicates whether partition is part of the union • + partition is present− partition is absent (variable) may be present or absent

  13. Examples of Types

  14. Tidy Types • Tidy type: tag is used at most once • Range of a type variables excludes the tags of partitions preceding it • Tidiness simplifies the pairwise unification of union types

  15. Example In the range of  excludes numbers, #t and all pairs.

  16. Type Schemes • Type scheme: 2 (TypeVar[FlagVar) • Substitutions map type variables to types and from flag variables to flags. • 0 is an instance of type scheme

  17. Type Schemes • Polymorphism is used for ordinary polymorphic types and for subsets of tidy union types • Simulate subtyping at function applications • Use unification to equate function input type and argument type

  18. Example A function with type scheme can be applied to values of type

  19. Supersets of Types • Constants and outputs of primitives may have a type that is a superset of their natural type • E.g., numbers have type scheme • Needed for typing (rather strange) expressions e.g. with type

  20. Types of Primitive Operations Unchecked operations Checked operations (accept all inputs) Could use + instead, but the flag variable is used for a technical reason (reverse flow)

  21. Static type inference rules

  22. Soft Type Checking • Checker makes untypable programs typeable by replacing some unchecked operations with checked ones • Introduce a meta-function SoftTypeOffor assigning type schemes to constants • For unchecked primitives, SoftTypeOf uses absent variables to keep track of uses that may not be safe

  23. Absent Variables and Flags • Absent variable: • SoftTypeOf of uses absent variables/flags where – or  is in the input type of a primitive e.g. TypeOf(add1) SoftTypeOf(add1)

  24. Empty Types • If the primitive operation p introduces an absent variable that is instantiated to a non-empty type, then p must be checked

  25. Soft Typing Rules

  26. Pattern Matching • “In ordinary statically typed languages, pattern matching is largely a notational convenience and could be provided as a macro.” (???) • In the soft type system, pattern matching allows the type checker to “learn” more precise types • Davies’s refinement checker also does this

  27. Typing Match Expressions • More challenging than ML typing, because types may overlap • To type • Compute a tidy type for each pi • Combine the pattern types to obtain the match input type  • If type of e is larger than , the match requires a run-time check

  28. Pattern Matching Examples Has input type nil+[cons+(12) • However, restrictions on tidiness can lead to imprecise input types • E.g. has type cons+( (true+[false+) (true+[false+) ) Therefore, match is inexhaustive

  29. Example: Tautology Checker • Boolean Formulae formula = .true [ false [ ((true [ false) !) taut : formula ! (true [ false)

  30. SML Version • Wright and Cartwright claim that “these injections and projections add both unnecessary semantic complexity and run-time overhead”

  31. Examples • Requires no run-time checks • flatten : 82.1.nil+[cons+(11) [ 2) !+ list+ (nil−[cons−[2) a: cons+( num+ (cons+(cons+(num+nil+)) (cons+(num+ nil+))) b: (list num)

  32. Examples • Suppose we add • Soft type checker output: • Rewritten program:

  33. Other Interesting Claims • Inherent incompleteness of static type checking “nourishes a never-ending search for better static type systems” • “Realistic statically typed languages such as C usually have some means of circumventing the type checker” • SML/NJ provides System.Unsafe.cast • Type refinements (à la Freeman and Pfenning) does “not alter the fundamental character of static type systems.”

  34. Discussion • Soft Scheme’s whole program analysis is not scalable • Unlike with type refinements, programmers cannot specify what invariants they actually care about • Their system seems ad-hoc and overly complex • Upshot: soft types improve Scheme somewhat, if you’re foolish enough to be using Scheme in the first place

More Related