Core C++

5.5. Function and program🔗

Definition5.5.1
Group: Functions, parameters and calls. (9)
Group member previews
Preview
Definition 5.1.1
Loading preview
Group member preview content is loaded from the rendered-fragment cache.
uses 1
Used by 2
Reverse dependency previews
Preview
Definition 5.5.2
Loading preview
Reverse dependency preview content is loaded from the rendered-fragment cache.
✓L∃∀N

A function is well typed when its body is, under the context of its parameters and its return type.

\dfrac{[x_1 \mapsto \tau_1, \ldots, x_k \mapsto \tau_k] \vdash c \dashv \Gamma'}{\vdash \tau\ f(\tau_1\,x_1, \ldots, \tau_k\,x_k)\ \{c\}}\;\textsf{(T-Fun)}

Lean code for Definition5.5.1●1 definition
  • defdefined in CoreCpp/Typing.lean
    complete
    def CoreCpp.Typing.fn (p : CoreCpp.Program) (f : CoreCpp.Fun) :
      CoreCpp.T Unit
    def CoreCpp.Typing.fn (p : CoreCpp.Program)
      (f : CoreCpp.Fun) : CoreCpp.T Unit
    A function is well typed when its parameter and return types have values
    and are well formed, and its body is, under the context of its parameters and
    its return type. A reference parameter has in Γ the type of its referent, as a
    local reference does.
    
    ```
    τ, τᵢ storable and well formed    [x₁ ↦ τ₁, …, xₖ ↦ τₖ] ⊢ c ⊣ Γ'
    ──────────────────────────────────────────────────────────────── (T-Fun)
    ⊢ τ f (τ₁ x₁, …, τₖ xₖ) { c }
    ```
    
Definition5.5.2
Group: Functions, parameters and calls. (9)
Group member previews
Preview
Definition 5.1.1
Loading preview
Group member preview content is loaded from the rendered-fragment cache.
Statement uses 2
Statement dependency previews
Preview
Definition 5.1.1
Loading preview
Statement dependency preview content is loaded from the rendered-fragment cache.
used by 0✓L∃∀N

A program is a list of functions with distinct names and a function int main(). The initial store is empty, because there are no global variables, and the result is the value of main().

\dfrac{\begin{array}{c} \text{distinct names} \qquad \vdash f_i \ \text{for each } i \\ \mathtt{main} \mapsto \mathtt{int\ main()}\ \{c\} \end{array}}{\vdash p}\;\textsf{(T-Program)}

\dfrac{[\,], \emptyset \vdash \mathtt{main}() \Rightarrow v, \sigma}{p \Rightarrow v}\;\textsf{(Program)}

Lean code for Definition5.5.2●4 definitions
  • abbrevdefined in CoreCpp/Syntax.lean
    complete
    abbrev CoreCpp.Program : Type
    abbrev CoreCpp.Program : Type
    A program is a list of declarations. There are no global variables. 
  • defdefined in CoreCpp/Typing.lean
    complete
    def CoreCpp.check (p₀ : CoreCpp.Program) : Except CoreCpp.TypeError Unit
    def CoreCpp.check (p₀ : CoreCpp.Program) :
      Except CoreCpp.TypeError Unit
    A program is well typed when class and function names are distinct up to
    overloading, every class and function is well typed and `int main()` exists.
    A template is not checked, only its instantiations are.
    
    ```
    names distinct up to overloading    ⊢ Cᵢ for each class    ⊢ fᵢ for each function
    main ↦ (int main() { c })
    ────────────────────────────────────────────────────────────────────────────── (T-Program)
    ⊢ p
    ```
    
  • defdefined in CoreCpp/Eval.lean
    complete
    def CoreCpp.runWith (trace : Bool) (p : CoreCpp.Program) :
      Except CoreCpp.Error CoreCpp.Val × Array CoreCpp.TraceEntry
    def CoreCpp.runWith (trace : Bool)
      (p : CoreCpp.Program) :
      Except CoreCpp.Error CoreCpp.Val ×
        Array CoreCpp.TraceEntry
    Program execution. The initial store is empty, because there are no global
    variables, and the result is the value returned by `main()`.
    
    ```
    main ↦ (int main() { c })    [], ∅ ⊢ main() ⇒ v, σ
    ─────────────────────────────────────────────────── (Program)
    p ⇒ v
    ```
    
    The objects created with new stay in σ until delete or the end of the
    program. The locals of main leave σ with the return of the call, so the
    final store holds objects only. The program is annotated with the static
    classes of method calls and deletes before it runs, when it is well typed,
    and otherwise runs as parsed, with every dispatch by the class tag.
    
  • defdefined in CoreCpp/Eval.lean
    complete
    def CoreCpp.run (p : CoreCpp.Program) : Except CoreCpp.Error CoreCpp.Val
    def CoreCpp.run (p : CoreCpp.Program) :
      Except CoreCpp.Error CoreCpp.Val