-- 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) 2024 Yamada Ryo
License     :  MPL-2.0 (see the file LICENSE)
Maintainer  :  ymdfield@outlook.jp
Stability   :  experimental
Portability :  portable
-}
module Control.Effect.Interpreter.Heftia.Unlift where

import Control.Effect (type (~>))
import Control.Effect.Hefty (Eff, interpretH, runEff, send0)
import Control.Freer (Freer)
import Data.Effect (LiftIns)
import Data.Effect.Unlift (UnliftBase (WithRunInBase), UnliftIO)
import Data.Hefty.Union (Union)

runUnliftBase ::
    forall b fr u c.
    (Freer c fr, Union u, c b) =>
    Eff u fr '[UnliftBase b] '[LiftIns b] ~> b
runUnliftBase :: forall (b :: * -> *) (fr :: (* -> *) -> * -> *)
       (u :: [(* -> *) -> * -> *] -> (* -> *) -> * -> *)
       (c :: (* -> *) -> Constraint).
(Freer c fr, Union u, c b) =>
Eff u fr '[UnliftBase b] '[LiftIns b] ~> b
runUnliftBase =
    forall (f :: * -> *) (fr :: (* -> *) -> * -> *)
       (u :: [(* -> *) -> * -> *] -> (* -> *) -> * -> *)
       (c :: (* -> *) -> Constraint).
(Freer c fr, Union u, c f) =>
Eff u fr '[] '[LiftIns f] ~> f
runEff forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (eh :: (* -> *) -> * -> *) (ehs :: [(* -> *) -> * -> *])
       (efs :: [(* -> *) -> * -> *]) (fr :: (* -> *) -> * -> *)
       (u :: [(* -> *) -> * -> *] -> (* -> *) -> * -> *)
       (c :: (* -> *) -> Constraint).
(Freer c fr, Union u) =>
(eh (Eff u fr '[eh] efs) ~> Eff u fr ehs efs)
-> Eff u fr '[eh] efs ~> Eff u fr ehs efs
interpretH \(WithRunInBase (forall x. Eff u fr '[UnliftBase b] '[LiftIns b] x -> b x) -> b x
f) ->
        forall (c :: (* -> *) -> Constraint) (fr :: (* -> *) -> * -> *)
       (u :: [(* -> *) -> * -> *] -> (* -> *) -> * -> *)
       (e :: (* -> *) -> * -> *) (eh :: [(* -> *) -> * -> *])
       (r :: [(* -> *) -> * -> *]).
(Freer c fr, Union u, HeadIns e) =>
UnliftIfSingle e ~> Eff u fr eh (e : r)
send0 forall a b. (a -> b) -> a -> b
$ (forall x. Eff u fr '[UnliftBase b] '[LiftIns b] x -> b x) -> b x
f forall (b :: * -> *) (fr :: (* -> *) -> * -> *)
       (u :: [(* -> *) -> * -> *] -> (* -> *) -> * -> *)
       (c :: (* -> *) -> Constraint).
(Freer c fr, Union u, c b) =>
Eff u fr '[UnliftBase b] '[LiftIns b] ~> b
runUnliftBase

runUnliftIO ::
    forall fr u c.
    (Freer c fr, Union u, c IO) =>
    Eff u fr '[UnliftIO] '[LiftIns IO] ~> IO
runUnliftIO :: forall (fr :: (* -> *) -> * -> *)
       (u :: [(* -> *) -> * -> *] -> (* -> *) -> * -> *)
       (c :: (* -> *) -> Constraint).
(Freer c fr, Union u, c IO) =>
Eff u fr '[UnliftIO] '[LiftIns IO] ~> IO
runUnliftIO = forall (b :: * -> *) (fr :: (* -> *) -> * -> *)
       (u :: [(* -> *) -> * -> *] -> (* -> *) -> * -> *)
       (c :: (* -> *) -> Constraint).
(Freer c fr, Union u, c b) =>
Eff u fr '[UnliftBase b] '[LiftIns b] ~> b
runUnliftBase
{-# INLINE runUnliftIO #-}