Module

Data.Functor.Variant

#VariantF

data VariantF (f :: # Type) a

Instances

#inj

inj :: forall sym f a r1 r2. Cons sym (FProxy f) r1 r2 => IsSymbol sym => Functor f => SProxy sym -> f a -> VariantF r2 a

Inject into the variant at a given label.

maybeAtFoo :: forall r. VariantF (foo :: FProxy Maybe | r) Int
maybeAtFoo = inj (SProxy :: SProxy "foo") (Just 42)

#prj

prj :: forall sym f a r1 r2 g. Cons sym (FProxy f) r1 r2 => Alternative g => IsSymbol sym => SProxy sym -> VariantF r2 a -> g (f a)

Attempt to read a variant at a given label.

case prj (SProxy :: SProxy "foo") maybeAtFoo of
  Just (Just i) -> i + 1
  _ -> 0

#on

on :: forall sym f a b r1 r2. Cons sym (FProxy f) r1 r2 => IsSymbol sym => SProxy sym -> (f a -> b) -> (VariantF r1 a -> b) -> VariantF r2 a -> b

Attempt to read a variant at a given label by providing branches. The failure branch receives the provided variant, but with the label removed.

#onMatch

onMatch :: forall rl r r1 r2 r3 a b. RowToList r rl => VariantFMatchCases rl r1 a b => Union r1 r2 r3 => {  | r } -> (VariantF r2 a -> b) -> VariantF r3 a -> b

Match a VariantF with a Record containing functions for handling cases. This is similar to on, except instead of providing a single label and handler, you can provide a record where each field maps to a particular VariantF case.

onMatch
 { foo: \foo -> "Foo: " <> maybe "nothing" id foo
 , bar: \bar -> "Bar: " <> snd bar
 }

Polymorphic functions in records (such as show or id) can lead to inference issues if not all polymorphic variables are specified in usage. When in doubt, label methods with specific types, such as show :: Int -> String, or give the whole record an appropriate type.

#case_

case_ :: forall a b. VariantF () a -> b

Combinator for exhaustive pattern matching.

caseFn :: VariantF (foo :: FProxy Maybe, bar :: FProxy (Tuple String), baz :: FProxy (Either String)) Int -> String
caseFn = case_
 # on (SProxy :: SProxy "foo") (\foo -> "Foo: " <> maybe "nothing" show foo)
 # on (SProxy :: SProxy "bar") (\bar -> "Bar: " <> show (snd bar))
 # on (SProxy :: SProxy "baz") (\baz -> "Baz: " <> either id show baz)

#match

match :: forall rl r r1 r2 a b. RowToList r rl => VariantFMatchCases rl r1 a b => Union r1 () r2 => {  | r } -> VariantF r2 a -> b

Combinator for exhaustive pattern matching using an onMatch case record.

matchFn :: VariantF (foo :: FProxy Maybe, bar :: FProxy (Tuple String), baz :: FProxy (Either String)) Int -> String
matchFn = match
 { foo: \foo -> "Foo: " <> maybe "nothing" show foo
 , bar: \bar -> "Bar: " <> show (snd bar)
 , baz: \baz -> "Baz: " <> either id show baz
 }

#default

default :: forall a b r. a -> VariantF r b -> a

Combinator for partial matching with a default value in case of failure.

caseFn :: forall r. VariantF (foo :: FProxy Maybe, bar :: FProxy (Tuple String) | r) Int -> String
caseFn = default "No match"
 # on (SProxy :: SProxy "foo") (\foo -> "Foo: " <> maybe "nothing" show foo)
 # on (SProxy :: SProxy "bar") (\bar -> "Bar: " <> show (snd bar))

#expand

expand :: forall lt mix gt a. Union lt mix gt => VariantF lt a -> VariantF gt a

Every VariantF lt a can be cast to some VariantF gt a as long as lt is a subset of gt.

#contract

contract :: forall lt gt f a. Alternative f => Contractable gt lt => VariantF gt a -> f (VariantF lt a)

A VariantF gt a can be cast to some VariantF lt a, where lt is is a subset of gt, as long as there is proof that the VariantF's runtime tag is within the subset of lt.

#UnvariantF

newtype UnvariantF r a

Constructors

#UnvariantF'

type UnvariantF' r a x = forall s f o. IsSymbol s => Cons s (FProxy f) o r => Functor f => SProxy s -> f a -> x

#unvariantF

unvariantF :: forall r a. VariantF r a -> UnvariantF r a

A low-level eliminator which reifies the IsSymbol, Cons and Functor constraints require to reconstruct the Variant. This lets you work generically with some VariantF at runtime.

#revariantF

revariantF :: forall r a. UnvariantF r a -> VariantF r a

Reconstructs a VariantF given an UnvariantF eliminator.

#VariantFShows

class VariantFShows (rl :: RowList) x  where

Members

Instances

#TraversableVFRL

class (FoldableVFRL rl row) <= TraversableVFRL (rl :: RowList) (row :: # Type) | rl -> row where

Members

Instances

#FoldableVFRL

class FoldableVFRL (rl :: RowList) (row :: # Type) | rl -> row where

Members

Instances

Re-exports from Data.Symbol

#SProxy

data SProxy (sym :: Symbol)

A value-level proxy for a type-level symbol.

Constructors

Re-exports from Data.Variant.Internal

#FProxy

data FProxy (a :: Type -> Type)

Proxy for a Functor.

Constructors

#Contractable

class Contractable gt lt 

Instances

#VariantFMatchCases

class VariantFMatchCases (rl :: RowList) (vo :: # Type) a b | rl -> vo a b

Instances

Modules