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

Run Nockma in an Anoma node #3128

Merged
merged 16 commits into from
Oct 29, 2024
3 changes: 3 additions & 0 deletions app/App.hs
Original file line number Diff line number Diff line change
Expand Up @@ -321,5 +321,8 @@ instance AppError Text where
instance AppError JuvixError where
appError = exitJuvixError

instance AppError SimpleError where
appError = exitFailMsg . toPlainText

class AppError e where
appError :: (Members '[App] r) => e -> Sem r a
20 changes: 16 additions & 4 deletions app/Commands/Compile/Anoma.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,31 @@ runCommand opts = do
let opts' = opts ^. anomaCompileCommonOptions
inputFile = opts' ^. compileInputFile
moutputFile = opts' ^. compileOutputFile
nockmaFile :: Path Abs File <- getOutputFile FileExtNockma inputFile moutputFile
res <- compileAnomaOpts opts'
outputAnomaResult (opts' ^. compileDebug) nockmaFile res

compileAnoma :: (Members AppEffects r) => Maybe (AppPath File) -> Sem r Nockma.AnomaResult
compileAnoma inputFile = do
let opts' =
defaultCompileCommonOptionsMain
{ _compileInputFile = inputFile
}
compileAnomaOpts opts'

compileAnomaOpts :: (Members AppEffects r) => CompileCommonOptions 'InputMain -> Sem r Nockma.AnomaResult
compileAnomaOpts opts' = do
coreRes <- fromCompileCommonOptionsMain opts' >>= compileToCore
entryPoint <-
applyOptions opts
applyOptions opts'
<$> getEntryPoint (opts' ^. compileInputFile)
nockmaFile :: Path Abs File <- getOutputFile FileExtNockma inputFile moutputFile
r <-
runReader entryPoint
. runError @JuvixError
. coreToAnoma
$ coreRes
^. coreResultModule
res <- getRight r
outputAnomaResult (opts' ^. compileDebug) nockmaFile res
getRight r

outputAnomaResult :: (Members '[EmbedIO, App, Files] r) => Bool -> Path Abs File -> Nockma.AnomaResult -> Sem r ()
outputAnomaResult debugOutput nockmaFile Nockma.AnomaResult {..} = do
Expand Down
10 changes: 10 additions & 0 deletions app/Commands/Compile/CommonOptions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ deriving stock instance (Typeable k, Data (InputFileType k)) => Data (CompileCom

makeLenses ''CompileCommonOptions

defaultCompileCommonOptionsMain :: CompileCommonOptions 'InputMain
defaultCompileCommonOptionsMain =
CompileCommonOptions
{ _compileInputFile = Nothing,
_compileOutputFile = Nothing,
_compileDebug = False,
_compileInliningDepth = defaultInliningDepth,
_compileOptimizationLevel = Just defaultOptimizationLevel
}

instance EntryPointOptions (CompileCommonOptions b) where
applyOptions opts e =
e
Expand Down
2 changes: 2 additions & 0 deletions app/Commands/Dev.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Commands.Dev
where

import Commands.Base
import Commands.Dev.Anoma qualified as Anoma
import Commands.Dev.Asm qualified as Asm
import Commands.Dev.Casm qualified as Casm
import Commands.Dev.Core qualified as Core
Expand Down Expand Up @@ -45,3 +46,4 @@ runCommand = \case
JuvixDevRepl opts -> Repl.runCommand opts
MigrateJuvixYaml opts -> runFilesIO $ MigrateJuvixYaml.runCommand opts
Nockma opts -> Nockma.runCommand opts
Anoma opts -> Anoma.runCommand opts
13 changes: 13 additions & 0 deletions app/Commands/Dev/Anoma.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Commands.Dev.Anoma
( module Commands.Dev.Anoma,
module Commands.Dev.Anoma.Options,
)
where

import Commands.Base
import Commands.Dev.Anoma.Node qualified as Node
import Commands.Dev.Anoma.Options

runCommand :: (Members AppEffects r) => AnomaCommand -> Sem r ()
runCommand = \case
Node opts -> Node.runCommand opts
14 changes: 14 additions & 0 deletions app/Commands/Dev/Anoma/Node.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Commands.Dev.Anoma.Node where

import Anoma.Effect
import Commands.Base
import Commands.Dev.Anoma.Node.Options

runCommand :: forall r. (Members AppEffects r) => NodeOptions -> Sem r ()
runCommand opts = runAppError @SimpleError
. runConcurrent
. runProcess
$ do
anomaDir :: AnomaPath <- AnomaPath <$> fromAppPathDir (opts ^. nodeAnomaPath)
runAnoma anomaDir $ do
void noHalt
15 changes: 15 additions & 0 deletions app/Commands/Dev/Anoma/Node/Options.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Commands.Dev.Anoma.Node.Options where

import CommonOptions

newtype NodeOptions = NodeOptions
{ _nodeAnomaPath :: AppPath Dir
}
deriving stock (Data)

makeLenses ''NodeOptions

parseNodeOptions :: Parser NodeOptions
parseNodeOptions = do
_nodeAnomaPath <- anomaDirOpt
pure NodeOptions {..}
24 changes: 24 additions & 0 deletions app/Commands/Dev/Anoma/Options.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Commands.Dev.Anoma.Options where

import Commands.Dev.Anoma.Node.Options
import CommonOptions

newtype AnomaCommand
= Node NodeOptions
deriving stock (Data)

parseAnomaCommand :: Parser AnomaCommand
parseAnomaCommand =
hsubparser
( mconcat
[commandNode]
)
where
commandNode :: Mod CommandFields AnomaCommand
commandNode = command "node" runInfo
where
runInfo :: ParserInfo AnomaCommand
runInfo =
info
(Node <$> parseNodeOptions)
(progDesc "Run an Anoma node and client.")
2 changes: 1 addition & 1 deletion app/Commands/Dev/Core/Repl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ parseText = Core.runParser replPath defaultModuleId
runRepl :: forall r. (Members '[EmbedIO, App] r) => CoreReplOptions -> Core.InfoTable -> Sem r ()
runRepl opts tab = do
putStr "> "
liftIO (hFlush stdout)
hFlush stdout
done <- liftIO isEOF
unless done $ do
s <- getLine
Expand Down
2 changes: 1 addition & 1 deletion app/Commands/Dev/Nockma/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ parseNockmaCommand =
runInfo =
info
(NockmaRun <$> parseNockmaRunOptions)
(progDesc "Run an Anoma program. It should be used with artefacts obtained from compilation with the anoma target.")
(progDesc ("Run an Anoma program. It should be used with artefacts obtained from compilation with the anoma target. If the --" <> anomaDirOptLongStr <> " is given, then it runs the code in an anoma node"))

commandFromAsm :: Mod CommandFields NockmaCommand
commandFromAsm = command "eval" fromAsmInfo
Expand Down
33 changes: 20 additions & 13 deletions app/Commands/Dev/Nockma/Run.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Commands.Dev.Nockma.Run where

import Anoma.Effect
import Commands.Base hiding (Atom)
import Commands.Dev.Nockma.Run.Options
import Juvix.Compiler.Nockma.Anoma
Expand All @@ -13,22 +14,28 @@ runCommand opts = do
afile <- fromAppPathFile inputFile
argsFile <- mapM fromAppPathFile (opts ^. nockmaRunArgs)
parsedArgs <- runAppError @JuvixError (mapM Nockma.cueJammedFileOrPretty argsFile)
parsedTerm <- checkCued (Nockma.cueJammedFileOrPretty afile)
parsedTerm <- runAppError @JuvixError (Nockma.cueJammedFileOrPretty afile)
case parsedTerm of
t@(TermCell {}) -> do
let formula = anomaCallTuple parsedArgs
(counts, res) <-
runOpCounts
. runReader defaultEvalOptions
. runOutputSem @(Term Natural) (logInfo . mkAnsiText . ppTrace)
$ evalCompiledNock' t formula
putStrLn (ppPrint res)
let statsFile = replaceExtension' ".profile" afile
writeFileEnsureLn statsFile (prettyText counts)
TermAtom {} -> exitFailMsg "Expected nockma input to be a cell"
t@(TermCell {}) -> case opts ^. nockmaRunAnomaDir of
Just path -> do
anomaDir <- AnomaPath <$> fromAppPathDir path
runInAnoma anomaDir t (unfoldTuple parsedArgs)
Nothing -> do
let formula = anomaCallTuple parsedArgs
(counts, res) <-
runOpCounts
. runReader defaultEvalOptions
. runOutputSem @(Term Natural) (logInfo . mkAnsiText . ppTrace)
$ evalCompiledNock' t formula
putStrLn (ppPrint res)
let statsFile = replaceExtension' ".profile" afile
writeFileEnsureLn statsFile (prettyText counts)
where
inputFile :: AppPath File
inputFile = opts ^. nockmaRunFile

checkCued :: Sem (Error JuvixError ': r) a -> Sem r a
checkCued = runErrorNoCallStackWith exitJuvixError
runInAnoma :: (Members AppEffects r) => AnomaPath -> Term Natural -> [Term Natural] -> Sem r ()
runInAnoma anoma t args = runAppError @SimpleError . runAnoma anoma $ do
res <- runNockma t args
putStrLn (ppPrint res)
2 changes: 2 additions & 0 deletions app/Commands/Dev/Nockma/Run/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import CommonOptions

data NockmaRunOptions = NockmaRunOptions
{ _nockmaRunFile :: AppPath File,
_nockmaRunAnomaDir :: Maybe (AppPath Dir),
_nockmaRunProfile :: Bool,
_nockmaRunArgs :: Maybe (AppPath File)
}
Expand All @@ -24,6 +25,7 @@ parseNockmaRunOptions = do
<> action "file"
)
pure AppPath {_pathIsInput = True, ..}
_nockmaRunAnomaDir <- optional anomaDirOpt
_nockmaRunProfile <-
switch
( long "profile"
Expand Down
10 changes: 10 additions & 0 deletions app/Commands/Dev/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module Commands.Dev.Options
)
where

import Commands.Dev.Anoma.Options
import Commands.Dev.Asm.Options hiding (Compile)
import Commands.Dev.Casm.Options
import Commands.Dev.Core.Options
Expand Down Expand Up @@ -52,6 +53,7 @@ data DevCommand
| JuvixDevRepl ReplOptions
| MigrateJuvixYaml MigrateJuvixYamlOptions
| Nockma NockmaCommand
| Anoma AnomaCommand
deriving stock (Data)

parseDevCommand :: Parser DevCommand
Expand All @@ -75,6 +77,7 @@ parseDevCommand =
commandJuvixDevRepl,
commandMigrateJuvixYaml,
commandLatex,
commandAnoma,
commandNockma
]
)
Expand Down Expand Up @@ -206,3 +209,10 @@ commandNockma =
info
(Nockma <$> parseNockmaCommand)
(progDesc "Subcommands related to the nockma backend")

commandAnoma :: Mod CommandFields DevCommand
commandAnoma =
command "anoma" $
info
(Anoma <$> parseAnomaCommand)
(progDesc "Subcommands related to the anoma")
23 changes: 21 additions & 2 deletions app/CommonOptions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import Juvix.Prelude.Pretty hiding (group, list)
import Options.Applicative hiding (helpDoc)
import Options.Applicative qualified as Opt
import Prettyprinter.Render.Terminal hiding (renderIO, renderStrict)
import System.Process
import System.Process as System
import Text.Read (readMaybe)
import Prelude qualified

Expand Down Expand Up @@ -76,6 +76,25 @@ parseInputFile = parseInputFiles . NonEmpty.singleton
numThreadsOpt :: ReadM NumThreads
numThreadsOpt = eitherReader readNumThreads

anomaDirOptLongStr :: forall str. (IsString str) => str
anomaDirOptLongStr = "anoma-dir"

anomaDirOpt :: Parser (AppPath Dir)
anomaDirOpt = do
path <-
option
somePreDirOpt
( long anomaDirOptLongStr
<> metavar "ANOMA_DIR"
<> help "Path to anoma repository"
<> action "directory"
)
return
AppPath
{ _pathIsInput = False,
_pathPath = path
}

parseNumThreads :: Parser NumThreads
parseNumThreads = do
option
Expand Down Expand Up @@ -209,7 +228,7 @@ enumCompleter _ = listCompleter [Juvix.show e | e <- allElements @a]
extCompleter :: FileExt -> Completer
extCompleter ext = mkCompleter $ \word -> do
let cmd = unwords ["compgen", "-o", "plusdirs", "-f", "-X", "!*" <> Prelude.show ext, "--", requote word]
result <- GHC.try @GHC.SomeException $ readProcess "bash" ["-c", cmd] ""
result <- GHC.try @GHC.SomeException $ System.readProcess "bash" ["-c", cmd] ""
return . lines . fromRight [] $ result

requote :: String -> String
Expand Down
1 change: 1 addition & 0 deletions include/anoma/start.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
IO.puts(Anoma.Node.Examples.ENode.start_node().grpc_port)
1 change: 1 addition & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ dependencies:
- ansi-terminal == 1.1.*
- base == 4.19.*
- base16-bytestring == 1.0.*
- base64-bytestring == 1.2.*
- bitvec == 1.1.*
- blaze-html == 0.9.*
- bytestring == 0.12.*
Expand Down
8 changes: 8 additions & 0 deletions src/Anoma/Effect.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Anoma.Effect
( module Anoma.Effect.Base,
module Anoma.Effect.RunNockma,
)
where

import Anoma.Effect.Base
import Anoma.Effect.RunNockma
Loading
Loading