Core C++

8.1. Interface🔗

Definition8.1.1
Group: The library, its interface and its intrinsics. (4)
Group member previews
Preview
Definition 8.1.2
Loading preview
Group member preview content is loaded from the rendered-fragment cache.
Statement uses 3
Statement dependency previews
Preview
Definition 1.4.4
Loading preview
Statement dependency preview content is loaded from the rendered-fragment cache.
Used by 4
Reverse dependency previews
Preview
Definition 8.1.2
Loading preview
Reverse dependency preview content is loaded from the rendered-fragment cache.
✓L∃∀N

An intrinsic L has a name, the number of its template parameters, and four judgments. The metavariable \bar{\tau} stands for its template arguments, u for a use, new, delete, a call, or a member such as operator[], and r for a value or a location.

| Judgment | Meaning | Lean | | --- | --- | --- | | \Gamma \vdash_L L\langle\bar{\tau}\rangle\ \mathsf{ok} | the instance is well formed | instOk | | \Gamma \vdash_L u : \tau_1 \times \cdots \times \tau_k \to \tau | the signature of the use u | sig | | \Gamma \vdash_L \tau \hookrightarrow L\langle\bar{\tau}\rangle | the type \tau converts to the instance | convFrom | | \sigma \vdash_L u(v_1, \ldots, v_k) \Rightarrow r, \sigma' | the result of the use u | eval |

The dynamic judgment may use one judgment of the language as a premise, the application of a closure of Definition 5.3.3. Three further marks say how the language treats the values of an instance. An object lives in the store and is reached by pointer, a type without a default is excluded from fields and elements, and a convertible type tells no two overloads apart.

