SpecDraft 0.1
Sections

Lena 0.1

This document is draft 0.1. The semantics and typing judgments it requires are normative.

1 Purpose and scope

Lena expresses computational meaning within a single call boundary, so that different source representations can be compared. The core defines integers, booleans, arithmetic faults, immutable bindings, conditionals, and early return. A core program takes one input tuple and observes one result.

Calls, loops, objects, addressable memory, concurrency, and external effects are outside the scope of 0.1.

Conformance tests observe · acceptance of pure core representations, and rejection of meaning outside the scope.

2 Abstract syntax and types

A program consists of a width, ordered parameters, a result type, and a body. The types are Int and Bool. A program has a single integer width throughout. The Rust algebraic data types are the canonical structure. The notation below names the parts of that structure.

Program      = Signature × Block
Signature    = Width × Parameter* × Type
Parameter    = BindingId × Type
Block        = Stmt* × Optional<Expr>
Stmt         = Let(BindingId, Type, Expr)
             | If(Expr, Stmt*, Stmt*)
             | Return(Expr)
Expr         = Literal(Value) | Var(BindingId) | Not(Expr)
             | Binary(BinaryOp, Expr, Expr)
             | If(Expr, Block, Block, Type) | Block(Block, Type)
             | Extension(E)

In the core instance, E has no values. Literals and binding references have the type of their value. not and the logical binary operations take booleans and yield a boolean. Arithmetic takes two integers and yields an integer. Ordering comparisons take two integers and yield a boolean. Equality comparisons take two values of the same type and yield a boolean. A condition must be a boolean.

If a block has a final expression, its type must equal the type the block requires. The type required of the program's block is the result type of the signature. Block expressions and conditional expressions use their stated result type. Both blocks of a conditional expression are checked against that type. The type of a let initializer must equal the declared type. The operand of every return must have the result type of the signature. Statements may follow a return. Type checking, name checking, and literal range checking also apply to unreachable statements and branches.

A final expression may be left out only when the statement sequence contains a return statement, or a conditional statement whose two branches both guarantee a return by the same rule. A return inside an expression does not count toward this syntactic condition.

Conformance tests observe · the type of every kind of expression, mismatched branch and return types, the condition for leaving out a final expression, and checks on unreachable syntax.

3 Values and the integer domain

The values of Bool are false and true. Integers are signed, and the reference width is 64 bits. A width w is an integer from 2 to 64. Every program states its width.

Let MIN = -2^(w-1) and MAX = 2^(w-1)-1. Integer inputs and literals must lie in this closed interval. The same rule applies to programs with a reduced width. An input or literal out of range is rejected. Implicit truncation, promotion, and conversion between booleans and integers are not allowed.

Before evaluation, the input tuple is checked against the signature for the number of values, the type at each position, and the integer range. A mismatch is an input rejection and produces no Outcome.

Conformance tests observe · both bounds at 2 bits and at 64 bits, literals and inputs out of range, and the distinction between booleans and integers.

4 Semantics of operations and language faults

The arithmetic rules below are defined over mathematical integers. A checked arithmetic node carries the LanguageFault record to use on each fault path. Until a fault is triggered, the record is held only as data. This section defines when a fault is triggered. The observation fields emitted on each path are used exactly as they appear in the node.

Operation Result and fault conditions
add, sub, mul Compute the mathematical sum, difference, and product respectively. If the result is out of range, the matching overflow fault is raised.
div, rem A zero divisor raises the division by zero fault. The combination of MIN and -1 raises the overflow fault in both operations.
div For valid operands, the quotient q is the integer truncated toward zero.
rem For valid operands, the result is a - q*b. When it is not zero, it has the sign of the dividend.
wrap-add, wrap-sub, wrap-mul The unique value in [MIN, MAX] congruent to the mathematical sum, difference, and product respectively. The modulus is 2^w. These operations raise no fault themselves.
eq, ne Identity of two values of the same type, and its negation.
lt, le, gt, ge Ordering of signed mathematical integers.
not, and, or Boolean operations. If the left operand of and is false, the right operand is skipped and the result is false. If the left operand of or is true, the right operand is skipped and the result is true. Otherwise the right operand is evaluated and its boolean is the result.

