Tom Ellis 2024-06-26 18:37:24 +01:00
parent ba3bcd8e16
commit be217da0a0
3 changed files with 24 additions and 19 deletions

View File

@ -246,6 +246,11 @@ class IsHandle (h :: Effects -> Type) where
-- other handles.
mapHandle :: (e :> es) => h e -> h es
class IsHandle1 (h :: Effects -> Effects -> Type) where
-- | Used to create compound effects, i.e. handles that contain
-- other handles.
mapHandle1 :: (e :> es) => h e' e -> h e' es
instance IsHandle (State s) where
mapHandle (UnsafeMkState s) = UnsafeMkState s

View File

@ -520,25 +520,25 @@ exampleCounter6 = runPureEff $ yieldToList $ \y -> do
-- FileSystem
data FileSystem es = MkFileSystem
data FileSystem e es = MkFileSystem
{ readFileImpl :: FilePath -> Eff es String,
writeFileImpl :: FilePath -> String -> Eff es ()
}
instance IsHandle FileSystem where
mapHandle (MkFileSystem read write) = MkFileSystem (useImpl . read) (fmap useImpl . write)
instance IsHandle1 FileSystem where
mapHandle1 (MkFileSystem read write) = MkFileSystem (useImpl . read) (fmap useImpl . write)
readFile :: (e :> es) => FileSystem e -> FilePath -> Eff es String
readFile :: (e :> es) => FileSystem e' e -> FilePath -> Eff es String
readFile fs filepath = useImpl (readFileImpl fs filepath)
writeFile :: (e :> es) => FileSystem e -> FilePath -> String -> Eff es ()
writeFile :: (e :> es) => FileSystem e' e -> FilePath -> String -> Eff es ()
writeFile fs filepath contents = useImpl (writeFileImpl fs filepath contents)
runFileSystemPure ::
(e1 :> es) =>
Exception String e1 ->
[(FilePath, String)] ->
(forall e2. FileSystem e2 -> Eff (e2 :& es) r) ->
(forall e2. FileSystem e' e2 -> Eff (e2 :& es) r) ->
Eff es r
runFileSystemPure ex fs0 k =
evalState fs0 $ \fs ->
@ -559,11 +559,11 @@ runFileSystemPure ex fs0 k =
}
runFileSystemIO ::
forall e1 e2 es r.
forall e1 e2 es r e'.
(e1 :> es, e2 :> es) =>
Exception String e1 ->
IOE e2 ->
(forall e. FileSystem e -> Eff (e :& es) r) ->
(forall e. FileSystem e' e -> Eff (e :& es) r) ->
Eff es r
runFileSystemIO ex io k =
useImplIn
@ -605,7 +605,7 @@ runFileSystemPure' ex fs0 k =
modify fs ((path, contents) :)
}
action :: (e :> es) => FileSystem e -> Eff es String
action :: (e :> es) => FileSystem e' e -> Eff es String
action fs = do
file <- readFile fs "/dev/null"
when (length file == 0) $ do
@ -623,9 +623,9 @@ exampleRunFileSystemPure :: Either String String
exampleRunFileSystemPure = runPureEff $ try $ \ex ->
runFileSystemPure ex [("/dev/null", "")] action
-- exampleRunFileSystemPure' :: Either String String
-- exampleRunFileSystemPure' = runPureEff $ try $ \ex ->
-- runFileSystemPure' ex [("/dev/null", "")] action'
exampleRunFileSystemPure' :: Either String String
exampleRunFileSystemPure' = runPureEff $ try $ \ex ->
runFileSystemPure' ex [("/dev/null", "")] action'
-- > exampleRunFileSystemPure
-- Left "File not found: /tmp/doesn't exist"

View File

@ -18,7 +18,7 @@ where
import Bluefin.Internal
( Eff,
Effects,
IsHandle (..),
IsHandle1 (..),
insertManySecond,
mergeEff,
type (:&),
@ -66,7 +66,7 @@ type Sig = Effects -> Type
-- get :: e0 :> e => 'Handle' (State s) e0 -> 'Eff' e s
-- put :: e0 :> e => 'Handle' (State s) e0 -> s -> 'Eff' e ()
-- @
type Handle f e = (forall e1. (e :> e1) => f e1)
type Handle f e = (forall e1. (e :> e1) => f e e1)
-- | Create a 'Handle' @h@ with signature @f@, using the given implementation @impl@.
--
@ -74,19 +74,19 @@ type Handle f e = (forall e1. (e :> e1) => f e1)
-- 'with' impl \\h -> ...
-- @
with ::
(IsHandle f) =>
(IsHandle1 f) =>
-- | Implementation with effect @e@
f e ->
f e e ->
(forall e0. Handle f e0 -> Eff (e0 :& e) a) ->
Eff e a
with handle action = mergeEff (action (mapHandle handle))
with handle action = mergeEff (action (mapHandle1 handle))
-- Internally, we just instantiate e0 with e.
within ::
(IsHandle f, e :> es) =>
(IsHandle1 f, e :> es) =>
(forall e0. Handle f e0 -> Eff (e0 :& e) r) ->
f es ->
f es es ->
Eff es r
within k fsh0 = with fsh0 (useImplWithin k)