HIW 2021
Sam Derbyshire, Tweag
August 22nd, 2021
GHC doesn’t understand basic arithmetic.
GHC can only reduce a + b when it knows what a is:
is it Zero or is it Succ i for some i?
Can’t reason about (+) parametrically:
Could not deduce n + Succ n ~ Succ (n + n)
Row: unordered association map, field name ⇝ type, e.g.
( "field1" :: Int, "field2" :: Bool ).
Can be used to model extensible records (order doesn’t matter).
To account for re-ordering, we need:
when k and l are distinct field names that don’t appear in the row r.
Problem: in GHC, we can’t have type-families on the LHS of a type family equation:
• Illegal type synonym family application ‘Insert l w r’ in instance:
Insert k v (Insert l w r)
GHC can’t allow type-families on the LHS of a type family equation in general: no guarantee type-family reduction is confluent.
Stephanie Weirich posed the challenge of implementing a strongly-typed representation of System F. [Talk] [GitHub]
Type family argument on the LHS of a type family equation.
pluginRewrite :: PluginDefs -> UniqFM TyCon TcPluginRewriter
pluginRewrite defs@( PluginDefs { applySubTyCon } ) =
listToUFM
[ ( applySubTyCon, rewriteApplySub defs ) ]
rewriteApplySub
:: PluginDefs
-> [ Ct ] -- Givens
-> [ Type ] -- Arguments to ApplySub
-> TcPluginM TcPluginRewriteResult
rewriteApplySub defs givens [ kϕ, kψ, k, sub, sub_arg ]
= ...
import GHC.TcPlugin.API
-- Want to use a 'Coercion' as equality constraint evidence.
help :: Coercion -> EvTerm
help = _
* Found hole: _ :: Coercion -> EvTerm
* Valid hole fits include
evCoercion :: Coercion -> EvTerm
import GHC.TcPlugin.API
-- Want to apply a 'TyCon' to arguments.
help :: TyCon -> [Type] -> Type
help = _
* Found hole: _ :: TyCon -> [Type] -> Type
* Valid hole fits include
mkTyConApp :: TyCon -> [Type] -> Type
Helpers for custom type errors.
Get your hands dirty: ghc-tcplugin-api on Hackage.
Type-family rewriting functionality in GHC 9.4.
Compatibility layer in ghc-tcplugin-api for GHC 9.0 and 9.2.
ghc-tcplugin-api on GitHub contains the System F example.
Slides: github.com/sheaf/HIW-tcplugins
Upcoming Tweag blog post: writing and debugging a type-checking plugin.
In depth review of GHC constraint solving and type family reduction.