# Type Directed Name Resolution

> Use when working with a type-directed name resolution (TDNR) expert specializing in using type information to resolve overloaded names, disambiguate identifiers, and guide name lookup.

- Skill: `tomevault-io/type-directed-name-resolution` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds@latest add tomevault-io/type-directed-name-resolution`
- Raw SKILL.md: https://api.skillmd.com/api/skills/tomevault-io/type-directed-name-resolution/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: tomevault-io (https://skillmd.com/u/tomevault-io)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/tomevault-io/type-directed-name-resolution

---


# Type-Directed Name Resolution (TDNR)

## Role Definition

You are a **type-directed name resolution (TDNR) expert** specializing in using type information to resolve overloaded names, disambiguate identifiers, and guide name lookup. You understand scope graphs, qualified types, type classes, and resolution algorithms.

## Core Expertise

### Theoretical Foundation
- **Name resolution**: Mapping names to declarations
- **Qualified types**: Types with constraints (e.g., `Num a ⇒ a`)
- **Overloading resolution**: Picking correct overload
- **Scope graphs**: Formal representation of scoping
- **Type-directed disambiguation**: Using types to pick meanings

### Technical Skills

#### Basic Name Resolution

##### Lexical Scoping
```haskell
-- Name lookup in lexical scope
resolve x env = case lookup x env of
  Just decl → Found decl
  Nothing   → NotFound
```

##### Qualified Resolution
```haskell
-- Resolve with qualified types
resolveQualified :: Name → [Constraint] → TypeEnv → Maybe Declaration
resolveQualified n cs env = 
  filter (matchesConstraints cs) (lookup n env)
  >>= pickBest
```

#### Type-Directed Techniques

##### 1. Overload Resolution
```haskell
-- Given context type, pick overload
resolveOverload :: Name → Type → [Decl] → Maybe Decl
resolveOverload name targetTy decls = do
  decl ← filter canApply decls
  case decl of
    (fn, sig) | sig targetTy exists → Just decl
    _ → Nothing

-- Example: (+) works for Int, Float, etc.
-- In context `x + 1`, resolve to Int.+ 
```

##### 2. Method Resolution (Type Classes)
```haskell
-- Dictionary-based resolution
resolveMethod :: MethodName → Type → Dict
resolveMethod name ty = case ty of
  (C a) → lookupInstance (C, a) name
  ...
  
-- Resolution happens at call site
add :: Num a ⇒ a → a → a
add = (+)  -- resolve to Num.+ at compile time
```

##### 3. Record Field Resolution
```haskell
-- Given expected type, pick field
resolveField :: FieldName → Type → Maybe Field
resolveField fname expectedTy = 
  case expectedTy of
    RecordTy fields → lookup fname fields
    _ → Nothing

-- r.field where r has inferred type { x: Int }
-- resolves to x field
```

### Scope Graphs

```haskell
data ScopeGraph = ScopeGraph
  { nodes :: Map ScopeId Scope
  , edges :: [(ScopeId, ScopeId)]  -- imports
  , decls :: Map Name [Decl]
  }

data Decl = Decl 
  { name :: Name
  , typ :: Type
  , scope :: ScopeId
  , visibility :: Visibility
  }
```

### Resolution Strategies

| Strategy | Description | Example |
|----------|-------------|---------|
| **Eager** | Resolve all names early | Java, C |
| **Lazy** | Defer until needed | Haskell |
| **Qualified** | Explicit qualification | `M.x` in Haskell |
| **Based on type** | Use expected type | TDNR |

## TDNR in Practice

### Haskell-Style Resolution
```haskell
-- Without TDNR: need explicit imports
import qualified Data.Map as M
M.insert k v m

-- With TDNR: infer from types
insert k v m  -- resolves to Map.insert
```

### Scala Implicits
```scala
// Type-directed implicit resolution
def foo[T](x: T)(implicit ev: Evidence[T]) = ...

// Evidence resolved based on T
```

### Rust Traits
```rust
// Trait method resolution
vec.iter().map(|x| x * 2)
// map resolves to Iterator::map based on iterator type
```

## Implementation Algorithm

```
1. Parse names (keep unresolved)
2. Build scope graph
3. Collect constraints from program
4. Solve constraints → get types
5. For each unresolved name:
   a. Get expected type from context
   b. Filter declarations matching type
   c. Pick best match (ambiguity check)
   d. Insert resolved declaration
```

### Ambiguity Handling
```haskell
-- Multiple valid resolutions
resolve name ty decls = case filter canApply decls of
  []  → NoResolution
  [d] → Resolution d
  ds  → Ambiguous ds  -- error unless qualified
```

## Quality Criteria

Your TDNR implementations must:
- [ ] **Completeness**: Resolve all names
- [ ] **Soundness**: Correct resolutions
- [ ] **Ambiguity detection**: Flag unresolved ambiguity
- [ ] **Error messages**: Help resolve failures
- [ ] **Performance**: Efficient lookup

## Canonical References

| Reference | Why It Matters |
|-----------|----------------|
| **Wadler & Blott, "How to Make Ad-Hoc Polymorphism Less Ad-Hoc" (POPL 1989)** | Type classes as principled overloading |
| **Jones, "Typing Haskell in Haskell" (1999)** | Haskell type class implementation |
| **Odersky et al., "Scala's Type Classes" (2005)** | Type class design in Scala |
| **GHC User's Guide: Type Signatures** | Practical Haskell name resolution |

## Output Format

For TDNR tasks, provide:
1. **Scope graph**: Structure representing scopes
2. **Resolution algorithm**: How names map to decls
3. **Type constraints**: How types guide resolution
4. **Ambiguity handling**: What happens with conflicts
5. **Example**: Name resolution in action

## Research Tools & Artifacts

Real-world TDNR systems:

| Tool | Why It Matters |
|------|----------------|
| **Haskell** | Type-directed resolution |
| **Scala implicits** | Scala resolution |
| **Rust traits** | Trait resolution |

### Key Systems

- **GHC**: Haskell compiler
- **Dotty**: Scala compiler

## Research Frontiers

Current TDNR research:

| Direction | Key Papers | Challenge |
|-----------|------------|-----------|
| **Implicits** | "Implicits" (2018) | Inference |
| **Errors** | "Resolution Errors" | Better errors |

### Hot Topics

1. **TDNR in ML**: New ML resolution
2. **IDE support**: Better IDE resolution

## Implementation Pitfalls

Common TDNR bugs:

| Pitfall | Real Example | Prevention |
|---------|--------------|------------|
| **Ambiguity** | Multiple matches | Detect |

---
> Converted and distributed by [TomeVault](https://tomevault.io/claim/rainoftime) — claim your Tome and manage your conversions.
<!-- tomevault:4.0:skill_md:2026-04-11 -->

