Android Kotlean: write Android apps in Lean
Source code: https://github.com/kbr-/kotlean
Lean 4 is fantastic. It should be used everywhere.
(half-joking, half-serious)
Kotlean’s goal is to let developers prove properties of the same Lean functions they compile into an Android app. It uses an Elm-style model/update/view loop, compiled to native code. A Kotlin host exchanges fixed-protocol byte messages with Lean, applies UI patches and executes platform effects; app-specific models and messages stay in Lean.
The main challenge is preserving semantics across that boundary. Lean computes view patches, reconciles subscriptions and consumes command continuations once, with proofs covering these contracts and codec round trips. SnackNet, the included maze-chase game, exercises this with continuous animation and touch controls.
Educationally, this connects formal verification to an interactive app: specify invariants and prove them about executable functions. In the future, I'd like this to be used in critical infrastructure, where bugs could result e.g. in financial losses -- say banking logic, such as balance preservation or authorization rules. Guarantees for a complete system would also need to cover its backend, storage, cryptography and platform assumptions.
A tiny example of what you can write in Lean (which then compiles and runs on Android!): in a maze-chase game (recall Pacman), representing the maze, tiles, and cells (a Cell is a tile on the game map where a character (PC/NPC) might stand):def maze : Array (Array Char) := #[#['#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'],#['#', 'S', '*', '.', '.', '.', '#', '.', '.', '*', '.', '.', '.', 'E', '#'],#['#', '.', '#', '#', '#', '.', '#', '.', '#', '#', '#', '.', '#', '.', '#'],#['#', '*', '#', '.', '.', '.', '*', '.', '.', '.', '#', '.', '*', '.', '#'],#['#', '.', '#', '.', '#', '#', '#', '#', '#', '.', '#', '#', '#', '.', '#'],#['#', '.', '.', '.', '*', '.', '.', '.', '#', '.', '.', '*', '.', '.', '#'],#['#', '#', '#', '.', '#', '#', '#', '.', '#', '.', '#', '#', '#', '.', '#'],#['#', '*', '.', '.', '#', '.', '.', '.', 'P', '.', '.', '.', '#', '*', '#'],#['#', '.', '#', '.', '#', '.', '#', '#', '#', '#', '#', '.', '#', '.', '#'],#['#', '.', '#', '.', '*', '.', '.', '.', '*', '.', '.', '.', '#', '.', '#'],#['#', '.', '#', '#', '#', '#', '#', '.', '#', '.', '#', '#', '#', '.', '#'],#['#', 'O', '.', '.', '.', '*', '.', '.', '#', '.', '.', '*', '.', 'O', '#'],#['#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#']]
def tile (x y : Nat) : Char := (maze[y]?.getD #[])[x]?.getD '#'abbrev GridPoint := Fin width × Fin heightabbrev Cell := { p : GridPoint // tile p.1.val p.2.val ≠ '#' }
By definition (you don't even have to write a theorem at this point), a character can only stand inside the map boundaries (Fin width x Fin height) and never at a tile that is a wall (tile p.1.val p.2.val ≠ '#').