Lean code for Definition8.1.1●6 definitions
  • inductive(4 constructors)defined in CoreCpp/Std/Intrinsic.lean
    complete
    inductive CoreCpp.Std.Use : Type
    inductive CoreCpp.Std.Use : Type
    The uses of an entity of the library that the language delegates. 
    CoreCpp.Std.Use.new : CoreCpp.Std.Use
    CoreCpp.Std.Use.member (m : String) : CoreCpp.Std.Use
    CoreCpp.Std.Use.call : CoreCpp.Std.Use
    CoreCpp.Std.Use.delete : CoreCpp.Std.Use
  • structure(3 fields)defined in CoreCpp/Std/Intrinsic.lean
    complete
    structure CoreCpp.Std.Sig : Type
    structure CoreCpp.Std.Sig : Type
    The signature of a use. The parameters take their arguments by value, and
    the mark `retLoc` says that the result is a location, as `v[i]` is. 
    params : List CoreCpp.Ty
    ret : CoreCpp.Ty
    retLoc : Bool
  • inductive(2 constructors)defined in CoreCpp/Std/Intrinsic.lean
    complete
    inductive CoreCpp.Std.Result : Type
    inductive CoreCpp.Std.Result : Type
    The result of a use, a value or a location. 
    CoreCpp.Std.Result.val (v : CoreCpp.Val) :
      CoreCpp.Std.Result
    CoreCpp.Std.Result.loc (l : CoreCpp.Loc) :
      CoreCpp.Std.Result
  • structure(8 fields)defined in CoreCpp/Std/Intrinsic.lean
    complete
    structure CoreCpp.Std.Statics : Type
    structure CoreCpp.Std.Statics : Type
    The static part of an intrinsic, the judgements the type checker uses. 
    name : String
    The qualified name of the declaration, `std::vector` or `assert`. 
    arity : Nat
    The number of template parameters, or of parameters for a function. 
    isObject : Bool
    The values of its instances live in the store and are never copied. 
    hasDefault : Bool
    Its instances have a default value, so a field or an element may have them. 
    convertible : Bool
    Another type converts to it, so a parameter of its type tells no two
    overloads apart. 
    instOk : List CoreCpp.Ty → Except String Unit
    Γ ⊢_L L⟨τ̄⟩ ok. 
    sig : CoreCpp.Std.Use → List CoreCpp.Ty → Except String CoreCpp.Std.Sig
    Γ ⊢_L u : τ̄ₐ → τ, at the template arguments τ̄. 
    convFrom : List CoreCpp.Ty → CoreCpp.Ty → Bool
    Γ ⊢_L τ ↪ L⟨τ̄⟩. 
  • structure(extends 1, 9 fields)defined in CoreCpp/Std/Intrinsic.lean
    complete
    structure CoreCpp.Std.Intrinsic : Type 1
    structure CoreCpp.Std.Intrinsic : Type 1
    An intrinsic, its static judgements and its dynamic one. The dynamic
    judgement is polymorphic in the monad of the evaluator, which passes the
    application of a closure as its first argument. 
    • CoreCpp.Std.Statics
    name : String
    Inherited from
    1. CoreCpp.Std.Statics
    arity : Nat
    Inherited from
    1. CoreCpp.Std.Statics
    isObject : Bool
    Inherited from
    1. CoreCpp.Std.Statics
    hasDefault : Bool
    Inherited from
    1. CoreCpp.Std.Statics
    convertible : Bool
    Inherited from
    1. CoreCpp.Std.Statics
    instOk : List CoreCpp.Ty → Except String Unit
    Inherited from
    1. CoreCpp.Std.Statics
    sig : CoreCpp.Std.Use → List CoreCpp.Ty → Except String CoreCpp.Std.Sig
    Inherited from
    1. CoreCpp.Std.Statics
    convFrom : List CoreCpp.Ty → CoreCpp.Ty → Bool
    Inherited from
    1. CoreCpp.Std.Statics
    eval : {m : Type → Type} →
      [Monad m] →
        [MonadExcept CoreCpp.Error m] →
          (CoreCpp.Val → List CoreCpp.Val → CoreCpp.Store → m (CoreCpp.Val × CoreCpp.Store)) →
            CoreCpp.Std.Use →
              List CoreCpp.Ty → List CoreCpp.Val → CoreCpp.Store → m (String × CoreCpp.Std.Result × CoreCpp.Store)
    σ ⊢_L u(v̄) ⇒ r, σ′, with the name of the rule that gives it. 
  • defdefined in CoreCpp/Std.lean
    complete
    def CoreCpp.Std.library : List CoreCpp.Std.Intrinsic
    def CoreCpp.Std.library :
      List CoreCpp.Std.Intrinsic
    The intrinsics, one per declaration of a header. 
Definition8.1.2
Group: The library, its interface and its intrinsics. (4)
Group member previews
Preview
Definition 8.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.5
Loading preview
Statement dependency preview content is loaded from the rendered-fragment cache.
Used by 4
Reverse dependency previews
Preview
Definition 3.6.5
Loading preview
Reverse dependency preview content is loaded from the rendered-fragment cache.
✓L∃∀N

The type checker rewrites every use of the library into the node L.u(e_1, \ldots, e_k), with the receiver as e_1 for a member. A call of a declared function is the use call, a call through a value of an instance is operator(), and indexing is operator[]. The evaluator has one rule for the node, whatever L. The arguments are values, evaluated left to right.

\dfrac{\Gamma \vdash_L L\langle\bar{\tau}\rangle\ \mathsf{ok} \qquad \Gamma \vdash_L \mathtt{new} : \tau_1 \times \cdots \times \tau_k \to \tau \qquad \Gamma \vdash e_i \lhd \tau_i}{\Gamma \vdash \mathtt{new}\ L\langle\bar{\tau}\rangle(e_1, \ldots, e_k) : \tau}\;\textsf{(T-NewLib)}

\dfrac{f \text{ declared by a header} \qquad \Gamma \vdash_f \mathtt{call} : \tau_1 \times \cdots \times \tau_k \to \tau \qquad \Gamma \vdash e_i \lhd \tau_i}{\Gamma \vdash f(e_1, \ldots, e_k) : \tau}\;\textsf{(T-CallLib)}

