Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promote use of MonadIO to minimize embed occurrences #2694

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions app/App.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ reAppIO ::
reAppIO args@RunAppIOArgs {..} =
reinterpret $ \case
AskPackageGlobal -> return (_runAppIOArgsRoot ^. rootPackageType `elem` [GlobalStdlib, GlobalPackageDescription, GlobalPackageBase])
FromAppPathFile p -> embed (prepathToAbsFile invDir (p ^. pathPath))
FromAppPathFile p -> prepathToAbsFile invDir (p ^. pathPath)
GetMainFile m -> getMainFile' m
FromAppPathDir p -> liftIO (prepathToAbsDir invDir (p ^. pathPath))
RenderStdOut t
Expand All @@ -84,7 +84,7 @@ reAppIO args@RunAppIOArgs {..} =
printErr e
ExitJuvixError e -> do
printErr e
embed exitFailure
exitFailure
ExitMsg exitCode t -> exitMsg' (exitWith exitCode) t
ExitFailMsg t -> exitMsg' exitFailure t
SayRaw b -> embed (ByteString.putStr b)
Expand All @@ -97,11 +97,11 @@ reAppIO args@RunAppIOArgs {..} =

getMainFile' :: (Members '[SCache Package, EmbedIO] r') => Maybe (AppPath File) -> Sem r' (Path Abs File)
getMainFile' = \case
Just p -> embed (prepathToAbsFile invDir (p ^. pathPath))
Just p -> prepathToAbsFile invDir (p ^. pathPath)
Nothing -> do
pkg <- getPkg
case pkg ^. packageMain of
Just p -> embed (prepathToAbsFile invDir p)
Just p -> prepathToAbsFile invDir p
Nothing -> missingMainErr

