module Fold.Shortcut.Conversion where

import Fold.Shortcut.Type

import Data.Functor ((<$>))
import Data.Functor.Identity (Identity)
import Data.Maybe (Maybe (Just))
import Fold.Effectful.Type (EffectfulFold)
import Fold.Nonempty.Type (NonemptyFold)
import Fold.Pure.Type (Fold (Fold))
import Fold.ShortcutNonempty.Type (ShortcutNonemptyFold (ShortcutNonemptyFold))
import Data.Void (absurd)

import qualified Fold.Pure.Conversion as Fold
import qualified Fold.Pure.Type as Fold
import qualified Fold.ShortcutNonempty.Type as ShortcutNonempty
import qualified Strict

fold :: Fold a b -> ShortcutFold a b
fold :: forall a b. Fold a b -> ShortcutFold a b
fold
  Fold{ x
initial :: ()
initial :: x
Fold.initial, x -> a -> x
step :: ()
step :: x -> a -> x
Fold.step, x -> b
extract :: ()
extract :: x -> b
Fold.extract } =
    ShortcutFold
      { initial :: Vitality Void x
initial = forall a b. Will -> b -> Vitality a b
Alive Will
Ambivalent x
initial
      , step :: x -> a -> Vitality Void x
step = \x
x a
a -> forall a b. Will -> b -> Vitality a b
Alive Will
Ambivalent (x -> a -> x
step x
x a
a)
      , extractDead :: Void -> b
extractDead = forall a. Void -> a
absurd
      , extractLive :: x -> b
extractLive = x -> b
extract
      }

effectfulFold :: EffectfulFold Identity a b -> ShortcutFold a b
effectfulFold :: forall a b. EffectfulFold Identity a b -> ShortcutFold a b
effectfulFold EffectfulFold Identity a b
x = forall a b. Fold a b -> ShortcutFold a b
fold (forall a b. EffectfulFold Identity a b -> Fold a b
Fold.effectfulFold EffectfulFold Identity a b
x)

nonemptyFold :: NonemptyFold a b -> ShortcutFold a (Maybe b)
nonemptyFold :: forall a b. NonemptyFold a b -> ShortcutFold a (Maybe b)
nonemptyFold NonemptyFold a b
x = forall a b. Fold a b -> ShortcutFold a b
fold (forall a b. NonemptyFold a b -> Fold a (Maybe b)
Fold.nonemptyFold NonemptyFold a b
x)

shortcutNonemptyFold :: ShortcutNonemptyFold a b -> ShortcutFold a (Maybe b)
shortcutNonemptyFold :: forall a b. ShortcutNonemptyFold a b -> ShortcutFold a (Maybe b)
shortcutNonemptyFold ShortcutNonemptyFold{ a -> Vitality x y
initial :: ()
initial :: a -> Vitality x y
ShortcutNonempty.initial,
        y -> a -> Vitality x y
step :: ()
step :: y -> a -> Vitality x y
ShortcutNonempty.step, x -> b
extractDead :: ()
extractDead :: x -> b
ShortcutNonempty.extractDead, y -> b
extractLive :: ()
extractLive :: y -> b
ShortcutNonempty.extractLive } =
    ShortcutFold
      { initial :: Vitality x (Maybe y)
initial = forall a b. Will -> b -> Vitality a b
Alive Will
Tenacious forall a. Maybe a
Strict.Nothing
      , step :: Maybe y -> a -> Vitality x (Maybe y)
step = \Maybe y
xm a
a -> forall a. a -> Maybe a
Strict.Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> case Maybe y
xm of
            Maybe y
Strict.Nothing -> a -> Vitality x y
initial a
a
            Strict.Just y
x -> y -> a -> Vitality x y
step y
x a
a
      , extractDead :: x -> Maybe b
extractDead = \x
x -> forall a. a -> Maybe a
Just (x -> b
extractDead x
x)
      , extractLive :: Maybe y -> Maybe b
extractLive = \Maybe y
xm -> y -> b
extractLive forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Maybe a -> Maybe a
Strict.lazy Maybe y
xm
      }