6.Β 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. 6.1. Logical Model
    1. 6.1.1. The IO, EIO and BaseIO Monads
    2. 6.1.2. Errors and Error Handling
      1. 6.1.2.1. Constructing IO Errors
  2. 6.2. Control Structures
  3. 6.3. Console Output
  4. 6.4. Mutable References
    1. 6.4.1. State Transformers
      1. 6.4.1.1. Reading and Writing
      2. 6.4.1.2. Comparisons
      3. 6.4.1.3. Concurrency
  5. 6.5. Files, File Handles, and Streams
    1. 6.5.1. Low-Level File API
    2. 6.5.2. Streams
    3. 6.5.3. Paths
    4. 6.5.4. Interacting with the Filesystem
    5. 6.5.5. Standard I/O
    6. 6.5.6. Files and Directories
  6. 6.6. Environment Variables
  7. 6.7. Timing
  8. 6.8. Processes
    1. 6.8.1. Current Process
    2. 6.8.2. Running Processes
  9. 6.9. Random Numbers
    1. 6.9.1. Random Generators
    2. 6.9.2. System Randomness
  10. 6.10. Tasks and Threads