or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

ast.mdbuild.mdcli.mdcodegen.mdcorefn.mddocs.mderrors.mdide.mdindex.mdinteractive.mdparser.mdsugar.mdtypes.md
tile.json

interactive.mddocs/

Interactive REPL System

The Interactive module provides the foundation for PSCi (PureScript interactive), the PureScript REPL. It handles command processing, module loading, expression evaluation, and state management for interactive development sessions.

Capabilities

Command Handling

Core command processing infrastructure for REPL interactions including expression evaluation, type querying, and module management.

-- | Process a REPL command in the PSCi context
handleCommand :: Command -> StateT PSCiState IO ()

-- | REPL commands
data Command
  = Expression P.Expr                                     -- Evaluate expression
  | TypeOf P.Expr                                        -- Show type of expression
  | KindOf P.SourceType                                  -- Show kind of type
  | ShowInfo P.Expr                                      -- Show detailed info
  | Directive Directive                                  -- Execute directive
  | Import ImportedModule                                -- Import module
  | LoadFile FilePath                                    -- Load PureScript file
  | Reset                                                -- Reset REPL state
  | Help (Maybe String)                                  -- Show help
  | Quit                                                 -- Exit REPL

REPL State Management

State management for interactive sessions including imports, let bindings, and loaded modules.

-- | PSCi state containing imports, bindings, and loaded modules
data PSCiState

-- | Configuration for PSCi sessions
newtype PSCiConfig = PSCiConfig
  { psciFileGlobs :: [String]                           -- File patterns to load
  }

-- | Initial empty PSCi state
initialPSCiState :: PSCiState

-- | Update imported modules in state
updateImportedModules :: [ImportedModule] -> PSCiState -> PSCiState

-- | Update loaded extern files
updateLoadedExterns :: [P.ExternsFile] -> PSCiState -> PSCiState

-- | Update let bindings
updateLets :: [(P.Ident, P.Expr)] -> PSCiState -> PSCiState

-- | Set interactive print function
setInteractivePrint :: P.Expr -> PSCiState -> PSCiState

State Accessors

Functions for accessing and querying PSCi state information.

-- | Get current exports
psciExports :: PSCiState -> P.Exports

-- | Get current imports  
psciImports :: PSCiState -> P.Imports

-- | Get loaded extern files
psciLoadedExterns :: PSCiState -> [P.ExternsFile]

-- | Get interactive print function
psciInteractivePrint :: PSCiState -> P.Expr

-- | Get imported modules
psciImportedModules :: PSCiState -> [ImportedModule]

-- | Get let bindings
psciLetBindings :: PSCiState -> [(P.Ident, P.Expr)]

Imported Module Types

Types for representing imported modules and their qualification settings.

-- | Imported module with qualification info
data ImportedModule = ImportedModule
  { importedModuleName :: P.ModuleName                   -- Module name
  , importedModuleAlias :: Maybe P.ModuleName            -- Import alias
  , importedModuleType :: P.ImportDeclarationType        -- Import type (qualified/unqualified)
  }

-- | Create qualified import
qualifiedImport :: P.ModuleName -> ImportedModule

-- | Create unqualified import
unqualifiedImport :: P.ModuleName -> ImportedModule

-- | Create aliased import
aliasedImport :: P.ModuleName -> P.ModuleName -> ImportedModule

Directives

REPL directives for controlling session behavior and querying information.

-- | REPL directives
data Directive
  = Browse P.ModuleName                                  -- Browse module exports
  | Load [FilePath]                                      -- Load source files
  | Foreign P.ModuleName                                 -- Show foreign imports
  | Reload                                               -- Reload all files
  | Clear                                                -- Clear loaded modules
  | Complete String                                      -- Tab completion
  | Set ReplQuery String                                 -- Set REPL option
  | Show ReplQuery                                       -- Show REPL option

-- | REPL configuration queries
data ReplQuery
  = QueryLoaded                                          -- Show loaded modules
  | QueryImport                                          -- Show imports
  | QueryPrint                                           -- Show print function

Expression Evaluation

Core expression evaluation and compilation functionality for REPL interactions.

