Module Checker
If you are interested in how the borrow checker of this demo is implemented, you may read through the modules in the Interesting Modules section. The modules in the Internal Helper Modules section are mainly for testing/presentation purpose, and are not related to the mechanisms of the borrow checker.
The recommended order to read the source code from scratch is:
SyntaxIR- the interface of
Typecheck - (
Typecheckat anytime if you are interested how the IR is generated) - the interface and document of
Location - the interface and document of
Type ComputeConstraintCheck- implementations of
Location,Typethat you are interested in
The overall structure of the borrow checker is:
- the surface syntax tree is converted to IR
- in the first phase, constraints (see
Location.ConstrSet) representing alias information and owner information of borrows are computed. The main functionality of this phase is to compute the constraint fixed-point for loops. SeeComputeConstraintfor more details - in the second phase, the set of invalid locations are tracked, and uses of invalid locations are detected and reported. The uses of some invalid locations (those that are invalidated in a loop and used incorrectly in the next loop. See "loop/effect-of-prev-loop2" in
Examplesfor a concrete example) cannot be detected at this phase. This kind of invalid locations are recorded after this phase, and the next phase detect incorrect uses of them - in the third phase, the invalid uses of the aforementioned loop-related locations are detected, using information recorded in the second phase
Interesting Modules
module Syntax : sig ... endthe module holding definitions of the surface syntax of the demo language, including expressions, programs, and syntactic types.
module IR : sig ... endthe module holding definitions of the ANF intermediate language used by the borrow checker.
module Typecheck : sig ... endthe module that performs type checking (not borrow checking) and convert a source program into the ANF IR. This module also defines a data structure holding source information that link IR values to their source in the surface syntax. See
Typecheck.type_info
module Location : sig ... endthe module for
locations, a compile-time notion of memory locations. This is the object that the borrow checker use to represent and track values at compile-time This module also contains some important location-related helper data structures.
module Type : sig ... endthe module holding the definition of semantic types, and various operations on them
module ComputeConstraint : sig ... endthe module holding constraint generation primitives of the borrow checker. It contains the first phase of the borrow checker, as well as several utilities used by later phases.
module Check : sig ... endthe module holding the second and third phases of the borrow checker.
Internal Helper Modules
module Pretty : sig ... endmodule Parser : sig ... endmodule Lexer : sig ... endmodule Examples : sig ... endval check_program : Syntax.function_def list -> unitcheck_program functionscombines all modules and phases of the borrow checker, and perform a complete check onfunctions. If it returns normally, there's no error infunctions, otherwise an exception is raised.