Create and explore your first project
Inspect the lema.project/v1 manifest and .lema/store created by lema new, add .lema and .lean modules with lema add, compare terminal and --json output, and render Pythonic views with lema view.
In Lesson 2 (Set up a machine for Lema), you ran lema new hello to scaffold a project and start its background server. Now you will look inside the project directory, see how lema.json and .lema/store organize your codebase, publish both a Pythonic .lema module and a .lean module with lema add, and render stored declarations back into Pythonic contract syntax with lema view.
To run these commands live, work inside the hello directory you created in Lesson 2 (with lema on your PATH). If you are reading on macOS or Windows, every command and output is shown in full below.
1. Inspect what lema new created
List the files inside your hello directory, including the hidden .lema/ directory:
ls -la
ls -la .lemalema.json
src/
.lema/
.lema/store/
.lema/lema.sock
.lema/lema.pid
.lema/lema.log
.lema/tokenEach path has a distinct job in your workflow:
| Path | What it holds | Should you commit or share it? |
|---|---|---|
lema.json | The normative project manifest (lema.project/v1), naming the project, namespace, deterministic salt, default ref (main), and ordered module list. | Yes, part of your project workspace. |
src/Main.lean | The starter module generated by lema new, ready to run on the Lema VM. | Yes, authored source files live under src/. |
.lema/store | The content-addressed store holding accepted objects, kernel receipts, contract selections (lema.contract/v1), snapshots (lema.snapshot/v2), and revisions (lema.revision/v1). | Export and share via lema export / lema import (Export and import a project). |
.lema/lema.sock | The local Unix domain socket where the background lema serve process listens (with automatic short-path fallback if the directory path exceeds the OS socket length limit). | No, ephemeral runtime socket. |
.lema/lema.log & .lema/lema.pid | Standard output/error logs and process ID of the detached background server. | No, local server state. |
.lema/token | The private authentication token (mode 0600) for the default local actor (dev) created by lema new. | Never share .lema/token; keep it owner-only (0600). |
Why does .lema/store already have 184 accepted objects and 42 aliases before you write a single line of code? Because lema new initializes the store with the namespace object (pinning Lean 4.34.1 and canonical encoding lema-core-v4), imports the pinned v0.2 foundational signature (Core, AppCmd, Frames, and Capability so your modules can immediately use Prog, Contract, and the six capability tags), registers the local actor dev, and creates the initial project reference main under a default lema.policy/v1.
2. Read the lema.json project manifest
Open lema.json:
cat lema.json{
"schema": "lema.project/v1",
"name": "hello",
"namespace": "Hello",
"salt": "hello-4f9c2a1b",
"default_ref": "main",
"modules": [
"src/Main.lean"
]
}Here is what each field in lema.project/v1 controls:
nameandnamespace: Names your project (hello) and sets the default authoring namespace prefix (Hello).salt(hello-4f9c2a1b): When a module defines a new inductive type orstructure(such as acontractblock), Lema uses this project salt alongside the module name to compute a deterministic structure seed. Keepsaltunchanged as your project evolves so structure identities remain stable across edits.default_ref(main): The semantic version control reference that commands likelema diffandlema committarget when you do not pass--ref.modules: The topologically ordered list of.lemaand.leanfiles belonging to the project. When you add a new file withlema add, Lema appends it tomodulesautomatically.
3. Add the starter module (lema add) and compare terminal vs. --json output
Look at the starter file src/Main.lean generated by lema new:
set_option autoImplicit false
namespace Lema
namespace Hello
def greeting : ByteArray := "hello from lema\n".toUTF8
end Hello
end LemaPublish src/Main.lean into your project store using lema add:
lema add src/Main.leanadded src/Main.lean
Hello.greeting #8f4a21c903e1 (def, checked, 0 axioms)When you run lema add interactively in a terminal, it prints a concise human summary: the published declaration name (Hello.greeting), a short 12-character prefix of its 64-character SHA-256 declaration ID (#8f4a21c903e1), its kind (def), its kernel verification status (checked), and its axiom count (0 axioms).
What if an AI coding agent such as Claude Code or Codex is running lema add in an automated loop and needs full 64-character SHA-256 IDs and machine-parseable fields? Pass --json (which Lema also enables automatically whenever standard output is piped to another program):
lema add --json src/Main.lean{
"status": "ok",
"files": [
"src/Main.lean"
],
"names": {
"Hello.greeting": {
"id": "8f4a21c903e1…",
"kind": "declaration",
"flavor": "def",
"axioms": []
}
},
"stored": 0,
"receipts": 5
}Notice that "stored" is 0 on the second run because Hello.greeting was already in .lema/store with the exact same SHA-256 hash (8f4a21c903e1...), so the store deduplicated every object automatically.
4. Add a Pythonic .lema module and see how cross-module imports work
Now create a Pythonic .lema file at src/Counter.lema that defines the advance function and Advances contract claim from Lesson 1:
cat > src/Counter.lema <<'EOF'
def advance(n: Nat) -> Nat:
n + 1
def Advances(f: Nat -> Nat) -> Prop:
forall n: Nat :: f(n) > n
select Advances(advance)
EOFAdd src/Counter.lema to the project with the exact same lema add command:
lema add src/Counter.lemaadded src/Counter.lema
Hello.advance #2c91e04b7a12 (def, checked, 0 axioms)
Hello.Advances #7d18f53c90e4 (def, checked, 0 axioms)
select Hello.Advances(Hello.advance) #b409a812e3f1 (contract, open)Look at what lema add src/Counter.lema just did for you:
- Parsed
.lemain Rust (view::parse_module): It parsedadvance,Advances, andselect Advances(advance)with exact 1-based line and column spans, lowered the declarations to Lean syntax viaview::lean_declaration, and sent them to the background server. - Checked both definitions in the Lean 4.34.1 kernel: Both
Hello.advance(Nat -> Nat) andHello.Advances((Nat -> Nat) -> Prop) were elaborated, exported into canonical objects, and accepted by source-free kernel replay with zero axioms, along with an authoring bundle (lema.authoring/v2) that records their symbolic names and source spans. - Constructed and kernel-checked the contract selection (
lema.contract/v1): Forselect Advances(advance), the store built the closed target propositionT = Hello.Advances Hello.advance, checkedtheorem : T -> T := fun h => hin the Lean kernel to verify thatTis a well-typedProp, pinnedHello.advance’s exact object ID (#2c91e04b7a12) intarget.realizations, and stored an immutablelema.contract/v1record withstatus: "open". - Updated
lema.json: Added"src/Counter.lema"to themoduleslist inlema.json. Whenever you pass multiple files tolema add FILE..or edit a module that downstream project modules reference,lema addtopologically orders the modules across the project’slema.authoring/v2symbolic bindings and re-adds any downstream modules whose symbolic imports moved.
5. See how caret diagnostics guide you and your coding agent
To see what happens when you or an AI coding agent make a syntax or type error, introduce a deliberate type mistake in src/Counter.lema by passing "bad" (a string) where Nat is expected, or write a quick test edit in src/Main.lean. Suppose an agent writes a function in src/Main.lean that tries to add a string to a Nat:
def broken (n : Nat) : Nat := n + "oops"When the agent runs lema add src/Main.lean, Lema maps the Lean elaborator’s line and column span back through the authoring header offset to report the exact location in src/Main.lean:
error: src/Main.lean:7:35: failed to synthesize instance HAdd Nat String ?m
|
7 | def broken (n : Nat) : Nat := n + "oops"
| ^^^^^^And if the agent runs lema add --json src/Main.lean, it receives a structured errors array:
{
"status": "error",
"errors": [
{
"file": "src/Main.lean",
"line": 7,
"column": 35,
"message": "failed to synthesize instance HAdd Nat String ?m"
}
]
}Because a failed lema add is atomic, no partial or broken objects ever enter .lema/store, and your previous accepted Hello.greeting binding remains untouched.
6. Render stored declarations in Pythonic syntax (lema view)
At any time, you can render any accepted declaration in .lema/store back into canonical Pythonic .lema syntax using lema view:
lema view Hello.advance Hello.Advancesdef Hello.advance(n: Nat) -> Nat:
n + 1
def Hello.Advances(f: Nat -> Nat) -> Prop:
forall n: Nat :: f(n) > nRemember that lema view does not read src/Counter.lema from disk. It reconstructs the Pythonic .lema text directly from the canonical content-addressed objects in .lema/store (crates/lema-core/src/view.rs) and runs view::rederive to confirm that parsing the rendered text reproduces the exact stored object IDs. Even if a declaration was originally written in Lean (src/Main.lean), lema view renders its signature and specification in the same Pythonic .lema format.
7. Check project status and commit your first semantic revision
Run lema status and lema diff to see what your workspace currently holds compared to the main reference:
lema status
lema diffref: main (unborn -> working tree)
exports (added: 3):
+ Hello.greeting #8f4a21c903e1 (def, checked)
+ Hello.advance #2c91e04b7a12 (def, checked)
+ Hello.Advances #7d18f53c90e4 (def, checked)
contracts (1 selected: 0 checked, 1 open):
~ select Hello.Advances(Hello.advance) [open]
target: forall n: Nat :: Hello.advance(n) > n
realizations: Hello.advance -> #2c91e04b7a12Notice how much clearer lema diff is than a line diff: it lists the three added exports by their semantic identity and shows that select Hello.Advances(Hello.advance) is currently open, pinned to Hello.advance at #2c91e04b7a12.
Save this initial state as a semantic revision on main using lema commit:
lema commit -m "Start hello project with greeting and open Counter contract"committed revision #5a30e91c4b28 to main (generation 2, 1 open obligation carried)Because the default local development policy on main allows committing revisions with carried open obligations while you iterate, lema commit snapshots your current workspace bindings (lema.binding-map/v1 and lema.snapshot/v2), records the open contract selection in revision.obligations, and advances main to generation 2.
Next
Continue to Lesson 4: Select a contract and prove it to see how you and your AI coding agent test and run code while a contract is open, prove single-law and multi-law contracts, and watch Store::check_witness turn an open selection into checked.
Lema v0.3.2 · Updated 29 October 2026 · Version and compatibility