IO Module
Input/output operations for console, files, directories, and paths.
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 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 IOError
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"
Return types
File and directory operations return Result to handle errors:
readFile : String -> Result String IOError— content or error messagewriteFile : String -> String -> Result Unit String— success or error messagefileSize : String -> Result Int IOError— 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 IOError
Read a line from input. Returns Ok with the line (without newline), or Err on failure.
import IO
IO.readLine ()Try itNotes: Takes Unit argument to trigger execution.
The input source depends on the host. In the CLI/REPL it reads the process's stdin. Under a Jupyter kernel it reads from the notebook frontend (an input box appears). If the frontend declines input (allow_stdin: false) or input is otherwise unavailable, it returns Err IOError::Interrupted. A user interrupt (the stop button) while waiting aborts the cell — it is not delivered as a catchable Err.
See also: IO.println
Files
IO.readFile
String -> Result String IOError
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 () IOError
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 () IOError
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.removeFile
String -> Result () IOError
Delete a file at the given path.
import IO
IO.removeFile "temp.txt"Try itNotes: Use removeDir for directories.
See also: IO.removeDir, IO.removeDirAll
IO.copyFile
String -> String -> Result () IOError
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 () IOError
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.fileSize
String -> Result Int IOError
Get the size of a file in bytes.
import IO
IO.fileSize "data.bin"
-- Ok 1024Try itSee also: IO.isFile
Directories
IO.listDir
String -> Result [String] IOError
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.currentDir
() -> Result String IOError
Get the current working directory.
import IO
IO.currentDir ()
-- Ok "/home/user/project"Try itNotes: Takes Unit argument to trigger execution.
See also: IO.setCurrentDir
IO.setCurrentDir
String -> Result () IOError
Set the current working directory.
import IO
IO.setCurrentDir "/home/user"Try itSee also: IO.currentDir
IO.createDir
String -> Result () IOError
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 () IOError
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 () IOError
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.removeFile
IO.removeDirAll
String -> Result () IOError
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.removeFile
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 IOError
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