Dynamic typing is a popular approach used by many programming languages, and an appealing tool for early prototyping. Contrary to Haskell’s strict approach, a dynamic typing compiler/interpreter is quite forgiving of how data of different types can be mixed and matched, replacing compilation errors with runtime exceptions. In GHC, the
Typeable type-class and the
Data.Dynamic type allow users to model dynamic types in Haskell, with explicit coercions to and from dynamic values using runtime type-representations. However, these are cumbersome in practice and often involve lots of syntax overhead when writing code involving dynamic types.
In this talk, we present the Dynamic Haskell plugin for GHC, which combines a type-checker plugin with a core-to-core pass to improve the ergonomics of dynamic types. In our type-directed approach, we automatically wrap values into dynamic values when called for by the types. For example, this allows users to write heterogenous lists such as
[1 :: Int, "A", ()] :: [Dynamic] without any additional constructors. In the other direction, we automatically unwrap dynamic values when called for, with appropriate errors in case of a mismatch between the expected and actual types.
Moreover, our plugin provides a view-pattern that allows users to explicitly match on the type and/or the constructor of a dynamic value, imitating Erlang-style pattern-matching code without additional syntax overhead.
To support type-classes, we compile type-class method calls by generating a virtual method table that selects the appropriate function at runtime based on the type-representation of the dynamic arguments. This enables type-class based dynamic dispatch for dynamic values giving a similar experience to other dynamically typed languages.
All these features combine to make the use of dynamic types more ergonomic in GHC and allows us to combine the rich type features of Haskell with the flexibility of dynamic values on a per-module basis.
Sun 22 AugDisplayed time zone: Seoul change
23:30 - 01:00
|Generalization is hard, but somebody's got to do it|
Richard A. Eisenberg Tweag
|A new interface for GHC typechecker plugins and type-family rewriting|
Sam Derbyshire TweagMedia Attached
|The Dynamic Haskell Plugin for GHC|
|GHC Status update|
Simon Peyton Jones Microsoft, UKFile Attached