HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Moduleghc-9.10.3GHC2021

GHC.Tc.Deriv.Utils

Error-checking and other utilities for deriving clauses or declarations.

  • 9 types
  • 22 values
  • Packageghc-9.10.3
  • Exports31
  • LanguageGHC2021
  • LicenceBSD-3-Clause
  • SourceUtils.hs
datadata DerivEnv
#

Contains all of the information known about a derived instance when determining what its EarlyDerivSpec should be. See Note [DerivEnv and DerivSpecMechanism].

Constructors

Instances1Outputable
datadata DerivSpecMechanism
#

What action to take in order to derive a class instance. See Note [DerivEnv and DerivSpecMechanism], as well as Note [Deriving strategies] in GHC.Tc.Deriv.

Constructors

Instances1Outputable
datadata DerivContext
#

Whether GHC is processing a deriving clause or a standalone deriving declaration.

Constructors

  • InferContext (Maybe SrcSpan)

    'InferContext mb_wildcard is either:

    • A deriving clause (in which case mb_wildcard is Nothing).

    • A standalone deriving declaration with an extra-constraints wildcard as the context (in which case mb_wildcard is Just loc, where loc is the location of the wildcard.

    GHC should infer the context.

  • SupplyContext ThetaType

    SupplyContext theta is a standalone deriving declaration, where theta is the context supplied by the user.

Instances1Outputable
datadata StockGenFns
#

Describes how to generate instance bindings (stock_gen_binds) and associated type family instances (stock_gen_fam_insts) for a particular stock-derived instance.

Constructors

  • StockGenFns
    • stock_gen_binds :: SrcSpan -> DerivInstTys -> TcM (LHsBinds GhcPs, [LSig GhcPs], Bag AuxBindSpec, [Name])

      Describes how to generate instance bindings for a stock-derived instance.

      This function takes two arguments:

      1. SrcSpan: the source location where the instance is being derived. This will eventually be instantiated with the ds_loc field of a DerivSpec.

      2. DerivInstTys: information about the argument types to which a class is applied in a derived instance. This will eventually be instantiated with the dsm_stock_dit field of a DerivSpecMechanism.

      This function returns four things:

      1. LHsBinds GhcPs: The derived instance's function bindings (e.g., compare (T x) (T y) = compare x y)

      2. [LSig GhcPs]: A list of instance specific signatures/pragmas. Most likely INLINE pragmas for class methods.

      3. Bag AuxBindSpec: Auxiliary bindings needed to support the derived instance. As examples, derived Eq and Ord instances sometimes require top-level con2tag functions. See Note [Auxiliary binders] in GHC.Tc.Deriv.Generate.

      4. [Name]: A list of Names for which -Wunused-binds should be suppressed. This is used to suppress unused warnings for record selectors when deriving Read, Show, or Generic. See Note [Deriving and unused record selectors].

    • stock_gen_fam_insts :: SrcSpan -> DerivInstTys -> TcM [FamInst]

      Describes how to generate associated type family instances for a stock-derived instance. This function takes the same arguments as the stock_gen_binds function but returns a list of FamInsts instead. Generating type family instances is done separately from stock_gen_binds since the type family instances must be generated before the instance bindings can be typechecked. See Note [Staging of tcDeriving] in GHC.Tc.Deriv.

Is GHC processing a standalone deriving declaration with an extra-constraints wildcard as the context? (e.g., deriving instance _ => Eq (Foo a))

datadata PredSpec
#

A PredSpec specifies a constraint to emitted when inferring the instance context for a derived instance in GHC.Tc.Deriv.simplifyInfer.

Constructors

  • SimplePredSpec

    An ordinary PredSpec that directly stores a PredType, which will be emitted as a wanted constraint in the constraint solving machinery. This is the simple case, as there are no skolems, metavariables, or given constraints involved.

  • SubTypePredSpec

    A special PredSpec that is only used by DeriveAnyClass. This will check if stps_ty_actual is a subtype of (i.e., more polymorphic than) stps_ty_expected in the constraint solving machinery, emitting an implication constraint as a side effect. For more details on how this works, see Note [Gathering and simplifying constraints for DeriveAnyClass] in GHC.Tc.Deriv.Infer.

Instances1Outputable
typetype ThetaSpec = [PredSpec]
#

A list of PredSpec constraints to simplify when inferring a derived instance's context. For the stock, newtype, and via deriving strategies, these will consist of SimplePredSpecs, and for DeriveAnyClass, these will consist of SubTypePredSpecs. Here is an example to illustrate the latter:

class Foo a where
  bar :: forall b. Ix b => a -> b -> String
  default bar :: forall y. (Show a, Ix y) => a -> y -> String
  bar x y = show x ++ show (range (y, y))

  baz :: Eq a => a -> a -> Bool
  default baz :: Ord a => a -> a -> Bool
  baz x y = compare x y == EQ

data Quux q = Quux deriving anyclass Foo

Then it would generate two SubTypePredSpecs, one for each method:

[ SubTypePredSpec
    { stps_ty_actual   = forall y. (Show (Quux q), Ix y) => Quux q -> y -> String
    , stps_ty_expected = forall b.                (Ix b) => Quux q -> b -> String
    , stps_ty_origin   = DerivClauseCtxt
    }
, SubTypePredSpec
    { stps_ty_actual   = Ord (Quux q) => Quux q -> Quux q -> Bool
    , stps_ty_expected = Eq  (Quux q) => Quux q -> Quux q -> Bool
    , stps_ty_origin   = DerivClauseCtxt
    }
]

(Note that the type variable q is bound by the data type Quux, and thus appears free in the stps_ty_actuals and stps_ty_expecteds.)

See Note [Gathering and simplifying constraints for DeriveAnyClass] in GHC.Tc.Deriv.Infer for an explanation of how these SubTypePredSpecs are used to compute implication constraints.