hanki
Hanki is a statically typed language with an effect system, actors with separate heaps and typed messages, and exact arithmetic on int, decimal and rational.
use io
actor Tab
state owed: decimal = 0
on add(amount: decimal) -> ()
owed += amount
end
on total() -> decimal
owed
end
end
def split(total: decimal) -> rational
total / 7
end
def main!() -> () [io, Crash]
tab = spawn Tab
tab.add(1.10).or_crash!("tab died")
tab.add(2.20).or_crash!("tab died")
total = tab.total().or_crash!("tab died")
share = split(total)
io.print!("a tab of #{total.to_fixed(2, decimal.HalfUp)} split seven ways is #{share.to_fixed(2, decimal.HalfUp)} each (exactly #{share})\n")
end
split is a pure function. The ! suffix marks main! as an action, and [io, Crash] after its return type lists the effects it may perform. to_fixed(2, decimal.HalfUp) renders the exact quotient to the nearest cent, and #{share} in the format string prints it as the ratio it is.
$ hanki run tab.hk
a tab of 3.30 split seven ways is 0.47 each (exactly 33/70)
$ hanki build tab.hk
hanki build: resolving dependencies
hanki build: checking tab.hk
hanki build: compiling to native code
hanki build: wrote out/tab.o
hanki build: linking
hanki build: linked out/tab
$ ./out/tab
a tab of 3.30 split seven ways is 0.47 each (exactly 33/70)
hanki run interprets the file, and hanki build compiles it to native code through LLVM. A compiled program produces the same output, diagnostics, effects and exit code as the interpreted one, and differs in execution time and memory use. A file whose first line is #!/usr/bin/env -S hanki run runs as an executable script.
Numeric types
Section 3 lists the numeric types. Where nothing in the context selects a type, an unsuffixed integer literal is an arbitrary-precision int and an unsuffixed fractional literal is a decimal. Arithmetic on int, decimal and rational never rounds, and a result has the smallest of the three types that represents its exact value. Fixed-width integers and floats are selected by a suffix, as in 5i32 and 0.1f64, or by a type annotation. Fixed-width integer arithmetic wraps at the type's width, and float arithmetic is IEEE 754.
hanki> 0.1 + 0.2
= 0.3
hanki> 1 / 3
= 1/3 (≈ 0.33333333)
hanki> (1 / 3) * 3
= 1
hanki> :type 1 / 3
1 / 3: rational
hanki> 0.1f64 + 0.2f64
= 0.30000000000000004
Effects
split above is a pure def. If its body calls an action, hanki check reports an error:
tab.hk:14:3: error[H0606]: pure `def` cannot call action `io.print!`: add `!` to the caller's name to make it an action (HANKI.md section 5)
14 | io.print!("splitting\n")
| ^^^^^^^^^^^^^^^^^^^^^^^^
An action declares its effects in a row after its return type. The checker verifies that each call in the body is covered by the row. hanki effects PATH reads the source and lists each effect the program can perform with the action or handler whose row declares it. Section 6 describes the effect atoms, user-defined effects and typed holes.
Diagnostics
Lint checks run inside hanki check beside the type checker, and every finding is an error. An unused import or a redundant return fails the command:
main.hk:2:1: error[H0625]: `use json` is unused; nothing here needs the `json.` qualifier
2 | use json
| ^^^^^^^^
hanki check --fix applies the machine-applicable corrections and re-runs the check.
REPL
The REPL uses the same front end as hanki run. Bindings and actors persist between inputs, and :reload PATH re-reads a file and replaces the session's definitions of the same names.
hanki> :load tab.hk
defined: Tab, split, main!
hanki> t = spawn Tab
hanki> t.add(0.1)
= Ok(())
hanki> t.add(0.2)
= Ok(())
hanki> t.total()
= Ok(0.3)
hanki> :type t
t: ActorRef<Tab>
Section 22.6 lists the commands.
Actors
Section 15 describes spawn, supervision, timers and shutdown. Each actor has its own heap and runs on an OS thread. Only its message handlers read and write its state, and a handler's arguments and return value are copied between the sender's heap and the actor's. A send returns a Result; the .or_crash! calls in main! above unwrap it. Sending to a dead actor, or to one whose mailbox is full, returns Err to the sender. An actor's supervisor is by default the actor that spawned it, and the supervisor's on actor_died handler is called with the dead actor's id and the cause of death.
hanki test PATH --seed 7 runs a project's tests under a deterministic actor schedule selected by the seed. Rerunning with that seed replays the schedule.
Memory is reclaimed by reference counting within each actor's heap when the last reference to a value is released. Values are acyclic. Reference counting reclaims every value, and there is no tracing collector.
Status
spec-first. Most of what follows describes the target design. Section 22 tracks what is implemented.
Section 22 describes the implementation status. The source tree is not yet public.
Hanki is licensed under 0BSD. The licensing page states the terms and their effect on a program built with Hanki.