1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
//! Canonicalization for structural type identity (Task #32: Graph Isomorphism)
//!
//! This module implements type canonicalization to achieve O(1) structural equality.
//! It transforms cyclic type definitions into trees using De Bruijn indices:
//!
//! - **Recursive(n)**: Self-reference N levels up the nesting path
//! - **BoundParameter(n)**: Type parameter using positional index for alpha-equivalence
//!
//! ## Key Concepts
//!
//! ### Structural vs Nominal Types
//!
//! - **`TypeAlias`**: Structural - `type A = { x: A }` and `type B = { x: B }`
//! should canonicalize to the same type with `Recursive(0)`
//! - **Interface/Class/Enum**: Nominal - Must remain as `Lazy(DefId)` for nominal identity
//!
//! ### De Bruijn Indices
//!
//! - `Recursive(0)`: Immediate self-reference
//! - `Recursive(1)`: One level up (parent in nesting chain)
//! - `BoundParameter(0)`: Innermost type parameter
//! - `BoundParameter(n)`: (n+1)th-most-recently-bound type parameter
//!
//! ## Usage
//!
//! Canonicalization is for **comparison and hashing only**, not for display.
//! Use `canonicalize()` to check if two types are structurally identical:
//!
//! ```rust,ignore
//! let canon_a = canonicalizer.canonicalize(type_a);
//! let canon_b = canonicalizer.canonicalize(type_b);
//! assert_eq!(canon_a, canon_b); // Same structure = same TypeId
//! ```
use crate::TypeDatabase;
use crate::def::DefId;
use crate::def::DefKind;
use crate::subtype::TypeResolver;
use crate::types::{IndexSignature, ObjectShapeId, TemplateSpan, TupleElement, TypeData, TypeId};
use rustc_hash::FxHashMap;
use tsz_common::interner::Atom;
/// Canonicalizer for structural type identity.
///
/// Transforms type aliases from cyclic graphs to trees using De Bruijn indices.
/// Only processes `DefKind::TypeAlias` (structural types), preserving nominal
/// types (Interface/Class/Enum) as `Lazy(DefId)`.
pub struct Canonicalizer<'a, R: TypeResolver> {
/// Type interner for creating new `TypeIds`
interner: &'a dyn TypeDatabase,
/// Type resolver for looking up definitions
resolver: &'a R,
/// Stack of `DefIds` currently being expanded (for Recursive(n))
def_stack: Vec<DefId>,
/// Stack of type parameter scopes (for BoundParameter(n))
/// Each scope is a list of parameter names in order
param_stack: Vec<Vec<Atom>>,
/// Cache to avoid re-canonicalizing the same type
cache: FxHashMap<TypeId, TypeId>,
}
impl<'a, R: TypeResolver> Canonicalizer<'a, R> {
/// Create a new Canonicalizer.
pub fn new(interner: &'a dyn TypeDatabase, resolver: &'a R) -> Self {
Canonicalizer {
interner,
resolver,
def_stack: Vec::new(),
param_stack: Vec::new(),
cache: FxHashMap::default(),
}
}
/// Canonicalize a type to its structural form.
///
/// Returns a `TypeId` that represents the canonical structural form.
/// Two types with the same structure will return the same `TypeId`.
pub fn canonicalize(&mut self, type_id: TypeId) -> TypeId {
// 1. Check cache
if let Some(&cached) = self.cache.get(&type_id) {
return cached;
}
// 2. Look up TypeData
let key = match self.interner.lookup(type_id) {
Some(k) => k,
None => return type_id, // Error/None - preserve as-is
};
let result = match key {
// Handle Type Alias Expansion (structural only)
TypeData::Lazy(def_id) => {
match self.resolver.get_def_kind(def_id) {
Some(DefKind::TypeAlias) => {
// Structural type: canonicalize recursively
self.canonicalize_type_alias(def_id)
}
_ => {
// Nominal type (Interface/Class/Enum): preserve identity
// But canonicalize generic arguments if it's an Application
// For now, just return the Lazy as-is (nominal types keep their identity)
type_id
}
}
}
// Handle Type Parameters -> De Bruijn indices
TypeData::TypeParameter(info) => {
if let Some(index) = self.find_param_index(info.name) {
self.interner.bound_parameter(index)
} else {
// Free variable (shouldn't happen in valid code)
type_id
}
}
// Recurse into composite types
TypeData::Array(elem) => {
let c_elem = self.canonicalize(elem);
self.interner.array(c_elem)
}
TypeData::Tuple(list_id) => {
let elements = self.interner.tuple_list(list_id);
let c_elements: Vec<TupleElement> = elements
.iter()
.map(|e| TupleElement {
type_id: self.canonicalize(e.type_id),
name: e.name,
optional: e.optional,
rest: e.rest,
})
.collect();
self.interner.tuple(c_elements)
}
TypeData::Union(members_id) => {
let members = self.interner.type_list(members_id);
let c_members: Vec<TypeId> =
members.iter().map(|&m| self.canonicalize(m)).collect();
// Sort and deduplicate (union is commutative)
// Sort by raw u32 value since TypeId doesn't implement Ord
let mut sorted = c_members;
sorted.sort_by_key(|t| t.0);
sorted.dedup();
self.interner.union(sorted)
}
TypeData::Intersection(members_id) => {
let members = self.interner.type_list(members_id);
// 1. Canonicalize all members
let c_members: Vec<TypeId> =
members.iter().map(|&m| self.canonicalize(m)).collect();
// 2. Separate callables (preserve order) from structural types (sort)
let mut structural = Vec::new();
let mut callables = Vec::new();
for m in c_members {
if self.is_callable_type(m) {
callables.push(m);
} else {
structural.push(m);
}
}
// 3. Sort structural members by canonical TypeId (commutative)
structural.sort_by_key(|t| t.0);
structural.dedup();
// 4. Combine: structural first (sorted), then callables (preserved order)
let mut final_members = structural;
final_members.extend(callables);
self.interner.intersection(final_members)
}
// Generic type application (e.g., Box<string>)
TypeData::Application(app_id) => {
let app = self.interner.type_application(app_id);
// Canonicalize base type
let c_base = self.canonicalize(app.base);
// Canonicalize all generic arguments
let c_args: Vec<TypeId> =
app.args.iter().map(|&arg| self.canonicalize(arg)).collect();
self.interner.application(c_base, c_args)
}
TypeData::Function(shape_id) => {
let shape = self.interner.function_shape(shape_id);
// Enter new scope if this function has type parameters (alpha-equivalence)
let pushed_scope = if !shape.type_params.is_empty() {
let param_names: Vec<Atom> = shape.type_params.iter().map(|p| p.name).collect();
self.param_stack.push(param_names);
true
} else {
false
};
// Canonicalize this_type if present
let c_this_type = shape.this_type.map(|t| self.canonicalize(t));
// Canonicalize return type
let c_return_type = self.canonicalize(shape.return_type);
// Canonicalize parameter types
let c_params: Vec<crate::types::ParamInfo> = shape
.params
.iter()
.map(|p| crate::types::ParamInfo {
name: p.name,
type_id: self.canonicalize(p.type_id),
optional: p.optional,
rest: p.rest,
})
.collect();
// Canonicalize type parameter constraints and defaults
let c_type_params: Vec<crate::types::TypeParamInfo> = shape
.type_params
.iter()
.map(|tp| crate::types::TypeParamInfo {
name: tp.name,
constraint: tp.constraint.map(|c| self.canonicalize(c)),
default: tp.default.map(|d| self.canonicalize(d)),
is_const: tp.is_const,
})
.collect();
// Canonicalize type predicate (if it has a type_id)
let c_type_predicate =
shape
.type_predicate
.as_ref()
.map(|pred| crate::types::TypePredicate {
asserts: pred.asserts,
target: pred.target.clone(),
type_id: pred.type_id.map(|t| self.canonicalize(t)),
parameter_index: pred.parameter_index,
});
// Pop scope
if pushed_scope {
self.param_stack.pop();
}
let new_shape = crate::types::FunctionShape {
type_params: c_type_params,
params: c_params,
this_type: c_this_type,
return_type: c_return_type,
type_predicate: c_type_predicate,
is_constructor: shape.is_constructor,
is_method: shape.is_method,
};
self.interner.function(new_shape)
}
TypeData::Callable(shape_id) => self.canonicalize_callable(shape_id),
// Task #39: Mapped type canonicalization for alpha-equivalence
// When comparing mapped types over type parameters (deferred), we need
// to canonicalize the constraint, template, and name_type to achieve
// structural identity. The type_param name is handled via param_stack.
TypeData::Mapped(mapped_id) => {
let mapped = self.interner.mapped_type(mapped_id);
// 1. Canonicalize the constraint FIRST (Outside scope)
// The iteration variable K is NOT visible in its own constraint
let c_constraint = self.canonicalize(mapped.constraint);
// 2. Enter new scope for the iteration variable (alpha-equivalence)
self.param_stack.push(vec![mapped.type_param.name]);
// 3. Canonicalize the template type (Inside scope - K is visible here)
let c_template = self.canonicalize(mapped.template);
// 4. Canonicalize name_type if present (Inside scope - as clause sees K)
let c_name_type = mapped.name_type.map(|t| self.canonicalize(t));
// 5. Pop scope
self.param_stack.pop();
// 6. Normalize the TypeParamInfo name for alpha-equivalence
// We must erase the original name ("K", "P", etc.) so that
// { [K in T]: K } and { [P in T]: P } hash to the same value.
// Since we use De Bruijn indices (BoundParameter) in the body,
// this name is never looked up, only used for hashing identity.
let mut c_type_param = mapped.type_param.clone();
c_type_param.name = self.interner.intern_string("");
// Also canonicalize constraint/default inside TypeParamInfo if present
c_type_param.constraint = c_type_param.constraint.map(|c| self.canonicalize(c));
c_type_param.default = c_type_param.default.map(|d| self.canonicalize(d));
let c_mapped = crate::types::MappedType {
type_param: c_type_param,
constraint: c_constraint,
template: c_template,
name_type: c_name_type,
readonly_modifier: mapped.readonly_modifier,
optional_modifier: mapped.optional_modifier,
};
self.interner.mapped(c_mapped)
}
// Object types: canonicalize property types while preserving metadata
TypeData::Object(shape_id) => self.canonicalize_object(shape_id, false),
TypeData::ObjectWithIndex(shape_id) => self.canonicalize_object(shape_id, true),
// Task #47: Template Literal canonicalization for alpha-equivalence
// Uppercase<T> and Uppercase<U> should be identical when T and U are identical
TypeData::TemplateLiteral(id) => {
let spans = self.interner.template_list(id);
let c_spans: Vec<TemplateSpan> = spans
.iter()
.map(|span| match span {
TemplateSpan::Text(atom) => TemplateSpan::Text(*atom),
TemplateSpan::Type(t) => TemplateSpan::Type(self.canonicalize(*t)),
})
.collect();
self.interner.template_literal(c_spans)
}
// Task #47: String Intrinsic canonicalization for alpha-equivalence
// Uppercase<T>, Lowercase<T>, etc. should canonicalize nested type parameters
TypeData::StringIntrinsic { kind, type_arg } => {
let c_arg = self.canonicalize(type_arg);
self.interner.string_intrinsic(kind, c_arg)
}
// Other types: preserve as-is (will be handled as needed)
_ => type_id,
};
self.cache.insert(type_id, result);
result
}
/// Canonicalize a type alias definition.
///
/// This handles:
/// - Cycle detection via `def_stack`
/// - Generic parameter scope management
/// - Recursive self-references -> Recursive(n)
fn canonicalize_type_alias(&mut self, def_id: DefId) -> TypeId {
// Check for cycles (mutual recursion or self-reference)
if let Some(depth) = self.get_recursion_depth(def_id) {
return self.interner.recursive(depth);
}
// Push to stack for cycle detection
self.def_stack.push(def_id);
// Enter new scope if generic
let params = self.resolver.get_lazy_type_params(def_id);
let pushed_scope = if let Some(ps) = params {
let param_names: Vec<Atom> = ps.iter().map(|p| p.name).collect();
self.param_stack.push(param_names);
true
} else {
false
};
// Resolve the alias body and canonicalize recursively
let body = self
.resolver
.resolve_lazy(def_id, self.interner)
.unwrap_or(TypeId::ERROR);
let canonical_body = self.canonicalize(body);
// Pop scope and def_stack
if pushed_scope {
self.param_stack.pop();
}
self.def_stack.pop();
canonical_body
}
/// Get the recursion depth for a `DefId` if it's in the `def_stack`.
///
/// Returns Some(depth) if the `DefId` is being expanded, where:
/// - 0 = immediate self-reference (current `DefId`)
/// - n = n levels up the nesting chain
fn get_recursion_depth(&self, def_id: DefId) -> Option<u32> {
self.def_stack
.iter()
.rev()
.position(|&d| d == def_id)
.map(|pos| pos as u32)
}
/// Find the De Bruijn index for a type parameter by name.
///
/// Searches from the top of the stack (innermost scope) downward.
/// Returns Some(index) if found, where:
/// - 0 = innermost parameter
/// - n = (n+1)th-most-recently-bound parameter
fn find_param_index(&self, name: Atom) -> Option<u32> {
let mut flattened_index = 0u32;
// Search from top of stack (innermost scope) to bottom
for scope in self.param_stack.iter().rev() {
for (idx, ¶m_name) in scope.iter().enumerate() {
if param_name == name {
// Calculate flattened index from innermost
let innermost_offset = scope.len() - idx - 1;
return Some(flattened_index + innermost_offset as u32);
}
}
flattened_index += scope.len() as u32;
}
None
}
/// Canonicalize an object type by recursively canonicalizing property types.
///
/// Preserves all metadata (names, optional, readonly, visibility, `parent_id`)
/// and nominal symbols. Only transforms the `TypeIds` within properties.
fn canonicalize_object(&mut self, shape_id: ObjectShapeId, _with_index: bool) -> TypeId {
let shape = self.interner.object_shape(shape_id);
// Canonicalize all properties
let mut new_props = Vec::with_capacity(shape.properties.len());
for prop in &shape.properties {
let mut new_prop = prop.clone();
// Canonicalize read type (getter/lookup)
new_prop.type_id = self.canonicalize(prop.type_id);
// Canonicalize write type (setter/assignment)
new_prop.write_type = self.canonicalize(prop.write_type);
// Preserve all other metadata as-is
// - name (Atom): Property names are NOT remapped
// - optional (bool): Part of type identity
// - readonly (bool): Part of type identity
// - is_method (bool): Part of type identity
// - visibility (Visibility): Part of type identity (nominal subtyping)
// - parent_id (Option<SymbolId>): Brand for private/protected members
new_props.push(new_prop);
}
// Canonicalize index signatures if present
let new_string_index = shape.string_index.as_ref().map(|idx| IndexSignature {
key_type: self.canonicalize(idx.key_type),
value_type: self.canonicalize(idx.value_type),
readonly: idx.readonly,
});
let new_number_index = shape.number_index.as_ref().map(|idx| IndexSignature {
key_type: self.canonicalize(idx.key_type),
value_type: self.canonicalize(idx.value_type),
readonly: idx.readonly,
});
// Preserve the symbol field for nominal types (class instances)
// This ensures that class A and class B with same properties remain distinct
let symbol = shape.symbol;
// Create new object shape with canonicalized types but preserved metadata
let new_shape = crate::types::ObjectShape {
flags: shape.flags,
properties: new_props,
string_index: new_string_index,
number_index: new_number_index,
symbol,
};
// Intern using the appropriate method
// Note: object_with_index takes ObjectShape by value and sorts properties
self.interner.object_with_index(new_shape)
}
/// Check if a type is a callable (Function or Callable).
fn is_callable_type(&self, type_id: TypeId) -> bool {
matches!(
self.interner.lookup(type_id),
Some(TypeData::Function(_) | TypeData::Callable(_))
)
}
/// Canonicalize a single call signature with type parameter scope management.
fn canonicalize_signature(
&mut self,
sig: &crate::types::CallSignature,
) -> crate::types::CallSignature {
// Enter new scope if this signature has type parameters (alpha-equivalence)
let pushed_scope = if !sig.type_params.is_empty() {
let param_names: Vec<Atom> = sig.type_params.iter().map(|p| p.name).collect();
self.param_stack.push(param_names);
true
} else {
false
};
// Canonicalize this_type if present
let c_this_type = sig.this_type.map(|t| self.canonicalize(t));
// Canonicalize return type
let c_return_type = self.canonicalize(sig.return_type);
// Canonicalize parameter types
let c_params: Vec<crate::types::ParamInfo> = sig
.params
.iter()
.map(|p| crate::types::ParamInfo {
name: p.name,
type_id: self.canonicalize(p.type_id),
optional: p.optional,
rest: p.rest,
})
.collect();
// Canonicalize type parameter constraints and defaults
let c_type_params: Vec<crate::types::TypeParamInfo> = sig
.type_params
.iter()
.map(|tp| crate::types::TypeParamInfo {
name: tp.name,
constraint: tp.constraint.map(|c| self.canonicalize(c)),
default: tp.default.map(|d| self.canonicalize(d)),
// Preserve other fields as-is
is_const: tp.is_const,
})
.collect();
// Canonicalize type predicate (if it has a type_id)
let c_type_predicate =
sig.type_predicate
.as_ref()
.map(|pred| crate::types::TypePredicate {
asserts: pred.asserts,
target: pred.target.clone(),
type_id: pred.type_id.map(|t| self.canonicalize(t)),
parameter_index: pred.parameter_index,
});
// Pop scope
if pushed_scope {
self.param_stack.pop();
}
crate::types::CallSignature {
type_params: c_type_params,
params: c_params,
this_type: c_this_type,
return_type: c_return_type,
type_predicate: c_type_predicate,
is_method: sig.is_method,
}
}
/// Canonicalize a callable type (overloaded functions).
fn canonicalize_callable(&mut self, shape_id: crate::types::CallableShapeId) -> TypeId {
let shape = self.interner.callable_shape(shape_id);
// Canonicalize all call signatures (order matters for overload resolution)
let c_call_signatures: Vec<crate::types::CallSignature> = shape
.call_signatures
.iter()
.map(|sig| self.canonicalize_signature(sig))
.collect();
// Canonicalize all construct signatures
let c_construct_signatures: Vec<crate::types::CallSignature> = shape
.construct_signatures
.iter()
.map(|sig| self.canonicalize_signature(sig))
.collect();
// Canonicalize properties
let mut new_props = Vec::with_capacity(shape.properties.len());
for prop in &shape.properties {
let mut new_prop = prop.clone();
new_prop.type_id = self.canonicalize(prop.type_id);
new_prop.write_type = self.canonicalize(prop.write_type);
new_props.push(new_prop);
}
// Canonicalize index signatures
let new_string_index =
shape
.string_index
.as_ref()
.map(|idx| crate::types::IndexSignature {
key_type: self.canonicalize(idx.key_type),
value_type: self.canonicalize(idx.value_type),
readonly: idx.readonly,
});
let new_number_index =
shape
.number_index
.as_ref()
.map(|idx| crate::types::IndexSignature {
key_type: self.canonicalize(idx.key_type),
value_type: self.canonicalize(idx.value_type),
readonly: idx.readonly,
});
let new_shape = crate::types::CallableShape {
call_signatures: c_call_signatures,
construct_signatures: c_construct_signatures,
properties: new_props,
string_index: new_string_index,
number_index: new_number_index,
symbol: shape.symbol,
};
self.interner.callable(new_shape)
}
/// Clear the cache (useful for testing or bulk operations).
pub fn clear_cache(&mut self) {
self.cache.clear();
}
}
#[cfg(test)]
#[path = "../tests/canonicalize_tests.rs"]
mod tests;