[commit: ghc] master: Track the order of user-written tyvars in DataCon (ef26182)

git at git.haskell.org git at git.haskell.org
Tue Oct 3 21:56:17 UTC 2017


Repository : ssh://git@git.haskell.org/ghc

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/ef26182e2014b0a2a029ae466a4b121bf235e4e4/ghc

>---------------------------------------------------------------

commit ef26182e2014b0a2a029ae466a4b121bf235e4e4
Author: Ryan Scott <ryan.gl.scott at gmail.com>
Date:   Tue Oct 3 14:58:27 2017 -0400

    Track the order of user-written tyvars in DataCon
    
    After typechecking a data constructor's type signature, its type
    variables are partitioned into two distinct groups: the universally
    quantified type variables and the existentially quantified type
    variables. Then, when prompted for the type of the data constructor,
    GHC gives this:
    
    ```lang=haskell
    MkT :: forall <univs> <exis>. (...)
    ```
    
    For H98-style datatypes, this is a fine thing to do. But for GADTs,
    this can sometimes produce undesired results with respect to
    `TypeApplications`. For instance, consider this datatype:
    
    ```lang=haskell
    data T a where
      MkT :: forall b a. b -> T a
    ```
    
    Here, the user clearly intended to have `b` be available for visible
    type application before `a`. That is, the user would expect
    `MkT @Int @Char` to be of type `Int -> T Char`, //not//
    `Char -> T Int`. But alas, up until now that was not how GHC
    operated—regardless of the order in which the user actually wrote
    the tyvars, GHC would give `MkT` the type:
    
    ```lang=haskell
    MkT :: forall a b. b -> T a
    ```
    
    Since `a` is universal and `b` is existential. This makes predicting
    what order to use for `TypeApplications` quite annoying, as
    demonstrated in #11721 and #13848.
    
    This patch cures the problem by tracking more carefully the order in
    which a user writes type variables in data constructor type
    signatures, either explicitly (with a `forall`) or implicitly
    (without a `forall`, in which case the order is inferred). This is
    accomplished by adding a new field `dcUserTyVars` to `DataCon`, which
    is a subset of `dcUnivTyVars` and `dcExTyVars` that is permuted to
    the order in which the user wrote them. For more details, refer to
    `Note [DataCon user type variables]` in `DataCon.hs`.
    
    An interesting consequence of this design is that more data
    constructors require wrappers. This is because the workers always
    expect the first arguments to be the universal tyvars followed by the
    existential tyvars, so when the user writes the tyvars in a different
    order, a wrapper type is needed to swizzle the tyvars around to match
    the order that the worker expects. For more details, refer to
    `Note [Data con wrappers and GADT syntax]` in `MkId.hs`.
    
    Test Plan: ./validate
    
    Reviewers: austin, goldfire, bgamari, simonpj
    
    Reviewed By: goldfire, simonpj
    
    Subscribers: ezyang, goldfire, rwbarton, thomie
    
    GHC Trac Issues: #11721, #13848
    
    Differential Revision: https://phabricator.haskell.org/D3687


>---------------------------------------------------------------

ef26182e2014b0a2a029ae466a4b121bf235e4e4
 compiler/backpack/RnModIface.hs                    |   4 +-
 compiler/basicTypes/DataCon.hs                     | 237 ++++++++++++++++-----
 compiler/basicTypes/DataCon.hs-boot                |   6 +-
 compiler/basicTypes/MkId.hs                        |  57 ++++-
 compiler/iface/BuildTyCl.hs                        |  25 ++-
 compiler/iface/IfaceSyn.hs                         |  59 ++---
 compiler/iface/IfaceType.hs                        |  13 +-
 compiler/iface/MkIface.hs                          |  21 +-
 compiler/iface/TcIface.hs                          |  26 ++-
 compiler/prelude/TysWiredIn.hs                     |  19 +-
 compiler/typecheck/TcTyClsDecls.hs                 | 131 +++++-------
 compiler/types/TyCoRep.hs                          |  15 +-
 compiler/vectorise/Vectorise/Generic/PData.hs      |   8 +-
 compiler/vectorise/Vectorise/Type/TyConDecl.hs     |   5 +-
 docs/users_guide/8.4.1-notes.rst                   |  11 +
 testsuite/tests/gadt/gadtSyntaxFail003.stderr      |   2 +-
 testsuite/tests/ghci/scripts/T11721.script         |   7 +
 testsuite/tests/ghci/scripts/T11721.stdout         |   3 +
 testsuite/tests/ghci/scripts/all.T                 |   1 +
 testsuite/tests/patsyn/should_fail/T11010.stderr   |   2 +-
 testsuite/tests/polykinds/T8566.stderr             |   2 +-
 testsuite/tests/typecheck/should_compile/T13848.hs |  41 ++++
 testsuite/tests/typecheck/should_compile/all.T     |   1 +
 23 files changed, 493 insertions(+), 203 deletions(-)

Diff suppressed because of size. To see it, use:

    git diff-tree --root --patch-with-stat --no-color --find-copies-harder --ignore-space-at-eol --cc ef26182e2014b0a2a029ae466a4b121bf235e4e4


More information about the ghc-commits mailing list