-- 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 Sayo Koyoneda
               (c) 2017 FP Complete
               (c) 2022 Fumiaki Kinoshita
License     :  MPL-2.0 (see the LICENSE file)
Maintainer  :  ymdfield@outlook.jp
Portability :  portable

An elaborator for the t'Control.Effect.Class.Resource.Resource' effect class.
-}
module Control.Monad.Hefty.Resource where

import Control.Effect (type (~>))
import Control.Monad.Hefty.Interpret (interpretH)
import Control.Monad.Hefty.Types (Eff, type (~~>))
import Data.Effect.OpenUnion.Internal.FO (type (<|))
import Data.Effect.OpenUnion.Internal.HO (type (<<|))
import Data.Effect.Resource (Resource (Bracket, BracketOnExcept))
import Data.Effect.Unlift (UnliftIO)
import UnliftIO (MonadUnliftIO, bracket, bracketOnError)

-- | Elaborates the `Resource` effect under the `UnliftIO` context.
runResourceIO
    :: (UnliftIO <<| eh, IO <| ef)
    => Eff (Resource ': eh) ef ~> Eff eh ef
runResourceIO :: forall (eh :: [EffectH]) (ef :: [EffectF]).
(UnliftIO <<| eh, IO <| ef) =>
Eff (Resource : eh) ef ~> Eff eh ef
runResourceIO = (Resource ~~> Eff eh ef) -> Eff (Resource : eh) ef ~> Eff eh ef
forall (e :: EffectH) (eh :: [EffectH]) (ef :: [EffectF]).
HFunctor e =>
(e ~~> Eff eh ef) -> Eff (e : eh) ef ~> Eff eh ef
interpretH Resource (Eff eh ef) x -> Eff eh ef x
Resource ~~> Eff eh ef
forall (m :: EffectF). MonadUnliftIO m => Resource ~~> m
elabResourceIO

elabResourceIO :: (MonadUnliftIO m) => Resource ~~> m
elabResourceIO :: forall (m :: EffectF). MonadUnliftIO m => Resource ~~> m
elabResourceIO = \case
    Bracket m a1
acquire a1 -> m ()
release a1 -> m x
thing -> m a1 -> (a1 -> m ()) -> (a1 -> m x) -> m x
forall (m :: EffectF) a b c.
MonadUnliftIO m =>
m a -> (a -> m b) -> (a -> m c) -> m c
bracket m a1
acquire a1 -> m ()
release a1 -> m x
thing
    BracketOnExcept m a1
acquire a1 -> m ()
onError a1 -> m x
thing -> m a1 -> (a1 -> m ()) -> (a1 -> m x) -> m x
forall (m :: EffectF) a b c.
MonadUnliftIO m =>
m a -> (a -> m b) -> (a -> m c) -> m c
bracketOnError m a1
acquire a1 -> m ()
onError a1 -> m x
thing
{-# INLINE elabResourceIO #-}