\dfrac{\Gamma \vdash e : L\langle\bar{\tau}\rangle \qquad \Gamma \vdash_L \mathtt{operator[]} : \tau_1 \to \tau \qquad \Gamma \vdash i \lhd \tau_1}{\Gamma \vdash e[i] : \tau}\;\textsf{(T-IndexLib)}

\dfrac{\begin{array}{c} \Gamma \vdash e : L\langle\bar{\tau}\rangle\qquad \Gamma \vdash_L \mathtt{operator[]} : \tau_1 \to \tau, \text{ a location} \\ \Gamma \vdash i \lhd \tau_1 \end{array}}{\Gamma \vdash_{\ell} e[i] : \tau}\;\textsf{(T-LocIndexLib)}

\dfrac{\rho, \sigma_{i-1} \vdash e_i \Rightarrow v_i, \sigma_i \quad (1 \le i \le k) \qquad \sigma_k \vdash_L \mathtt{new}\langle\bar{\tau}\rangle(v_1, \ldots, v_k) \Rightarrow v, \sigma'}{\rho, \sigma_0 \vdash \mathtt{new}\ L\langle\bar{\tau}\rangle(e_1, \ldots, e_k) \Rightarrow v, \sigma'}\;\textsf{(NewLib)}

\dfrac{\rho, \sigma_{i-1} \vdash e_i \Rightarrow v_i, \sigma_i \quad (1 \le i \le k) \qquad \sigma_k \vdash_L u(v_1, \ldots, v_k) \Rightarrow r, \sigma'}{\rho, \sigma_0 \vdash L.u(e_1, \ldots, e_k) \Rightarrow v, \sigma'}\;\textsf{(Lib)}

In the rule Lib the result is v = r for a value and v = \sigma'(r) for a location. With the same premises, a use that gives a location denotes it.

\dfrac{\rho, \sigma_{i-1} \vdash e_i \Rightarrow v_i, \sigma_i \quad (1 \le i \le k) \qquad \sigma_k \vdash_L u(v_1, \ldots, v_k) \Rightarrow \ell, \sigma'}{\rho, \sigma_0 \vdash L.u(e_1, \ldots, e_k) \Rightarrow_{\ell} \ell, \sigma'}\;\textsf{(LocLib)}

The command delete e on a pointer to an instance frees what the intrinsic owns and then the location of the instance.

