7.1. Overloading
-
CoreCpp.sigOf[complete] -
CoreCpp.Program.funsNamed[complete] -
CoreCpp.Program.findMethods[complete] -
CoreCpp.Typing.distinguishable[complete]
A name denotes an overload set, the functions of the program with that name, or the methods of that name along the chain of a class, one per signature. A method of a derived class replaces the one of a base with the same signature, and a different signature is another overload, so the subset has no name hiding. Two declarations of one name live together only when they differ.
\dfrac{\text{arities differ, or } \exists i.\ p_i \neq q_i \text{ and neither } p_i \text{ nor } q_i \text{ is convertible}}{\text{the two declarations are overloads}}\;\textsf{(Distinguishable)}
A type is convertible when the library marks it so, as std::function of Definition 8.2.2. The restriction is the check 5 of the design. It keeps a lambda argument from deciding a call, which would ask for the type of the lambda before the candidate that gives it is known.
Lean code for Definition7.1.1●4 definitions
Associated Lean declarations
-
CoreCpp.sigOf[complete]
-
CoreCpp.Program.funsNamed[complete]
-
CoreCpp.Program.findMethods[complete]
-
CoreCpp.Typing.distinguishable[complete]
-
CoreCpp.sigOf[complete] -
CoreCpp.Program.funsNamed[complete] -
CoreCpp.Program.findMethods[complete] -
CoreCpp.Typing.distinguishable[complete]
-
defdefined in CoreCpp/Syntax.leancomplete
def CoreCpp.sigOf (ps : List CoreCpp.Param) : List CoreCpp.Ty
def CoreCpp.sigOf (ps : List CoreCpp.Param) : List CoreCpp.Ty
The signature of a list of parameters, the types alone, which names one member of an overload set.
-
defdefined in CoreCpp/Syntax.leancomplete
def CoreCpp.Program.funsNamed (p : CoreCpp.Program) (f : String) : List CoreCpp.Fun
def CoreCpp.Program.funsNamed (p : CoreCpp.Program) (f : String) : List CoreCpp.Fun
The overload set of the functions named f.
-
defdefined in CoreCpp/Syntax.leancomplete
def CoreCpp.Program.findMethods (p : CoreCpp.Program) (c m : String) : List (CoreCpp.Method × String)
def CoreCpp.Program.findMethods (p : CoreCpp.Program) (c m : String) : List (CoreCpp.Method × String)
The overload set of the method m as seen from class c, one method per signature, the nearest in the chain of c, each with the class that declares it. A method of a derived class replaces the one of a base with the same signature, and a different signature is another overload, without the name hiding of C++.
-
defdefined in CoreCpp/Typing.leancomplete
def CoreCpp.Typing.distinguishable (ps qs : List CoreCpp.Param) : Bool
def CoreCpp.Typing.distinguishable (ps qs : List CoreCpp.Param) : Bool
Two overloads of one name are declared together only when they differ in arity or in a parameter of a type other than `std::function`, the check 5 of the design. The restriction keeps a lambda argument from deciding a call, which would ask for the type of the lambda before the candidate is known. ``` arities differ, or ∃ i. pᵢ ≠ qᵢ and neither pᵢ nor qᵢ is std::function ──────────────────────────────────────────────────────────────────── (Distinguishable) the two declarations are overloads ```
-
CoreCpp.Typing.pickOverload[complete] -
CoreCpp.Typing.resolveFun[complete] -
CoreCpp.Typing.resolveMethod[complete]
A call collects the candidates of its arity that accept the arguments, each argument by value acceptable at the type of its parameter and each argument of a reference parameter denoting a location of that type. The choice is the exact candidate when several accept.
\dfrac{\begin{array}{c} A = \text{the candidates of } \mathrm{cand}(f, k) \text{ that accept } e_1, \ldots, e_k \\ A \text{ has one element whose parameters are} \\ \text{exactly the types of the arguments, or } A \text{ is a singleton} \end{array}}{\text{the call of } f \text{ selects that candidate}}\;\textsf{(T-Overload)}
With A empty the error is the one of the single candidate of that arity, when there is one, and no overload otherwise. With two or more in A and no exact one the call is ambiguous. Core C++ does not rank conversion sequences as C++ does, so the rule fits in one line on the board and the error is predictable.
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 Definition7.1.2●3 definitions
Associated Lean declarations
-
CoreCpp.Typing.pickOverload[complete]
-
CoreCpp.Typing.resolveFun[complete]
-
CoreCpp.Typing.resolveMethod[complete]
-
CoreCpp.Typing.pickOverload[complete] -
CoreCpp.Typing.resolveFun[complete] -
CoreCpp.Typing.resolveMethod[complete]
-
opaquedefined in CoreCpp/Typing.leancomplete
opaque CoreCpp.Typing.pickOverload (p : CoreCpp.Program) (Γ : CoreCpp.TEnv) (who : String) (cands : List (List CoreCpp.Param)) (es : List CoreCpp.Expr) : CoreCpp.T Nat
opaque CoreCpp.Typing.pickOverload (p : CoreCpp.Program) (Γ : CoreCpp.TEnv) (who : String) (cands : List (List CoreCpp.Param)) (es : List CoreCpp.Expr) : CoreCpp.T Nat
The candidate of an overload set that a call selects, by its index. The candidates that accept the arguments are collected, and the choice is the exact one when several accept. ``` A = the candidates of cand(f, k) that accept e₁, …, eₖ A has one element whose parameters are exactly the types of the arguments, or A is a singleton ─────────────────────────────────────────────────────────────────────────────── (T-Overload) the call of f selects that candidate ``` With A empty the error is the one of the single candidate of that arity, when there is one, and `no overload` otherwise. With two or more in A and no exact one the call is ambiguous. Core C++ does not rank conversion sequences, so the rule fits in one line on the board.
-
opaquedefined in CoreCpp/Typing.leancomplete
opaque CoreCpp.Typing.resolveFun (p : CoreCpp.Program) (Γ : CoreCpp.TEnv) (f : String) (es : List CoreCpp.Expr) : CoreCpp.T CoreCpp.Fun
opaque CoreCpp.Typing.resolveFun (p : CoreCpp.Program) (Γ : CoreCpp.TEnv) (f : String) (es : List CoreCpp.Expr) : CoreCpp.T CoreCpp.Fun
The function a call selects out of the overload set of its name.
-
opaquedefined in CoreCpp/Typing.leancomplete
opaque CoreCpp.Typing.resolveMethod (p : CoreCpp.Program) (Γ : CoreCpp.TEnv) (recv : CoreCpp.Expr) (arrow : Bool) (m : String) (es : List CoreCpp.Expr) : CoreCpp.T (CoreCpp.Method × String)
opaque CoreCpp.Typing.resolveMethod (p : CoreCpp.Program) (Γ : CoreCpp.TEnv) (recv : CoreCpp.Expr) (arrow : Bool) (m : String) (es : List CoreCpp.Expr) : CoreCpp.T (CoreCpp.Method × String)
The method a call selects, with the class that declares it, the rules T-Method and T-MethodArrow with the overload set of the name.
-
CoreCpp.FunEnv.lookupSig[complete] -
CoreCpp.Eval.resolve[complete] -
CoreCpp.Typing.annotate[complete]
Overloading adds no evaluation rule. The type checker writes the signature of the chosen candidate into the tree, the field sig of a call, and the evaluator looks a function or a method up by name and signature instead of by name alone. A program that was not annotated, which the checker rejects first, runs with the first candidate of the name.
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 Definition7.1.3●3 definitions
Associated Lean declarations
-
CoreCpp.FunEnv.lookupSig[complete]
-
CoreCpp.Eval.resolve[complete]
-
CoreCpp.Typing.annotate[complete]
-
CoreCpp.FunEnv.lookupSig[complete] -
CoreCpp.Eval.resolve[complete] -
CoreCpp.Typing.annotate[complete]
-
defdefined in CoreCpp/Semantics.leancomplete
def CoreCpp.FunEnv.lookupSig (fs : CoreCpp.FunEnv) (f : String) (sig : Option (List CoreCpp.Ty)) : Option CoreCpp.Fun
def CoreCpp.FunEnv.lookupSig (fs : CoreCpp.FunEnv) (f : String) (sig : Option (List CoreCpp.Ty)) : Option CoreCpp.Fun
The function named f with the signature the type checker chose, or the first one of the name when the program was not annotated.
-
defdefined in CoreCpp/Eval.leancomplete
def CoreCpp.Eval.resolve (fs : CoreCpp.FunEnv) (s t m : String) (sig : Option (List CoreCpp.Ty)) : CoreCpp.M (CoreCpp.Method × String)
def CoreCpp.Eval.resolve (fs : CoreCpp.FunEnv) (s t m : String) (sig : Option (List CoreCpp.Ty)) : CoreCpp.M (CoreCpp.Method × String)
The method m of class s with the signature the type checker chose, the nearest in the chain of s, dispatched by the tag t when that method is virtual. The signature picks one member of an overload set, and without it, in an unannotated program, the first of the name serves.
-
defdefined in CoreCpp/Typing.leancomplete
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.