{-# 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/.

module Data.Effect.Unlift where

import Data.Effect.Tag (type (##))

data UnliftBase b f (a :: Type) where
    WithRunInBase :: ((forall x. f x -> b x) -> b a) -> UnliftBase b f a

makeEffectH [''UnliftBase]

type UnliftIO = UnliftBase IO

pattern WithRunInIO :: (f ~> IO -> IO a) -> UnliftIO f a
pattern $bWithRunInIO :: forall (f :: * -> *) a. ((f ~> IO) -> IO a) -> UnliftIO f a
$mWithRunInIO :: forall {r} {f :: * -> *} {a}.
UnliftIO f a -> (((f ~> IO) -> IO a) -> r) -> ((# #) -> r) -> r
WithRunInIO f = WithRunInBase f
{-# COMPLETE WithRunInIO #-}

withRunInIO :: UnliftIO <<: f => (f ~> IO -> IO a) -> f a
withRunInIO :: forall (f :: * -> *) a.
(UnliftIO <<: f) =>
((f ~> IO) -> IO a) -> f a
withRunInIO = forall (b :: * -> *) a (f :: * -> *).
SendSig (UnliftBase b) f =>
((forall x. f x -> b x) -> b a) -> f a
withRunInBase
{-# INLINE withRunInIO #-}

withRunInIO' :: forall tag f a. UnliftIO ## tag <<: f => (f ~> IO -> IO a) -> f a
withRunInIO' :: forall {k} (tag :: k) (f :: * -> *) a.
((UnliftIO ## tag) <<: f) =>
((f ~> IO) -> IO a) -> f a
withRunInIO' = forall {k} (tag :: k) (b :: * -> *) a (f :: * -> *).
SendSig (TagH (UnliftBase b) tag) f =>
((forall x. f x -> b x) -> b a) -> f a
withRunInBase' @tag
{-# INLINE withRunInIO' #-}

withRunInIO'' :: forall key f a. SendSigBy key UnliftIO f => (f ~> IO -> IO a) -> f a
withRunInIO'' :: forall {k} (key :: k) (f :: * -> *) a.
SendSigBy key UnliftIO f =>
((f ~> IO) -> IO a) -> f a
withRunInIO'' = forall {k} (key :: k) (b :: * -> *) a (f :: * -> *).
SendSigBy key (UnliftBase b) f =>
((forall x. f x -> b x) -> b a) -> f a
withRunInBase'' @key
{-# INLINE withRunInIO'' #-}