HORIZON HASKELLDocslts/ghc-10.0.x77a86622026-10-31Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 77a8662 · 2026-10-31

Modulevinyl-0.14.3Haskell2010

Data.Vinyl.CoRec

Co-records: open sum types.

Consider a record with three fields A, B, and C. A record is a product of its fields; that is, it contains all of them: A, B, and C. If we want to talk about a value whose type is one of those three types, it is any one of type A, B, or C. The type CoRec '[A,B,C] corresponds to this sum type.

Contents
  1. Extracting values from a CoRec/Pattern matching on a CoRec
  2. Safe Variants
Synopsis
  • Packagevinyl-0.14.3
  • Exports32
  • LanguageHaskell2010
  • LicenceMIT
  • SourceCoRec.hs
newtypenewtype Op b a

A function type constructor that takes its arguments in the reverse order.

Constructors

classclass ShowF (f :: k -> Type) (a :: k) where

Helper for writing a Show instance for CoRec. This lets us ask for a Show constraint on the type formed by applying a type constructor to a type index.

Methods

Instances1ShowF
  • Show (f a) => ShowF f aDefined in vinyl-0.14.3 · Data.Vinyl.CoRec
valuecoRecMap :: (forall (x :: k). f x -> g x) -> CoRec f ts -> CoRec g ts

Apply a natural transformation to a variant.

valueonCoRec
  1. :: RPureConstrained c ts
  2. => forall (a :: k1). (a ∈ ts, c a) => f a -> g b
  3. -> CoRec f ts
  4. -> g b

Apply methods from a type class to a CoRec. Intended for use with TypeApplications, e.g. onCoRec @Show show r

Extracting values from a CoRec/Pattern matching on a CoRec

valuevariantIndexOf :: CoRec f ts -> Int

Compute a runtime Int index identifying the position of the variant held by a CoRec f ts in the type-level list ts.

valuematch :: CoRec Identity ts -> Handlers ts b -> b

Pattern match on a CoRec by specifying handlers for each case. Note that the order of the Handlers has to match the type level list (t:ts).

Example1 expression
:{let testCoRec = Col (Identity False) :: CoRec Identity [Int, String, Bool] inmatch testCoRec $      (H $ \i -> "my Int is the successor of " ++ show (i - 1))   :& (H $ \s -> "my String is: " ++ s)   :& (H $ \b -> "my Bool is not: " ++ show (not b) ++ " thus it is " ++ show b)   :& RNil:}"my Bool is not: True thus it is False"
classclass RIndex t ts ~ i => Match1 t (ts :: [Type]) (i :: Nat) where

Helper for handling a variant of a CoRec: either the function is applied to the variant or the type of the CoRec is refined to reflect the fact that the variant is not compatible with the type of the would-be handler.

Methods

Instances2Match1
  • Match1 t (t ': ts) 'ZDefined in vinyl-0.14.3 · Data.Vinyl.CoRec
  • (Match1 t ts i, RIndex t (s ': ts) ~ 'S i, RDelete t (s ': ts) ~ (s ': RDelete t ts)) => Match1 t (s ': ts) ('S i)Defined in vinyl-0.14.3 · Data.Vinyl.CoRec
newtypenewtype Handler b a

Newtype around functions for a to b

Constructors

  • H (a -> b)
typetype Handlers (ts :: [Type]) b = Rec (Handler b) ts

'Handlers ts b', is essentially a list of functions, one for each type in ts. All functions produce a value of type b. Hence, 'Handlers ts b' would represent something like the type-level list: [t -> b | t in ts ]

Safe Variants