Skip to content

Type System ​

Neo uses a two-level type system:

  1. Compile-time meta types govern parameters, expressions evaluated during checking/elaboration, and structural references (UInt, Pos, Bool, LogicType, Timing, LogicVal, InterfaceRef, InterfaceInst, ModuleInst, lambdas, and logic functions).
  2. Hardware logic types (LogicType values) classify the signals, ports, wires, and registers that exist in the generated circuit (Bits(w), Int(w), Clock, Enum, Struct, Union, Option(T), Tuple(...), Array(T, len)).

Compile-Time Meta Types ​

UInt and Pos ​

Neo provides two non-negative integer parameter types backed by Z3 integer expressions:

  • UInt (>= 0): Non-negative compile-time integer.
    • Supports +, -, *, /, %, and comparisons (<, <=, >, >=, ==, != returning Bool).
    • Subtraction a - b is Z3-checked for a >= b (uint_underflow).
    • Division a / b and modulo a % b are Z3-checked for b >= 1 (division_by_zero).
    • Supports zero-based array indexing (arr[0]), zero-width types (Bits(0), Int(0), Array(T, 0)), and zero-width constants (0#0).
    • Methods: .toBits(w: UInt) -> Bits(w) constant, .toInt(w: UInt) -> Int(w) constant.
  • Pos (>= 1): Strictly positive compile-time integer.
    • Inside a declaration body, Z3 assumes param >= 1.
    • At every instantiation call site, Z3 proves the passed UInt argument satisfies arg >= 1 (argument_invariant_error).
    • Ideal for non-empty widths and lengths: Array(T, len) with data[len - 1], clog2(n), .repeat(n), and .sextend(w2).

Bool ​

Compile-time boolean with literals true and false:

  • Unary: !b
  • Binary: &&, ||, ^, ==, !=
  • Methods: b.toBits() converts a compile-time Bool into a 1-bit Constant-timed Bits(1) hardware value.
  • Control flow: if cond { ... } else { ... } on a compile-time Bool evaluates statically (branching symbolically in Z3 when cond depends on symbolic parameters).

LogicType ​

LogicType is the meta type of hardware types treated as first-class compile-time values. Built-in type constructors (Bits, Int, Array, Option, Tuple) and parameterized struct/union declarations are compile-time callables returning a LogicType.

Every LogicType exposes the .width property, which computes its packed bit width as a UInt:

neo
let total_w = MyStruct.width;

Timing ​

Represents a cycle-accurate timing value in Neo's timing lattice (Constant, SingleCycle(trigger, offset, clock), Dynamic(clock)). Supports t + n (Timing + UInt), Timing(), Timing(clk), and Dynamic(clk). See Timing & Clock Domains.

LogicVal ​

Represents a hardware wire, port, register, or combinational expression. Each LogicVal carries:

  • A LogicType
  • A TimingVal (including its ClockDomain)
  • Optional assignability metadata (AssignPath) when it refers to a drivable hardware sink

Every LogicVal supports .castTo(T: LogicType), which reinterprets its bit representation as T when Z3 proves equal bit widths.

Clock ​

Clock is a built-in 1-bit LogicType representing a clock signal. Clock-typed ports and wires carry unique clock-domain identities used by Timing(clk), Dynamic(clk), and seq clk { ... } for static clock-domain crossing (CDC) verification.

Interfaces, Modules, and Functions ​

  • Interface references & instances: interface declarations and Decoupled(T) produce interface types used in provides/consumes clauses; instantiated interfaces expose directional fields and can be wired with connect(a, b).
  • Module references & instances: mod inst = MyMod(...); binds a ModuleInst exposing its ports and interfaces as named fields.
  • Compile-time lambdas: (a, b) => expr abstract over compile-time expressions.
  • Hardware logic functions: #[caps](input x(t): T) -> output y(t): U => expr abstract over combinational hardware logic, invoked via fn#(arg) or passed to Array.map(fn).

Hardware Logic Types & Subtyping ​

Hardware TypePacked WidthBit Layout
Bits(w)wRaw bit vector (w >= 0)
Int(w)wInteger bit vector (Int(w) <: Bits(w))
Clock1Single clock wire with unique domain identity
Enummax(1, bit_len(max_val))Minimum unsigned width (at least 1 bit)
Structsum(field.width)LSB-first field packing
Uniontag_w + max(payload.width)[data (MSB), tag (LSB)]; 1-variant has tag_w = 0
Option(T)1 + T.width[payload (MSB), valid_tag (LSB)] (Invalid = 0, Valid = 1)
Tuple(T0, ...)sum(Ti.width)LSB-first element packing (.0, .1, ...)
Array(T, len)len * T.widthLSB-first element packing ([0] at LSB)

Subtyping and Type Joins ​

  • Int(w) <: Bits(w): Any Int(w) is implicitly assignable to Bits(w) (proven equal width via Z3). Converting Bits(w) back to Int(w) requires .asInt().
  • Branch type joins: When branches of if, case, match, or ternary ?: produce Int(w) and Bits(w) of equal width, the joined result widens to Bits(w).
  • Nominal identity: Distinct enum, struct, union, and interface declarations are nominally typed and cannot be implicitly interchanged (use .castTo(T) for explicit same-width bit reinterpretation).