{-# LANGUAGE TemplateHaskell #-}
module Polysemy.Input
(
Input (..)
, input
, runInputConst
, runInputList
, runInputSem
) where
import Data.Foldable (for_)
import Data.List (uncons)
import Polysemy
import Polysemy.State
data Input i m a where
Input :: Input i m i
makeSem ''Input
runInputConst :: i -> Sem (Input i ': r) a -> Sem r a
runInputConst c = interpret $ \case
Input -> pure c
{-# INLINE runInputConst #-}
runInputList
:: [i]
-> Sem (Input (Maybe i) ': r) a
-> Sem r a
runInputList is = fmap snd . runState is . reinterpret
(\case
Input -> do
s <- gets uncons
for_ s $ put . snd
pure $ fmap fst s
)
{-# INLINE runInputList #-}
runInputSem :: forall i r a. Sem r i -> Sem (Input i ': r) a -> Sem r a
runInputSem m = interpret $ \case
Input -> m
{-# INLINE runInputSem #-}