Core C++

5.1. Call🔗

Definition5.1.1
Group: Functions, parameters and calls. (9)
Group member previews
Preview
Definition 5.2.1
Loading preview
Group member preview content is loaded from the rendered-fragment cache.
Statement uses 5
Statement dependency previews
Preview
Definition 2.1.5
Loading preview
Statement dependency preview content is loaded from the rendered-fragment cache.
Used by 6
Reverse dependency previews
Preview
Definition 4.7.1
Loading preview
Reverse dependency preview content is loaded from the rendered-fragment cache.
✓L∃∀N

Let f \mapsto \tau\ f(\tau_1\,x_1, \ldots, \tau_k\,x_k)\ \{c\} be in the program. The environment of the function holds only the parameters, because there are no global variables.

Each argument is checked at the type of its parameter by the judgment \Gamma \vdash e \lhd \tau of Definition 5.3.1, so that a lambda may be an argument. A variable f of function type in \Gamma hides the function named f, and the call is then Definition 5.3.3.

\dfrac{f \notin \Gamma \qquad \Gamma \vdash e_i \lhd \tau_i \quad (1 \le i \le k)}{\Gamma \vdash f(e_1, \ldots, e_k) : \tau}\;\textsf{(T-Call)}

\dfrac{\begin{array}{c} \rho, \sigma_{i-1} \vdash e_i \Rightarrow v_i, \sigma_i \quad (1 \le i \le k,\ \sigma_0 = \sigma) \\ (\ell_i, \sigma'_i) = \mathrm{alloc}(\sigma'_{i-1}, v_i) \quad (1 \le i \le k,\ \sigma'_0 = \sigma_k) \\ [x_1 \mapsto \ell_1, \ldots, x_k \mapsto \ell_k], \sigma'_k \vdash c \Rightarrow \mathsf{ret}\,v, \rho', \sigma'' \end{array}}{\rho, \sigma \vdash f(e_1, \ldots, e_k) \Rightarrow v, \sigma'' \setminus (\{\ell_1, \ldots, \ell_k\} \cup (\rho' \setminus \rho_f))}\;\textsf{(Call)}

The return frees the copies of the arguments and the locals the body declared, \rho' \setminus \rho_f read as the owned bindings the body added, so after a call the store holds only what existed before it and the objects created with new.

When the body ends with \mathsf{normal}, the result is \mathsf{void} if \tau = \mathsf{void} and error otherwise. A missing return in a non void function is an evaluation error, not a type error.

The rule above is the case in which every parameter is by value, and Definition 5.2.1 gives the case of a parameter by reference.

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 Definition5.1.1●4 definitions
  • opaquedefined in CoreCpp/Typing.lean
    complete
    opaque CoreCpp.Typing.expr (p : CoreCpp.Program) (Γ : CoreCpp.TEnv) :
      CoreCpp.Expr → CoreCpp.T CoreCpp.Ty
    opaque CoreCpp.Typing.expr (p : CoreCpp.Program)
      (Γ : CoreCpp.TEnv) :
      CoreCpp.Expr → CoreCpp.T CoreCpp.Ty
    Γ ⊢ e : τ 
  • opaquedefined in CoreCpp/Eval.lean
    complete
    opaque CoreCpp.Eval.expr (fs : CoreCpp.FunEnv) (ρ : CoreCpp.Env)
      (σ : CoreCpp.Store) (e : CoreCpp.Expr) :
      CoreCpp.M (CoreCpp.Val × CoreCpp.Store)
    opaque CoreCpp.Eval.expr (fs : CoreCpp.FunEnv)
      (ρ : CoreCpp.Env) (σ : CoreCpp.Store)
      (e : CoreCpp.Expr) :
      CoreCpp.M (CoreCpp.Val × CoreCpp.Store)
    ρ, σ ⊢ e ⇒ v, σ' 
  • structure(4 fields)defined in CoreCpp/Syntax.lean
    complete
    structure CoreCpp.Fun : Type
    structure CoreCpp.Fun : Type
    ret : CoreCpp.Ty
    name : String
    params : List CoreCpp.Param
    body : List CoreCpp.Cmd
  • structure(3 fields)defined in CoreCpp/Syntax.lean
    complete
    structure CoreCpp.Param : Type
    structure CoreCpp.Param : Type
    A parameter. `byRef` marks a `τ&` parameter, bound to the location of the
    argument instead of to a fresh copy. 
    ty : CoreCpp.Ty
    name : String
    byRef : Bool