Copyright | (c) Raghu Kaippully 2020 |
---|---|
License | MPL-2.0 |
Maintainer | rkaippully@gmail.com |
Safe Haskell | None |
Language | Haskell2010 |
Traits are optional attributes that a value might posess. For example,
a list containing totally ordered values might have a Maximum
trait
where the associated attribute is the maximum value. The trait exists
only if the list is non-empty.
Traits help to access these attributes in a type-safe manner.
Traits are somewhat similar to refinement types, but allow arbitrary attributes to be associated with a value instead of only a predicate.
Synopsis
- class Monad m => Trait t a m where
- type Val t a
- type Fail t a
- check :: a -> m (CheckResult t a)
- data CheckResult t a
- = CheckSuccess a (Val t a)
- | CheckFail (Fail t a)
- data Linked (ts :: [Type]) a
- type family Traits ts a m :: Constraint where ...
- linkzero :: a -> Linked '[] a
- linkplus :: Trait t a m => Linked ts a -> m (Either (Fail t a) (Linked (t ': ts) a))
- linkminus :: Linked (t ': ts) a -> Linked ts a
- unlink :: Linked ts a -> a
- class Has t ts where
- traitValue :: Linked ts a -> Tagged t (Val t a)
- type family Have ts qs :: Constraint where ...
Core Types
class Monad m => Trait t a m where Source #
A Trait
is an optional attribute t
associated with a value a
.
The check
function validates the presence of the trait for a
given value. Checking the presence of the trait can optionally
modify the value as well.
check :: a -> m (CheckResult t a) Source #
Checks the presence of the associated attribute.
Instances
data CheckResult t a Source #
Result of a check
operation
CheckSuccess a (Val t a) | |
CheckFail (Fail t a) |
Instances
(Eq a, Eq (Val t a), Eq (Fail t a)) => Eq (CheckResult t a) Source # | |
Defined in WebGear.Trait (==) :: CheckResult t a -> CheckResult t a -> Bool # (/=) :: CheckResult t a -> CheckResult t a -> Bool # | |
(Read a, Read (Val t a), Read (Fail t a)) => Read (CheckResult t a) Source # | |
Defined in WebGear.Trait readsPrec :: Int -> ReadS (CheckResult t a) # readList :: ReadS [CheckResult t a] # readPrec :: ReadPrec (CheckResult t a) # readListPrec :: ReadPrec [CheckResult t a] # | |
(Show a, Show (Val t a), Show (Fail t a)) => Show (CheckResult t a) Source # | |
Defined in WebGear.Trait showsPrec :: Int -> CheckResult t a -> ShowS # show :: CheckResult t a -> String # showList :: [CheckResult t a] -> ShowS # |
type family Traits ts a m :: Constraint where ... Source #
Constraint for functions that use multiple traits
Linking values with traits
linkplus :: Trait t a m => Linked ts a -> m (Either (Fail t a) (Linked (t ': ts) a)) Source #
Attempt to link an additional trait with an already linked value
linkminus :: Linked (t ': ts) a -> Linked ts a Source #
Remove the leading trait from the linked value
Retrive trait attributes from linked values
Constraint that proves that the trait t
is present somewhere in
the list of traits ts
.