-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Self-normalizing applicative expressions -- -- An applicative functor transformer to normalize expressions using -- (<$>), (<*>), and pure into a -- linear list of actions. See ApNormalize to get started. @package ap-normalize @version 0.1.0.1 -- | This structure is part of the definition of Aps. module ApNormalize.DList -- | Type of applicative difference lists. -- -- An applicative transformer which accumulates f-actions in a -- left-nested composition using (<*>). -- -- ApDList represents a sequence of f-actions u1 :: f -- x1, ... un :: f xn as "term with a hole" (_ -- <*> u1 <*> ... <*> un) :: f r. -- -- That hole must have type _ :: f (x1 -> ... -> un -> -- r); the variable number of arrows is hidden by existential -- quantification and continuation passing. -- -- To help ensure that syntactic invariant, the Functor and -- Applicative instances for ApDList have no constraints. -- liftApDList is the only function whose signature requires an -- Applicative f constraint, wrapping each action -- u inside one (<*>). newtype ApDList f a ApDList :: (forall r. Yoneda f (a -> r) -> f r) -> ApDList f a -- | A difference list with one element u, denoted _ <*> -- u. liftApDList :: Applicative f => f a -> ApDList f a -- | Complete a difference list, filling the hole with the first argument. lowerApDList :: Yoneda f (b -> c) -> ApDList f b -> f c -- | A delayed application of fmap which can be fused with an inner -- fmap or liftA2. -- -- This is the same definition as in the kan-extensions library, but we -- redefine it to not pay for all the dependencies. newtype Yoneda f a Yoneda :: (forall x. (a -> x) -> f x) -> Yoneda f a instance GHC.Base.Functor (ApNormalize.DList.ApDList f) instance GHC.Base.Applicative (ApNormalize.DList.ApDList f) instance GHC.Base.Functor (ApNormalize.DList.Yoneda f) -- | The definition of Aps. Most of this is reexported by -- ApNormalize. module ApNormalize.Aps -- | An applicative functor transformer which accumulates -- f-actions (things of type f x) in a normal form. -- -- It constructs a value of type f a with the following -- syntactic invariant. It depends on the number of f-actions -- a1 ... an composing it, which are delimited using -- liftAps: -- --
-- data Example a = Example a Bool [a] (Example a) -- -- traverseE :: Applicative f => (a -> f b) -> Example a -> f (Example b) -- traverseE go (Example a b c d) = -- Example -- <$> go a -- <*> pure b -- <*> traverse go c -- <*> traverseE go d -- -- 1 <$>, 3 <*> ---- -- Using this library, we can compose actions in a specialized -- applicative functor Aps f, keeping the code in roughly -- the same structure. In the following snippet, identifiers exported by -- the library are highlighted. -- --
-- traverseE :: Applicative f => (a -> f b) -> Example a -> f (Example b) -- traverseE go (Example a b c d) = -- Example -- <$>^ go a -- <*> pure b -- <*>^ traverse go c -- <*>^ traverseE go d -- & lowerAps -- -- 1 <$>, 3 <*> ---- -- GHC simplifies that traversal to the following, using only two -- combinators in total. -- --
-- traverseE :: Applicative f => (a -> f b) -> Example a -> f (Example b) -- traverseE go (Example a b c d) = -- liftA2 (\a' -> Example a' b) -- (go a) -- (traverse go c) -- <*> traverseE go d -- -- 1 liftA2, 1 <*> ---- -- The following example with a tree-shaped structure also reduces to the -- same list-shaped expression above. -- --
-- traverseE :: Applicative f => (a -> f b) -> Example a -> f (Example b) -- traverseE go (Example a b c d) = -- (\((a', b'), (c', d')) -> Example a' b' c' d') -- <$> ((,) <$> ((,) <$>^ go a -- <*> pure b) -- <*> ((,) <$>^ traverse go c -- <*>^ traverseE go d)) -- & lowerAps -- -- 4 <$>, 3 <*> ---- -- Such structure occurs when using an intermediate definition (which -- itself uses the applicative operators) as the right operand of -- (<$>) or (<*>). This could -- also be found in a naive generic implementation of traverse -- using GHC.Generics. -- --
-- >>> 5 & (+1) & show -- "6" --(&) :: a -> (a -> b) -> b infixl 1 &