Conformance tests observe · arithmetic bounds, zero divisors, the MIN / -1 combination in both operations, negative remainders, the difference between checked and wrapping arithmetic, and comparison and short circuits.

5 Bindings and evaluation order

Parameters are bound in the body in input order. Duplicate parameter IDs are rejected. Inside a block, let evaluates its initializer in the existing environment. If that yields a value, the name is bound to it for the statements that follow and for the final expression. Binding the same ID again shadows it from that point on. A shadowing binding may have a different type from the earlier one. A block, and each branch of a conditional statement, keeps the bindings made inside it from reaching outside.

Statements run from first to last. A binary expression evaluates its left operand first. The right operand is evaluated only when the left operand yielded a value and did not short circuit. The operation applies only once both values are in hand. If a fault or an internal defect occurs first, its observation is returned at once. An early return also stops the rest of the evaluation. If the source evaluates in a different order, the adapter expresses that order with bindings.

Conformance tests observe · initializers and shadowing, the scope of branches, the first result of an expression that can fault on either side, and a reordered source evaluation order.

6 Control

The condition is evaluated first. If it yields a boolean, only the selected branch runs. When a branch of a conditional statement completes normally, the next statement runs. A final expression that completes normally inside a block yields the value of the block. The value of the top-level block is the value of the program.

return e evaluates e first. If that yields a value, it leaves every level of nesting as the result of the whole program. If evaluating e already ended in a fault, an internal defect, or an early return, that result propagates.

Conformance tests observe · the unselected branch staying unevaluated, a return propagating out of nested blocks and out of operands, and a fault while the return value is computed.

7 Observation

Outcome<D> = Value(Value)
           | LanguageFault(kind, cause, location)
           | InternalDefect(D)

kind and cause are nonempty strings. They are compared exactly, by case and by Unicode scalar sequence. Lowering carries the fault code and cause reported under the source's observation convention into these fields. The match between the trigger conditions of checked arithmetic and the observation fields is checked as a normalization obligation of that source.

location is the identifier of an original source and a half-open UTF-8 byte range [start, end) counted from 0. The source identifier must be nonempty and must resolve in the bound set of sources. The range must be nonempty and must fall on character boundaries in the original. The S-expression carries the source identifier and both offsets. The generated output's own line numbers are never used as the observed location instead. If a source emits only point positions, both sides are turned into ranges through the common source mapping stated in the contract.

The two adapters in one comparison map locations into the same set of original sources fixed by the contract. A location in generated output uses the original range that the generation process points to. The mapping is bound to the original bytes and to the evidence for each location. If the matching range cannot be determined, processing stops with a contract violation.

The core has the InternalDefect variant from the start. In the pure core, D is empty. In extension evaluation, a defect of the modeled target is recorded with the extension's closed defect type. Two defect observations are equal when the full text header of the module where the defect arose and the canonical code are each equal. Cell, block, and binding IDs are auxiliary diagnostics. Gaps and implementation failures in the evaluator itself are handled by Yana.

Conformance tests observe · the three observation variants, different representations pointing to the same original range, observations that differ only in cause or location, a range that cannot be determined, and the distinction between target defects and evaluator defects.

8 Handling unsupported meaning

An adapter returns success only for forms whose source meaning it can express completely. On meeting an unknown meaning or an unsupported source form, it names the part and the reason and stops during lowering as unsupported. If it breaks the structure, types, or source links the contract requires, it stops with a contract violation.

The normalization obligation also covers whether observations survive in the parts an adapter removed or rewrote. An observable effect may not be dropped or approximated by a different source meaning. Running out of checking resources is a separate execution limit.

Conformance tests observe · unknown source forms, wrong types or source mappings in supported forms, and the distinction between resource exhaustion and unsupported meaning.

9 Extension rules

Each module must provide a versioned name, closed node and defect types, typing rules, execution rules, and the points where it attaches to the core. An extension expression fills an expression position of the core and follows the core rules for types, evaluation order, and the propagation of early exits. The module defines the top-level structure of its extension. Extension defects are observed through the existing variants of §7.

