Module

Data.Variant

Package
purescript-variant
Repository
natefaubion/purescript-variant

#Variant Source

data Variant :: Row Type -> Type

Instances

#inj Source

inj :: forall r2 r1 a sym. RowCons sym a r1 r2 => IsSymbol sym => SProxy sym -> a -> Variant r2

Inject into the variant at a given label.

intAtFoo :: forall r. Variant (foo :: Int | r)
intAtFoo = inj (SProxy :: SProxy "foo") 42

#prj Source

prj :: forall f r2 r1 a sym. RowCons sym a r1 r2 => IsSymbol sym => Alternative f => SProxy sym -> Variant r2 -> f a

Attempt to read a variant at a given label.

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

#on Source

on :: forall r2 r1 b a sym. RowCons sym a r1 r2 => IsSymbol sym => SProxy sym -> (a -> b) -> (Variant r1 -> b) -> Variant r2 -> 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.

#case_ Source

case_ :: forall a. Variant () -> a

Combinator for exhaustive pattern matching.

caseFn :: Variant (foo :: Int, bar :: String, baz :: Boolean) -> String
caseFn = case_
 # on (SProxy :: SProxy "foo") (\foo -> "Foo: " <> show foo)
 # on (SProxy :: SProxy "bar") (\bar -> "Bar: " <> bar)
 # on (SProxy :: SProxy "baz") (\baz -> "Baz: " <> show baz)

#default Source

default :: forall r a. a -> Variant r -> a

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

caseFn :: forall r. Variant (foo :: Int, bar :: String | r) -> String
caseFn = default "No match"
 # on (SProxy :: SProxy "foo") (\foo -> "Foo: " <> show foo)
 # on (SProxy :: SProxy "bar") (\bar -> "Bar: " <> bar)

#match Source

match :: forall result record variant. VariantRecordMatching variant record result => Record record -> Variant variant -> result

Match a variant with a record containing methods to handle each case to produce a result.

This means that if variant contains a row of type a, a row with the same label must have type a -> result in record, where result is the same type for every row of record.

Polymorphic methods in record may create problems with the type system if the polymorphism is not fully generalized to the whole record type or 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.

#expand Source

expand :: forall gt a lt. Union lt a gt => Variant lt -> Variant gt

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

#contract Source

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

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

#VariantEqs Source

class VariantEqs (rl :: RowList)  where

Members

Instances

#VariantOrds Source

class VariantOrds (rl :: RowList)  where

Members

Instances

#VariantShows Source

class VariantShows (rl :: RowList)  where

Members

Instances

Re-exports from Data.Symbol

#SProxy Source

data SProxy (sym :: Symbol)

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

Constructors

Re-exports from Data.Variant.Internal

#Contractable Source

class Contractable gt lt 

Instances