ghc-9.6.3: The GHC API

Safe Haskell | Safe-Inferred
Language | Haskell2010

GHC.Tc.Deriv.Functor

Description

The deriving code for the Functor, Foldable, and Traversable classes

Synopsis

- data FFoldType a = FT {
  - ft_triv :: a
  - ft_var :: a
  - ft_co_var :: a
  - ft_fun :: a -> a -> a
  - ft_tup :: TyCon -> [a] -> a
  - ft_ty_app :: Type -> Type -> a -> a
  - ft_bad_app :: a
  - ft_forall :: TcTyVar -> a -> a
  }
- functorLikeTraverse :: forall a. TyVar -> FFoldType a -> Type -> a
- deepSubtypesContaining :: TyVar -> Type -> [TcType]
- foldDataConArgs :: FFoldType a -> DataCon -> DerivInstTys -> [a]
- gen_Functor_binds :: SrcSpan -> DerivInstTys -> (LHsBinds GhcPs, Bag AuxBindSpec)
- gen_Foldable_binds :: SrcSpan -> DerivInstTys -> (LHsBinds GhcPs, Bag AuxBindSpec)
- gen_Traversable_binds :: SrcSpan -> DerivInstTys -> (LHsBinds GhcPs, Bag AuxBindSpec)

# Documentation

data FFoldType a Source #

Constructors

FT |

Fields

- ft_triv :: a

  Does not contain variable

- ft_var :: a

  The variable itself

- ft_co_var :: a

  The variable itself, contravariantly

- ft_fun :: a -> a -> a

  Function type

- ft_tup :: TyCon -> [a] -> a

  Tuple type. The `[a]` is the result of folding over the arguments of the tuple.

- ft_ty_app :: Type -> Type -> a -> a

  Type app, variable only in last argument. The two `Type`s are the function and argument parts of `fun_ty arg_ty`, respectively.

- ft_bad_app :: a

  Type app, variable other than in last argument

- ft_forall :: TcTyVar -> a -> a

  Forall type

functorLikeTraverse Source #

Arguments

:: forall a. TyVar | Variable to look for
-> FFoldType a | How to fold
-> Type | Type to process
-> a |

deepSubtypesContaining :: TyVar -> Type -> [TcType] Source #

Return all syntactic subterms of a `Type` that are applied to the `TyVar` argument. This determines what constraints should be inferred for derived `Functor`, `Foldable`, and `Traversable` instances in GHC.Tc.Deriv.Infer. For instance, if we have:

    data Foo a = MkFoo Int a (Maybe a) (Either Int (Maybe a))

Then the following would hold:

- ``deepSubtypesContaining` a Int` would return `[]`, since `Int` does not contain the type variable `a` at all.
- ``deepSubtypesContaining` a a` would return `[]`. Although the type `a` contains the type variable `a`, it is not applied to `a`, which is the criterion that `deepSubtypesContaining` checks for.
- ``deepSubtypesContaining` a (Maybe a)` would return `[Maybe]`, as `Maybe` is applied to `a`.
- ``deepSubtypesContaining` a (Either Int (Maybe a))` would return `[Either Int, Maybe]`. Both of these types are applied to `a` through composition.

As used in GHC.Tc.Deriv.Infer, the `Type` argument will always come from `derivDataConInstArgTys`, so it is important that the `TyVar` comes from `dataConUnivTyVars` to match. Make sure not to take the `TyVar` from `tyConTyVars`, as these differ from the `dataConUnivTyVars` when the data type is a GADT. (See #22167 for what goes wrong if `tyConTyVars` is used.)

foldDataConArgs :: FFoldType a -> DataCon -> DerivInstTys -> [a] Source #

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

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

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