module Fold.Pure.Type where

import Control.Applicative (Applicative, liftA2, pure, (<*>))
import Data.Functor (Functor, fmap)
import Data.Monoid (Monoid, mempty)
import Data.Semigroup (Semigroup, (<>))

import qualified Strict

{- | Processes inputs of type @a@ and results in a value of type @b@ -}
data Fold a b = forall x. Fold
    { ()
initial :: x
    , ()
step :: x -> a -> x
    , ()
extract :: x -> b
    }

instance Functor (Fold a) where
    fmap :: forall a b. (a -> b) -> Fold a a -> Fold a b
fmap a -> b
f Fold{ x
initial :: x
initial :: ()
initial, x -> a -> x
step :: x -> a -> x
step :: ()
step, x -> a
extract :: x -> a
extract :: ()
extract } =
        Fold{ x
initial :: x
initial :: x
initial, x -> a -> x
step :: x -> a -> x
step :: x -> a -> x
step, extract :: x -> b
extract = \x
x -> a -> b
f (x -> a
extract x
x) }

instance Applicative (Fold a) where
    pure :: forall a. a -> Fold a a
pure a
b = Fold{ initial :: ()
initial = (), step :: () -> a -> ()
step = \() a
_ -> (), extract :: () -> a
extract = \() -> a
b }

    <*> :: forall a b. Fold a (a -> b) -> Fold a a -> Fold a b
(<*>)
        Fold{ initial :: ()
initial = x
initialL, step :: ()
step = x -> a -> x
stepL, extract :: ()
extract = x -> a -> b
extractL }
        Fold{ initial :: ()
initial = x
initialR, step :: ()
step = x -> a -> x
stepR, extract :: ()
extract = x -> a
extractR } =
          Fold
            { initial :: Tuple2 x x
initial = forall a b. a -> b -> Tuple2 a b
Strict.Tuple2 x
initialL x
initialR
            , step :: Tuple2 x x -> a -> Tuple2 x x
step = \(Strict.Tuple2 x
xL x
xR) a
a -> forall a b. a -> b -> Tuple2 a b
Strict.Tuple2 (x -> a -> x
stepL x
xL a
a) (x -> a -> x
stepR x
xR a
a)
            , extract :: Tuple2 x x -> b
extract = \(Strict.Tuple2 x
xL x
xR) -> x -> a -> b
extractL x
xL (x -> a
extractR x
xR)
            }

instance Semigroup b => Semigroup (Fold a b) where
    <> :: Fold a b -> Fold a b -> Fold a b
(<>) = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 forall a. Semigroup a => a -> a -> a
(<>)

instance Monoid b => Monoid (Fold a b) where
    mempty :: Fold a b
mempty = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Monoid a => a
mempty