Type System
Neo uses a two-level type system:
- 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). - Hardware logic types (
LogicTypevalues) 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 (<,<=,>,>=,==,!=returningBool). - Subtraction
a - bis Z3-checked fora >= b(uint_underflow). - Division
a / band moduloa % bare Z3-checked forb >= 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.
- Supports
Pos(>= 1): Strictly positive compile-time integer.- Inside a declaration body, Z3 assumes
param >= 1. - At every instantiation call site, Z3 proves the passed
UIntargument satisfiesarg >= 1(argument_invariant_error). - Ideal for non-empty widths and lengths:
Array(T, len)withdata[len - 1],clog2(n),.repeat(n), and.sextend(w2).
- Inside a declaration body, Z3 assumes
Bool
Compile-time boolean with literals true and false:
- Unary:
!b - Binary:
&&,||,^,==,!= - Methods:
b.toBits()converts a compile-timeBoolinto a 1-bitConstant-timedBits(1)hardware value. - Control flow:
if cond { ... } else { ... }on a compile-timeBoolevaluates statically (branching symbolically in Z3 whenconddepends 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:
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 itsClockDomain) - 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:
interfacedeclarations andDecoupled(T)produce interface types used inprovides/consumesclauses; instantiated interfaces expose directional fields and can be wired withconnect(a, b). - Module references & instances:
mod inst = MyMod(...);binds aModuleInstexposing its ports and interfaces as named fields. - Compile-time lambdas:
(a, b) => exprabstract over compile-time expressions. - Hardware logic functions:
#[caps](input x(t): T) -> output y(t): U => exprabstract over combinational hardware logic, invoked viafn#(arg)or passed toArray.map(fn).
Hardware Logic Types & Subtyping
| Hardware Type | Packed Width | Bit Layout |
|---|---|---|
Bits(w) | w | Raw bit vector (w >= 0) |
Int(w) | w | Integer bit vector (Int(w) <: Bits(w)) |
Clock | 1 | Single clock wire with unique domain identity |
Enum | max(1, bit_len(max_val)) | Minimum unsigned width (at least 1 bit) |
Struct | sum(field.width) | LSB-first field packing |
Union | tag_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.width | LSB-first element packing ([0] at LSB) |
Subtyping and Type Joins
Int(w) <: Bits(w): AnyInt(w)is implicitly assignable toBits(w)(proven equal width via Z3). ConvertingBits(w)back toInt(w)requires.asInt().- Branch type joins: When branches of
if,case,match, or ternary?:produceInt(w)andBits(w)of equal width, the joined result widens toBits(w). - Nominal identity: Distinct
enum,struct,union, andinterfacedeclarations are nominally typed and cannot be implicitly interchanged (use.castTo(T)for explicit same-width bit reinterpretation).