1
1
mirror of https://github.com/anoma/juvix.git synced 2024-12-29 18:43:42 +03:00
juvix/app/Commands/Repl.hs

608 lines
22 KiB
Haskell
Raw Normal View History

module Commands.Repl where
import Commands.Base hiding
( command,
)
import Commands.Repl.Base
import Commands.Repl.Options
import Control.Exception (throwIO)
import Control.Monad.Except qualified as Except
import Control.Monad.Reader qualified as Reader
import Control.Monad.State.Strict qualified as State
import Control.Monad.Trans.Class (lift)
upgrade to Ghc 9.8.1 (#2624) This PR updates the stackage LTS resolver to `nightly-2024-02-06` which uses GHC 9.8.1 ## Upgrade notes You will need to update your HLS to [2.6.0.0](https://github.com/haskell/haskell-language-server/releases/tag/2.6.0.0), this release contains support for GHC 9.8.1 ## Fixes ### `haskeline` / `repline` We have removed the custom haskeline / repline forks used in the build. This is because we had trouble overriding haskeline as it is bundled with GHC and the stackage resolver uses this bundled version. We were using a custom fork of haskeline to implement [mapInputT_](https://github.com/anoma/juvix/blob/15c0685c911a7ce3d2ee8c1597a1e6dd96e707bf/app/Commands/Repl.hs#L409) in the Juvix REPL, required to implement error handling. This requires private API from the Haskeline library. Instead of using a custom fork we use TemplateHaskell to obtain access to the private API we need. See [DarkArts.hs](https://github.com/anoma/juvix/blob/15c0685c911a7ce3d2ee8c1597a1e6dd96e707bf/src/Juvix/Prelude/DarkArts.hs) and [HaskelineJB.hs](https://github.com/anoma/juvix/blob/15c0685c911a7ce3d2ee8c1597a1e6dd96e707bf/app/HaskelineJH.hs). To obtain access to the private API, we adapted a method from [a Tweag blogpost](https://www.tweag.io/blog/2021-01-07-haskell-dark-arts-part-i/) and [repo](https://github.com/tweag/th-jailbreak) - updating it for GHC 9.8.1. ### `aeson-better-errors` The `aeson-better-errors` library has not been updated to work with `mtl-2.3.0` so it cannot work with the new stackage resolver. We are using a [fork](https://github.com/Vekhir/aeson-better-errors.git) which has been updated. We should consider replacing this library in future, see https://github.com/anoma/juvix/issues/2621 ### `path` The `path` library now includes API `splitDrive` and `dropDrive` so we can remove our versions of those functions from the prelude. ### `with-utf8` We no longer need to depend on `with-utf8`. We were using this package for UTF-8 versions of `readFile` and `writeFile` APIs. These APIs are now available in the `text` package. ### Compiler warnings GHC 9.8.1 introduces several new compiler warnings. * We have suppressed `missing-role-annotations` and `missing-poly-kind-signatures` * We added our own versions of `head` and `tail` to work around the new `partial-tx` warning introduced for those functions in `Data.List`. * We fixed up the code to avoid the [term-variable-capture](https://ghc.gitlab.haskell.org/ghc/doc/users_guide/using-warnings.html#ghc-flag--Wterm-variable-capture) warning.
2024-02-07 12:47:48 +03:00
import Control.Monad.Trans.Reader (mapReaderT)
import Data.String.Interpolate (i)
upgrade to Ghc 9.8.1 (#2624) This PR updates the stackage LTS resolver to `nightly-2024-02-06` which uses GHC 9.8.1 ## Upgrade notes You will need to update your HLS to [2.6.0.0](https://github.com/haskell/haskell-language-server/releases/tag/2.6.0.0), this release contains support for GHC 9.8.1 ## Fixes ### `haskeline` / `repline` We have removed the custom haskeline / repline forks used in the build. This is because we had trouble overriding haskeline as it is bundled with GHC and the stackage resolver uses this bundled version. We were using a custom fork of haskeline to implement [mapInputT_](https://github.com/anoma/juvix/blob/15c0685c911a7ce3d2ee8c1597a1e6dd96e707bf/app/Commands/Repl.hs#L409) in the Juvix REPL, required to implement error handling. This requires private API from the Haskeline library. Instead of using a custom fork we use TemplateHaskell to obtain access to the private API we need. See [DarkArts.hs](https://github.com/anoma/juvix/blob/15c0685c911a7ce3d2ee8c1597a1e6dd96e707bf/src/Juvix/Prelude/DarkArts.hs) and [HaskelineJB.hs](https://github.com/anoma/juvix/blob/15c0685c911a7ce3d2ee8c1597a1e6dd96e707bf/app/HaskelineJH.hs). To obtain access to the private API, we adapted a method from [a Tweag blogpost](https://www.tweag.io/blog/2021-01-07-haskell-dark-arts-part-i/) and [repo](https://github.com/tweag/th-jailbreak) - updating it for GHC 9.8.1. ### `aeson-better-errors` The `aeson-better-errors` library has not been updated to work with `mtl-2.3.0` so it cannot work with the new stackage resolver. We are using a [fork](https://github.com/Vekhir/aeson-better-errors.git) which has been updated. We should consider replacing this library in future, see https://github.com/anoma/juvix/issues/2621 ### `path` The `path` library now includes API `splitDrive` and `dropDrive` so we can remove our versions of those functions from the prelude. ### `with-utf8` We no longer need to depend on `with-utf8`. We were using this package for UTF-8 versions of `readFile` and `writeFile` APIs. These APIs are now available in the `text` package. ### Compiler warnings GHC 9.8.1 introduces several new compiler warnings. * We have suppressed `missing-role-annotations` and `missing-poly-kind-signatures` * We added our own versions of `head` and `tail` to work around the new `partial-tx` warning introduced for those functions in `Data.List`. * We fixed up the code to avoid the [term-variable-capture](https://ghc.gitlab.haskell.org/ghc/doc/users_guide/using-warnings.html#ghc-flag--Wterm-variable-capture) warning.
2024-02-07 12:47:48 +03:00
import HaskelineJB
import Juvix.Compiler.Concrete.Data.Scope (scopePath)
import Juvix.Compiler.Concrete.Data.Scope qualified as Scoped
import Juvix.Compiler.Concrete.Data.ScopedName (absTopModulePath)
import Juvix.Compiler.Concrete.Data.ScopedName qualified as Scoped
import Juvix.Compiler.Concrete.Language qualified as Concrete
import Juvix.Compiler.Concrete.Pretty qualified as Concrete
import Juvix.Compiler.Core qualified as Core
import Juvix.Compiler.Core.Extra.Value
import Juvix.Compiler.Core.Info qualified as Info
import Juvix.Compiler.Core.Info.NoDisplayInfo qualified as Info
import Juvix.Compiler.Core.Pretty qualified as Core
import Juvix.Compiler.Internal.Language qualified as Internal
import Juvix.Compiler.Internal.Pretty qualified as Internal
import Juvix.Compiler.Pipeline.Repl
import Juvix.Compiler.Store.Extra
import Juvix.Data.CodeAnn (Ann)
import Juvix.Data.Error.GenericError qualified as Error
import Juvix.Data.NameKind
import Juvix.Extra.Paths qualified as P
import Juvix.Extra.Stdlib
import Juvix.Extra.Version
import Juvix.Prelude.Pretty qualified as P
import System.Console.ANSI qualified as Ansi
import System.Console.Haskeline
import System.Console.Repline
import System.Console.Repline qualified as Repline
printHelpTxt :: ReplOptions -> Repl ()
printHelpTxt opts = do
liftIO $ do
putStrLn normalCmds
let isDev = opts ^. replIsDev
when isDev (putStrLn devCmds)
where
normalCmds :: Text
normalCmds =
[__i|
EXPRESSION Evaluate an expression in the context of the currently loaded module
:help Print help text and describe options
:load FILE Load a file into the REPL
:reload Reload the currently loaded file
:type EXPRESSION Infer the type of an expression
:def IDENTIFIER Print the definition of the identifier
2023-05-30 19:19:39 +03:00
:doc IDENTIFIER Print the documentation of the identifier
:core EXPRESSION Translate the expression to JuvixCore
:multiline Start a multi-line input. Submit with <Ctrl-D>
:root Print the current project root
:version Display the Juvix version
:quit Exit the REPL
|]
devCmds :: Text
devCmds =
[__i|
:dev DEV CMD Command reserved for debugging
|]
replDefaultLoc :: Interval
replDefaultLoc = singletonInterval (mkInitialLoc P.replPath)
replFromJust :: Repl a -> Maybe a -> Repl a
replFromJust err = maybe err return
replFromEither :: Either JuvixError a -> Repl a
replFromEither = either (lift . Except.throwError) return
replGetContext :: Repl ReplContext
replGetContext = State.gets (^. replStateContext) >>= replFromJust noFileLoadedErr
replError :: AnsiText -> Repl a
replError msg =
lift
. Except.throwError
. JuvixError
$ GenericError
{ _genericErrorLoc = replDefaultLoc,
_genericErrorMessage = msg,
_genericErrorIntervals = [replDefaultLoc]
}
noFileLoadedErr :: Repl a
noFileLoadedErr = replError (mkAnsiText @Text "No file loaded. Load a file using the `:load FILE` command.")
welcomeMsg :: (MonadIO m) => m ()
welcomeMsg = liftIO (putStrLn [i|Juvix REPL version #{versionTag}: https://juvix.org. Run :help for help|])
multilineCmd :: String
multilineCmd = "multiline"
quit :: String -> Repl ()
quit _ = liftIO (throwIO Interrupt)
loadEntryPoint :: EntryPoint -> Repl ()
loadEntryPoint ep = do
artif <- runReplPipelineIO ep
let newCtx =
ReplContext
{ _replContextArtifacts = artif,
_replContextEntryPoint = ep
}
State.modify (set replStateContext (Just newCtx))
let epPath :: Maybe (Path Abs File) = ep ^. entryPointModulePath
Parallel pipeline (#2779) This pr introduces parallelism in the pipeline to gain performance. I've included benchmarks at the end. - Closes #2750. # Flags: There are two new global flags: 1. `-N / --threads`. It is used to set the number of capabilities. According to [GHC documentation](https://hackage.haskell.org/package/base-4.20.0.0/docs/GHC-Conc.html#v:setNumCapabilities): _Set the number of Haskell threads that can run truly simultaneously (on separate physical processors) at any given time_. When compiling in parallel, we create this many worker threads. The default value is `-N auto`, which sets `-N` to half the number of logical cores, capped at 8. 2. `--dev-show-thread-ids`. When given, the thread id is printed in the compilation progress log. E.g. ![image](https://github.com/anoma/juvix/assets/5511599/9359fae2-0be1-43e5-8d74-faa82cba4034) # Parallel compilation 1. I've added `src/Parallel/ParallelTemplate.hs` which contains all the concurrency related code. I think it is good to keep this code separated from the actual compiler code. 2. I've added a progress log (only for the parallel driver) that outputs a log of the compilation progress, similar to what stack/cabal do. # Code changes: 1. I've removed the `setup` stage where we were registering dependencies. Instead, the dependencies are registered when the `pathResolver` is run for the first time. This way it is safer. 1. Now the `ImportTree` is needed to run the pipeline. Cycles are detected during the construction of this tree, so I've removed `Reader ImportParents` from the pipeline. 3. For the package pathresolver, we do not support parallelism yet (we could add support for it in the future, but the gains will be small). 4. When `-N1`, the pipeline remains unchanged, so performance should be the same as in the main branch (except there is a small performance degradation due to adding the `-threaded` flag). 5. I've introduced `PipelineOptions`, which are options that are used to pass options to the effects in the pipeline. 6. `PathResolver` constraint has been removed from the `upTo*` functions in the pipeline due to being redundant. 7. I've added a lot of `NFData` instances. They are needed to force the full evaluation of `Stored.ModuleInfo` in each of the threads. 2. The `Cache` effect uses [`SharedState`](https://hackage.haskell.org/package/effectful-core-2.3.0.1/docs/Effectful-State-Static-Shared.html) as opposed to [`LocalState`](https://hackage.haskell.org/package/effectful-core-2.3.0.1/docs/Effectful-Writer-Static-Local.html). Perhaps we should provide different versions. 3. I've added a `Cache` handler that accepts a setup function. The setup is triggered when a miss is detected. It is used to lazily compile the modules in parallel. # Tests 1. I've adapted the smoke test suite to ignore the progress log in the stderr. 5. I've had to adapt `tests/positive/Internal/Lambda.juvix`. Due to laziness, a crash happening in this file was not being caught. The problem is that in this file we have a lambda function with different number of patterns in their clauses, which we currently do not support (https://github.com/anoma/juvix/issues/1706). 6. I've had to comment out the definition ``` x : Box ((A : Type) → A → A) := box λ {A a := a}; ``` From the test as it was causing a crash (https://github.com/anoma/juvix/issues/2247). # Future Work 1. It should be investigated how much performance we lose by fully evaluating the `Stored.ModuleInfo`, since some information in it will be discarded. It may be possible to be more fine-grained when forcing evaluation. 8. The scanning of imports to build the import tree is sequential. Now, we build the import tree from the entry point module and only the modules that are imported from it are in the tree. However, we have discussed that at some point we should make a distinction between `juvix` _the compiler_ and `juvix` _the build tool_. When using `juvix` as a build tool it makes sense to typecheck/compile (to stored core) all modules in the project. When/if we do this, scanning imports in all modules in parallel becomes trivial. 9. The implementation of the `ParallelTemplate` uses low level primitives such as [forkIO](https://hackage.haskell.org/package/base-4.20.0.0/docs/Control-Concurrent.html#v:forkIO). At some point it should be refactored to use safer functions from the [`Effectful.Concurrent.Async`](https://hackage.haskell.org/package/effectful-2.3.0.0/docs/Effectful-Concurrent-Async.html) module. 10. The number of cores and worker threads that we spawn is determined by the command line. Ideally, we could use to import tree to compute an upper bound to the ideal number of cores to use. 11. We could add an animation that displays which modules are being compiled in parallel and which have finished being compiled. # Benchmarks On some benchmarks, I include the GHC runtime option [`-A`](https://downloads.haskell.org/ghc/latest/docs/users_guide/runtime_control.html#rts-flag--A%20%E2%9F%A8size%E2%9F%A9), which sometimes makes a good impact on performance. Thanks to @paulcadman for pointing this out. I've figured a good combination of `-N` and `-A` through trial and error (but this oviously depends on the cpu and juvix projects). ## Typecheck the standard library ### Clean run (88% faster than main): ``` hyperfine --warmup 1 --prepare 'juvix clean' 'juvix -N 4 typecheck Stdlib/Prelude.juvix +RTS -A33554432' 'juvix -N 4 typecheck Stdlib/Prelude.juvix' 'juvix-main typecheck Stdlib/Prelude.juvix' Benchmark 1: juvix -N 4 typecheck Stdlib/Prelude.juvix +RTS -A33554432 Time (mean ± σ): 444.1 ms ± 6.5 ms [User: 1018.0 ms, System: 77.7 ms] Range (min … max): 432.6 ms … 455.9 ms 10 runs Benchmark 2: juvix -N 4 typecheck Stdlib/Prelude.juvix Time (mean ± σ): 628.3 ms ± 23.9 ms [User: 1227.6 ms, System: 69.5 ms] Range (min … max): 584.7 ms … 670.6 ms 10 runs Benchmark 3: juvix-main typecheck Stdlib/Prelude.juvix Time (mean ± σ): 835.9 ms ± 12.3 ms [User: 788.5 ms, System: 31.9 ms] Range (min … max): 816.0 ms … 853.6 ms 10 runs Summary juvix -N 4 typecheck Stdlib/Prelude.juvix +RTS -A33554432 ran 1.41 ± 0.06 times faster than juvix -N 4 typecheck Stdlib/Prelude.juvix 1.88 ± 0.04 times faster than juvix-main typecheck Stdlib/Prelude.juvix ``` ### Cached run (43% faster than main): ``` hyperfine --warmup 1 'juvix -N 4 typecheck Stdlib/Prelude.juvix +RTS -A33554432' 'juvix -N 4 typecheck Stdlib/Prelude.juvix' 'juvix-main typecheck Stdlib/Prelude.juvix' Benchmark 1: juvix -N 4 typecheck Stdlib/Prelude.juvix +RTS -A33554432 Time (mean ± σ): 241.3 ms ± 7.3 ms [User: 538.6 ms, System: 101.3 ms] Range (min … max): 231.5 ms … 251.3 ms 11 runs Benchmark 2: juvix -N 4 typecheck Stdlib/Prelude.juvix Time (mean ± σ): 235.1 ms ± 12.0 ms [User: 405.3 ms, System: 87.7 ms] Range (min … max): 216.1 ms … 253.1 ms 12 runs Benchmark 3: juvix-main typecheck Stdlib/Prelude.juvix Time (mean ± σ): 336.7 ms ± 13.3 ms [User: 269.5 ms, System: 67.1 ms] Range (min … max): 316.9 ms … 351.8 ms 10 runs Summary juvix -N 4 typecheck Stdlib/Prelude.juvix ran 1.03 ± 0.06 times faster than juvix -N 4 typecheck Stdlib/Prelude.juvix +RTS -A33554432 1.43 ± 0.09 times faster than juvix-main typecheck Stdlib/Prelude.juvix ``` ## Typecheck the test suite of the containers library At the moment this is the biggest juvix project that we have. ### Clean run (105% faster than main) ``` hyperfine --warmup 1 --prepare 'juvix clean' 'juvix -N 6 typecheck Main.juvix +RTS -A67108864' 'juvix -N 4 typecheck Main.juvix' 'juvix-main typecheck Main.juvix' Benchmark 1: juvix -N 6 typecheck Main.juvix +RTS -A67108864 Time (mean ± σ): 1.006 s ± 0.011 s [User: 2.171 s, System: 0.162 s] Range (min … max): 0.991 s … 1.023 s 10 runs Benchmark 2: juvix -N 4 typecheck Main.juvix Time (mean ± σ): 1.584 s ± 0.046 s [User: 2.934 s, System: 0.149 s] Range (min … max): 1.535 s … 1.660 s 10 runs Benchmark 3: juvix-main typecheck Main.juvix Time (mean ± σ): 2.066 s ± 0.010 s [User: 1.939 s, System: 0.089 s] Range (min … max): 2.048 s … 2.077 s 10 runs Summary juvix -N 6 typecheck Main.juvix +RTS -A67108864 ran 1.57 ± 0.05 times faster than juvix -N 4 typecheck Main.juvix 2.05 ± 0.03 times faster than juvix-main typecheck Main.juvix ``` ### Cached run (54% faster than main) ``` hyperfine --warmup 1 'juvix -N 6 typecheck Main.juvix +RTS -A33554432' 'juvix -N 4 typecheck Main.juvix' 'juvix-main typecheck Main.juvix' Benchmark 1: juvix -N 6 typecheck Main.juvix +RTS -A33554432 Time (mean ± σ): 551.8 ms ± 13.2 ms [User: 1419.8 ms, System: 199.4 ms] Range (min … max): 535.2 ms … 570.6 ms 10 runs Benchmark 2: juvix -N 4 typecheck Main.juvix Time (mean ± σ): 636.7 ms ± 17.3 ms [User: 1006.3 ms, System: 196.3 ms] Range (min … max): 601.6 ms … 655.3 ms 10 runs Benchmark 3: juvix-main typecheck Main.juvix Time (mean ± σ): 847.2 ms ± 58.9 ms [User: 710.1 ms, System: 126.5 ms] Range (min … max): 731.1 ms … 890.0 ms 10 runs Summary juvix -N 6 typecheck Main.juvix +RTS -A33554432 ran 1.15 ± 0.04 times faster than juvix -N 4 typecheck Main.juvix 1.54 ± 0.11 times faster than juvix-main typecheck Main.juvix ```
2024-05-31 14:41:30 +03:00
whenJust epPath $ \path -> liftIO (putStrLn [i|OK loaded: #{toFilePath @String path}|])
reloadFile :: String -> Repl ()
reloadFile _ = replGetContext >>= loadEntryPoint . (^. replContextEntryPoint)
pSomeFile :: String -> Prepath File
pSomeFile = mkPrepath
loadFile :: Prepath File -> Repl ()
loadFile f = do
entryPoint <- getReplEntryPointFromPrepath f
loadEntryPoint entryPoint
loadDefaultPrelude :: Repl ()
loadDefaultPrelude =
whenJustM
defaultPreludeEntryPoint
loadEntryPoint
getReplEntryPoint :: (Root -> a -> GlobalOptions -> IO EntryPoint) -> a -> Repl EntryPoint
getReplEntryPoint f inputFile = do
root <- Reader.asks (^. replRoot)
gopts <- State.gets (^. replStateGlobalOptions)
liftIO (set entryPointSymbolPruningMode KeepAll <$> f root inputFile gopts)
getReplEntryPointFromPrepath :: Prepath File -> Repl EntryPoint
Improve performance of formatting a project (#2863) Currently formatting a project is equivalent to running `juvix format` on each individual file. Hence, the performance is quadratic wrt the number of modules in the project. This pr fixes that and we now we only process each module once. # Benchmark (1236% faster :rocket:) Checking the standard library ``` hyperfine --warmup 1 'juvix format --check' 'juvix-main format --check' Benchmark 1: juvix format --check Time (mean ± σ): 450.6 ms ± 33.7 ms [User: 707.2 ms, System: 178.7 ms] Range (min … max): 396.0 ms … 497.0 ms 10 runs Benchmark 2: juvix-main format --check Time (mean ± σ): 6.019 s ± 0.267 s [User: 9.333 s, System: 1.512 s] Range (min … max): 5.598 s … 6.524 s 10 runs Summary juvix format --check ran 13.36 ± 1.16 times faster than juvix-main format --check ``` # Other changes: 1. The `EntryPoint` field `entryPointModulePath` is now optional. 2. I've introduced a new type `TopModulePathKey` which is analogous to `TopModulePath` but wihout location information. It is used in hashmap keys where the location in the key is never used. This is useful as we can now get a `TopModulePathKey` from a `Path Rel File`. 3. I've refactored the `_formatInput` field in `FormatOptions` so that it doesn't need to be a special case anymore. 4. I've introduced a new effect `Forcing` that allows to individually force fields of a record type with a convenient syntax. 5. I've refactored some of the constraints in scoping so that they only require `Reader Package` instead of `Reader EntryPoint`. 6. I've introduced a new type family so that local modules are no longer required to have `ModuleId` from their type. Before, they were assigned one, but it was never used. # Future work: 1. For project-wise formatting, the compilation is done in parallel, but the formatting is still done sequentially. That should be improved.
2024-07-01 19:05:24 +03:00
getReplEntryPointFromPrepath = getReplEntryPoint (\r x -> runM . runTaggedLockPermissive . entryPointFromGlobalOptionsPre r (Just x))
getReplEntryPointFromPath :: Path Abs File -> Repl EntryPoint
Improve performance of formatting a project (#2863) Currently formatting a project is equivalent to running `juvix format` on each individual file. Hence, the performance is quadratic wrt the number of modules in the project. This pr fixes that and we now we only process each module once. # Benchmark (1236% faster :rocket:) Checking the standard library ``` hyperfine --warmup 1 'juvix format --check' 'juvix-main format --check' Benchmark 1: juvix format --check Time (mean ± σ): 450.6 ms ± 33.7 ms [User: 707.2 ms, System: 178.7 ms] Range (min … max): 396.0 ms … 497.0 ms 10 runs Benchmark 2: juvix-main format --check Time (mean ± σ): 6.019 s ± 0.267 s [User: 9.333 s, System: 1.512 s] Range (min … max): 5.598 s … 6.524 s 10 runs Summary juvix format --check ran 13.36 ± 1.16 times faster than juvix-main format --check ``` # Other changes: 1. The `EntryPoint` field `entryPointModulePath` is now optional. 2. I've introduced a new type `TopModulePathKey` which is analogous to `TopModulePath` but wihout location information. It is used in hashmap keys where the location in the key is never used. This is useful as we can now get a `TopModulePathKey` from a `Path Rel File`. 3. I've refactored the `_formatInput` field in `FormatOptions` so that it doesn't need to be a special case anymore. 4. I've introduced a new effect `Forcing` that allows to individually force fields of a record type with a convenient syntax. 5. I've refactored some of the constraints in scoping so that they only require `Reader Package` instead of `Reader EntryPoint`. 6. I've introduced a new type family so that local modules are no longer required to have `ModuleId` from their type. Before, they were assigned one, but it was never used. # Future work: 1. For project-wise formatting, the compilation is done in parallel, but the formatting is still done sequentially. That should be improved.
2024-07-01 19:05:24 +03:00
getReplEntryPointFromPath = getReplEntryPoint (\r a -> runM . runTaggedLockPermissive . entryPointFromGlobalOptions r (Just a))
displayVersion :: String -> Repl ()
displayVersion _ = liftIO (putStrLn versionTag)
replCommand :: ReplOptions -> String -> Repl ()
replCommand opts input_ = catchAll $ do
ctx <- replGetContext
let tab = Core.computeCombinedInfoTable $ ctx ^. replContextArtifacts . artifactCoreModule
evalRes <- compileThenEval ctx input_
whenJust evalRes $ \n ->
if
| Info.member Info.kNoDisplayInfo (Core.getInfo n) -> return ()
| opts ^. replPrintValues ->
renderOutLn (Core.ppOut opts (toValue tab n))
| otherwise -> renderOutLn (Core.ppOut opts n)
where
compileThenEval :: ReplContext -> String -> Repl (Maybe Core.Node)
compileThenEval ctx s = compileString >>= mapM eval
where
artif :: Artifacts
artif = ctx ^. replContextArtifacts
eval :: Core.Node -> Repl Core.Node
eval n = do
gopts <- State.gets (^. replStateGlobalOptions)
ep <- getReplEntryPointFromPrepath (mkPrepath (toFilePath P.replPath))
let shouldDisambiguate :: Bool
shouldDisambiguate = not (opts ^. replNoDisambiguate)
(artif', n') <-
replFromEither
. run
. runReader ep
. runError @JuvixError
. runState artif
. runTransformations shouldDisambiguate (opts ^. replTransformations)
$ n
liftIO (doEvalIO' (project gopts ^. Core.optFieldSize) artif' n') >>= replFromEither
doEvalIO' :: Natural -> Artifacts -> Core.Node -> IO (Either JuvixError Core.Node)
doEvalIO' fsize artif' n =
mapLeft (JuvixError @Core.CoreError)
<$> Core.doEvalIO (Just fsize) False replDefaultLoc (Core.computeCombinedInfoTable $ artif' ^. artifactCoreModule) n
compileString :: Repl (Maybe Core.Node)
compileString = do
(artifacts, res) <- compileReplInputIO' ctx (strip (pack s))
res' <- replFromEither res
State.modify (over (replStateContext . _Just) (set replContextArtifacts artifacts))
return res'
core :: String -> Repl ()
core input_ = do
ctx <- replGetContext
opts <- Reader.asks (^. replOptions)
compileRes <- compileReplInputIO' ctx (strip (pack input_)) >>= replFromEither . snd
whenJust compileRes (renderOutLn . Core.ppOut opts)
dev :: String -> Repl ()
dev input_ = do
ctx <- replGetContext
if
| input_ == scoperStateCmd -> do
renderOutLn (Concrete.ppTrace (ctx ^. replContextArtifacts . artifactScoperState))
| otherwise ->
renderOutLn
( "Unrecognized command "
<> input_
<> "\nAvailable commands: "
<> unwords cmds
)
where
cmds :: [String]
cmds = [scoperStateCmd]
scoperStateCmd :: String
scoperStateCmd = "scoperState"
ppConcrete :: (Concrete.PrettyPrint a) => a -> Repl AnsiText
ppConcrete a = do
gopts <- State.gets (^. replStateGlobalOptions)
let popts :: GenericOptions = project' gopts
return (Concrete.ppOut popts a)
printConcrete :: (Concrete.PrettyPrint a) => a -> Repl ()
2023-05-30 19:19:39 +03:00
printConcrete = ppConcrete >=> renderOut
printConcreteLn :: (Concrete.PrettyPrint a) => a -> Repl ()
2023-05-30 19:19:39 +03:00
printConcreteLn = ppConcrete >=> renderOutLn
replParseIdentifiers :: String -> Repl (NonEmpty Concrete.ScopedIden)
replParseIdentifiers input_ =
replExpressionUpToScopedAtoms (strip (pack input_))
>>= getIdentifiers
where
getIdentifiers :: Concrete.ExpressionAtoms 'Concrete.Scoped -> Repl (NonEmpty Concrete.ScopedIden)
getIdentifiers as = mapM getIdentifier (as ^. Concrete.expressionAtoms)
where
getIdentifier :: Concrete.ExpressionAtom 'Concrete.Scoped -> Repl (Concrete.ScopedIden)
getIdentifier = \case
Concrete.AtomIdentifier a -> return a
Concrete.AtomParens p
| Concrete.ExpressionIdentifier a <- p -> return a
| Concrete.ExpressionParensIdentifier a <- p -> return a
_ -> err
where
err :: Repl a
err = replError (mkAnsiText @Text ":def expects one or more identifiers")
getScopedInfoTable :: Repl Scoped.InfoTable
getScopedInfoTable = do
artifs <- (^. replContextArtifacts) <$> replGetContext
let tab0 = artifs ^. artifactScopeTable
return $ tab0 <> computeCombinedScopedInfoTable (artifs ^. artifactModuleTable)
2023-05-30 19:19:39 +03:00
printDocumentation :: String -> Repl ()
printDocumentation = replParseIdentifiers >=> printIdentifiers
where
printIdentifiers :: NonEmpty Concrete.ScopedIden -> Repl ()
printIdentifiers (d :| ds) = do
printIdentifier d
whenJust (nonEmpty ds) $ \ds' -> replNewline >> printIdentifiers ds'
where
printIdentifier :: Concrete.ScopedIden -> Repl ()
printIdentifier s = do
let n = s ^. Concrete.scopedIdenFinal . Scoped.nameId
2023-07-26 10:59:50 +03:00
mdoc <- case getNameKind s of
KNameAxiom -> getDocAxiom n
KNameInductive -> getDocInductive n
KNameLocal -> return Nothing
KNameFunction -> getDocFunction n
KNameConstructor -> getDocConstructor n
KNameLocalModule -> impossible
KNameTopModule -> impossible
KNameAlias -> impossible
KNameFixity -> impossible
2023-05-30 19:19:39 +03:00
printDoc mdoc
where
printDoc :: Maybe (Concrete.Judoc 'Concrete.Scoped) -> Repl ()
printDoc = \case
Nothing -> do
let s' :: Doc Ann = pretty s
msg = "No documentation available for" <+> s'
renderOutLn (toAnsiText True msg)
2023-05-30 19:19:39 +03:00
Just ju -> printConcrete ju
getDocFunction :: Scoped.NameId -> Repl (Maybe (Concrete.Judoc 'Concrete.Scoped))
getDocFunction fun = do
tbl :: Scoped.InfoTable <- getScopedInfoTable
let def = tbl ^?! Scoped.infoFunctions . at fun . _Just
return (def ^. Concrete.signDoc)
2023-05-30 19:19:39 +03:00
getDocInductive :: Scoped.NameId -> Repl (Maybe (Concrete.Judoc 'Concrete.Scoped))
getDocInductive ind = do
tbl :: Scoped.InfoTable <- getScopedInfoTable
let def :: Concrete.InductiveDef 'Concrete.Scoped = tbl ^?! Scoped.infoInductives . at ind . _Just
2023-05-30 19:19:39 +03:00
return (def ^. Concrete.inductiveDoc)
getDocAxiom :: Scoped.NameId -> Repl (Maybe (Concrete.Judoc 'Concrete.Scoped))
getDocAxiom ax = do
tbl :: Scoped.InfoTable <- getScopedInfoTable
let def :: Concrete.AxiomDef 'Concrete.Scoped = tbl ^?! Scoped.infoAxioms . at ax . _Just
2023-05-30 19:19:39 +03:00
return (def ^. Concrete.axiomDoc)
getDocConstructor :: Scoped.NameId -> Repl (Maybe (Concrete.Judoc 'Concrete.Scoped))
getDocConstructor c = do
tbl :: Scoped.InfoTable <- getScopedInfoTable
let def = tbl ^?! Scoped.infoConstructors . at c . _Just
return (def ^. Concrete.constructorDoc)
2023-05-30 19:19:39 +03:00
printDefinition :: String -> Repl ()
printDefinition = replParseIdentifiers >=> printIdentifiers
where
printIdentifiers :: NonEmpty Concrete.ScopedIden -> Repl ()
printIdentifiers (d :| ds) = do
printIdentifier d
whenJust (nonEmpty ds) $ \ds' -> replNewline >> printIdentifiers ds'
where
printIdentifier :: Concrete.ScopedIden -> Repl ()
2023-07-26 10:59:50 +03:00
printIdentifier s =
let n = s ^. Concrete.scopedIdenFinal . Scoped.nameId
2023-07-26 10:59:50 +03:00
in case getNameKind s of
KNameAxiom -> printAxiom n
KNameInductive -> printInductive n
KNameLocal -> return ()
KNameFunction -> printFunction n
KNameConstructor -> printConstructor n
KNameLocalModule -> impossible
KNameTopModule -> impossible
KNameFixity -> impossible
KNameAlias -> impossible
where
upgrade to Ghc 9.8.1 (#2624) This PR updates the stackage LTS resolver to `nightly-2024-02-06` which uses GHC 9.8.1 ## Upgrade notes You will need to update your HLS to [2.6.0.0](https://github.com/haskell/haskell-language-server/releases/tag/2.6.0.0), this release contains support for GHC 9.8.1 ## Fixes ### `haskeline` / `repline` We have removed the custom haskeline / repline forks used in the build. This is because we had trouble overriding haskeline as it is bundled with GHC and the stackage resolver uses this bundled version. We were using a custom fork of haskeline to implement [mapInputT_](https://github.com/anoma/juvix/blob/15c0685c911a7ce3d2ee8c1597a1e6dd96e707bf/app/Commands/Repl.hs#L409) in the Juvix REPL, required to implement error handling. This requires private API from the Haskeline library. Instead of using a custom fork we use TemplateHaskell to obtain access to the private API we need. See [DarkArts.hs](https://github.com/anoma/juvix/blob/15c0685c911a7ce3d2ee8c1597a1e6dd96e707bf/src/Juvix/Prelude/DarkArts.hs) and [HaskelineJB.hs](https://github.com/anoma/juvix/blob/15c0685c911a7ce3d2ee8c1597a1e6dd96e707bf/app/HaskelineJH.hs). To obtain access to the private API, we adapted a method from [a Tweag blogpost](https://www.tweag.io/blog/2021-01-07-haskell-dark-arts-part-i/) and [repo](https://github.com/tweag/th-jailbreak) - updating it for GHC 9.8.1. ### `aeson-better-errors` The `aeson-better-errors` library has not been updated to work with `mtl-2.3.0` so it cannot work with the new stackage resolver. We are using a [fork](https://github.com/Vekhir/aeson-better-errors.git) which has been updated. We should consider replacing this library in future, see https://github.com/anoma/juvix/issues/2621 ### `path` The `path` library now includes API `splitDrive` and `dropDrive` so we can remove our versions of those functions from the prelude. ### `with-utf8` We no longer need to depend on `with-utf8`. We were using this package for UTF-8 versions of `readFile` and `writeFile` APIs. These APIs are now available in the `text` package. ### Compiler warnings GHC 9.8.1 introduces several new compiler warnings. * We have suppressed `missing-role-annotations` and `missing-poly-kind-signatures` * We added our own versions of `head` and `tail` to work around the new `partial-tx` warning introduced for those functions in `Data.List`. * We fixed up the code to avoid the [term-variable-capture](https://ghc.gitlab.haskell.org/ghc/doc/users_guide/using-warnings.html#ghc-flag--Wterm-variable-capture) warning.
2024-02-07 12:47:48 +03:00
printLocation :: (HasLoc c) => c -> Repl ()
printLocation def = do
s' <- ppConcrete s
let txt :: Text = " is " <> prettyText (nameKindWithArticle (getNameKind s)) <> " defined at " <> prettyText (getLoc def)
renderOutLn (s' <> mkAnsiText txt)
printFunction :: Scoped.NameId -> Repl ()
printFunction fun = do
tbl :: Scoped.InfoTable <- getScopedInfoTable
case tbl ^. Scoped.infoFunctions . at fun of
Just def -> do
printLocation def
printConcreteLn def
Nothing -> return ()
printInductive :: Scoped.NameId -> Repl ()
printInductive ind = do
tbl :: Scoped.InfoTable <- getScopedInfoTable
let def :: Concrete.InductiveDef 'Concrete.Scoped = tbl ^?! Scoped.infoInductives . at ind . _Just
printLocation def
2023-05-30 19:19:39 +03:00
printConcreteLn def
printAxiom :: Scoped.NameId -> Repl ()
printAxiom ax = do
tbl :: Scoped.InfoTable <- getScopedInfoTable
let def :: Concrete.AxiomDef 'Concrete.Scoped = tbl ^?! Scoped.infoAxioms . at ax . _Just
printLocation def
2023-05-30 19:19:39 +03:00
printConcreteLn def
printConstructor :: Scoped.NameId -> Repl ()
printConstructor c = do
tbl :: Scoped.InfoTable <- getScopedInfoTable
let ind = tbl ^?! Scoped.infoConstructors . at c . _Just . Concrete.constructorInductiveName
printInductive (ind ^. Scoped.nameId)
inferType :: String -> Repl ()
inferType input_ = do
gopts <- State.gets (^. replStateGlobalOptions)
n <- replExpressionUpToTyped (strip (pack input_))
renderOutLn (Internal.ppOut (project' @GenericOptions gopts) (n ^. Internal.typedType))
replCommands :: ReplOptions -> [(String, String -> Repl ())]
replCommands opts = catchable ++ nonCatchable
where
nonCatchable :: [(String, String -> Repl ())]
nonCatchable =
[ ("quit", quit)
]
catchable :: [(String, String -> Repl ())]
catchable =
map
(second (catchAll .))
[ ("help", const (printHelpTxt opts)),
-- `multiline` is included here for auto-completion purposes only.
-- `repline`'s `multilineCommand` logic overrides this no-op.
(multilineCmd, const (return ())),
("load", loadFile . pSomeFile),
("reload", reloadFile),
("root", printRoot),
("def", printDefinition),
2023-05-30 19:19:39 +03:00
("doc", printDocumentation),
("type", inferType),
("version", displayVersion),
("core", core),
("dev", dev)
]
upgrade to Ghc 9.8.1 (#2624) This PR updates the stackage LTS resolver to `nightly-2024-02-06` which uses GHC 9.8.1 ## Upgrade notes You will need to update your HLS to [2.6.0.0](https://github.com/haskell/haskell-language-server/releases/tag/2.6.0.0), this release contains support for GHC 9.8.1 ## Fixes ### `haskeline` / `repline` We have removed the custom haskeline / repline forks used in the build. This is because we had trouble overriding haskeline as it is bundled with GHC and the stackage resolver uses this bundled version. We were using a custom fork of haskeline to implement [mapInputT_](https://github.com/anoma/juvix/blob/15c0685c911a7ce3d2ee8c1597a1e6dd96e707bf/app/Commands/Repl.hs#L409) in the Juvix REPL, required to implement error handling. This requires private API from the Haskeline library. Instead of using a custom fork we use TemplateHaskell to obtain access to the private API we need. See [DarkArts.hs](https://github.com/anoma/juvix/blob/15c0685c911a7ce3d2ee8c1597a1e6dd96e707bf/src/Juvix/Prelude/DarkArts.hs) and [HaskelineJB.hs](https://github.com/anoma/juvix/blob/15c0685c911a7ce3d2ee8c1597a1e6dd96e707bf/app/HaskelineJH.hs). To obtain access to the private API, we adapted a method from [a Tweag blogpost](https://www.tweag.io/blog/2021-01-07-haskell-dark-arts-part-i/) and [repo](https://github.com/tweag/th-jailbreak) - updating it for GHC 9.8.1. ### `aeson-better-errors` The `aeson-better-errors` library has not been updated to work with `mtl-2.3.0` so it cannot work with the new stackage resolver. We are using a [fork](https://github.com/Vekhir/aeson-better-errors.git) which has been updated. We should consider replacing this library in future, see https://github.com/anoma/juvix/issues/2621 ### `path` The `path` library now includes API `splitDrive` and `dropDrive` so we can remove our versions of those functions from the prelude. ### `with-utf8` We no longer need to depend on `with-utf8`. We were using this package for UTF-8 versions of `readFile` and `writeFile` APIs. These APIs are now available in the `text` package. ### Compiler warnings GHC 9.8.1 introduces several new compiler warnings. * We have suppressed `missing-role-annotations` and `missing-poly-kind-signatures` * We added our own versions of `head` and `tail` to work around the new `partial-tx` warning introduced for those functions in `Data.List`. * We fixed up the code to avoid the [term-variable-capture](https://ghc.gitlab.haskell.org/ghc/doc/users_guide/using-warnings.html#ghc-flag--Wterm-variable-capture) warning.
2024-02-07 12:47:48 +03:00
mapInputT_ :: (m () -> m ()) -> InputT m () -> InputT m ()
mapInputT_ f =
mkInputT
. mapReaderT
( mapReaderT
( mapReaderT
(mapReaderT (mapReaderT f))
)
)
. unInputT
catchAll :: Repl () -> Repl ()
catchAll = Repline.dontCrash . catchJuvixError
where
catchJuvixError :: Repl () -> Repl ()
upgrade to Ghc 9.8.1 (#2624) This PR updates the stackage LTS resolver to `nightly-2024-02-06` which uses GHC 9.8.1 ## Upgrade notes You will need to update your HLS to [2.6.0.0](https://github.com/haskell/haskell-language-server/releases/tag/2.6.0.0), this release contains support for GHC 9.8.1 ## Fixes ### `haskeline` / `repline` We have removed the custom haskeline / repline forks used in the build. This is because we had trouble overriding haskeline as it is bundled with GHC and the stackage resolver uses this bundled version. We were using a custom fork of haskeline to implement [mapInputT_](https://github.com/anoma/juvix/blob/15c0685c911a7ce3d2ee8c1597a1e6dd96e707bf/app/Commands/Repl.hs#L409) in the Juvix REPL, required to implement error handling. This requires private API from the Haskeline library. Instead of using a custom fork we use TemplateHaskell to obtain access to the private API we need. See [DarkArts.hs](https://github.com/anoma/juvix/blob/15c0685c911a7ce3d2ee8c1597a1e6dd96e707bf/src/Juvix/Prelude/DarkArts.hs) and [HaskelineJB.hs](https://github.com/anoma/juvix/blob/15c0685c911a7ce3d2ee8c1597a1e6dd96e707bf/app/HaskelineJH.hs). To obtain access to the private API, we adapted a method from [a Tweag blogpost](https://www.tweag.io/blog/2021-01-07-haskell-dark-arts-part-i/) and [repo](https://github.com/tweag/th-jailbreak) - updating it for GHC 9.8.1. ### `aeson-better-errors` The `aeson-better-errors` library has not been updated to work with `mtl-2.3.0` so it cannot work with the new stackage resolver. We are using a [fork](https://github.com/Vekhir/aeson-better-errors.git) which has been updated. We should consider replacing this library in future, see https://github.com/anoma/juvix/issues/2621 ### `path` The `path` library now includes API `splitDrive` and `dropDrive` so we can remove our versions of those functions from the prelude. ### `with-utf8` We no longer need to depend on `with-utf8`. We were using this package for UTF-8 versions of `readFile` and `writeFile` APIs. These APIs are now available in the `text` package. ### Compiler warnings GHC 9.8.1 introduces several new compiler warnings. * We have suppressed `missing-role-annotations` and `missing-poly-kind-signatures` * We added our own versions of `head` and `tail` to work around the new `partial-tx` warning introduced for those functions in `Data.List`. * We fixed up the code to avoid the [term-variable-capture](https://ghc.gitlab.haskell.org/ghc/doc/users_guide/using-warnings.html#ghc-flag--Wterm-variable-capture) warning.
2024-02-07 12:47:48 +03:00
catchJuvixError = mkHaskelineT . mapInputT_ catchErrorS . unHaskelineT
where
upgrade to Ghc 9.8.1 (#2624) This PR updates the stackage LTS resolver to `nightly-2024-02-06` which uses GHC 9.8.1 ## Upgrade notes You will need to update your HLS to [2.6.0.0](https://github.com/haskell/haskell-language-server/releases/tag/2.6.0.0), this release contains support for GHC 9.8.1 ## Fixes ### `haskeline` / `repline` We have removed the custom haskeline / repline forks used in the build. This is because we had trouble overriding haskeline as it is bundled with GHC and the stackage resolver uses this bundled version. We were using a custom fork of haskeline to implement [mapInputT_](https://github.com/anoma/juvix/blob/15c0685c911a7ce3d2ee8c1597a1e6dd96e707bf/app/Commands/Repl.hs#L409) in the Juvix REPL, required to implement error handling. This requires private API from the Haskeline library. Instead of using a custom fork we use TemplateHaskell to obtain access to the private API we need. See [DarkArts.hs](https://github.com/anoma/juvix/blob/15c0685c911a7ce3d2ee8c1597a1e6dd96e707bf/src/Juvix/Prelude/DarkArts.hs) and [HaskelineJB.hs](https://github.com/anoma/juvix/blob/15c0685c911a7ce3d2ee8c1597a1e6dd96e707bf/app/HaskelineJH.hs). To obtain access to the private API, we adapted a method from [a Tweag blogpost](https://www.tweag.io/blog/2021-01-07-haskell-dark-arts-part-i/) and [repo](https://github.com/tweag/th-jailbreak) - updating it for GHC 9.8.1. ### `aeson-better-errors` The `aeson-better-errors` library has not been updated to work with `mtl-2.3.0` so it cannot work with the new stackage resolver. We are using a [fork](https://github.com/Vekhir/aeson-better-errors.git) which has been updated. We should consider replacing this library in future, see https://github.com/anoma/juvix/issues/2621 ### `path` The `path` library now includes API `splitDrive` and `dropDrive` so we can remove our versions of those functions from the prelude. ### `with-utf8` We no longer need to depend on `with-utf8`. We were using this package for UTF-8 versions of `readFile` and `writeFile` APIs. These APIs are now available in the `text` package. ### Compiler warnings GHC 9.8.1 introduces several new compiler warnings. * We have suppressed `missing-role-annotations` and `missing-poly-kind-signatures` * We added our own versions of `head` and `tail` to work around the new `partial-tx` warning introduced for those functions in `Data.List`. * We fixed up the code to avoid the [term-variable-capture](https://ghc.gitlab.haskell.org/ghc/doc/users_guide/using-warnings.html#ghc-flag--Wterm-variable-capture) warning.
2024-02-07 12:47:48 +03:00
printErrorS :: JuvixError -> ReplS ()
printErrorS e = do
opts <- State.gets (^. replStateGlobalOptions)
hasAnsi <- liftIO (Ansi.hSupportsANSIColor stderr)
liftIO
. hPutStrLn stderr
. run
. runReader (project' @GenericOptions opts)
$ Error.render (renderType opts hasAnsi) Nothing e
where
renderType :: GlobalOptions -> Bool -> Error.RenderType
renderType opts hasAnsi
| opts ^. globalVSCode = Error.RenderVSCode
| opts ^. globalNoColors || not hasAnsi = Error.RenderText
| otherwise = Error.RenderAnsi
upgrade to Ghc 9.8.1 (#2624) This PR updates the stackage LTS resolver to `nightly-2024-02-06` which uses GHC 9.8.1 ## Upgrade notes You will need to update your HLS to [2.6.0.0](https://github.com/haskell/haskell-language-server/releases/tag/2.6.0.0), this release contains support for GHC 9.8.1 ## Fixes ### `haskeline` / `repline` We have removed the custom haskeline / repline forks used in the build. This is because we had trouble overriding haskeline as it is bundled with GHC and the stackage resolver uses this bundled version. We were using a custom fork of haskeline to implement [mapInputT_](https://github.com/anoma/juvix/blob/15c0685c911a7ce3d2ee8c1597a1e6dd96e707bf/app/Commands/Repl.hs#L409) in the Juvix REPL, required to implement error handling. This requires private API from the Haskeline library. Instead of using a custom fork we use TemplateHaskell to obtain access to the private API we need. See [DarkArts.hs](https://github.com/anoma/juvix/blob/15c0685c911a7ce3d2ee8c1597a1e6dd96e707bf/src/Juvix/Prelude/DarkArts.hs) and [HaskelineJB.hs](https://github.com/anoma/juvix/blob/15c0685c911a7ce3d2ee8c1597a1e6dd96e707bf/app/HaskelineJH.hs). To obtain access to the private API, we adapted a method from [a Tweag blogpost](https://www.tweag.io/blog/2021-01-07-haskell-dark-arts-part-i/) and [repo](https://github.com/tweag/th-jailbreak) - updating it for GHC 9.8.1. ### `aeson-better-errors` The `aeson-better-errors` library has not been updated to work with `mtl-2.3.0` so it cannot work with the new stackage resolver. We are using a [fork](https://github.com/Vekhir/aeson-better-errors.git) which has been updated. We should consider replacing this library in future, see https://github.com/anoma/juvix/issues/2621 ### `path` The `path` library now includes API `splitDrive` and `dropDrive` so we can remove our versions of those functions from the prelude. ### `with-utf8` We no longer need to depend on `with-utf8`. We were using this package for UTF-8 versions of `readFile` and `writeFile` APIs. These APIs are now available in the `text` package. ### Compiler warnings GHC 9.8.1 introduces several new compiler warnings. * We have suppressed `missing-role-annotations` and `missing-poly-kind-signatures` * We added our own versions of `head` and `tail` to work around the new `partial-tx` warning introduced for those functions in `Data.List`. * We fixed up the code to avoid the [term-variable-capture](https://ghc.gitlab.haskell.org/ghc/doc/users_guide/using-warnings.html#ghc-flag--Wterm-variable-capture) warning.
2024-02-07 12:47:48 +03:00
catchErrorS :: ReplS () -> ReplS ()
catchErrorS = (`Except.catchError` printErrorS)
defaultMatcher :: [(String, CompletionFunc ReplS)]
defaultMatcher = [(":load", fileCompleter)]
optsCompleter :: WordCompleter ReplS
optsCompleter n = do
opts <- Reader.asks (^. replOptions)
let names = (":" <>) . fst <$> replCommands opts
return (filter (isPrefixOf n) names)
replBanner :: MultiLine -> Repl String
replBanner = \case
MultiLine -> return "... "
SingleLine -> do
mmodulePath <-
State.gets
( ^?
replStateContext
. _Just
. replContextArtifacts
. artifactMainModuleScope
. _Just
. scopePath
. absTopModulePath
)
return $ case mmodulePath of
Just path -> [i|#{unpack (P.prettyText path)}> |]
Nothing -> "juvix> "
replPrefix :: Maybe Char
replPrefix = Just ':'
replMultilineCommand :: Maybe String
replMultilineCommand = Just multilineCmd
replInitialiser :: Repl ()
replInitialiser = do
gopts <- State.gets (^. replStateGlobalOptions)
opts <- Reader.asks (^. replOptions)
welcomeMsg
unless
(opts ^. replNoPrelude || gopts ^. globalNoStdlib)
(maybe loadDefaultPrelude (loadFile . (^. pathPath)) (opts ^. replInputFile))
replFinaliser :: Repl ExitDecision
replFinaliser = return Exit
replTabComplete :: CompleterStyle ReplS
replTabComplete = Prefix (wordCompleter optsCompleter) defaultMatcher
printRoot :: String -> Repl ()
printRoot _ = do
r <- State.gets (^. replStateRoot . rootRootDir)
putStrLn (pack (toFilePath r))
runCommand :: (Members '[EmbedIO, App, TaggedLock] r) => ReplOptions -> Sem r ()
runCommand opts = do
root <- askRoot
pkg <- askPackage
let replAction :: ReplS ()
2022-12-20 15:05:40 +03:00
replAction = do
evalReplOpts
ReplOpts
{ prefix = replPrefix,
multilineCommand = replMultilineCommand,
initialiser = replInitialiser,
finaliser = replFinaliser,
tabComplete = replTabComplete,
command = replCommand opts,
options = replCommands opts,
banner = replBanner
2022-12-20 15:05:40 +03:00
}
globalOptions <- askGlobalOptions
let env =
ReplEnv
{ _replRoot = root,
_replOptions = opts,
_replPackage = pkg
}
iniState =
ReplState
{ _replStateRoot = root,
_replStateContext = Nothing,
_replStateGlobalOptions = globalOptions
}
e <-
Replace `polysemy` by `effectful` (#2663) The following benchmark compares juvix 0.6.0 with polysemy and a new version (implemented in this pr) which replaces polysemy by effectful. # Typecheck standard library without caching ``` hyperfine --warmup 2 --prepare 'juvix-polysemy clean' 'juvix-polysemy typecheck Stdlib/Prelude.juvix' 'juvix-effectful typecheck Stdlib/Prelude.juvix' Benchmark 1: juvix-polysemy typecheck Stdlib/Prelude.juvix Time (mean ± σ): 3.924 s ± 0.143 s [User: 3.787 s, System: 0.084 s] Range (min … max): 3.649 s … 4.142 s 10 runs Benchmark 2: juvix-effectful typecheck Stdlib/Prelude.juvix Time (mean ± σ): 2.558 s ± 0.074 s [User: 2.430 s, System: 0.084 s] Range (min … max): 2.403 s … 2.646 s 10 runs Summary juvix-effectful typecheck Stdlib/Prelude.juvix ran 1.53 ± 0.07 times faster than juvix-polysemy typecheck Stdlib/Prelude.juvix ``` # Typecheck standard library with caching ``` hyperfine --warmup 1 'juvix-effectful typecheck Stdlib/Prelude.juvix' 'juvix-polysemy typecheck Stdlib/Prelude.juvix' --min-runs 20 Benchmark 1: juvix-effectful typecheck Stdlib/Prelude.juvix Time (mean ± σ): 1.194 s ± 0.068 s [User: 0.979 s, System: 0.211 s] Range (min … max): 1.113 s … 1.307 s 20 runs Benchmark 2: juvix-polysemy typecheck Stdlib/Prelude.juvix Time (mean ± σ): 1.237 s ± 0.083 s [User: 0.997 s, System: 0.231 s] Range (min … max): 1.061 s … 1.476 s 20 runs Summary juvix-effectful typecheck Stdlib/Prelude.juvix ran 1.04 ± 0.09 times faster than juvix-polysemy typecheck Stdlib/Prelude.juvix ```
2024-03-21 15:09:34 +03:00
liftIO
. Except.runExceptT
. (`State.evalStateT` iniState)
. (`Reader.runReaderT` env)
$ replAction
case e of
Left {} -> error "impossible: uncaught exception"
Right () -> return ()
-- | If the package contains the stdlib as a dependency, loads the Prelude
defaultPreludeEntryPoint :: Repl (Maybe EntryPoint)
defaultPreludeEntryPoint = do
root <- State.gets (^. replStateRoot)
let buildRoot = root ^. rootRootDir
buildDir = resolveAbsBuildDir buildRoot (root ^. rootBuildDir)
pkg <- Reader.asks (^. replPackage)
Replace `polysemy` by `effectful` (#2663) The following benchmark compares juvix 0.6.0 with polysemy and a new version (implemented in this pr) which replaces polysemy by effectful. # Typecheck standard library without caching ``` hyperfine --warmup 2 --prepare 'juvix-polysemy clean' 'juvix-polysemy typecheck Stdlib/Prelude.juvix' 'juvix-effectful typecheck Stdlib/Prelude.juvix' Benchmark 1: juvix-polysemy typecheck Stdlib/Prelude.juvix Time (mean ± σ): 3.924 s ± 0.143 s [User: 3.787 s, System: 0.084 s] Range (min … max): 3.649 s … 4.142 s 10 runs Benchmark 2: juvix-effectful typecheck Stdlib/Prelude.juvix Time (mean ± σ): 2.558 s ± 0.074 s [User: 2.430 s, System: 0.084 s] Range (min … max): 2.403 s … 2.646 s 10 runs Summary juvix-effectful typecheck Stdlib/Prelude.juvix ran 1.53 ± 0.07 times faster than juvix-polysemy typecheck Stdlib/Prelude.juvix ``` # Typecheck standard library with caching ``` hyperfine --warmup 1 'juvix-effectful typecheck Stdlib/Prelude.juvix' 'juvix-polysemy typecheck Stdlib/Prelude.juvix' --min-runs 20 Benchmark 1: juvix-effectful typecheck Stdlib/Prelude.juvix Time (mean ± σ): 1.194 s ± 0.068 s [User: 0.979 s, System: 0.211 s] Range (min … max): 1.113 s … 1.307 s 20 runs Benchmark 2: juvix-polysemy typecheck Stdlib/Prelude.juvix Time (mean ± σ): 1.237 s ± 0.083 s [User: 0.997 s, System: 0.231 s] Range (min … max): 1.061 s … 1.476 s 20 runs Summary juvix-effectful typecheck Stdlib/Prelude.juvix ran 1.04 ± 0.09 times faster than juvix-polysemy typecheck Stdlib/Prelude.juvix ```
2024-03-21 15:09:34 +03:00
mstdlibPath <- runM (runFilesIO (packageStdlib buildRoot buildDir (pkg ^. packageDependencies)))
case mstdlibPath of
Just stdlibPath ->
Just
. set entryPointResolverRoot stdlibPath
<$> getReplEntryPointFromPath (stdlibPath <//> P.preludePath)
Nothing -> return Nothing
2022-12-20 15:05:40 +03:00
replMakeAbsolute :: SomeBase b -> Repl (Path Abs b)
replMakeAbsolute = \case
Abs p -> return p
Rel r -> do
invokeDir <- State.gets (^. replStateRoot . rootInvokeDir)
2022-12-20 15:05:40 +03:00
return (invokeDir <//> r)
replExpressionUpToScopedAtoms :: Text -> Repl (Concrete.ExpressionAtoms 'Concrete.Scoped)
replExpressionUpToScopedAtoms txt = do
ctx <- replGetContext
x <-
Replace `polysemy` by `effectful` (#2663) The following benchmark compares juvix 0.6.0 with polysemy and a new version (implemented in this pr) which replaces polysemy by effectful. # Typecheck standard library without caching ``` hyperfine --warmup 2 --prepare 'juvix-polysemy clean' 'juvix-polysemy typecheck Stdlib/Prelude.juvix' 'juvix-effectful typecheck Stdlib/Prelude.juvix' Benchmark 1: juvix-polysemy typecheck Stdlib/Prelude.juvix Time (mean ± σ): 3.924 s ± 0.143 s [User: 3.787 s, System: 0.084 s] Range (min … max): 3.649 s … 4.142 s 10 runs Benchmark 2: juvix-effectful typecheck Stdlib/Prelude.juvix Time (mean ± σ): 2.558 s ± 0.074 s [User: 2.430 s, System: 0.084 s] Range (min … max): 2.403 s … 2.646 s 10 runs Summary juvix-effectful typecheck Stdlib/Prelude.juvix ran 1.53 ± 0.07 times faster than juvix-polysemy typecheck Stdlib/Prelude.juvix ``` # Typecheck standard library with caching ``` hyperfine --warmup 1 'juvix-effectful typecheck Stdlib/Prelude.juvix' 'juvix-polysemy typecheck Stdlib/Prelude.juvix' --min-runs 20 Benchmark 1: juvix-effectful typecheck Stdlib/Prelude.juvix Time (mean ± σ): 1.194 s ± 0.068 s [User: 0.979 s, System: 0.211 s] Range (min … max): 1.113 s … 1.307 s 20 runs Benchmark 2: juvix-polysemy typecheck Stdlib/Prelude.juvix Time (mean ± σ): 1.237 s ± 0.083 s [User: 0.997 s, System: 0.231 s] Range (min … max): 1.061 s … 1.476 s 20 runs Summary juvix-effectful typecheck Stdlib/Prelude.juvix ran 1.04 ± 0.09 times faster than juvix-polysemy typecheck Stdlib/Prelude.juvix ```
2024-03-21 15:09:34 +03:00
runM
. runError
. evalState (ctx ^. replContextArtifacts)
. runReader (ctx ^. replContextEntryPoint)
$ expressionUpToAtomsScoped P.replPath txt
replFromEither x
replExpressionUpToTyped :: Text -> Repl Internal.TypedExpression
replExpressionUpToTyped txt = do
ctx <- replGetContext
x <-
Replace `polysemy` by `effectful` (#2663) The following benchmark compares juvix 0.6.0 with polysemy and a new version (implemented in this pr) which replaces polysemy by effectful. # Typecheck standard library without caching ``` hyperfine --warmup 2 --prepare 'juvix-polysemy clean' 'juvix-polysemy typecheck Stdlib/Prelude.juvix' 'juvix-effectful typecheck Stdlib/Prelude.juvix' Benchmark 1: juvix-polysemy typecheck Stdlib/Prelude.juvix Time (mean ± σ): 3.924 s ± 0.143 s [User: 3.787 s, System: 0.084 s] Range (min … max): 3.649 s … 4.142 s 10 runs Benchmark 2: juvix-effectful typecheck Stdlib/Prelude.juvix Time (mean ± σ): 2.558 s ± 0.074 s [User: 2.430 s, System: 0.084 s] Range (min … max): 2.403 s … 2.646 s 10 runs Summary juvix-effectful typecheck Stdlib/Prelude.juvix ran 1.53 ± 0.07 times faster than juvix-polysemy typecheck Stdlib/Prelude.juvix ``` # Typecheck standard library with caching ``` hyperfine --warmup 1 'juvix-effectful typecheck Stdlib/Prelude.juvix' 'juvix-polysemy typecheck Stdlib/Prelude.juvix' --min-runs 20 Benchmark 1: juvix-effectful typecheck Stdlib/Prelude.juvix Time (mean ± σ): 1.194 s ± 0.068 s [User: 0.979 s, System: 0.211 s] Range (min … max): 1.113 s … 1.307 s 20 runs Benchmark 2: juvix-polysemy typecheck Stdlib/Prelude.juvix Time (mean ± σ): 1.237 s ± 0.083 s [User: 0.997 s, System: 0.231 s] Range (min … max): 1.061 s … 1.476 s 20 runs Summary juvix-effectful typecheck Stdlib/Prelude.juvix ran 1.04 ± 0.09 times faster than juvix-polysemy typecheck Stdlib/Prelude.juvix ```
2024-03-21 15:09:34 +03:00
runM
. runError
. evalState (ctx ^. replContextArtifacts)
. runReader (ctx ^. replContextEntryPoint)
$ expressionUpToTyped P.replPath txt
replFromEither x
compileReplInputIO' :: (MonadIO m) => ReplContext -> Text -> m (Artifacts, (Either JuvixError (Maybe Core.Node)))
compileReplInputIO' ctx txt =
runM
. runState (ctx ^. replContextArtifacts)
. runReader (ctx ^. replContextEntryPoint)
$ do
r <- compileReplInputIO P.replPath txt
return (extractNode <$> r)
where
extractNode :: ReplPipelineResult -> Maybe Core.Node
extractNode = \case
ReplPipelineResultNode n -> Just n
ReplPipelineResultImport {} -> Nothing
ReplPipelineResultOpen {} -> Nothing
render' :: (P.HasAnsiBackend a, P.HasTextBackend a) => a -> Repl ()
render' t = do
opts <- State.gets (^. replStateGlobalOptions)
hasAnsi <- liftIO (Ansi.hSupportsANSIColor stdout)
liftIO (P.renderIO (not (opts ^. globalNoColors) && hasAnsi) t)
replNewline :: Repl ()
replNewline = liftIO (putStrLn "")
renderOut :: (P.HasAnsiBackend a, P.HasTextBackend a) => a -> Repl ()
renderOut = render'
renderOutLn :: (P.HasAnsiBackend a, P.HasTextBackend a) => a -> Repl ()
renderOutLn t = renderOut t >> replNewline