On-Device AI with Apple Foundation Models
File: AppleSystemModel.lean
Recent versions of macOS include an on-device language model. Scripts can call it with the fm command-line tool. This is the shortest program in the book. It needs no API key and no network.
You need a Mac with Apple silicon, macOS 15.1 or later, Apple Intelligence turned on, and the fm tool installed. The model runs on the machine, so prompts and replies stay local. If Apple Intelligence is off or the tool is missing, fm exits with an error and the program prints its stderr.
The Code
1 -- lean --run AppleSystemModel.lean (macOS Apple Foundation Model)
2
3 def appleFM (prompt : String) : IO String := do
4 let out ← IO.Process.output {
5 cmd := "fm", args := #["respond", "--no-stream", prompt] }
6 if out.exitCode != 0 then
7 throw (IO.userError s!"fm failed: {out.stderr}")
8 return out.stdout.trimAscii.toString
9
10 def main : IO Unit := do
11 IO.println (← appleFM "what is 1 + 10?")
How It Works
IO.Process.outputstarts a program, waits for it to end, and returns a structure with three fields:exitCode,stdout, andstderr.- Its argument is a structure written with braces.
cmdis the program name andargsis an array of arguments. Lean passes each argument to the program as written. No shell reads them, so a prompt that contains quotes or spaces is safe. throw (IO.userError ...)stops theIOaction with an error message.trimAsciiremoves the newline thatfmprints after the reply.
The fm Command
| Part of the call | Effect |
|---|---|
respond |
asks the model for a reply to the prompt |
--no-stream |
prints the whole reply at once instead of token by token |
| the prompt | the question, passed as one argument |
Other flags, such as a system prompt, temperature, and a token limit, depend on the installed version. Run fm --help to see the current list.
Lean 4 Notes
The IO type. A value of type IO String is an action that produces a string when it runs. Calling appleFM "hi" builds the action. The action runs only when it becomes part of main. Inside a do block, ← runs an action and gives you its result.
if without else. In a do block an if can stand alone. The line if out.exitCode != 0 then throw ... does nothing when the test is false.
Errors. throw ends the action with an error. If nothing handles the error, the program prints it and exits with a failure code. To handle one, wrap the call:
1 try
2 IO.println (← appleFM "hi")
3 catch e =>
4 IO.println s!"caught: {e}"
The catch pattern names the error, and IO.Error has a ToString instance. This keeps the program running when fm is missing.
return. return x ends the do block with the value x. As the last line of a block it is the same as pure x.
Structure arguments. IO.Process.output takes one argument of type IO.Process.SpawnArgs. Braces build a value of that type. The fields I do not set, such as the working directory and extra environment variables, keep their defaults.
Three forms of main. Lean accepts main : IO Unit, main : IO UInt32, and main : List String → IO UInt32. The UInt32 is the exit code of the program, and the list holds the command-line arguments.
Running It
You need a Mac with Apple Intelligence turned on. The Common Lisp edition of this chapter notes that you must also accept the license for the tool with sudo fm license.
1 $ lean --run AppleSystemModel.lean
2 11
Using It
Change the prompt in main. To take the prompt from the command line, change the type of main to List String → IO Unit. Lean then passes the arguments that follow the file name: lean --run AppleSystemModel.lean "your prompt".
1 def main (args : List String) : IO Unit := do
2 let prompt := args.head?.getD "what is 1 + 10?"
3 IO.println (← appleFM prompt)
The Ollama chapter follows the same steps: an IO function builds an argument list, runs a program with IO.Process.output, checks exitCode, and trims stdout. Apple runs a local tool with no key; Ollama sends HTTP through curl and parses JSON. The error paths differ for that reason: fm reports a process failure, and Ollama reports a missing JSON field.