Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Expert skill for implementing parametric polymorphism including type parameter bounds, monomorphization, type erasure, variance, higher-kinded types, and associated types.
.claude/skills/a5c-ai-generics-implementation/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-01 | ✗→✓ | ▲ Improved | 40% | 0% |
| case-02 | ✗→✓ | ▲ Improved | 37% | 0% |
| case-03 | ✗→✓ | ▲ Improved | 28% | 0% |
| case-06 | ✗→✓ | ▲ Improved | 119% | 0% |
| case-14 | ✗→✓ | ▲ Improved | 149% | 0% |
Implement parametric polymorphism for programming languages including generics, type bounds, and compilation strategies.
Invoke this skill when you need to:
| Parameter | Type | Required | Description | |-----------|------|----------|-------------| | compilationStrategy | string | Yes | Strategy (monomorphization, erasure, dictionary) | | features | array | No | Features to implement | | varianceModel | string | No | Variance handling (explicit, inferred, none) | | boundsSystem | object | No | Bounds system configuration |
json{ "compilationStrategy": "monomorphization", // Rust, C++ "compilationStrategy": "erasure", // Java, TypeScript "compilationStrategy": "dictionary" // Haskell, Swift witness tables }
json{ "features": [ "type-parameters", "trait-bounds", "associated-types", "variance", "higher-kinded-types", "default-type-parameters", "const-generics", "where-clauses", "specialization" ] }
generics/
├── syntax/
│ ├── type-params.grammar # Type parameter syntax
│ ├── bounds.grammar # Bounds and constraints
│ └── where-clause.grammar # Where clause syntax
├── typing/
│ ├── generic-types.ts # Generic type representation
│ ├── bounds-checking.ts # Bounds verification
│ ├── variance.ts # Variance checking
│ └── instantiation.ts # Type instantiation
├── compilation/
│ ├── monomorphization.ts # Monomorphization
│ ├── erasure.ts # Type erasure
│ └── dictionary.ts # Dictionary passing
├── inference/
│ ├── type-inference.ts # Generic type inference
│ └── constraint-solving.ts # Constraint resolution
└── tests/
├── bounds.test.ts
├── variance.test.ts
└── compilation.test.tstypescript// Basic generics struct Vec<T> { data: T[], len: usize } // Multiple type parameters struct HashMap<K, V> { buckets: Array<(K, V)> } // Type parameter bounds fn sort<T: Ord>(arr: &mut [T]) { ... } // Where clauses for complex bounds fn process<T, U>(t: T, u: U) -> bool where T: Clone + Debug, U: AsRef<T> { ... } // Default type parameters struct Container<T = i32> { value: T } // Const generics struct Array<T, const N: usize> { data: [T; N] }
typescriptinterface GenericType { name: string; typeParams: TypeParameter[]; body: Type; } interface TypeParameter { name: string; bounds: TypeBound[]; variance: Variance; default?: Type; } interface TypeBound { trait: TraitRef; // Additional constraints } type Variance = 'covariant' | 'contravariant' | 'invariant' | 'bivariant'; // Type application interface TypeApplication { generic: GenericType; args: Type[]; }
typescript// Monomorphization: generate specialized code for each type instantiation interface MonomorphizationContext { instantiations: Map<string, Type[]>[]; // Track all instantiations generatedCode: Map<string, GeneratedFunction>; } function monomorphize( program: Program, entryPoints: FunctionRef[] ): MonomorphizedProgram { const ctx: MonomorphizationContext = { instantiations: [], generatedCode: new Map() }; // Collect all instantiations starting from entry points for (const entry of entryPoints) { collectInstantiations(entry, ctx); } // Generate specialized code for each instantiation for (const [signature, typeArgs] of ctx.instantiations) { const original = lookupGenericFunction(signature); const specialized = specializeFunction(original, typeArgs); ctx.generatedCode.set(mangleName(signature, typeArgs), specialized); } return buildMonomorphizedProgram(ctx); } function specializeFunction( fn: GenericFunction, typeArgs: Type[] ): SpecializedFunction { // Substitute type parameters with concrete types const substitution = buildSubstitution(fn.typeParams, typeArgs); return { name: mangleName(fn.name, typeArgs), params: fn.params.map(p => substituteType(p.type, substitution)), returnType: substituteType(fn.returnType, substitution), body: substituteInBody(fn.body, substitution) }; } // Name mangling for monomorphized functions function mangleName(baseName: string, typeArgs: Type[]): string { return `${baseName}_${typeArgs.map(typeToString).join('_')}`; }
typescript// Type erasure: erase generic types at runtime, use casts function eraseGenericType(type: Type): Type { if (type.kind === 'typeParam') { // Erase to bound (or Object if unbounded) return type.bounds.length > 0 ? type.bounds[0] // Erase to first bound : ObjectType; } if (type.kind === 'application') { // Erase type arguments return eraseGenericType(type.generic); } if (type.kind === 'generic') { // Erase body return eraseGenericType(type.body); } return type; } // Insert casts at usage sites function insertCasts(expr: Expr, expectedType: Type, actualType: Type): Expr { const erasedExpected = eraseGenericType(expectedType); const erasedActual = eraseGenericType(actualType); if (!typesEqual(erasedExpected, erasedActual)) { return { type: 'cast', expr: expr, targetType: erasedExpected }; } return expr; }
typescript// Variance checking type Variance = 'covariant' | 'contravariant' | 'invariant' | 'bivariant'; interface VarianceChecker { // Compute variance of type parameter in type computeVariance(typeParam: TypeParameter, type: Type): Variance; // Check if variance annotation is correct checkVariance(generic: GenericType): VarianceError[]; // Infer variance from usage inferVariance(generic: GenericType): Map<TypeParameter, Variance>; } function computeVariance(param: TypeParameter, type: Type): Variance { switch (type.kind) { case 'typeParam': return type.name === param.name ? 'covariant' : 'bivariant'; case 'function': // Contravariant in parameter types, covariant in return const paramVariance = combineVariances( type.params.map(p => flipVariance(computeVariance(param, p))) ); const returnVariance = computeVariance(param, type.returnType); return combineVariance(paramVariance, returnVariance); case 'application': // Combine based on declared variance of type constructor return combineVariances( type.args.map((arg, i) => { const declaredVariance = type.generic.typeParams[i].variance; const usageVariance = computeVariance(param, arg); return multiplyVariance(declaredVariance, usageVariance); }) ); case 'mutable': // Mutable positions are invariant return 'invariant'; default: return 'bivariant'; } } // Variance rules for subtyping function isSubtype(sub: Type, sup: Type): boolean { if (sub.kind === 'application' && sup.kind === 'application') { if (sub.generic !== sup.generic) return false; return sub.args.every((subArg, i) => { const supArg = sup.args[i]; const variance = sub.generic.typeParams[i].variance; switch (variance) { case 'covariant': return isSubtype(subArg, supArg); case 'contravariant': return isSubtype(supArg, subArg); case 'invariant': return typesEqual(subArg, supArg); case 'bivariant': return true; } }); } // ... other cases }
typescript// Trait bound checking interface BoundsChecker { // Check if type satisfies bound satisfiesBound(type: Type, bound: TypeBound): boolean; // Find implementation for trait resolveImpl(type: Type, trait: TraitRef): TraitImpl | null; // Check where clause checkWhereClause(clause: WhereClause, env: TypeEnv): boolean; } function satisfiesBound(type: Type, bound: TypeBound): boolean { // Look for trait implementation const impl = findTraitImpl(type, bound.trait); if (!impl) return false; // Check associated type constraints for (const [name, constraint] of bound.associatedTypes) { const actualType = resolveAssociatedType(impl, name); if (!typesEqual(actualType, constraint)) return false; } return true; } // Where clause example: // where T: Iterator<Item = U>, U: Display interface WhereClause { constraints: BoundConstraint[]; } interface BoundConstraint { type: Type; bounds: TypeBound[]; }
typescript// Associated types in traits trait Iterator { type Item; fn next(&mut self) -> Option<Self::Item>; } impl Iterator for Range { type Item = i32; fn next(&mut self) -> Option<i32> { ... } } // Associated type representation interface AssociatedType { name: string; bounds: TypeBound[]; default?: Type; } interface TraitImpl { trait: TraitRef; forType: Type; associatedTypes: Map<string, Type>; methods: Map<string, Function>; } // Resolve associated type function resolveAssociatedType( type: Type, trait: TraitRef, assocName: string ): Type { const impl = findTraitImpl(type, trait); if (!impl) throw new Error(`No impl of ${trait} for ${type}`); const assocType = impl.associatedTypes.get(assocName); if (!assocType) throw new Error(`Associated type ${assocName} not found`); return assocType; }
typescript// Higher-kinded types: types that take type constructors as parameters // Kind system type Kind = | { kind: 'type' } // * - concrete type | { kind: 'arrow'; from: Kind; to: Kind }; // * -> * - type constructor // Example: Functor takes a type constructor F : * -> * trait Functor<F: * -> *> { fn map<A, B>(fa: F<A>, f: A -> B) -> F<B>; } // Implementation interface HigherKindedType { name: string; kind: Kind; } function checkKind(type: Type, expectedKind: Kind): boolean { const actualKind = inferKind(type); return kindsEqual(actualKind, expectedKind); } function inferKind(type: Type): Kind { if (type.kind === 'typeParam') { return type.declaredKind; } if (type.kind === 'application') { // F<A> : check F : K1 -> K2 and A : K1, result is K2 const fnKind = inferKind(type.constructor); if (fnKind.kind !== 'arrow') throw new Error('Expected type constructor'); checkKind(type.arg, fnKind.from); return fnKind.to; } // ... other cases }
| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-01 | fail→pass | 50,103 | 45,405 | -9% | 1 | 1 | 0% | 8,282 | 11,626 | +40% | 0 | 0 | — |
case-02 | fail→pass | 42,157 | 28,480 | -32% | 1 | 1 | 0% | 7,143 | 9,770 | +37% | 0 | 0 | — |
case-03 | fail→pass | 36,842 | 30,658 | -17% | 1 | 1 | 0% | 8,251 | 10,537 | +28% | 0 | 0 | — |
case-04 | fail→fail | 22,674 | 24,036 | +6% | 1 | 1 | 0% | 3,843 | 7,946 | +107% | 0 | 0 | — |
case-05 | pass→pass | 24,990 | 26,000 | +4% | 1 | 1 | 0% | 5,123 | 9,031 | +76% | 0 | 0 | — |
case-06 | fail→pass | 27,208 | 32,643 | +20% | 1 | 1 | 0% | 4,641 | 10,151 | +119% | 0 | 0 | — |
case-07 | pass→pass | 29,021 | 17,984 | -38% | 1 | 1 | 0% | 4,578 | 6,932 | +51% | 0 | 0 | — |
case-08 | pass→pass | 21,235 | 30,171 | +42% | 1 | 1 | 0% | 4,133 | 9,391 | +127% | 0 | 0 | — |
case-09 | pass→pass | 22,130 | 22,235 | +0% | 1 | 1 | 0% | 4,393 | 7,898 | +80% | 0 | 0 | — |
case-10 | pass→pass | 16,851 | 20,171 | +20% | 1 | 1 | 0% | 3,586 | 7,604 | +112% | 0 | 0 | — |
case-11 | pass→pass | 26,083 | 14,430 | -45% | 1 | 1 | 0% | 3,979 | 6,338 | +59% | 0 | 0 | — |
case-12 | pass→pass | 22,735 | 20,725 | -9% | 1 | 1 | 0% | 4,502 | 7,685 | +71% | 0 | 0 | — |
case-13 | pass→pass | 25,126 | 22,106 | -12% | 1 | 1 | 0% | 5,294 | 7,986 | +51% | 0 | 0 | — |
case-14 | fail→pass | 14,705 | 18,167 | +24% | 1 | 1 | 0% | 2,770 | 6,885 | +149% | 0 | 0 | — |
case-15 | fail→pass | 31,589 | 14,917 | -53% | 1 | 1 | 0% | 5,015 | 6,040 | +20% | 0 | 0 | — |
case-16 | pass→pass | 23,195 | 18,694 | -19% | 1 | 1 | 0% | 3,640 | 6,921 | +90% | 0 | 0 | — |
case-17 | pass→pass | 16,561 | 15,606 | -6% | 1 | 1 | 0% | 3,083 | 6,282 | +104% | 0 | 0 | — |
case-18 | pass→pass | 16,569 | 25,021 | +51% | 1 | 1 | 0% | 3,111 | 8,343 | +168% | 0 | 0 | — |
case-19 | pass→pass | 20,415 | 17,735 | -13% | 1 | 1 | 0% | 3,802 | 7,091 | +87% | 0 | 0 | — |
case-20 | pass→pass | 956,725 | 19,960 | -98% | 1 | 1 | 0% | 3,894 | 7,881 | +102% | 0 | 0 | — |
case-21 | pass→pass | 13,456 | 748,740 | +5464% | 1 | 1 | 0% | 2,756 | 7,080 | +157% | 0 | 0 | — |
case-22 | pass→pass | 24,638 | 24,678 | +0% | 1 | 1 | 0% | 5,314 | 8,416 | +58% | 0 | 0 | — |
case-23 | pass→pass | 18,192 | 22,649 | +24% | 1 | 1 | 0% | 3,695 | 8,016 | +117% | 0 | 0 | — |
case-24 | pass→fail | 31,498 | 44,257 | +41% | 1 | 1 | 0% | 6,336 | 11,563 | +82% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 24 cases were attempted. The headline lift of +21 percentage points is the difference between those two pass rates over the 24 comparable cases. 1 case got worse with the skill loaded, and it is included in that figure.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
Other measured skills in the registry, with their headline benchmark lift.