{-# 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-2024 Sayo Koyoneda
License     :  MPL-2.0 (see the file LICENSE)
Maintainer  :  ymdfield@outlook.jp

Realizes [@unliftio@](https://hackage.haskell.org/package/unliftio) in the form of higher-order effects.
-}
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 $mWithRunInIO :: forall {r} {f :: * -> *} {a}.
UnliftIO f a -> (((f ~> IO) -> IO a) -> r) -> ((# #) -> r) -> r
$bWithRunInIO :: forall (f :: * -> *) a. ((f ~> IO) -> IO a) -> UnliftIO f a
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 x. f x -> IO x) -> IO a) -> f a
forall (b :: * -> *) a (f :: * -> *).
SendHOE (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 (tag :: k) (b :: * -> *) a (f :: * -> *).
SendHOE (TagH (UnliftBase b) tag) f =>
((forall x. f x -> b x) -> b a) -> f a
forall {k} (tag :: k) (b :: * -> *) a (f :: * -> *).
SendHOE (TagH (UnliftBase b) tag) f =>
((forall x. f x -> b x) -> b a) -> f a
withRunInBase' @tag
{-# INLINE withRunInIO' #-}

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