## MLton 20241230+git20251029+dfsg-5

# AST

AST is the IntermediateLanguage produced by the FrontEnd and translated by Elaborate to CoreML.

## Description

The abstract syntax tree produced by the FrontEnd.

## Implementation

- `ast-programs.sig`

- `ast-programs.fun`

- `ast-modules.sig`

- `ast-modules.fun`

- `ast-core.sig`

- `ast-core.fun`

- `ast`

## Type Checking

The AST IntermediateLanguage has no independent type checker. Type inference is performed on an AST program as part of Elaborate.

## Details and Notes

### Source locations

MLton makes use of a relatively clean method for annotating the abstract syntax tree with source location information. Every source program phrase is "wrapped" with the `WRAPPED` interface:

    Unresolved directive in AST.adoc - include::https://raw.github.com/MLton/mlton/master/mlton/control/wrapped.sig[indent=0,lines=8..19]

The key idea is that `node'` is the type of an unannotated syntax phrase and `obj` is the type of its annotated counterpart. In the implementation, every `node'` is annotated with a `Region.t` (`region.sig`, `region.sml`), which describes the syntax phrase’s left source position and right source position, where `SourcePos.t` (`source-pos.sig`, `source-pos.sml`) denotes a particular file, line, and column. A typical use of the `WRAPPED` interface is illustrated by the following code:

    Unresolved directive in AST.adoc - include::https://raw.github.com/MLton/mlton/master/mlton/ast/ast-core.sig[indent=0,lines=47..68]

Thus, AST nodes are cleanly separated from source locations. By way of contrast, consider the approach taken by SML/NJ (and also by the CKit Library). Each datatype denoting a syntax phrase dedicates a special constructor for annotating source locations:

    datatype pat = WildPat                             (* empty pattern *)
                 | AppPat of {constr:pat,argument:pat} (* application *)
                 | MarkPat of pat * region             (* mark a pattern *)

The main drawback of this approach is that static type checking is not sufficient to guarantee that the AST emitted from the front-end is properly annotated.