-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFileIOAndMLState.hs
More file actions
57 lines (51 loc) · 1.62 KB
/
FileIOAndMLState.hs
File metadata and controls
57 lines (51 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-|
Module : Control.Runner.FileIOAndMLState
Description : Combination of file IO and ML-style state, using the pairing of runners
Copyright : (c) Danel Ahman, 2019
License : MIT
Maintainer : danel.ahman@eesti.ee
Stability : experimental
This module uses the pairing of runners (`pairRunners`) to combine the file
IO runners from `FileIO` and the ML-style state runner from `MLState`.
-}
module Control.Runner.FileIOAndMLState
(
withFile, ioMltopLevel
) where
import Control.Runner
import Control.Runner.FileIO hiding (withFile)
import Control.Runner.MLState hiding (mlTopLevel)
import System.IO hiding (withFile)
-- | A variant of the with-file construct that runs user code that can
-- perform effects both from the `File` and `MLState` effects. For
-- simplicity, currently limited to the use of one file at a time.
withFile :: FilePath -> User '[File,MLState] a -> User '[IO,MLState] a
withFile fn c =
run
(pairRunners fioRunner fwdRunner)
(return ((),()))
(
run
(pairRunners fhRunner (fwdRunner :: Runner '[MLState] '[FileIO,MLState] ()))
(do s <- fioFhInitialiser fn;
return (s,()))
c
(\ x (s,()) -> fioFhFinaliser x s)
)
(\ x _ -> return x)
-- | Top level for running user code that can perform
-- both `IO` and `MLState` effects.
ioMltopLevel :: User '[IO,MLState] a -> IO a
ioMltopLevel m =
ioTopLevel
(
run
(pairRunners fwdRunner mlRunner)
(do h <- mlInitialiser;
return ((),h))
m
(\ x _ -> return x)
)