ghc-9.6.3: The GHC API

Safe Haskell | Safe-Inferred
Language | Haskell2010

GHC.Tc.Deriv.Generate

Description

Generating derived instance declarations

This module is nominally ``subordinate'` to GHC.Tc.Deriv, which is the ``official'` interface to deriving-related things.

This is where we do all the grimy bindings' generation.

Synopsis

- data AuxBindSpec
  - = DerivTag2Con TyCon RdrName
  - | DerivMaxTag TyCon RdrName
  - | DerivDataDataType TyCon RdrName [RdrName]
  - | DerivDataConstr DataCon RdrName RdrName
- gen_Eq_binds :: SrcSpan -> DerivInstTys -> TcM (LHsBinds GhcPs, Bag AuxBindSpec)
- gen_Ord_binds :: SrcSpan -> DerivInstTys -> TcM (LHsBinds GhcPs, Bag AuxBindSpec)
- gen_Enum_binds :: SrcSpan -> DerivInstTys -> TcM (LHsBinds GhcPs, Bag AuxBindSpec)
- gen_Bounded_binds :: SrcSpan -> DerivInstTys -> (LHsBinds GhcPs, Bag AuxBindSpec)
- gen_Ix_binds :: SrcSpan -> DerivInstTys -> TcM (LHsBinds GhcPs, Bag AuxBindSpec)
- gen_Show_binds :: (Name -> Fixity) -> SrcSpan -> DerivInstTys -> (LHsBinds GhcPs, Bag AuxBindSpec)
- gen_Read_binds :: (Name -> Fixity) -> SrcSpan -> DerivInstTys -> (LHsBinds GhcPs, Bag AuxBindSpec)
- gen_Data_binds :: SrcSpan -> DerivInstTys -> TcM (LHsBinds GhcPs, Bag AuxBindSpec)
- gen_Lift_binds :: SrcSpan -> DerivInstTys -> (LHsBinds GhcPs, Bag AuxBindSpec)
- gen_Newtype_binds :: SrcSpan -> Class -> [TyVar] -> [Type] -> Type -> (LHsBinds GhcPs, [LSig GhcPs])
- gen_Newtype_fam_insts :: SrcSpan -> Class -> [TyVar] -> [Type] -> Type -> TcM [FamInst]
- mkCoerceClassMethEqn :: Class -> [TyVar] -> [Type] -> Type -> Id -> Pair Type
- genAuxBinds :: SrcSpan -> Bag AuxBindSpec -> Bag (LHsBind GhcPs, LSig GhcPs)
- ordOpTbl :: [(Type, (RdrName, RdrName, RdrName, RdrName, RdrName))]
- boxConTbl :: [(Type, LHsExpr GhcPs -> LHsExpr GhcPs)]
- litConTbl :: [(Type, LHsExpr GhcPs -> LHsExpr GhcPs)]
- mkRdrFunBind :: LocatedN RdrName -> [LMatch GhcPs (LHsExpr GhcPs)] -> LHsBind GhcPs
- mkRdrFunBindEC :: Arity -> (LHsExpr GhcPs -> LHsExpr GhcPs) -> LocatedN RdrName -> [LMatch GhcPs (LHsExpr GhcPs)] -> LHsBind GhcPs
- mkRdrFunBindSE :: Arity -> LocatedN RdrName -> [LMatch GhcPs (LHsExpr GhcPs)] -> LHsBind GhcPs
- error_Expr :: FastString -> LHsExpr GhcPs
- getPossibleDataCons :: TyCon -> [Type] -> [DataCon]
- data DerivInstTys = DerivInstTys {
  - dit_cls_tys :: [Type]
  - dit_tc :: TyCon
  - dit_tc_args :: [Type]
  - dit_rep_tc :: TyCon
  - dit_rep_tc_args :: [Type]
  - dit_dc_inst_arg_env :: DataConEnv [Type]
  }
- buildDataConInstArgEnv :: TyCon -> [Type] -> DataConEnv [Type]
- derivDataConInstArgTys :: DataCon -> DerivInstTys -> [Type]
- substDerivInstTys :: Subst -> DerivInstTys -> DerivInstTys
- zonkDerivInstTys :: ZonkEnv -> DerivInstTys -> TcM DerivInstTys

# Documentation

data AuxBindSpec Source #

A declarative description of an auxiliary binding that should be generated. See `Note [Auxiliary binders]` for a more detailed description of how these are used.

Constructors

DerivTag2Con TyCon RdrName | `$tag2con`: Given a tag, computes the corresponding data constructor
DerivMaxTag TyCon RdrName | `$maxtag`: The maximum possible tag value among a data type's constructors
DerivDataDataType TyCon RdrName [RdrName] | `$t`: The `DataType` representation for a `Data` instance
DerivDataConstr DataCon RdrName RdrName | `$c`: The `Constr` representation for a `Data` instance

gen_Eq_binds :: SrcSpan -> DerivInstTys -> TcM (LHsBinds GhcPs, Bag AuxBindSpec) Source #

gen_Ord_binds :: SrcSpan -> DerivInstTys -> TcM (LHsBinds GhcPs, Bag AuxBindSpec) Source #

gen_Enum_binds :: SrcSpan -> DerivInstTys -> TcM (LHsBinds GhcPs, Bag AuxBindSpec) Source #

gen_Bounded_binds :: SrcSpan -> DerivInstTys -> (LHsBinds GhcPs, Bag AuxBindSpec) Source #

gen_Ix_binds :: SrcSpan -> DerivInstTys -> TcM (LHsBinds GhcPs, Bag AuxBindSpec) Source #

gen_Show_binds :: (Name -> Fixity) -> SrcSpan -> DerivInstTys -> (LHsBinds GhcPs, Bag AuxBindSpec) Source #

gen_Read_binds :: (Name -> Fixity) -> SrcSpan -> DerivInstTys -> (LHsBinds GhcPs, Bag AuxBindSpec) Source #

gen_Data_binds :: SrcSpan -> DerivInstTys -> TcM (LHsBinds GhcPs, Bag AuxBindSpec) Source #

gen_Lift_binds :: SrcSpan -> DerivInstTys -> (LHsBinds GhcPs, Bag AuxBindSpec) Source #

gen_Newtype_binds :: SrcSpan -> Class -> [TyVar] -> [Type] -> Type -> (LHsBinds GhcPs, [LSig GhcPs]) Source #

gen_Newtype_fam_insts :: SrcSpan -> Class -> [TyVar] -> [Type] -> Type -> TcM [FamInst] Source #

mkCoerceClassMethEqn :: Class -> [TyVar] -> [Type] -> Type -> Id -> Pair Type Source #

genAuxBinds :: SrcSpan -> Bag AuxBindSpec -> Bag (LHsBind GhcPs, LSig GhcPs) Source #

Take a `Bag` of `AuxBindSpec`s and generate the code for auxiliary bindings based on the declarative descriptions in the supplied `AuxBindSpec`s. See `Note [Auxiliary binders]`.

ordOpTbl :: [(Type, (RdrName, RdrName, RdrName, RdrName, RdrName))] Source #

boxConTbl :: [(Type, LHsExpr GhcPs -> LHsExpr GhcPs)] Source #

litConTbl :: [(Type, LHsExpr GhcPs -> LHsExpr GhcPs)] Source #

mkRdrFunBind :: LocatedN RdrName -> [LMatch GhcPs (LHsExpr GhcPs)] -> LHsBind GhcPs Source #

mkRdrFunBindEC :: Arity -> (LHsExpr GhcPs -> LHsExpr GhcPs) -> LocatedN RdrName -> [LMatch GhcPs (LHsExpr GhcPs)] -> LHsBind GhcPs Source #

Produces a function binding. When no equations are given, it generates a binding of the given arity and an empty case expression for the last argument that it passes to the given function to produce the right-hand side.

mkRdrFunBindSE :: Arity -> LocatedN RdrName -> [LMatch GhcPs (LHsExpr GhcPs)] -> LHsBind GhcPs Source #

Produces a function binding. When there are no equations, it generates a binding with the given arity that produces an error based on the name of the type of the last argument.

error_Expr :: FastString -> LHsExpr GhcPs Source #

getPossibleDataCons :: TyCon -> [Type] -> [DataCon] Source #

`getPossibleDataCons tycon tycon_args` returns the constructors of `tycon` whose return types match when checked against `tycon_args`.

See Note [Filter out impossible GADT data constructors]

data DerivInstTys Source #

Information about the arguments to the class in a stock- or newtype-derived instance. For a `deriving`-generated instance declaration such as this one:

    instance Ctx => Cls cls_ty_1 ... cls_ty_m (TC tc_arg_1 ... tc_arg_n) where ...

- `dit_cls_tys` corresponds to `cls_ty_1 ... cls_ty_m`.
- `dit_tc` corresponds to `TC`.
- `dit_tc_args` corresponds to `tc_arg_1 ... tc_arg_n`.

See `Note [DerivEnv and DerivSpecMechanism]` in GHC.Tc.Deriv.Utils for a more in-depth explanation, including the relationship between `dit_tc` `dit_rep_tc` and `dit_tc_args` `dit_rep_tc_args`.

A `DerivInstTys` value can be seen as a more structured representation of the `denv_inst_tys` in a `DerivEnv`, as the `denv_inst_tys` is equal to `dit_cls_tys ++ [`mkTyConApp` dit_tc dit_tc_args]`. Other parts of the instance declaration can be found in the `DerivEnv`. For example, the `Cls` in the example above corresponds to the `denv_cls` field of `DerivEnv`.

Similarly, the type variables that appear in a `DerivInstTys` value are the same type variables as the `denv_tvs` in the parent `DerivEnv`. Accordingly, if we are inferring an instance context, the type variables will be `TcTyVar` skolems. Otherwise, they will be ordinary `TyVar`s. See `Note [Overlap and deriving]` in GHC.Tc.Deriv.Infer.

Constructors

DerivInstTys |

Fields

- dit_cls_tys :: [Type]

  Other arguments to the class except the last

- dit_tc :: TyCon

  Type constructor for which the instance is requested (last arguments to the type class)

- dit_tc_args :: [Type]

  Arguments to the type constructor

- dit_rep_tc :: TyCon

  The representation tycon for `dit_tc` (for data family instances). Otherwise the same as `dit_tc`.

- dit_rep_tc_args :: [Type]

  The representation types for `dit_tc_args` (for data family instances). Otherwise the same as `dit_tc_args`.

- dit_dc_inst_arg_env :: DataConEnv [Type]

  The cached results of instantiating each data constructor's field types using ``dataConInstUnivs` data_con `dit_rep_tc_args``. See `Note [Instantiating field types in stock deriving]`.

  This field is only used for stock-derived instances and goes unused for newtype-derived instances. It is put here mainly for the sake of convenience.

#### Instances

Instances details

Outputable DerivInstTys Source # |
Instance details Defined in GHC.Tc.Deriv.Generate Methods ppr :: DerivInstTys -> SDoc Source #

buildDataConInstArgEnv :: TyCon -> [Type] -> DataConEnv [Type] Source #

``buildDataConInstArgEnv` tycon arg_tys` constructs a cache that maps each of `tycon`'s data constructors to their field types, with are to be instantiated with `arg_tys`. See `Note [Instantiating field types in stock deriving]`.

derivDataConInstArgTys :: DataCon -> DerivInstTys -> [Type] Source #

Look up a data constructor's instantiated field types in a `DerivInstTys`. See `Note [Instantiating field types in stock deriving]`.

substDerivInstTys :: Subst -> DerivInstTys -> DerivInstTys Source #

Apply a substitution to all of the `Type`s contained in a `DerivInstTys`. See `Note [Instantiating field types in stock deriving]` for why we need to substitute into a `DerivInstTys` in the first place.

zonkDerivInstTys :: ZonkEnv -> DerivInstTys -> TcM DerivInstTys Source #

Zonk the `TcTyVar`s in a `DerivInstTys` value to `TyVar`s. See `Note [What is zonking?]` in GHC.Tc.Utils.TcMType.

This is only used in the final zonking step when inferring the context for a derived instance. See `Note [Overlap and deriving]` in GHC.Tc.Deriv.Infer.