{-# LANGUAGE AllowAmbiguousTypes #-}

-- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at https://mozilla.org/MPL/2.0/.

{- |
Copyright   :  (c) 2023 Yamada Ryo
License     :  MPL-2.0 (see the file LICENSE)
Maintainer  :  ymdfield@outlook.jp
Stability   :  experimental
Portability :  portable
-}
module Data.Effect.Writer where

data Tell w a where
    Tell :: w -> Tell w ()

data WriterH w f a where
    Listen :: f a -> WriterH w f (w, a)
    Censor :: (w -> w) -> f a -> WriterH w f a

makeEffect [''Tell] [''WriterH]

pass :: (Tell w <: m, WriterH w <<: m, Monad m) => m (w -> w, a) -> m a
pass :: forall w (m :: * -> *) a.
(Tell w <: m, WriterH w <<: m, Monad m) =>
m (w -> w, a) -> m a
pass m (w -> w, a)
m = do
    (w
w, (w -> w
f, a
a)) <- forall a w (f :: * -> *). SendSig (WriterH w) f => f a -> f (w, a)
listen m (w -> w, a)
m
    forall w (f :: * -> *). SendIns (Tell w) f => w -> f ()
tell forall a b. (a -> b) -> a -> b
$ w -> w
f w
w
    forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a