Esc
Start typing to search...

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 message
  • writeFile : String -> String -> Result Unit String — success or error message
  • getFileSize : String -> Result Int String — size in bytes or error message

Path functions that may not have an answer return Maybe:

  • parentDir : String -> Maybe String
  • extension : String -> Maybe String

Security

VariableEffect
KEEL_IO_DISABLED=0Enable file/directory IO
KEEL_IO_READ_ONLY=1Allow only read operations
KEEL_IO_SANDBOX=/pathRestrict to a directory

Functions

Console

IO.print

a -> ()

Print a value to stdout without a newline.

Example:
import IO

IO.print "Hello"

-- outputs: Hello
Try it

Notes: Flushes stdout after printing.

See also: IO.println, IO.eprint

IO.println

a -> ()

Print a value to stdout with a newline.

Example:
import IO

IO.println "Hello"

-- outputs: Hello\
Try it

See also: IO.print, IO.eprintln

IO.eprint

a -> ()

Print a value to stderr without a newline.

Example:
import IO

IO.eprint "Error"
Try it

Notes: Flushes stderr after printing.

See also: IO.eprintln, IO.print

IO.eprintln

a -> ()

Print a value to stderr with a newline.

Example:
import IO

IO.eprintln "Error occurred"
Try it

See 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.

Example:
import IO

IO.readLine ()
Try it

Notes: 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.

Example:
import IO

IO.readFile "config.txt"
Try it

See also: IO.writeFile, IO.appendFile

IO.writeFile

String -> String -> Result () String

Write a string to a file, creating or truncating it.

Example:
import IO

IO.writeFile "output.txt" "Hello, World!"
Try it

Notes: 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.

Example:
import IO

IO.appendFile "log.txt" "New entry\n"
Try it

See also: IO.writeFile, IO.readFile

IO.fileExists

String -> Bool

Check if a file or directory exists at the given path.

Example:
import IO

IO.fileExists "config.txt"

-- True or False
Try it

See also: IO.isFile, IO.isDir

IO.deleteFile

String -> Result () String

Delete a file at the given path.

Example:
import IO

IO.deleteFile "temp.txt"
Try it

Notes: Use removeDir for directories.

See also: IO.removeDir, IO.removeDirAll

IO.copyFile

String -> String -> Result () String

Copy a file from source to destination.

Example:
import IO

IO.copyFile "src.txt" "dst.txt"
Try it

See also: IO.renameFile

IO.renameFile

String -> String -> Result () String

Rename or move a file from source to destination.

Example:
import IO

IO.renameFile "old.txt" "new.txt"
Try it

See also: IO.copyFile

File info

IO.isDir

String -> Bool

Check if the path is a directory.

Example:
import IO

IO.isDir "/home/user"

-- True
Try it

See also: IO.isFile, IO.fileExists

IO.isFile

String -> Bool

Check if the path is a file.

Example:
import IO

IO.isFile "config.txt"

-- True
Try it

See also: IO.isDir, IO.fileExists

IO.getFileSize

String -> Result Int String

Get the size of a file in bytes.

Example:
import IO

IO.getFileSize "data.bin"

-- Ok 1024
Try it

See also: IO.isFile

Directories

IO.listDir

String -> Result [String] String

List the contents of a directory.

Example:
import IO

IO.listDir "."

-- Ok ["file1.txt", "subdir", ...]
Try it

Notes: Returns file names only, not full paths.

See also: IO.isDir

IO.getCurrentDir

() -> Result String String

Get the current working directory.

Example:
import IO

IO.getCurrentDir ()

-- Ok "/home/user/project"
Try it

Notes: Takes Unit argument to trigger execution.

See also: IO.setCurrentDir

IO.setCurrentDir

String -> Result () String

Set the current working directory.

Example:
import IO

IO.setCurrentDir "/home/user"
Try it

See also: IO.getCurrentDir

IO.createDir

String -> Result () String

Create a single directory.

Example:
import IO

IO.createDir "newdir"
Try it

Notes: 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.

Example:
import IO

IO.createDirAll "a/b/c"
Try it

Notes: No error if directory already exists.

See also: IO.createDir, IO.removeDirAll

IO.removeDir

String -> Result () String

Remove an empty directory.

Example:
import IO

IO.removeDir "emptydir"
Try it

Notes: 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.

Example:
import IO

IO.removeDirAll "project"
Try it

Notes: 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.

Example:
import IO

IO.joinPath "/home/user" "file.txt"

-- "/home/user/file.txt"
Try it

Notes: 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.

Example:
import IO

IO.parentDir "/home/user/file.txt"

-- Just "/home/user"
Try it

See also: IO.fileName, IO.joinPath

IO.fileName

String -> Maybe String

Get the file name from a path.

Example:
import IO

IO.fileName "/home/user/file.txt"

-- Just "file.txt"
Try it

See also: IO.parentDir, IO.extension

IO.extension

String -> Maybe String

Get the extension from a path.

Example:
import IO

IO.extension "file.txt"
-- Just "txt"
Try it

Notes: Does not include the leading dot.

See also: IO.fileName

IO.absolutePath

String -> Result String String

Convert a path to an absolute path.

Example:
import IO

IO.absolutePath "./file.txt"

-- Ok "/home/user/project/file.txt"
Try it

Notes: Resolves symlinks and normalizes the path.

See also: IO.joinPath

Environment

IO.getEnv

String -> Maybe String

Get the value of an environment variable.

Example:
import IO

IO.getEnv "PATH"

-- Just "/usr/bin:..."
Try it

See also: IO.setEnv, IO.getArgs

IO.setEnv

String -> String -> ()

Set an environment variable.

Example:
import IO

IO.setEnv "MY_VAR" "my_value"
Try it

Notes: Changes only affect the current process.

See also: IO.getEnv

IO.getArgs

() -> [String]

Get the command line arguments.

Example:
import IO

IO.getArgs ()

-- ["arg1", "arg2", ...]
Try it

Notes: 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.

Example:
import IO

IO.exit 0
(-- exits with success
IO.exit 1)

-- exits with error
Try it

Notes: This function never returns.