HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulevinyl-0.14.3Haskell2010

Data.Vinyl.Class.Method

This module uses RecAll to extend common typeclass methods to records. Generally, it is preferable to use the original typeclass methods to these variants. For example, in most places where recCompare could be used, you could use compare instead. They are useful in scenarios that involve working on unknown subsets of a record's fields because RecAll constraints can easily be weakened. An example of this is given at the bottom of this page.

  • 1 type
  • 3 classes
  • 17 values
  • Packagevinyl-0.14.3
  • Exports23
  • LanguageHaskell2010
  • LicenceMIT
  • SourceMethod.hs

Mapping methods over records

7 declarations
classclass RecMapMethod (c :: Type -> Constraint) (f :: u -> Type) (ts :: [u]) where
#

Apply a typeclass method to each field of a Rec where the class constrains the index of the field, but not its interpretation functor.

Methods

Instances2RecMapMethod
valuermapMethodF
  1. :: (Functor f, FieldPayload f ~ 'FieldId, RecMapMethod c f ts)
  2. => forall a. c a => a -> a
  3. -> Rec f ts
  4. -> Rec f ts
#

Apply a typeclass method to each field of a Rec f ts using the Functor instance for f to lift the function into the functor. This is a commonly-used specialization of rmapMethod composed with fmap.

classclass RecMapMethod1 (c :: Type -> Constraint) (f :: u -> Type) (ts :: [u]) where
#

Apply a typeclass method to each field of a Rec where the class constrains the field when considered as a value interpreted by the record's interpretation functor.

Methods

Instances2RecMapMethod1
classclass RecPointed (c :: Type -> Constraint) (f :: u -> Type) (ts :: [u]) where
#

Generate a record from fields derived from type class instances.

Methods

Instances2RecPointed
  • RecPointed c f '[]Defined in vinyl-0.14.3 · Data.Vinyl.Class.Method
  • (c (f t), RecPointed c f ts) => RecPointed c f (t ': ts)Defined in vinyl-0.14.3 · Data.Vinyl.Class.Method

Support for RecMapMethod

3 declarations
datadata FieldTyper
#

When we wish to apply a typeclass method to each field of a Rec, we typically care about typeclass instances of the record field types irrespective of the record's functor context. To expose the field types themselves, we utilize a constraint built from a defunctionalized type family in the rmapMethod method. The symbols of the function space are defined by this data type.

Eq Functions

1 declaration

Ord Functions

1 declaration

Monoid Functions

3 declarations
valuerecMempty :: RecAll f rs Monoid => Rec proxy rs -> Rec f rs
#

This function differs from the original mempty in that it takes an argument. In some cases, you will already have a record of the type you are interested in, and that can be passed an the argument. In other situations where this is not the case, you may need the interpretation function of the argument record to be Const () or Proxy so the you can generate the argument with rpure.

Num Functions

6 declarations

Bounded Functions

2 declarations

Example

0 declarations

This module provides variants of typeclass methods that have a RecAll constraint instead of the normal typeclass constraint. For example, a type-specialized compare would look like this:

compare :: Ord (Rec f rs) => Rec f rs -> Rec f rs -> Ordering

The recCompare function looks like this:

recCompare :: RecAll f rs Ord => Rec f rs -> Rec f rs -> Ordering

The only difference is the constraint. Let's look at a potential use case for these functions.

Let's write a function that projects out a subrecord from two records and then compares those for equality. We can write this with the <: operator from Data.Vinyl.Lens and the normal compare function. We don't need recCompare:

-- This needs ScopedTypeVariables
projectAndCompare :: forall super sub f. (super <: sub, Ord (Rec f sub))
                  => Proxy sub -> Rec f super -> Rec f super -> Ordering
projectAndCompare _ a b = compare (rcast a :: Rec f sub) (rcast b :: Rec f sub)

That works fine for the majority of use cases, and it is probably how you should write the function if it does everything you need. However, let's consider a somewhat more complicated case.

What if the exact subrecord we were projecting couldn't be known at compile time? Assume that the end user was allowd to choose the fields on which he or she wanted to compare records. The projectAndCompare function cannot handle this because of the Ord (Rec f sub) constraint. Even if we amend the constraint to read Ord (Rec f super) instead, we cannot use this information to recover the Ord (Rec f sub) constraint that we need. Let's try another approach.

We can use the following GADT to prove subsethood:

data Sublist (super :: [k]) (sub :: [k]) where
  SublistNil   :: Sublist '[]
  SublistSuper :: Proxy r -> Sublist super sub -> Sublist (r ': super) sub
  SublistBoth  :: Proxy r -> Sublist super sub -> Sublist (r ': super) (r ': sub)

projectRec :: Sublist super sub -> Rec f super -> Rec f sub
projectRec s r = case s of
  SublistNil -> RNil
  SublistBoth n snext -> case r of
    rhead :& rtail -> rhead :& projectRec snext rtail
  SublistSuper n snext -> case r of
    rhead :& rtail -> projectRec snext rtail

It is also possible to write a typeclass to generate Sublists implicitly, but that is beyond the scope of this example. Let's now write a function to use Sublist to weaken a RecAll constraint:

import Data.Vinyl.Core hiding (Dict)
import Data.Constraint

weakenRecAll :: Proxy f -> Proxy c -> Sublist super sub -> RecAll f super c :- RecAll f sub c
weakenRecAll f c s = case s of
  SublistNil -> Sub Dict
  SublistSuper _ snext -> Sub $ case weakenRecAll f c snext of
    Sub Dict -> Dict
  SublistBoth _ snext -> Sub $ case weakenRecAll f c snext of
    Sub Dict -> Dict

Now we can write a different version of our original function:

-- This needs ScopedTypeVariables
projectAndCompare2 :: forall super sub f. (RecAll f super Ord)
                   => Sublist super sub -> Rec f super -> Rec f super -> Ordering
projectAndCompare2 s a b = case weakenRecAll (Proxy :: Proxy f) (Proxy :: Proxy Ord) s of
  Sub Dict -> recCompare (projectRec s a) (projectRec s b)

Notice that in this case, the Ord constraint applies to the full set of fields and is then weakened to target a subset of them instead.