diff options
author | vivid-synth <vivid.haskell@gmail.com> | 2017-02-14 09:51:54 -0500 |
---|---|---|
committer | Ben Gamari <ben@smart-cactus.org> | 2017-02-14 10:53:01 -0500 |
commit | c3bbd1afc85cd634d8d26e27bafb92cc7481667b (patch) | |
tree | 729e78fe6b02eecef0035d4220cdad44d3b401a0 /docs | |
parent | 2f1017b924740e66f093b0baba62ac0b1528abf8 (diff) | |
download | haskell-c3bbd1afc85cd634d8d26e27bafb92cc7481667b.tar.gz |
Allow type defaulting for multi-param type classes with ExtendedDefaultRules
Expressions like the following will now typecheck:
```
data A x = A deriving Show
class ToA a x where
toA :: a -> A x
instance ToA Integer x where
toA _ = A
main = print (toA 5 :: A Bool)
```
The new defaulting rules are
Find all the unsolved constraints. Then:
* Find those that have exactly one free type variable, and partition
that subset into groups that share a common type variable `a`.
* Now default `a` (to one of the types in the default list) if at least
one of the classes `Ci` is an interactive class
Reviewers: goldfire, bgamari, austin, mpickering, simonpj
Reviewed By: bgamari, simonpj
Subscribers: mpickering, simonpj, goldfire, thomie
Differential Revision: https://phabricator.haskell.org/D2822
Diffstat (limited to 'docs')
-rw-r--r-- | docs/users_guide/8.2.1-notes.rst | 3 | ||||
-rw-r--r-- | docs/users_guide/ghci.rst | 20 |
2 files changed, 18 insertions, 5 deletions
diff --git a/docs/users_guide/8.2.1-notes.rst b/docs/users_guide/8.2.1-notes.rst index 00e6c7c4b5..d70dc50e42 100644 --- a/docs/users_guide/8.2.1-notes.rst +++ b/docs/users_guide/8.2.1-notes.rst @@ -154,6 +154,9 @@ Compiler allocation and a potential space leak when deriving ``Functor`` for a recursive type. +- The :ghc-flag:`-XExtendedDefaultRules` extension now defaults multi-parameter + typeclasses. See :ghc-ticket:`12923`. + GHCi ~~~~ diff --git a/docs/users_guide/ghci.rst b/docs/users_guide/ghci.rst index fa00b80244..04864cda83 100644 --- a/docs/users_guide/ghci.rst +++ b/docs/users_guide/ghci.rst @@ -1040,17 +1040,27 @@ and defaults the type variable if 3. At least one of the classes ``Ci`` is numeric. At the GHCi prompt, or with GHC if the :ghc-flag:`-XExtendedDefaultRules` flag -is given, the following additional differences apply: +is given, the types are instead resolved with the following method: -- Rule 2 above is relaxed thus: *All* of the classes ``Ci`` are - single-parameter type classes. +Find all the unsolved constraints. Then: -- Rule 3 above is relaxed thus: At least one of the classes ``Ci`` is - an *interactive class* (defined below). +- Find those that are of form ``(C a)`` where ``a`` is a type variable, and + partition those constraints into groups that share a common type variable ``a``. + +- Keep only the groups in which at least one of the classes is an + **interactive class** (defined below). + +- Now, for each remaining group G, try each type ``ty`` from the default-type list + in turn; if setting ``a = ty`` would allow the constraints in G to be completely + solved. If so, default ``a`` to ``ty``. - The unit type ``()`` and the list type ``[]`` are added to the start of the standard list of types which are tried when doing type defaulting. +Note that any multi-parameter constraints ``(D a b)`` or ``(D [a] Int)`` do not +participate in the process (either to help or to hinder); but they must of course +be soluble once the defaulting process is complete. + The last point means that, for example, this program: :: main :: IO () |