\dfrac{\begin{array}{c} \rho, \sigma \vdash e \Rightarrow \mathsf{loc}\,\ell, \sigma_0\qquad \sigma_0(\ell) = \mathsf{lib}\,L\,\bar{\ell} \\ \sigma_0 \vdash_L \mathtt{delete}(\sigma_0(\ell)) \Rightarrow \mathsf{void}, \sigma_1 \end{array}}{\rho, \sigma \vdash \mathtt{delete}\ e \Rightarrow \mathsf{normal}, \rho, \sigma_1 \setminus \{\ell\}}\;\textsf{(DeleteLib)}

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 Definition8.1.2●6 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/Typing.lean
    complete
    opaque CoreCpp.Typing.lval (p : CoreCpp.Program) (Γ : CoreCpp.TEnv) :
      CoreCpp.Expr → CoreCpp.T CoreCpp.Ty
    opaque CoreCpp.Typing.lval (p : CoreCpp.Program)
      (Γ : CoreCpp.TEnv) :
      CoreCpp.Expr → CoreCpp.T CoreCpp.Ty
    The expressions that denote a location, with their type. A variable, a
    dereferenced pointer, a field of an object, a field through a pointer, and a
    use of the library or a member that gives a location. A variable captured by copy inside a lambda denotes no
    writable location.
    
    ```
    Γ(x) = τ, x not captured   Γ ⊢ e : τ*       Γ ⊢ e : C, C has τ f
    ──────────────────────── (T-LocVar)  ─────────── (T-LocDeref)  ────────────────── (T-LocField)
    Γ ⊢ₗ x : τ                 Γ ⊢ₗ *e : τ      Γ ⊢ₗ e.f : τ
    ```
    
    ```
    Γ ⊢ e : C*, C has τ f
    ────────────────────── (T-LocArrow)
    Γ ⊢ₗ e->f : τ
    
    Γ ⊢ e : L⟨τ̄⟩    Γ ⊢_L operator[] : τ₁ → τ, a location    Γ ⊢ i ◁ τ₁
    ─────────────────────────────────────────────────────────────────── (T-LocIndexLib)
    Γ ⊢ₗ e[i] : τ
    ```
    
  • defdefined in CoreCpp/Typing.lean
    complete
    def CoreCpp.Typing.annotate (p₀ : CoreCpp.Program) :
      CoreCpp.T CoreCpp.Program
    def CoreCpp.Typing.annotate
      (p₀ : CoreCpp.Program) :
      CoreCpp.T CoreCpp.Program
    The program with the static classes, the chosen overloads and the
    reference returns filled in. Fails only on an ill typed program, which
    `check` rejects first. 
  • 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, σ' 
  • opaquedefined in CoreCpp/Eval.lean
    complete
    opaque CoreCpp.Eval.lval (fs : CoreCpp.FunEnv) (ρ : CoreCpp.Env)
      (σ : CoreCpp.Store) (e : CoreCpp.Expr) :
      CoreCpp.M (CoreCpp.Loc × CoreCpp.Store)
    opaque CoreCpp.Eval.lval (fs : CoreCpp.FunEnv)
      (ρ : CoreCpp.Env) (σ : CoreCpp.Store)
      (e : CoreCpp.Expr) :
      CoreCpp.M (CoreCpp.Loc × CoreCpp.Store)
    ρ, σ ⊢ e ⇒ₗ ℓ, σ', the expressions that denote a location. A variable, a
    dereferenced pointer, a field of an object, a field through a pointer and an
    element of a vector.
    
    ```
    ρ(x) = ℓ                     ρ, σ ⊢ e ⇒ loc ℓ, σ'
    ──────────────────── (LocVar)  ────────────────────── (LocDeref)    nullptr is error
    ρ, σ ⊢ x ⇒ₗ ℓ, σ             ρ, σ ⊢ *e ⇒ₗ ℓ, σ'
    ```
    
    ```
    ρ, σ ⊢ e ⇒ₗ ℓ, σ'    σ'(ℓ) = obj C [… f ↦ ℓ_f …]
    ─────────────────────────────────────────────── (LocField)
    ρ, σ ⊢ e.f ⇒ₗ ℓ_f, σ'
    ```
    
    ```
    ρ, σ ⊢ e ⇒ loc ℓ, σ'    σ'(ℓ) = obj C [… f ↦ ℓ_f …]
    ────────────────────────────────────────────────── (LocArrow)    e->f is (*e).f
    ρ, σ ⊢ e->f ⇒ₗ ℓ_f, σ'
    ```
    
    An element of a vector is a use of the library, the rule LocLib.
    
  • opaquedefined in CoreCpp/Eval.lean
    complete
    opaque CoreCpp.Eval.libUse (fs : CoreCpp.FunEnv) (ρ : CoreCpp.Env)
      (σ : CoreCpp.Store) (n : String) (u : CoreCpp.Std.Use)
      (ts : List CoreCpp.Ty) (vs : List CoreCpp.Val) :
      CoreCpp.M (CoreCpp.Std.Result × CoreCpp.Store)
    opaque CoreCpp.Eval.libUse (fs : CoreCpp.FunEnv)
      (ρ : CoreCpp.Env) (σ : CoreCpp.Store)
      (n : String) (u : CoreCpp.Std.Use)
      (ts : List CoreCpp.Ty)
      (vs : List CoreCpp.Val) :
      CoreCpp.M
        (CoreCpp.Std.Result × CoreCpp.Store)
    σ ⊢_L u(v̄) ⇒ r, σ′, the premise of the library, with the application of a
    closure as the one judgement of the language it may use.