Skip to content

Examples

Each example isolates a syntax or runtime rule. The language reference contains the full semantics and constraints.

print("hello, Nilakan")

Output: "hello, Nilakan"

def area(width: num, height: num) -> num:
return width * height
print(area(4, 5))

Output: 20

xs = [1, 2].append(3)
print(xs)
seen = {"a", "b"}
print("a" in seen)
counts = {"a": 1}
print(counts.set("b", 2))

Output:

[1, 2, 3]
True
{"a": 1, "b": 2}
def exchange() -> tuple[num, num]:
init:
left = 1
right = 2
done = False
while not done:
left' = right
right' = left
done' = True
return left, right
print(exchange())

Output: (2, 1). Both right-hand sides read the same current state.

def swap_edges(values: list[num]) -> list[num]:
init:
arr = values
step = 0
while step < 1:
arr'[0] = arr[2]
arr'[2] = arr[0]
step' = step + 1
return arr
print(swap_edges([1, 2, 3]))

Output: [3, 2, 1]. Both sparse writes read the same frozen pre-round list.

def accumulate() -> list[num]:
init:
xs = [1, 2]
step = 0
pragma rhs_prime: idempotent
while step < 1:
xs'[1] = xs'[0] + 1
xs'[0] = 11 if xs[0] < 11 else xs[0]
step' = 1
return xs
print(accumulate())

Output: [11, 12]. Dependency scheduling evaluates the write to index 0 before the primed read used for index 1.

Intentional failure — NLK2011
value: num = "not a number"

Expected result: a fatal TypeError/NLK2011 contract diagnostic; this example intentionally does not run successfully.

def first_or_error(xs: list[num]) -> num | err:
return try xs.get(0)
def safe_first(xs: list[num]) -> num:
return first_or_error(xs) catch 0
print(safe_first([]))

Output: 0. The missing index becomes IndexError/NLK4004, which catch replaces.