Core C++

4.2. Local reference🔗

Definition4.2.1
Group: Variables, update and commands. (9)
Group member previews
Preview
Definition 4.1.1
Loading preview
Group member preview content is loaded from the rendered-fragment cache.
Statement uses 4
Statement dependency previews
Preview
Definition 2.1.4
Loading preview
Statement dependency preview content is loaded from the rendered-fragment cache.
used by 0✓L∃∀N

The local reference τ& y = e binds a second name to the location e denotes, without allocating.

The initialiser must denote a location and have the declared type, and the reference has in \Gamma the type of its referent, because every read and write through it is a read or write at the referent.

The binding is an alias, not owned, so the exit of the block that declared the reference leaves the location in \sigma.

A reference to a temporary, to nullptr or to an expression without a location does not exist, and a dangling reference is impossible by construction, because a reference names only locations of enclosing blocks or of objects in the store.

\dfrac{\Gamma \vdash_{\ell} e : \tau \qquad \tau \text{ has values}}{\Gamma \vdash \tau\mathtt{\&}\ x = e \dashv \Gamma[x \mapsto \tau]}\;\textsf{(T-DeclRef)}

\dfrac{\rho, \sigma \vdash e \Rightarrow_{\ell} \ell, \sigma'}{\rho, \sigma \vdash \tau\mathtt{\&}\ x = e \Rightarrow \mathsf{normal}, \rho[x \mapsto \ell], \sigma'}\;\textsf{(DeclRef)}

Some of the implementations are partial, so Lean records opaque constants that carry the type and not the body, and no property of them is proved here.

Lean code for Definition4.2.1●3 definitions
  • opaquedefined in CoreCpp/Typing.lean
    complete
    opaque CoreCpp.Typing.cmd (p : CoreCpp.Program) (τᵣ : CoreCpp.Ty)
      (Γ : CoreCpp.TEnv) : CoreCpp.Cmd → CoreCpp.T CoreCpp.TEnv
    opaque CoreCpp.Typing.cmd (p : CoreCpp.Program)
      (τᵣ : CoreCpp.Ty) (Γ : CoreCpp.TEnv) :
      CoreCpp.Cmd → CoreCpp.T CoreCpp.TEnv
    Γ ⊢ c ⊣ Γ', under the return type τᵣ of the enclosing function. 
  • opaquedefined in CoreCpp/Eval.lean
    complete
    opaque CoreCpp.Eval.cmd (fs : CoreCpp.FunEnv) (ρ : CoreCpp.Env)
      (σ : CoreCpp.Store) (c : CoreCpp.Cmd) :
      CoreCpp.M (CoreCpp.Ctrl × CoreCpp.Env × CoreCpp.Store)
    opaque CoreCpp.Eval.cmd (fs : CoreCpp.FunEnv)
      (ρ : CoreCpp.Env) (σ : CoreCpp.Store)
      (c : CoreCpp.Cmd) :
      CoreCpp.M
        (CoreCpp.Ctrl ×
          CoreCpp.Env × CoreCpp.Store)
    ρ, σ ⊢ c ⇒ r, ρ', σ' 
  • defdefined in CoreCpp/Semantics.lean
    complete
    def CoreCpp.Env.alias (ρ : CoreCpp.Env) (x : String) (l : CoreCpp.Loc) :
      CoreCpp.Env
    def CoreCpp.Env.alias (ρ : CoreCpp.Env)
      (x : String) (l : CoreCpp.Loc) :
      CoreCpp.Env
    ρ[x ↦ ℓ] for a reference, a binding to a location that already exists.