-- | Compile and evaluate an expression in PSCi
evalExpression :: P.Expr -> StateT PSCiState IO (Either P.MultipleErrors String)

-- | Get the type of an expression
getExpressionType :: P.Expr -> StateT PSCiState IO (Either P.MultipleErrors P.SourceType)

-- | Get the kind of a type
getTypeKind :: P.SourceType -> StateT PSCiState IO (Either P.MultipleErrors P.SourceKind)

-- | Rebuild modules with current state
rebuildModules :: StateT PSCiState IO (Either P.MultipleErrors ())

Environment Setup

Functions for setting up and configuring the PSCi environment.

-- | Create initial PSCi environment
psciEnvironment :: PSCiConfig -> IO (Either P.MultipleErrors PSCiState)

-- | Load initial modules and dependencies
loadInitialModules :: [FilePath] -> StateT PSCiState IO (Either P.MultipleErrors ())

-- | Setup default imports (Prelude etc.)
setupDefaultImports :: StateT PSCiState IO (Either P.MultipleErrors ())

Usage Examples

Basic REPL Session

import Language.PureScript.Interactive

-- Initialize REPL state
initializeRepl :: IO (Either P.MultipleErrors PSCiState)
initializeRepl = do
  let config = PSCiConfig ["src/**/*.purs"]
  psciEnvironment config

-- Evaluate an expression
evalInRepl :: String -> StateT PSCiState IO (Either P.MultipleErrors String)
evalInRepl exprString = do
  case P.parseExpression exprString of
    Left parseError -> return $ Left parseError
    Right expr -> evalExpression expr

-- Get type information
getTypeInRepl :: String -> StateT PSCiState IO (Either P.MultipleErrors P.SourceType)
getTypeInRepl exprString = do
  case P.parseExpression exprString of
    Left parseError -> return $ Left parseError
    Right expr -> getExpressionType expr

Managing Imports

-- Import a module qualified
importModuleQualified :: P.ModuleName -> StateT PSCiState IO ()
importModuleQualified modName = do
  let importedMod = qualifiedImport modName
  modify (updateImportedModules [importedMod])

-- Import module with alias
importModuleAs :: P.ModuleName -> P.ModuleName -> StateT PSCiState IO ()
importModuleAs modName alias = do
  let importedMod = aliasedImport modName alias
  modify (updateImportedModules [importedMod])

-- Add let binding to REPL state
addLetBinding :: P.Ident -> P.Expr -> StateT PSCiState IO ()
addLetBinding ident expr = do
  currentBindings <- gets psciLetBindings
  let newBindings = (ident, expr) : currentBindings
  modify (updateLets newBindings)

Command Processing

-- Process a command string
processCommand :: String -> StateT PSCiState IO ()
processCommand cmdString = 
  case parseCommand cmdString of
    Just cmd -> handleCommand cmd
    Nothing -> liftIO $ putStrLn "Invalid command"

-- Handle expression evaluation
handleExpressionCommand :: P.Expr -> StateT PSCiState IO ()
handleExpressionCommand expr = do
  result <- evalExpression expr
  case result of
    Left errors -> liftIO $ P.printErrors errors
    Right output -> liftIO $ putStrLn output

-- Handle type query
handleTypeCommand :: P.Expr -> StateT PSCiState IO ()
handleTypeCommand expr = do
  result <- getExpressionType expr
  case result of
    Left errors -> liftIO $ P.printErrors errors
    Right typ -> liftIO $ putStrLn $ P.prettyPrintType typ

Session Management

-- Load files into REPL session
loadFiles :: [FilePath] -> StateT PSCiState IO (Either P.MultipleErrors ())
loadFiles paths = do
  result <- loadInitialModules paths
  case result of
    Left errors -> return $ Left errors
    Right _ -> do
      rebuildModules

-- Reset session to initial state
resetSession :: StateT PSCiState IO ()
resetSession = do
  initial <- liftIO $ psciEnvironment (PSCiConfig [])
  case initial of
    Left _ -> return () -- Keep current state on error
    Right newState -> put newState

-- Clear all imports and bindings
clearSession :: StateT PSCiState IO ()
clearSession = do
  modify (updateImportedModules [])
  modify (updateLets [])
  setupDefaultImports