8.Β IOπŸ”—

Lean is a pure functional programming language. While Lean code is strictly evaluated at run time, the order of evaluation that is used during type checking, especially while checking definitional equality, is formally unspecified and makes use of a number of heuristics that improve performance but are subject to change. This means that simply adding operations that perform side effects (such as file I/O, exceptions, or mutable references) would lead to programs in which the order of effects is unspecified. During type checking, even terms with free variables are reduced; this would make side effects even more difficult to predict. Finally, a basic principle of Lean's logic is that functions are functions that map each element of the domain to a unique element of the range. Including side effects such as console I/O, arbitrary mutable state, or random number generation would violate this principle.

Programs that may have side effects have a type (typically IO Ξ±) that distinguishes them from pure functions. Logically speaking, IO describes the sequencing and data dependencies of side effects. Many of the basic side effects, such as reading from files, are opaque constants from the perspective of Lean's logic. Others are specified by code that is logically equivalent to the run-time version. At run time, the compiler produces ordinary code.

  1. 8.1. Logical Model
    1. 8.1.1. The IO, EIO and BaseIO Monads
    2. 8.1.2. Errors and Error Handling
      1. 8.1.2.1. Constructing IO Errors
  2. 8.2. Control Structures
  3. 8.3. Console Output
  4. 8.4. Mutable References
    1. 8.4.1. State Transformers
      1. 8.4.1.1. Reading and Writing
      2. 8.4.1.2. Comparisons
      3. 8.4.1.3. Concurrency
  5. 8.5. Files, File Handles, and Streams
    1. 8.5.1. Low-Level File API
    2. 8.5.2. Streams
    3. 8.5.3. Paths
    4. 8.5.4. Interacting with the Filesystem
    5. 8.5.5. Standard I/O
    6. 8.5.6. Files and Directories
  6. 8.6. Environment Variables
  7. 8.7. Timing
  8. 8.8. Processes
    1. 8.8.1. Current Process
    2. 8.8.2. Running Processes
  9. 8.9. Random Numbers
    1. 8.9.1. Random Generators
    2. 8.9.2. System Randomness
  10. 8.10. Tasks and Threads