Safe Haskell | Safe-Inferred |
---|---|
Language | GHC2021 |
While writing function decorators, we often need to store the arguments
of the function in a n-ary product.
is useful for that. multiuncurry
@(<-)
>>>
:{
type Fun0 = Int type UFun0 = NP I '[] -> Int type Fun1 = Bool -> Int type UFun1 = NP I '[Bool] -> Int type Fun2 = Char -> Bool -> Int type UFun2 = NP I '[Char, Bool] -> Int ufun0 :: UFun0 = multiuncurry @(->) @_ @_ @Fun0 $ 5 ufun1 :: UFun1 = multiuncurry @(->) @_ @_ @Fun1 $ \_ -> 5 ufun2 :: UFun2 = multiuncurry @(->) @_ @_ @Fun2 $ \_ _ -> 5 fun0 :: Fun0 = multicurry @(->) @_ @_ ufun0 fun1 :: Fun1 = multicurry @(->) @_ @_ ufun1 fun2 :: Fun2 = multicurry @(->) @_ @_ ufun2 :}
Less often, when processing the result of functions, we have a nested chain
of Either
s like Either Err1 (Either Err2 (Either Err3 Success))
, and want to put all the errors in a top-level Left
branch,
and the lone Success
value in a top-level Right
branch.
is useful for that. multiuncurry
@Either
>>>
:{
type Eith0 = Int type Eith1 = Either Bool Int type Eith2 = Either Char (Either Bool Int) type UEith0 = Either (NS I '[]) Int type UEith1 = Either (NS I '[Bool]) Int type UEith2 = Either (NS I '[Char, Bool]) Int ueith0 :: UEith0 = multiuncurry @Either @_ @_ @Eith0 5 ueith1 :: UEith1 = multiuncurry @Either @_ @_ @Eith1 $ Right 5 ueith2 :: UEith2 = multiuncurry @Either @_ @_ @Eith2 $ Right (Right 5) eith0 :: Eith0 = multicurry @Either @_ @_ ueith0 eith1 :: Eith1 = multicurry @Either @_ @_ ueith1 eith2 :: Eith2 = multicurry @Either @_ @_ ueith2 :}
The Multicurryable
class will get terribly confused if it can't determine
the rightmost type, because it can't be sure it's not another (->)
, or
another Either
. So use it only with concrete rightmost types, not
polymorphic ones.
Synopsis
- class Multicurryable (f :: Type -> Type -> Type) (items :: [Type]) a curried | f items a -> curried, f curried -> items a where
- type UncurriedArgs f :: [Type] -> Type
- multiuncurry :: curried -> f (UncurriedArgs f items) a
- multicurry :: f (UncurriedArgs f items) a -> curried
- class MulticurryableF items a curried (decomp :: Where) | items a -> curried decomp, curried decomp -> items a
- type family IsFunction f :: Where where ...
- class MulticurryableE items a curried (decomp :: Where) | items a -> curried decomp, curried decomp -> items a
- type family IsEither f :: Where where ...
- data NP (a :: k -> Type) (b :: [k]) where
- data NS (a :: k -> Type) (b :: [k]) where
- newtype I a = I a
Multi-argument currying/uncurrying.
class Multicurryable (f :: Type -> Type -> Type) (items :: [Type]) a curried | f items a -> curried, f curried -> items a where Source #
type UncurriedArgs f :: [Type] -> Type Source #
multiuncurry :: curried -> f (UncurriedArgs f items) a Source #
multicurry :: f (UncurriedArgs f items) a -> curried Source #
Instances
MulticurryableE items a curried (IsEither curried) => Multicurryable Either items a curried Source # | The instance for |
Defined in Multicurryable multiuncurry :: curried -> Either (UncurriedArgs Either items) a Source # multicurry :: Either (UncurriedArgs Either items) a -> curried Source # | |
MulticurryableF items a curried (IsFunction curried) => Multicurryable (->) items a curried Source # | The instance for functions provides conventional currying/uncurrying, only
that it works for multiple arguments, and the uncurried arguments are stored
in a |
Defined in Multicurryable type UncurriedArgs (->) :: [Type] -> Type Source # multiuncurry :: curried -> UncurriedArgs (->) items -> a Source # multicurry :: (UncurriedArgs (->) items -> a) -> curried Source # |
Helpers for (->)
class MulticurryableF items a curried (decomp :: Where) | items a -> curried decomp, curried decomp -> items a Source #
multiuncurryF, multicurryF
type family IsFunction f :: Where where ... Source #
IsFunction (_ -> _) = 'NotYetThere | |
IsFunction _ = 'AtTheTip |
Helpers for Either
class MulticurryableE items a curried (decomp :: Where) | items a -> curried decomp, curried decomp -> items a Source #
multiuncurryE, multicurryE
sop-core re-exports
data NP (a :: k -> Type) (b :: [k]) where #
An n-ary product.
The product is parameterized by a type constructor f
and
indexed by a type-level list xs
. The length of the list
determines the number of elements in the product, and if the
i
-th element of the list is of type x
, then the i
-th
element of the product is of type f x
.
The constructor names are chosen to resemble the names of the list constructors.
Two common instantiations of f
are the identity functor I
and the constant functor K
. For I
, the product becomes a
heterogeneous list, where the type-level list describes the
types of its components. For
, the product becomes a
homogeneous list, where the contents of the type-level list are
ignored, but its length still specifies the number of elements.K
a
In the context of the SOP approach to generic programming, an n-ary product describes the structure of the arguments of a single data constructor.
Examples:
I 'x' :* I True :* Nil :: NP I '[ Char, Bool ] K 0 :* K 1 :* Nil :: NP (K Int) '[ Char, Bool ] Just 'x' :* Nothing :* Nil :: NP Maybe '[ Char, Bool ]
Nil :: forall {k} (a :: k -> Type). NP a ('[] :: [k]) | |
(:*) :: forall {k} (a :: k -> Type) (x :: k) (xs :: [k]). a x -> NP a xs -> NP a (x ': xs) infixr 5 |
Instances
HTrans (NP :: (k1 -> Type) -> [k1] -> Type) (NP :: (k2 -> Type) -> [k2] -> Type) | |
Defined in Data.SOP.NP htrans :: forall c (xs :: l1) (ys :: l2) proxy f g. AllZipN (Prod NP) c xs ys => proxy c -> (forall (x :: k10) (y :: k20). c x y => f x -> g y) -> NP f xs -> NP g ys # hcoerce :: forall (f :: k10 -> Type) (g :: k20 -> Type) (xs :: l1) (ys :: l2). AllZipN (Prod NP) (LiftedCoercible f g) xs ys => NP f xs -> NP g ys # | |
HAp (NP :: (k -> Type) -> [k] -> Type) | |
HCollapse (NP :: (k -> Type) -> [k] -> Type) | |
Defined in Data.SOP.NP | |
HPure (NP :: (k -> Type) -> [k] -> Type) | |
HSequence (NP :: (k -> Type) -> [k] -> Type) | |
Defined in Data.SOP.NP hsequence' :: forall (xs :: l) f (g :: k0 -> Type). (SListIN NP xs, Applicative f) => NP (f :.: g) xs -> f (NP g xs) # hctraverse' :: forall c (xs :: l) g proxy f f'. (AllN NP c xs, Applicative g) => proxy c -> (forall (a :: k0). c a => f a -> g (f' a)) -> NP f xs -> g (NP f' xs) # htraverse' :: forall (xs :: l) g f f'. (SListIN NP xs, Applicative g) => (forall (a :: k0). f a -> g (f' a)) -> NP f xs -> g (NP f' xs) # | |
HTraverse_ (NP :: (k -> Type) -> [k] -> Type) | |
Defined in Data.SOP.NP hctraverse_ :: forall c (xs :: l) g proxy f. (AllN NP c xs, Applicative g) => proxy c -> (forall (a :: k0). c a => f a -> g ()) -> NP f xs -> g () # htraverse_ :: forall (xs :: l) g f. (SListIN NP xs, Applicative g) => (forall (a :: k0). f a -> g ()) -> NP f xs -> g () # | |
(All (Compose Monoid f) xs, All (Compose Semigroup f) xs) => Monoid (NP f xs) | Since: sop-core-0.4.0.0 |
All (Compose Semigroup f) xs => Semigroup (NP f xs) | Since: sop-core-0.4.0.0 |
All (Compose Show f) xs => Show (NP f xs) | |
All (Compose NFData f) xs => NFData (NP f xs) | Since: sop-core-0.2.5.0 |
Defined in Data.SOP.NP | |
All (Compose Eq f) xs => Eq (NP f xs) | |
(All (Compose Eq f) xs, All (Compose Ord f) xs) => Ord (NP f xs) | |
type AllZipN (NP :: (k -> Type) -> [k] -> Type) (c :: a -> b -> Constraint) | |
Defined in Data.SOP.NP | |
type Same (NP :: (k1 -> Type) -> [k1] -> Type) | |
type Prod (NP :: (k -> Type) -> [k] -> Type) | |
type UnProd (NP :: (k -> Type) -> [k] -> Type) | |
type SListIN (NP :: (k -> Type) -> [k] -> Type) | |
Defined in Data.SOP.NP | |
type CollapseTo (NP :: (k -> Type) -> [k] -> Type) a | |
Defined in Data.SOP.NP | |
type AllN (NP :: (k -> Type) -> [k] -> Type) (c :: k -> Constraint) | |
Defined in Data.SOP.NP |
data NS (a :: k -> Type) (b :: [k]) where #
An n-ary sum.
The sum is parameterized by a type constructor f
and
indexed by a type-level list xs
. The length of the list
determines the number of choices in the sum and if the
i
-th element of the list is of type x
, then the i
-th
choice of the sum is of type f x
.
The constructor names are chosen to resemble Peano-style
natural numbers, i.e., Z
is for "zero", and S
is for
"successor". Chaining S
and Z
chooses the corresponding
component of the sum.
Examples:
Z :: f x -> NS f (x ': xs) S . Z :: f y -> NS f (x ': y ': xs) S . S . Z :: f z -> NS f (x ': y ': z ': xs) ...
Note that empty sums (indexed by an empty list) have no non-bottom elements.
Two common instantiations of f
are the identity functor I
and the constant functor K
. For I
, the sum becomes a
direct generalization of the Either
type to arbitrarily many
choices. For
, the result is a homogeneous choice type,
where the contents of the type-level list are ignored, but its
length specifies the number of options.K
a
In the context of the SOP approach to generic programming, an n-ary sum describes the top-level structure of a datatype, which is a choice between all of its constructors.
Examples:
Z (I 'x') :: NS I '[ Char, Bool ] S (Z (I True)) :: NS I '[ Char, Bool ] S (Z (K 1)) :: NS (K Int) '[ Char, Bool ]
Z :: forall {k} (a :: k -> Type) (x :: k) (xs :: [k]). a x -> NS a (x ': xs) | |
S :: forall {k} (a :: k -> Type) (xs :: [k]) (x :: k). NS a xs -> NS a (x ': xs) |
Instances
HTrans (NS :: (k1 -> Type) -> [k1] -> Type) (NS :: (k2 -> Type) -> [k2] -> Type) | |
Defined in Data.SOP.NS htrans :: forall c (xs :: l1) (ys :: l2) proxy f g. AllZipN (Prod NS) c xs ys => proxy c -> (forall (x :: k10) (y :: k20). c x y => f x -> g y) -> NS f xs -> NS g ys # hcoerce :: forall (f :: k10 -> Type) (g :: k20 -> Type) (xs :: l1) (ys :: l2). AllZipN (Prod NS) (LiftedCoercible f g) xs ys => NS f xs -> NS g ys # | |
HAp (NS :: (k -> Type) -> [k] -> Type) | |
HApInjs (NS :: (k -> Type) -> [k] -> Type) | |
HCollapse (NS :: (k -> Type) -> [k] -> Type) | |
Defined in Data.SOP.NS | |
HExpand (NS :: (k -> Type) -> [k] -> Type) | |
HIndex (NS :: (k -> Type) -> [k] -> Type) | |
HSequence (NS :: (k -> Type) -> [k] -> Type) | |
Defined in Data.SOP.NS hsequence' :: forall (xs :: l) f (g :: k0 -> Type). (SListIN NS xs, Applicative f) => NS (f :.: g) xs -> f (NS g xs) # hctraverse' :: forall c (xs :: l) g proxy f f'. (AllN NS c xs, Applicative g) => proxy c -> (forall (a :: k0). c a => f a -> g (f' a)) -> NS f xs -> g (NS f' xs) # htraverse' :: forall (xs :: l) g f f'. (SListIN NS xs, Applicative g) => (forall (a :: k0). f a -> g (f' a)) -> NS f xs -> g (NS f' xs) # | |
HTraverse_ (NS :: (k -> Type) -> [k] -> Type) | |
Defined in Data.SOP.NS hctraverse_ :: forall c (xs :: l) g proxy f. (AllN NS c xs, Applicative g) => proxy c -> (forall (a :: k0). c a => f a -> g ()) -> NS f xs -> g () # htraverse_ :: forall (xs :: l) g f. (SListIN NS xs, Applicative g) => (forall (a :: k0). f a -> g ()) -> NS f xs -> g () # | |
All (Compose Show f) xs => Show (NS f xs) | |
All (Compose NFData f) xs => NFData (NS f xs) | Since: sop-core-0.2.5.0 |
Defined in Data.SOP.NS | |
All (Compose Eq f) xs => Eq (NS f xs) | |
(All (Compose Eq f) xs, All (Compose Ord f) xs) => Ord (NS f xs) | |
type Same (NS :: (k1 -> Type) -> [k1] -> Type) | |
type Prod (NS :: (k -> Type) -> [k] -> Type) | |
type SListIN (NS :: (k -> Type) -> [k] -> Type) | |
Defined in Data.SOP.NS | |
type CollapseTo (NS :: (k -> Type) -> [k] -> Type) a | |
Defined in Data.SOP.NS | |
type AllN (NS :: (k -> Type) -> [k] -> Type) (c :: k -> Constraint) | |
Defined in Data.SOP.NS |
The identity type functor.
Like Identity
, but with a shorter name.
I a |
Instances
Foldable I | |
Defined in Data.SOP.BasicFunctors fold :: Monoid m => I m -> m # foldMap :: Monoid m => (a -> m) -> I a -> m # foldMap' :: Monoid m => (a -> m) -> I a -> m # foldr :: (a -> b -> b) -> b -> I a -> b # foldr' :: (a -> b -> b) -> b -> I a -> b # foldl :: (b -> a -> b) -> b -> I a -> b # foldl' :: (b -> a -> b) -> b -> I a -> b # foldr1 :: (a -> a -> a) -> I a -> a # foldl1 :: (a -> a -> a) -> I a -> a # elem :: Eq a => a -> I a -> Bool # maximum :: Ord a => I a -> a # | |
Eq1 I | Since: sop-core-0.2.4.0 |
Ord1 I | Since: sop-core-0.2.4.0 |
Defined in Data.SOP.BasicFunctors | |
Read1 I | Since: sop-core-0.2.4.0 |
Show1 I | Since: sop-core-0.2.4.0 |
Traversable I | |
Applicative I | |
Functor I | |
Monad I | |
NFData1 I | Since: sop-core-0.2.5.0 |
Defined in Data.SOP.BasicFunctors | |
Monoid a => Monoid (I a) | Since: sop-core-0.4.0.0 |
Semigroup a => Semigroup (I a) | Since: sop-core-0.4.0.0 |
Generic (I a) | |
Read a => Read (I a) | |
Show a => Show (I a) | |
NFData a => NFData (I a) | Since: sop-core-0.2.5.0 |
Defined in Data.SOP.BasicFunctors | |
Eq a => Eq (I a) | |
Ord a => Ord (I a) | |
type Rep (I a) | |
Defined in Data.SOP.BasicFunctors |