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:
registerJoinPointcreates?H : Unit → Nat → Propand binds__do_jp_spec : ∀ r x, ?H r x → ⊤ ⊑ wp⟦__do_jp r x⟧ Qin the goal of theif. It adds the goal∀ r x, ?H r x → ⊤ ⊑ wp⟦pure (x + 1)⟧ Qfor the join RHS.- In the
thenbranch, under the hypothesish₁ : n > 0,wpJump?closes the goal⊤ ⊑ wp⟦__do_jp () 1⟧ Qwith__do_jp_spec () 1 ?pf₁and records the jump. Theelsebranch records the jump__do_jp () 2underh₂ : ¬n > 0with?pf₂. vcgenreaches the RHS goal after all goals of the join body, so both jumps are known by then.finalizeJoinPointsets?Hto the disjunction of the jumps, each over the locals of its branch and its arguments other thanr : 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 anelse ifchain 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.