A verification profile states the supported modules and the version of each. Unknown modules and versions are unsupported. The core depends on no extension. Each extension spec declares the modules and versions it depends on, and the dependency graph must be acyclic.

An extension never changes or replaces the typing, evaluation, or observation rules of core nodes. It does not reuse core tags. The new tags of modules used together must not overlap either. The core hands only the nodes in extension positions to their module. Adding or removing an extension must change zero lines of the core spec and zero lines of the core crate.

Conformance tests observe · a core build with no extensions, extension ordering and the propagation of early exits, and unknown modules or versions.

10 Text form

The grammar below defines the S-expression of a core program.

Program   = (lena-0.1 W (params Param*) T Block)
Param     = (U T)
T         = int | bool
Block     = (block (do Stmt*) Tail)
Tail      = (yield Expr) | (no-yield)
Stmt      = (let U T Expr) | (return Expr)
          | (if-stmt Expr (do Stmt*) (do Stmt*))
Expr      = (int I) | (bool true) | (bool false) | (var U)
          | (not Expr) | (if T Expr Block Block) | (scope T Block)
          | (C Fault Expr Expr) | (D Fault Fault Expr Expr) | (P Expr Expr)
C         = add | sub | mul
D         = div | rem
P         = wrap-add | wrap-sub | wrap-mul | eq | ne | lt | le | gt | ge | and | or
Fault     = (fault String String (span String U64 U64))

W is the width of §3. U is an unsigned 32-bit binding ID, U64 is an unsigned 64-bit offset, and I is a signed 64-bit integer literal. Numbers are written in decimal. The only sign allowed is - on a negative number. Zero is written 0, and leading zeros are not allowed. The faults of D come in the order division by zero, then overflow. The strings of Fault come in the order kind, then cause. span holds the source ID, the start, and the end, in that order.

Reading checks the core grammar and the static rules. The binding check confirms the source ID of every fault record and its byte boundaries within the original, whether or not the record is reachable. Unknown body tags, wrong argument counts, and a trailing second expression are rejected. ASCII space, tab, line feed, and carriage return are allowed between tokens and before and after the whole expression. A final line break is also allowed. An expression preceded by a UTF-8 BOM is rejected. Parentheses and the two ends of a string are token boundaries in themselves. There are no comments. Strings are enclosed in double quotes. The escapes are \", \\, \n, \r, \t, and \u{hex}. A Unicode escape denotes a valid scalar value with 1 to 6 ASCII hexadecimal digits. The ASCII control characters are U+0000 through U+001F and U+007F. These characters must be escaped inside a string.

S-expression lists nest at most 128 deep. The top-level list has depth 1, and each list inside a list adds 1. Atoms and strings add no depth. Going past the limit is rejected as a resource limit error.

Canonical output puts no space between an opening parenthesis and the first item, or between the last item and the closing parenthesis. Items are separated by a single ASCII space. There is no whitespace before or after the body and no final line break. In strings, double quote, backslash, LF, CR, and tab are written with the short escapes. The other ASCII control characters are written as lowercase \u{hex} without leading zeros. All other scalar values are written as they are.

Printing a valid structure and reading it back yields the identical structure. Binding IDs, list order including declarations and blocks, values, and sources are compared exactly. The comparison relies on neither alpha equivalence nor the original whitespace.

Conformance tests observe · the mapping between tags and fields, the structural round trip of every core node, normalization of noncanonical whitespace, string and number bounds, and rejection of syntax and type errors.

11 Versions and compatibility

A header has the form lena-[module-]number.number. The brackets mean the module part may be left out. Each number part is one or more ASCII decimal digits. Each word of a module name starts with an ASCII lowercase letter. Lowercase letters and digits may follow. Words are joined with hyphens. The header of core 0.1 is lena-0.1.

A header that fits this form but names an unsupported module or version is unsupported. A header that does not fit the form is a syntax error. Changing the semantics, the typing rules, or the canonical form assigns a new version. There is no reinterpretation and no automatic version promotion.

Conformance tests observe · version identification, rejection of unsupported versions, and telling apart inputs whose versions or extension sets differ.

Source revision 4259231 ·