7.3. Parametric polymorphism
-
CoreCpp.Decl[complete] -
CoreCpp.Program.templates[complete] -
CoreCpp.classType[complete]
A class template is a class with a type parameter, template<typename T> class C, and it is instantiated only in type position, C<int>. The name of an instantiation is the chain the design prints for the type, so the name in the source and the name of the expanded class agree, and the parser builds it when it reads a type. One type parameter, by the grammar, which is the check 3 of the design.
Lean code for Definition7.3.1●3 definitions
Associated Lean declarations
-
CoreCpp.Decl[complete]
-
CoreCpp.Program.templates[complete]
-
CoreCpp.classType[complete]
-
CoreCpp.Decl[complete] -
CoreCpp.Program.templates[complete] -
CoreCpp.classType[complete]
-
inductivedefined in CoreCpp/Syntax.leancomplete
inductive CoreCpp.Decl : Type
inductive CoreCpp.Decl : Type
Top level declarations. A `tmpl T C` is a class template, the class `C` with the type parameter `T`, which is never checked and never run. Only its instantiations are, and `Templates.instantiate` adds one class per instantiation the program mentions. A `libTmpl L Ts` is a class template of the library and a `libFn f τ ps` a function of the library, both declared without a body and implemented by an intrinsic.
Constructors
CoreCpp.Decl.cls (c : CoreCpp.ClassDecl) : CoreCpp.Decl
CoreCpp.Decl.fn (f : CoreCpp.Fun) : CoreCpp.Decl
CoreCpp.Decl.tmpl (param : String) (c : CoreCpp.ClassDecl) : CoreCpp.Decl
CoreCpp.Decl.libTmpl (name : String) (params : List String) : CoreCpp.Decl
CoreCpp.Decl.libFn (name : String) (ret : CoreCpp.Ty) (params : List CoreCpp.Param) : CoreCpp.Decl
-
defdefined in CoreCpp/Syntax.leancomplete
def CoreCpp.Program.templates (p : CoreCpp.Program) : List (String × CoreCpp.ClassDecl)
def CoreCpp.Program.templates (p : CoreCpp.Program) : List (String × CoreCpp.ClassDecl)
The class templates of the program, each with its type parameter.
-
opaquedefined in CoreCpp/Parser.leancomplete
opaque CoreCpp.classType : CoreCpp.P CoreCpp.Ty
opaque CoreCpp.classType : CoreCpp.P CoreCpp.Ty
A class name possibly qualified by namespaces and possibly instantiating a class template. ``` ClassType = NsId "::" QualTail | TypeId [ TemplateArgs ] ; ``` An unqualified name is read in the enclosing namespace, and a class template takes one template argument. The instantiation is named by the chain the design prints for the type, so `Stack<int>` in the source and the expanded class have the same name, and `Templates.instantiate` adds the class before the program is checked.
-
CoreCpp.Templates.instantiate[complete] -
CoreCpp.Templates.substClass[complete] -
CoreCpp.Templates.substTy[complete] -
CoreCpp.Templates.substName[complete]
Instantiation is substitution, and it precedes every other judgment.
\dfrac{\begin{array}{c} p \text{ has } \mathtt{template{<}typename\ T{>}\ class}\ C\qquad C{<}\tau{>} \text{ mentioned in } p \\ C{<}\tau{>} \notin \text{class table of } p \end{array}}{p \longrightarrow p, \mathtt{class}\ C{<}\tau{>}\ \{ \ldots [T := \tau] \ldots \}}\;\textsf{(Inst)}
The substitution replaces the type T in every field, parameter, result, local declaration, vector element and class name the body mentions, and it rewrites the name of a mentioned class in its own chain, so Node<T>* inside Lista<T> becomes Node<int>* in Lista<int>.
The rule applies to a fixed point, because the class it adds may mention another instantiation, and it is idempotent, so check and runWith may both apply it.
A template is never checked, only its instantiations are, as in C++, and two instantiations of one template are two independent classes with no subtype relation between them.
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.3.2●4 definitions
Associated Lean declarations
-
CoreCpp.Templates.instantiate[complete]
-
CoreCpp.Templates.substClass[complete]
-
CoreCpp.Templates.substTy[complete]
-
CoreCpp.Templates.substName[complete]
-
CoreCpp.Templates.instantiate[complete] -
CoreCpp.Templates.substClass[complete] -
CoreCpp.Templates.substTy[complete] -
CoreCpp.Templates.substName[complete]
-
opaquedefined in CoreCpp/Templates.leancomplete
opaque CoreCpp.Templates.instantiate (p : CoreCpp.Program) (fuel : Nat := 32) : Except String CoreCpp.Program
opaque CoreCpp.Templates.instantiate (p : CoreCpp.Program) (fuel : Nat := 32) : Except String CoreCpp.Program
Adds one class per instantiation the program mentions, to a fixed point. Idempotent, so `check` and `runWith` may both apply it.
-
defdefined in CoreCpp/Templates.leancomplete
def CoreCpp.Templates.substClass (param : String) (τ : CoreCpp.Ty) (c : CoreCpp.ClassDecl) : CoreCpp.ClassDecl
def CoreCpp.Templates.substClass (param : String) (τ : CoreCpp.Ty) (c : CoreCpp.ClassDecl) : CoreCpp.ClassDecl
The class of the template instantiated at τ, with the name the source writes for it.
-
opaquedefined in CoreCpp/Templates.leancomplete
opaque CoreCpp.Templates.substTy (param : String) (τ : CoreCpp.Ty) : CoreCpp.Ty → CoreCpp.Ty
opaque CoreCpp.Templates.substTy (param : String) (τ : CoreCpp.Ty) : CoreCpp.Ty → CoreCpp.Ty
`[T := τ]` on a type.
-
opaquedefined in CoreCpp/Templates.leancomplete
opaque CoreCpp.Templates.substName (param repl n : String) : String
opaque CoreCpp.Templates.substName (param repl n : String) : String
Substitutes the type parameter in a mangled class name, `[T := int]` on `Node<T>` giving `Node<int>`. The parameter may also be the whole name.