Documentation

Lean.Elab.Tactic.VCGen.JoinPoint

Join points (vcgen +jp) #

The do-elaborator shares the code after an if or a match through a join point. This module lets vcgen +jp prove that code once instead of once per branch. For example,

def f (n : Nat) : Id Nat := do
  let mut x := 0
  if n > 0 then x := 1 else x := 2
  return x + 1

elaborates to

have __do_jp := fun (r : Unit) (x : Nat) => pure (x + 1)
if n > 0 then __do_jp () 1 else __do_jp () 2

The join RHS fun r x => pure (x + 1) is the shared code, and the join body if n > 0 then … jumps to it. Without +jp, vcgen unfolds __do_jp at each jump, so k consecutive splits give 2ᵏ copies of the join RHS. With +jp, vcgen takes three steps:

  1. registerJoinPoint creates ?H : Unit → Nat → Prop and binds __do_jp_spec : ∀ r x, ?H r x → ⊤ ⊑ wp⟦__do_jp r x⟧ Q in the goal of the if. It adds the goal ∀ r x, ?H r x → ⊤ ⊑ wp⟦pure (x + 1)⟧ Q for the join RHS.
  2. In the then branch, under the hypothesis h₁ : n > 0, wpJump? closes the goal ⊤ ⊑ wp⟦__do_jp () 1⟧ Q with __do_jp_spec () 1 ?pf₁ and records the jump. The else branch records the jump __do_jp () 2 under h₂ : ¬n > 0 with ?pf₂.
  3. vcgen reaches the RHS goal after all goals of the join body, so both jumps are known by then. finalizeJoinPoint sets ?H to the disjunction of the jumps, each over the locals of its branch and its arguments other than r : Unit, fun r x => (n > 0 ∧ x = 1) ∨ (¬n > 0 ∧ x = 2). It proves ?pf₁ := Or.inl ⟨h₁, rfl⟩ and ?pf₂ := Or.inr ⟨h₂, rfl⟩. Jumps under a common local share its binder: the jumps of an else if chain share the conditions above them. A parameter that every jump passes the same term for gets its equation once, above the disjunction.

The RHS goal of step 1 thus receives the hypothesis

(n > 0 ∧ x = 1) ∨ (¬n > 0 ∧ x = 2)

This ?H is the strongest postcondition of the join body at its jumps to __do_jp, over the join parameters r and x.

For a stateful program, ?H also takes the states. In StateM Nat, with both jumps in the state t, it is s = t ∧ ((n > 0 ∧ x = 1) ∨ (¬n > 0 ∧ x = 2)).

Register the join point jp := val that wpLet? introduced into the goal goal : pre ⊑ wp⟦…⟧ post eposts ss over the states ss. Returns scope with jp, goal with __do_jp_spec : ∀ xs ss, ?H xs ss → ⊤ ⊑ wp⟦jp xs⟧ post eposts ss, and the goal ∀ xs ss, ?H xs ss → ⊤ ⊑ wp⟦val xs⟧ post eposts ss for the join RHS val. Returns none if jp is no __do_jp with a lambda RHS under +jp, or if the states have no base lattice instance.

Equations
  • One or more equations did not get rendered due to their size.
Instances For

    Close the goal ⊤ ⊑ wp⟦jp args⟧ post eposts ss of a jump to a join point of scope with __do_jp_spec args ss ?pf, where finalizeJoinPoint assigns ?pf : ?H args ss. A jump with a precondition other than ⊤ unfolds jp instead.

    Equations
    • One or more equations did not get rendered due to their size.
    Instances For

      Assign ?H of jp the equations of the parameters that all its jumps agree on, above the trie of the jumps, False if no jump reaches jp, and each jump's ?pf its proof. vcgen processes the RHS goal of jp after all goals below the registration, so every jump is known by then.

      Equations
      • One or more equations did not get rendered due to their size.
      Instances For