Data.Variant
- Package
- purescript-variant
- Repository
- natefaubion/purescript-variant
#Variant Source
data Variant :: Row Type -> Type
data Variant t0
Instances
(RowToList r rl, VariantTags rl, VariantEqs rl) => Eq (Variant r)
(RowToList r rl, VariantTags rl, VariantEqs rl, VariantOrds rl, VariantBounded rl) => Bounded (Variant r)
(RowToList r rl, VariantTags rl, VariantEqs rl, VariantOrds rl, VariantBoundedEnums rl) => Enum (Variant r)
(RowToList r rl, VariantTags rl, VariantEqs rl, VariantOrds rl, VariantBoundedEnums rl) => BoundedEnum (Variant r)
(RowToList r rl, VariantTags rl, VariantEqs rl, VariantOrds rl) => Ord (Variant r)
(RowToList r rl, VariantTags rl, VariantShows rl) => Show (Variant r)
#onMatch Source
onMatch :: forall rl r r1 r2 r3 b. RowToList r rl => VariantMatchCases rl r1 b => Union r1 r2 r3 => Record r -> (Variant r2 -> b) -> Variant r3 -> b
Match a Variant
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
Variant
case.
onMatch
{ foo: \foo -> "Foo: " <> foo
, bar: \bar -> "Bar: " <> 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.
#over Source
over :: forall r rl ri ro r1 r2 r3. RowToList r rl => VariantMapCases rl ri ro => Union ri r2 r1 => Union ro r2 r3 => Record r -> Variant r1 -> Variant r3
Map over some labels and leave the rest unchanged. For example:
over { label: show :: Int -> String }
:: forall r. Variant ( label :: Int | r ) -> Variant ( label :: String | r )
over r
is like expand # overSome r
but with a more easily
solved constraint (i.e. it can be solved once the type of r
is known).
#overOne Source
overOne :: forall sym a b r1 r2 r3 r4. IsSymbol sym => Cons sym a r1 r2 => Cons sym b r4 r3 => Proxy sym -> (a -> b) -> (Variant r1 -> Variant r3) -> Variant r2 -> Variant r3
Map over one case of a variant, putting the result back at the same label, with a fallback function to handle the remaining cases.
#overSome Source
overSome :: forall r rl ri ro r1 r2 r3 r4. RowToList r rl => VariantMapCases rl ri ro => Union ri r2 r1 => Union ro r4 r3 => Record r -> (Variant r2 -> Variant r3) -> Variant r1 -> Variant r3
Map over several cases of a variant using a Record
containing functions
for each case. Each case gets put back at the same label it was matched
at, i.e. its label in the record. Labels not found in the record are
handled using the fallback function.
#case_ Source
case_ :: forall a. Variant () -> a
Combinator for exhaustive pattern matching.
caseFn :: Variant (foo :: Int, bar :: String, baz :: Boolean) -> String
caseFn = case_
# on (Proxy :: Proxy "foo") (\foo -> "Foo: " <> show foo)
# on (Proxy :: Proxy "bar") (\bar -> "Bar: " <> bar)
# on (Proxy :: Proxy "baz") (\baz -> "Baz: " <> show baz)
#match Source
match :: forall rl r r1 r2 b. RowToList r rl => VariantMatchCases rl r1 b => Union r1 () r2 => Record r -> Variant r2 -> b
Combinator for exhaustive pattern matching using an onMatch
case record.
matchFn :: Variant (foo :: Int, bar :: String, baz :: Boolean) -> String
matchFn = match
{ foo: \foo -> "Foo: " <> show foo
, bar: \bar -> "Bar: " <> bar
, baz: \baz -> "Baz: " <> show baz
}
#default Source
default :: forall a r. 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 (Proxy :: Proxy "foo") (\foo -> "Foo: " <> show foo)
# on (Proxy :: Proxy "bar") (\bar -> "Bar: " <> bar)
#traverse Source
traverse :: forall r rl ri ro r1 r2 r3 m. RowToList r rl => VariantTraverseCases m rl ri ro => Union ri r2 r1 => Union ro r2 r3 => Applicative m => Record r -> Variant r1 -> m (Variant r3)
Traverse over some labels and leave the rest unchanged.
(Implemented by expanding after traverseSome
.)
#traverseOne Source
traverseOne :: forall sym a b r1 r2 r3 r4 m. IsSymbol sym => Cons sym a r1 r2 => Cons sym b r4 r3 => Functor m => Proxy sym -> (a -> m b) -> (Variant r1 -> m (Variant r3)) -> Variant r2 -> m (Variant r3)
Traverse over one case of a variant (in a functorial/monadic context m
),
putting the result back at the same label, with a fallback function.
#traverseSome Source
traverseSome :: forall r rl ri ro r1 r2 r3 r4 m. RowToList r rl => VariantTraverseCases m rl ri ro => Union ri r2 r1 => Union ro r4 r3 => Functor m => Record r -> (Variant r2 -> m (Variant r3)) -> Variant r1 -> m (Variant r3)
Traverse over several cases of a variant using a Record
containing
traversals. Each case gets put back at the same label it was matched
at, i.e. its label in the record. Labels not found in the record are
handled using the fallback function.
#contract Source
contract :: forall lt gt f. 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
.
#Unvariant' Source
type Unvariant' :: Row Type -> Type -> Type
type Unvariant' r x = forall s t o. IsSymbol s => Cons s t o r => Proxy s -> t -> x
#VariantEqs Source
class VariantEqs :: RowList Type -> Constraint
class VariantEqs rl where
Members
variantEqs :: Proxy rl -> List (VariantCase -> VariantCase -> Boolean)
Instances
VariantEqs Nil
(VariantEqs rs, Eq a) => VariantEqs (Cons sym a rs)
#VariantOrds Source
class VariantOrds :: RowList Type -> Constraint
class VariantOrds rl where
Members
variantOrds :: Proxy rl -> List (VariantCase -> VariantCase -> Ordering)
Instances
VariantOrds Nil
(VariantOrds rs, Ord a) => VariantOrds (Cons sym a rs)
#VariantShows Source
class VariantShows :: RowList Type -> Constraint
class VariantShows rl where
Members
variantShows :: Proxy rl -> List (VariantCase -> String)
Instances
VariantShows Nil
(VariantShows rs, Show a) => VariantShows (Cons sym a rs)
#VariantBounded Source
class VariantBounded :: RowList Type -> Constraint
class VariantBounded rl where
Members
variantBounded :: Proxy rl -> List (BoundedDict VariantCase)
Instances
VariantBounded Nil
(VariantBounded rs, Bounded a) => VariantBounded (Cons sym a rs)
#VariantBoundedEnums Source
class VariantBoundedEnums :: RowList Type -> Constraint
class (VariantBounded rl) <= VariantBoundedEnums rl where
Members
variantBoundedEnums :: Proxy rl -> List (BoundedEnumDict VariantCase)
Instances
VariantBoundedEnums Nil
(VariantBoundedEnums rs, BoundedEnum a) => VariantBoundedEnums (Cons sym a rs)
Re-exports from Data.Variant.Internal
#Contractable Source
class Contractable :: forall k. Row k -> Row k -> Constraint
class Contractable gt lt
Instances
(RowToList lt ltl, Union lt a gt, VariantTags ltl) => Contractable gt lt
#VariantMapCases Source
class VariantMapCases :: RowList Type -> Row Type -> Row Type -> Constraint
class VariantMapCases (rl :: RowList Type) (ri :: Row Type) (ro :: Row Type) | rl -> ri ro
Instances
(Cons sym a ri' ri, Cons sym b ro' ro, VariantMapCases rl ri' ro', TypeEquals k (a -> b)) => VariantMapCases (Cons sym k rl) ri ro
VariantMapCases Nil () ()
#VariantMatchCases Source
class VariantMatchCases :: RowList Type -> Row Type -> Type -> Constraint
class VariantMatchCases rl vo b | rl -> vo b
Instances
(VariantMatchCases rl vo' b, Cons sym a vo' vo, TypeEquals k (a -> b)) => VariantMatchCases (Cons sym k rl) vo b
VariantMatchCases Nil () b
#VariantTraverseCases Source
class VariantTraverseCases :: (Type -> Type) -> RowList Type -> Row Type -> Row Type -> Constraint
class VariantTraverseCases (m :: Type -> Type) (rl :: RowList Type) (ri :: Row Type) (ro :: Row Type) | rl -> ri ro
Instances
(Cons sym a ri' ri, Cons sym b ro' ro, VariantTraverseCases m rl ri' ro', TypeEquals k (a -> m b)) => VariantTraverseCases m (Cons sym k rl) ri ro
VariantTraverseCases m Nil () ()