IO Module
Input/output operations for console, files, directories, and environment.
IO operations are side-effecting and interact with the outside world. File system access is disabled by default for security — enable it with KEEL_IO_DISABLED=0. Console and environment operations are always allowed.
Common patterns
import IO
-- Console output
IO.println "Hello, world!"
IO.eprintln "Warning: something happened"
-- File operations (return Result)
let content = IO.readFile "data.txt" -- Result String String
IO.writeFile "out.txt" "hello" -- Result Unit String
IO.appendFile "log.txt" "new line\n" -- Result Unit String
-- Path manipulation (pure, always available)
IO.joinPath "/home" "user" -- "/home/user"
IO.extension "photo.jpg" -- Just "jpg"
IO.fileName "/path/to/file.txt" -- "file.txt"
-- Environment
IO.getEnv "HOME" -- Maybe String
Return types
File and directory operations return Result to handle errors:
readFile : String -> Result String String— content or error messagewriteFile : String -> String -> Result Unit String— success or error messagegetFileSize : String -> Result Int String— size in bytes or error message
Path functions that may not have an answer return Maybe:
parentDir : String -> Maybe Stringextension : String -> Maybe String
Security
| Variable | Effect |
|---|---|
KEEL_IO_DISABLED=0 | Enable file/directory IO |
KEEL_IO_READ_ONLY=1 | Allow only read operations |
KEEL_IO_SANDBOX=/path | Restrict to a directory |
Functions
Console
IO.print
a -> ()
Print a value to stdout without a newline.
import IO
IO.print "Hello"
-- outputs: HelloTry itNotes: Flushes stdout after printing.
See also: IO.println, IO.eprint
IO.println
a -> ()
Print a value to stdout with a newline.
import IO
IO.println "Hello"
-- outputs: Hello\Try itSee also: IO.print, IO.eprintln
IO.eprint
a -> ()
Print a value to stderr without a newline.
import IO
IO.eprint "Error"Try itNotes: Flushes stderr after printing.
See also: IO.eprintln, IO.print
IO.eprintln
a -> ()
Print a value to stderr with a newline.
import IO
IO.eprintln "Error occurred"Try itSee also: IO.eprint, IO.println
IO.readLine
() -> Result String String
Read a line from stdin. Returns Ok with the line (without newline), or Err on failure.
import IO
IO.readLine ()Try itNotes: Takes Unit argument to trigger execution.
See also: IO.println
Files
IO.readFile
String -> Result String String
Read the entire contents of a file as a string.
import IO
IO.readFile "config.txt"Try itSee also: IO.writeFile, IO.appendFile
IO.writeFile
String -> String -> Result () String
Write a string to a file, creating or truncating it.
import IO
IO.writeFile "output.txt" "Hello, World!"Try itNotes: Overwrites existing file contents.
See also: IO.readFile, IO.appendFile
IO.appendFile
String -> String -> Result () String
Append a string to a file, creating it if it doesn't exist.
import IO
IO.appendFile "log.txt" "New entry\n"Try itSee also: IO.writeFile, IO.readFile
IO.fileExists
String -> Bool
Check if a file or directory exists at the given path.
import IO
IO.fileExists "config.txt"
-- True or FalseTry itIO.deleteFile
String -> Result () String
Delete a file at the given path.
import IO
IO.deleteFile "temp.txt"Try itNotes: Use removeDir for directories.
See also: IO.removeDir, IO.removeDirAll
IO.copyFile
String -> String -> Result () String
Copy a file from source to destination.
import IO
IO.copyFile "src.txt" "dst.txt"Try itSee also: IO.renameFile
IO.renameFile
String -> String -> Result () String
Rename or move a file from source to destination.
import IO
IO.renameFile "old.txt" "new.txt"Try itSee also: IO.copyFile
File info
IO.isDir
String -> Bool
Check if the path is a directory.
import IO
IO.isDir "/home/user"
-- TrueTry itSee also: IO.isFile, IO.fileExists
IO.isFile
String -> Bool
Check if the path is a file.
import IO
IO.isFile "config.txt"
-- TrueTry itSee also: IO.isDir, IO.fileExists
IO.getFileSize
String -> Result Int String
Get the size of a file in bytes.
import IO
IO.getFileSize "data.bin"
-- Ok 1024Try itSee also: IO.isFile
Directories
IO.listDir
String -> Result [String] String
List the contents of a directory.
import IO
IO.listDir "."
-- Ok ["file1.txt", "subdir", ...]Try itNotes: Returns file names only, not full paths.
See also: IO.isDir
IO.getCurrentDir
() -> Result String String
Get the current working directory.
import IO
IO.getCurrentDir ()
-- Ok "/home/user/project"Try itNotes: Takes Unit argument to trigger execution.
See also: IO.setCurrentDir
IO.setCurrentDir
String -> Result () String
Set the current working directory.
import IO
IO.setCurrentDir "/home/user"Try itSee also: IO.getCurrentDir
IO.createDir
String -> Result () String
Create a single directory.
import IO
IO.createDir "newdir"Try itNotes: Fails if parent doesn't exist. Use createDirAll for nested creation.
See also: IO.createDirAll, IO.removeDir
IO.createDirAll
String -> Result () String
Create a directory and all parent directories.
import IO
IO.createDirAll "a/b/c"Try itNotes: No error if directory already exists.
See also: IO.createDir, IO.removeDirAll
IO.removeDir
String -> Result () String
Remove an empty directory.
import IO
IO.removeDir "emptydir"Try itNotes: Fails if directory is not empty. Use removeDirAll to remove with contents.
See also: IO.removeDirAll, IO.deleteFile
IO.removeDirAll
String -> Result () String
Remove a directory and all its contents.
import IO
IO.removeDirAll "project"Try itNotes: Use with caution - this is recursive and permanent!
See also: IO.removeDir, IO.deleteFile
Paths (pure)
IO.joinPath
String -> String -> String
Join two path segments together.
import IO
IO.joinPath "/home/user" "file.txt"
-- "/home/user/file.txt"Try itNotes: Handles path separators correctly for the platform.
See also: IO.parentDir, IO.fileName
IO.parentDir
String -> Maybe String
Get the parent directory of a path.
import IO
IO.parentDir "/home/user/file.txt"
-- Just "/home/user"Try itSee also: IO.fileName, IO.joinPath
IO.fileName
String -> Maybe String
Get the file name from a path.
import IO
IO.fileName "/home/user/file.txt"
-- Just "file.txt"Try itSee also: IO.parentDir, IO.extension
IO.extension
String -> Maybe String
Get the extension from a path.
import IO
IO.extension "file.txt"
-- Just "txt"Try itNotes: Does not include the leading dot.
See also: IO.fileName
IO.absolutePath
String -> Result String String
Convert a path to an absolute path.
import IO
IO.absolutePath "./file.txt"
-- Ok "/home/user/project/file.txt"Try itNotes: Resolves symlinks and normalizes the path.
See also: IO.joinPath
Environment
IO.getEnv
String -> Maybe String
Get the value of an environment variable.
import IO
IO.getEnv "PATH"
-- Just "/usr/bin:..."Try itSee also: IO.setEnv, IO.getArgs
IO.setEnv
String -> String -> ()
Set an environment variable.
import IO
IO.setEnv "MY_VAR" "my_value"Try itNotes: Changes only affect the current process.
See also: IO.getEnv
IO.getArgs
() -> [String]
Get the command line arguments.
import IO
IO.getArgs ()
-- ["arg1", "arg2", ...]Try itNotes: Excludes the program name. Takes Unit to trigger execution.
See also: IO.getEnv
Process
IO.exit
Int -> ()
Exit the process with the given exit code.
import IO
IO.exit 0
(-- exits with success
IO.exit 1)
-- exits with errorTry itNotes: This function never returns.