packages feed

heftia-effects-0.5.0.0: src/Control/Monad/Hefty/Input.hs

-- SPDX-License-Identifier: MPL-2.0

{- |
Copyright   :  (c) 2024 Sayo Koyoneda
License     :  MPL-2.0 (see the LICENSE file)
Maintainer  :  ymdfield@outlook.jp

Interpreters for the t'Input' effect.
-}
module Control.Monad.Hefty.Input (
    module Control.Monad.Hefty.Input,
    module Data.Effect.Input,
)
where

import Control.Arrow ((>>>))
import Control.Monad.Hefty (Eff, interpret, raiseUnder, type (~>))
import Control.Monad.Hefty.State (evalState)
import Data.Effect.Input
import Data.Effect.State (gets, put)
import Data.List (uncons)

-- | Interprets the t'Input' effect by executing the given input handler each time an input is required.
runInputEff
    :: forall i ef eh
     . Eff eh ef i
    -> Eff eh (Input i ': ef) ~> Eff eh ef
runInputEff a = interpret \Input -> a

-- | Interprets the t'Input' effect by providing the given constant as input.
runInputConst
    :: forall i ef eh
     . i
    -> Eff eh (Input i ': ef) ~> Eff eh ef
runInputConst i = interpret \Input -> pure i

{- |
Interprets the t'Input' effect by using the given list as a series of inputs.

Each time 'input' is called, it retrieves elements from the list one by one from the beginning, and after all elements are consumed, 'Nothing' is returned indefinitely.
-}
runInputList :: forall i r. [i] -> Eff '[] (Input (Maybe i) ': r) ~> Eff '[] r
runInputList is =
    raiseUnder
        >>> int
        >>> evalState is
  where
    int = interpret \Input -> do
        is' <- gets @[i] uncons
        mapM_ (put . snd) is'
        pure $ fst <$> is'