Nick Pollard
We need to satisfy the Monoid class, and be able to lift our underlying type
data ReadResource a where
ReadFile :: FilePath -> ReadResource String
ReadEnv :: String -> ReadResource String
type Resource a = Ap ReadResource a -- Ap is the Free Applicative
loadFile = liftAp . ReadFile
loadEnv = liftAp . ReadEnv
data Foo = Foo Int Int Int
resource :: Resource Foo
resource = Foo
<$> (read <$> loadFile "first.txt")
<*> (read <$> loadFile "second.txt")
<*> (read <$> loadEnv "VAR_THIRD")files :: Resource a -> [FilePath]
files prg = getConst $ runAp inputFiles prg
where inputFiles :: ReadResource a -> Const [FilePath] a
inputFiles (ReadFile file) = Const [file]
inputFiles _ = Const []
vars :: Resource a -> [String]
vars prg = getConst $ runAp envVars prg
where envVars :: ReadResource a -> Const [String] a
envVars (ReadEnv var) = Const [var]
envVars _ = Const []