diff options
author | Adam Gundry <adam@well-typed.com> | 2017-02-14 09:53:28 -0500 |
---|---|---|
committer | Ben Gamari <ben@smart-cactus.org> | 2017-02-14 10:53:01 -0500 |
commit | da493897ac6ee2b17a0c58b51315f9d136de730d (patch) | |
tree | 08e13ee790290eada30f1ff1c7d1a2cae9f9d69b /libraries | |
parent | c3bbd1afc85cd634d8d26e27bafb92cc7481667b (diff) | |
download | haskell-da493897ac6ee2b17a0c58b51315f9d136de730d.tar.gz |
Implement HasField constraint solving and modify OverloadedLabels
This implements automatic constraint solving for the new HasField class
and modifies the existing OverloadedLabels extension, as described in
the GHC proposal
(https://github.com/ghc-proposals/ghc-proposals/pull/6). Per the current
form of the proposal, it does *not* currently introduce a separate
`OverloadedRecordFields` extension.
This replaces D1687.
The users guide documentation still needs to be written, but I'll do
that after the implementation is merged, in case there are further
design changes.
Test Plan: new and modified tests in overloadedrecflds
Reviewers: simonpj, goldfire, dfeuer, bgamari, austin, hvr
Reviewed By: bgamari
Subscribers: maninalift, dfeuer, ysangkok, thomie, mpickering
Differential Revision: https://phabricator.haskell.org/D2708
Diffstat (limited to 'libraries')
-rw-r--r-- | libraries/base/GHC/OverloadedLabels.hs | 38 | ||||
-rw-r--r-- | libraries/base/GHC/Records.hs | 34 | ||||
-rw-r--r-- | libraries/base/base.cabal | 1 |
3 files changed, 57 insertions, 16 deletions
diff --git a/libraries/base/GHC/OverloadedLabels.hs b/libraries/base/GHC/OverloadedLabels.hs index f4a76cf8ea..7e27cf6bf4 100644 --- a/libraries/base/GHC/OverloadedLabels.hs +++ b/libraries/base/GHC/OverloadedLabels.hs @@ -1,48 +1,54 @@ -{-# LANGUAGE NoImplicitPrelude - , MultiParamTypeClasses - , MagicHash - , KindSignatures +{-# LANGUAGE AllowAmbiguousTypes , DataKinds + , FlexibleInstances + , KindSignatures + , MultiParamTypeClasses + , ScopedTypeVariables + , TypeApplications #-} ----------------------------------------------------------------------------- -- | -- Module : GHC.OverloadedLabels --- Copyright : (c) Adam Gundry 2015 +-- Copyright : (c) Adam Gundry 2015-2016 -- License : see libraries/base/LICENSE -- -- Maintainer : cvs-ghc@haskell.org -- Stability : internal -- Portability : non-portable (GHC extensions) -- --- This module defines the `IsLabel` class is used by the --- OverloadedLabels extension. See the +-- This module defines the 'IsLabel' class is used by the +-- @OverloadedLabels@ extension. See the -- <https://ghc.haskell.org/trac/ghc/wiki/Records/OverloadedRecordFields/OverloadedLabels wiki page> -- for more details. -- --- The key idea is that when GHC sees an occurrence of the new --- overloaded label syntax @#foo@, it is replaced with +-- When @OverloadedLabels@ is enabled, if GHC sees an occurrence of +-- the overloaded label syntax @#foo@, it is replaced with -- --- > fromLabel (proxy# :: Proxy# "foo") :: alpha +-- > fromLabel @"foo" :: alpha -- -- plus a wanted constraint @IsLabel "foo" alpha@. -- +-- Note that if @RebindableSyntax@ is enabled, the desugaring of +-- overloaded label syntax will make use of whatever @fromLabel@ is in +-- scope. +-- ----------------------------------------------------------------------------- -- Note [Overloaded labels] -- ~~~~~~~~~~~~~~~~~~~~~~~~ -- An overloaded label is represented by the 'HsOverLabel' constructor --- of 'HsExpr', which stores a 'FastString'. It is passed through --- unchanged by the renamer, and the type-checker transforms it into a --- call to 'fromLabel'. See Note [Type-checking overloaded labels] in --- TcExpr for more details in how type-checking works. +-- of 'HsExpr', which stores the 'FastString' text of the label and an +-- optional id for the 'fromLabel' function to use (if +-- RebindableSyntax is enabled) . The type-checker transforms it into +-- a call to 'fromLabel'. See Note [Type-checking overloaded labels] +-- in TcExpr for more details in how type-checking works. module GHC.OverloadedLabels ( IsLabel(..) ) where import GHC.Base ( Symbol ) -import GHC.Exts ( Proxy# ) class IsLabel (x :: Symbol) a where - fromLabel :: Proxy# x -> a + fromLabel :: a diff --git a/libraries/base/GHC/Records.hs b/libraries/base/GHC/Records.hs new file mode 100644 index 0000000000..43c3931e86 --- /dev/null +++ b/libraries/base/GHC/Records.hs @@ -0,0 +1,34 @@ +{-# LANGUAGE AllowAmbiguousTypes + , FunctionalDependencies + , KindSignatures + , MultiParamTypeClasses + , PolyKinds + #-} + +----------------------------------------------------------------------------- +-- | +-- Module : GHC.Records +-- Copyright : (c) Adam Gundry 2015-2016 +-- License : see libraries/base/LICENSE +-- +-- Maintainer : cvs-ghc@haskell.org +-- Stability : internal +-- Portability : non-portable (GHC extensions) +-- +-- This module defines the 'HasField' class used by the +-- @OverloadedRecordFields@ extension. See the +-- <https://ghc.haskell.org/trac/ghc/wiki/Records/OverloadedRecordFields +-- wiki page> for more details. +-- +----------------------------------------------------------------------------- + +module GHC.Records + ( HasField(..) + ) where + +-- | Constraint representing the fact that the field @x@ belongs to +-- the record type @r@ and has field type @a@. This will be solved +-- automatically, but manual instances may be provided as well. +class HasField (x :: k) r a | x r -> a where + -- | Selector function to extract the field from the record. + getField :: r -> a diff --git a/libraries/base/base.cabal b/libraries/base/base.cabal index 691dc83909..49e23e5c97 100644 --- a/libraries/base/base.cabal +++ b/libraries/base/base.cabal @@ -264,6 +264,7 @@ Library GHC.Ptr GHC.Read GHC.Real + GHC.Records GHC.RTS.Flags GHC.ST GHC.StaticPtr |