missingMainErr :: (Members '[EmbedIO] r') => Sem r' x
Expand All @@ -116,7 +116,10 @@ reAppIO args@RunAppIOArgs {..} =
g :: GlobalOptions
g = _runAppIOArgsGlobalOptions
printErr e =
embed $ hPutStrLn stderr $ run $ runReader (project' @GenericOptions g) $ Error.render (not (_runAppIOArgsGlobalOptions ^. globalNoColors)) (g ^. globalOnlyErrors) e
hPutStrLn stderr
. run
. runReader (project' @GenericOptions g)
$ Error.render (not (_runAppIOArgsGlobalOptions ^. globalNoColors)) (g ^. globalOnlyErrors) e

getEntryPoint' :: (Members '[EmbedIO, TaggedLock] r) => RunAppIOArgs -> AppPath File -> Sem r EntryPoint
getEntryPoint' RunAppIOArgs {..} inputFile = do
Expand Down
2 changes: 1 addition & 1 deletion app/Commands/Dev/Casm/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ runCommand opts = do
Right (labi, code) ->
case Casm.validate labi code of
Left err -> exitJuvixError (JuvixError err)
Right () -> embed $ print (Casm.runCode labi code)
Right () -> print (Casm.runCode labi code)
where
file :: AppPath File
file = opts ^. casmRunInputFile
2 changes: 1 addition & 1 deletion app/Commands/Dev/Core/Read.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ runCommand opts = do
let r = run $ runReader (project @GlobalOptions @Core.CoreOptions gopts) $ runError @JuvixError $ Core.applyTransformations (project opts ^. coreReadTransformations) (Core.moduleFromInfoTable tab)
tab0 <- getRight $ mapLeft JuvixError r
let tab' = Core.computeCombinedInfoTable $ if project opts ^. coreReadNoDisambiguate then tab0 else Core.disambiguateNames tab0
embed (Scoper.scopeTrace tab')
Scoper.scopeTrace tab'
unless (project opts ^. coreReadNoPrint) $ do
renderStdOut (Pretty.ppOut opts tab')
whenJust (tab' ^. Core.infoMain) $ \sym -> doEval gopts tab' (fromJust $ tab' ^. Core.identContext . at sym)
Expand Down
8 changes: 4 additions & 4 deletions app/Commands/Dev/Core/Repl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ runRepl opts tab = do
embed (hFlush stdout)
done <- embed isEOF
unless done $ do
s <- embed getLine
s <- getLine
case fromText (strip s) of
":q" -> return ()
":h" -> do
embed showReplHelp
showReplHelp
runRepl opts tab
':' : 'p' : ' ' : s' ->
case parseText tab (fromString s') of
Expand Down Expand Up @@ -133,14 +133,14 @@ runRepl opts tab = do
putStrLn ""
runRepl opts tab'

showReplWelcome :: (Members '[EmbedIO, App] r) => Sem r ()
showReplWelcome :: (MonadIO m) => m ()
showReplWelcome = do
putStrLn "JuvixCore REPL"
putStrLn ""
putStrLn "Type \":h\" for help."
putStrLn ""

showReplHelp :: IO ()
showReplHelp :: (MonadIO m) => m ()
showReplHelp = do
putStrLn ""
putStrLn "JuvixCore REPL"
Expand Down
2 changes: 1 addition & 1 deletion app/Commands/Dev/Geb/Repl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ printError e = do
. runReader (project' @GenericOptions opts)
$ Error.render useAnsi False e
)
liftIO $ hPutStrLn stderr errorText
hPutStrLn stderr errorText

printEvalResult :: Either JuvixError Geb.RunEvalResult -> Repl ()
printEvalResult = \case
Expand Down
6 changes: 3 additions & 3 deletions app/Commands/Extra/Compile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,9 @@ findClangUsingEnvVar = do
clangBinPath = fmap (<//> $(mkRelFile "bin/clang")) <$> llvmDistPath

llvmDistPath :: Sem r (Maybe (Path Abs Dir))
llvmDistPath = do
p <- embed (lookupEnv llvmDistEnvironmentVar)
embed (mapM parseAbsDir p)
llvmDistPath = liftIO $ do
p <- lookupEnv llvmDistEnvironmentVar
mapM parseAbsDir p

data ClangPath
= ClangSystemPath (Path Abs File)
Expand Down
7 changes: 2 additions & 5 deletions app/Commands/Format.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,14 @@ runCommand opts = do
TargetStdin -> do
entry <- getEntryPointStdin
runReader entry formatStdin

let exitFail :: IO a
exitFail = exitWith (ExitFailure 1)
case res of
FormatResultFail -> embed exitFail
FormatResultFail -> exitFailure
FormatResultNotFormatted ->
{- use exit code 1 for
* unformatted files when using --check
* when running the formatter on a Juvix project
-}
when (opts ^. formatCheck || isTargetProject target) (embed exitFail)
when (opts ^. formatCheck || isTargetProject target) exitFailure
FormatResultOK -> pure ()

renderModeFromOptions :: FormatTarget -> FormatOptions -> FormattedFileInfo -> FormatRenderMode
Expand Down
8 changes: 4 additions & 4 deletions app/Commands/Init.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ checkNotInProject =
err :: Sem r ()
err = do
say "You are already in a Juvix project"
embed exitFailure
exitFailure

checkPackage :: forall r. (Members '[EmbedIO] r) => Sem r ()
checkPackage = do
Expand All @@ -66,7 +66,7 @@ checkPackage = do
case ep of
Left {} -> do
say "Package.juvix is invalid. Please raise an issue at https://github.com/anoma/juvix/issues"
embed exitFailure
exitFailure
Right {} -> return ()

getPackage :: forall r. (Members '[EmbedIO] r) => Sem r Package
Expand Down Expand Up @@ -110,7 +110,7 @@ getProjName = do
where
go :: Sem r Text
go = do
txt <- embed getLine
txt <- getLine
if
| Text.null txt, Just def' <- def -> return def'
| otherwise ->
Expand All @@ -137,7 +137,7 @@ tryAgain = say "Please, try again:"

getVersion :: forall r. (Members '[EmbedIO] r) => Sem r SemVer
getVersion = do
txt <- embed getLine
txt <- getLine
if
| Text.null txt -> return defaultVersion
| otherwise -> case parse semver' txt of
Expand Down
10 changes: 5 additions & 5 deletions app/TopCommand.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ import Juvix.Extra.Version
import System.Environment (getProgName)
import TopCommand.Options

showHelpText :: IO ()
showHelpText :: (MonadIO m) => m ()
showHelpText = do
let p = prefs showHelpOnEmpty
progn <- getProgName
progn <- liftIO getProgName
let helpText = parserFailure p descr (ShowHelpText Nothing) []
(msg, _) = renderFailure helpText progn
putStrLn (pack msg)

runTopCommand :: forall r. (Members '[EmbedIO, App, Resource, TaggedLock] r) => TopCommand -> Sem r ()
runTopCommand = \case
DisplayVersion -> embed runDisplayVersion
DisplayNumericVersion -> embed runDisplayNumericVersion
DisplayHelp -> embed showHelpText
DisplayVersion -> runDisplayVersion
DisplayNumericVersion -> runDisplayNumericVersion
DisplayHelp -> showHelpText
Doctor opts -> runLogIO (Doctor.runCommand opts)
Init opts -> runLogIO (Init.init opts)
Dev opts -> Dev.runCommand opts
Expand Down
2 changes: 1 addition & 1 deletion src/Juvix/Compiler/Asm/Interpreter/Runtime.hs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ runRuntime tab = runState (RuntimeState (CallStack []) emptyFrame [] Nothing tab
hEvalRuntime :: forall r a. (Member EmbedIO r) => Handle -> InfoTable -> Sem (Runtime ': r) a -> Sem r a
hEvalRuntime h tab r = do
(s, a) <- runRuntime tab r
mapM_ (embed . hPutStrLn h) (reverse (s ^. runtimeMessages))
mapM_ (hPutStrLn h) (reverse (s ^. runtimeMessages))
return a

evalRuntime :: forall r a. (Member EmbedIO r) => InfoTable -> Sem (Runtime ': r) a -> Sem r a
Expand Down
2 changes: 1 addition & 1 deletion src/Juvix/Compiler/Casm/Translation/FromSource.hs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ parseMemRef = do
r <- register
off <- parseOffset
rbracket
return $ MemRef {_memRefReg = r, _memRefOff = off}
return MemRef {_memRefReg = r, _memRefOff = off}

parseLabel :: (Member LabelInfoBuilder r) => ParsecS r LabelRef
parseLabel = do
Expand Down
2 changes: 1 addition & 1 deletion src/Juvix/Compiler/Core/Scoper.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ scopeErr msg = do
throw @ScopeError ("Scope error in the definition of " <> show sym <> "\n" <> msg)

-- | prints the scope error without exiting
scopeTrace :: InfoTable -> IO ()
scopeTrace :: (MonadIO m) => InfoTable -> m ()
scopeTrace i = whenJust (scopeCheck i) putStrLn
2 changes: 1 addition & 1 deletion src/Juvix/Compiler/Nockma/Stdlib.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Juvix.Compiler.Nockma.Stdlib where

import Juvix.Compiler.Nockma.Translation.FromSource.QQ
import Juvix.Prelude
import Juvix.Prelude.Base

stdlib :: Term Natural
stdlib =
Expand Down
2 changes: 1 addition & 1 deletion src/Juvix/Compiler/Nockma/StdlibFunction.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Juvix.Compiler.Nockma.StdlibFunction where

import Juvix.Compiler.Nockma.Translation.FromSource.QQ
import Juvix.Prelude hiding (Path)
import Juvix.Prelude.Base

-- | The stdlib paths are obtained from the Urbit dojo
-- * Load the stdlib file into the Urbit dojo
Expand Down
20 changes: 12 additions & 8 deletions src/Juvix/Compiler/Pipeline/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -100,36 +100,40 @@ runIO opts entry = runIOEither entry >=> mayThrow
where
mayThrow :: (Members '[EmbedIO] r') => Either JuvixError x -> Sem r' x
mayThrow = \case
Left err -> runReader opts $ printErrorAnsiSafe err >> embed exitFailure
Left err -> runReader opts $ printErrorAnsiSafe err >> exitFailure
Right r -> return r

runReplPipelineIO :: EntryPoint -> IO Artifacts
runReplPipelineIO :: (MonadIO m) => EntryPoint -> m Artifacts
runReplPipelineIO = runReplPipelineIO' defaultGenericOptions

runReplPipelineIO' :: GenericOptions -> EntryPoint -> IO Artifacts
runReplPipelineIO' :: forall m. (MonadIO m) => GenericOptions -> EntryPoint -> m Artifacts
runReplPipelineIO' opts entry = runReplPipelineIOEither entry >>= mayThrow
where
mayThrow :: Either JuvixError r -> IO r
mayThrow :: Either JuvixError r -> m r
mayThrow = \case
Left err -> runM . runReader opts $ printErrorAnsiSafe err >> embed exitFailure
Left err -> liftIO . runM . runReader opts $ printErrorAnsiSafe err >> exitFailure
Right r -> return r

runReplPipelineIOEither ::
(MonadIO m) =>
EntryPoint ->
IO (Either JuvixError Artifacts)
m (Either JuvixError Artifacts)
runReplPipelineIOEither = runReplPipelineIOEither' LockModePermissive

runReplPipelineIOEither' ::
forall m.
(MonadIO m) =>
LockMode ->
EntryPoint ->
IO (Either JuvixError Artifacts)
m (Either JuvixError Artifacts)
runReplPipelineIOEither' lockMode entry = do
let hasInternet = not (entry ^. entryPointOffline)
runPathResolver'
| mainIsPackageFile entry = runPackagePathResolverArtifacts (entry ^. entryPointResolverRoot)
| otherwise = runPathResolverArtifacts
eith <-
runFinal
liftIO
. runFinal
. resourceToIOFinal
. embedToFinal @IO
. evalInternet hasInternet
Expand Down
8 changes: 4 additions & 4 deletions src/Juvix/Compiler/Reg/Interpreter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -332,14 +332,14 @@ runIO hin hout infoTable = \case
_regErrorLoc = Nothing
}
ValConstr (Constr (BuiltinTag TagWrite) [ValString s]) -> do
embed $ hPutStr hout s
hPutStr hout s
return ValVoid
ValConstr (Constr (BuiltinTag TagWrite) [arg]) -> do
embed $ hPutStr hout (ppPrint infoTable arg)
hPutStr hout (ppPrint infoTable arg)
return ValVoid
ValConstr (Constr (BuiltinTag TagReadLn) []) -> do
embed $ hFlush hout
s <- embed $ hGetLine hin
liftIO $ hFlush hout
s <- liftIO $ hGetLine hin
return (ValString s)
val ->
return val
Expand Down
4 changes: 2 additions & 2 deletions src/Juvix/Compiler/Tree/Evaluator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,10 @@ hRunIO hin hout infoTable = \case
!x'' = hEval hout infoTable code
hRunIO hin hout infoTable x''
ValConstr (Constr (BuiltinTag TagWrite) [ValString s]) -> do
liftIO $ hPutStr hout s
hPutStr hout s
return ValVoid
ValConstr (Constr (BuiltinTag TagWrite) [arg]) -> do
liftIO $ hPutStr hout (ppPrint infoTable arg)
hPutStr hout (ppPrint infoTable arg)
return ValVoid
ValConstr (Constr (BuiltinTag TagReadLn) []) -> do
liftIO $ hFlush hout
Expand Down
6 changes: 3 additions & 3 deletions src/Juvix/Compiler/Tree/EvaluatorEff.hs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ hEvalIOEither hin hout infoTable funInfo = do
v <- eval infoTable (funInfo ^. functionCode)
hRunIO hin hout infoTable v
let handleTrace :: forall q. (MonadIO q) => Value -> q ()
handleTrace = liftIO . hPutStrLn hout . printValue infoTable
handleTrace = hPutStrLn hout . printValue infoTable
liftIO
. runEff
. runError @TreeError
Expand All @@ -303,10 +303,10 @@ hRunIO hin hout infoTable = \case
res <- eval infoTable code
hRunIO hin hout infoTable res
ValConstr (Constr (BuiltinTag TagWrite) [ValString s]) -> do
liftIO $ hPutStr hout s
hPutStr hout s
return ValVoid
ValConstr (Constr (BuiltinTag TagWrite) [arg]) -> do
liftIO $ hPutStr hout (ppPrint infoTable arg)
hPutStr hout (ppPrint infoTable arg)
return ValVoid
ValConstr (Constr (BuiltinTag TagReadLn) []) -> do
liftIO $ hFlush hout
Expand Down
16 changes: 8 additions & 8 deletions src/Juvix/Extra/Version.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,20 @@ shortHash = projectOrUnknown (take 7 . giHash)
versionTag :: Text
versionTag = versionDoc <> "-" <> shortHash

progName :: IO Text
progName = pack . toUpperFirst <$> getProgName
progName :: (MonadIO m) => m Text
progName = pack . toUpperFirst <$> liftIO getProgName

progNameVersion :: IO Text
progNameVersion :: (MonadIO m) => m Text
progNameVersion = do
pName <- progName
return (pName <> " version " <> versionDoc)

progNameVersionTag :: IO Text
progNameVersionTag :: (MonadIO m) => m Text
progNameVersionTag = do
progNameV <- progNameVersion
return (progNameV <> "-" <> shortHash)

infoVersionRepo :: IO (Doc a)
infoVersionRepo :: (MonadIO m) => m (Doc a)
infoVersionRepo = do
pNameTag <- progNameVersionTag
return
Expand All @@ -69,10 +69,10 @@ infoVersionRepo = do
<> line
)

runDisplayVersion :: IO ()
runDisplayVersion :: (MonadIO m) => m ()
runDisplayVersion = do
v <- layoutPretty defaultLayoutOptions <$> infoVersionRepo
renderIO stdout v
liftIO (renderIO stdout v)

runDisplayNumericVersion :: IO ()
runDisplayNumericVersion :: (MonadIO m) => m ()
runDisplayNumericVersion = putStrLn versionDoc
Loading
Loading