@@ -13,7 +13,9 @@ pub enum Type {
13
13
String ,
14
14
Bool ,
15
15
Float64 ,
16
+ // Special types
16
17
Unit ,
18
+ Universe ,
17
19
// Complex types
18
20
Array ( Box < Type > ) ,
19
21
Closure ( Box < Type > , Box < Type > ) ,
@@ -82,7 +84,7 @@ impl<T> Typed<T> {
82
84
/// The TypeRegistry keeps track of the inheritance relationships between
83
85
/// types, particularly for user-defined ADTs. It provides methods to register
84
86
/// types and check subtyping relationships.
85
- #[ derive( Debug ) ]
87
+ #[ derive( Debug , Clone ) ]
86
88
pub struct TypeRegistry {
87
89
subtypes : HashMap < Identifier , HashSet < Identifier > > ,
88
90
}
@@ -148,6 +150,8 @@ impl TypeRegistry {
148
150
}
149
151
150
152
match ( child, parent) {
153
+ // Universe is the top type - everything is a subtype of Universe
154
+ ( _, Type :: Universe ) => true ,
151
155
// Check transitive inheritance
152
156
( Type :: Adt ( child_name) , Type :: Adt ( parent_name) ) => {
153
157
if child_name == parent_name {
@@ -247,12 +251,20 @@ mod type_registry_tests {
247
251
assert ! ( registry. is_subtype( & Type :: String , & Type :: String ) ) ;
248
252
assert ! ( registry. is_subtype( & Type :: Float64 , & Type :: Float64 ) ) ;
249
253
assert ! ( registry. is_subtype( & Type :: Unit , & Type :: Unit ) ) ;
254
+ assert ! ( registry. is_subtype( & Type :: Universe , & Type :: Universe ) ) ;
250
255
251
256
// Different primitive types should not be subtypes
252
257
assert ! ( !registry. is_subtype( & Type :: Int64 , & Type :: Bool ) ) ;
253
258
assert ! ( !registry. is_subtype( & Type :: String , & Type :: Int64 ) ) ;
254
259
assert ! ( !registry. is_subtype( & Type :: Float64 , & Type :: Int64 ) ) ;
255
260
assert ! ( !registry. is_subtype( & Type :: Unit , & Type :: Bool ) ) ;
261
+
262
+ // All types should be subtypes of Universe
263
+ assert ! ( registry. is_subtype( & Type :: Int64 , & Type :: Universe ) ) ;
264
+ assert ! ( registry. is_subtype( & Type :: Bool , & Type :: Universe ) ) ;
265
+ assert ! ( registry. is_subtype( & Type :: String , & Type :: Universe ) ) ;
266
+ assert ! ( registry. is_subtype( & Type :: Float64 , & Type :: Universe ) ) ;
267
+ assert ! ( registry. is_subtype( & Type :: Unit , & Type :: Universe ) ) ;
256
268
}
257
269
258
270
#[ test]
@@ -277,6 +289,9 @@ mod type_registry_tests {
277
289
& Type :: Array ( Box :: new( Type :: Array ( Box :: new( Type :: Int64 ) ) ) )
278
290
) ) ;
279
291
292
+ // Array of any type is subtype of Universe
293
+ assert ! ( registry. is_subtype( & Type :: Array ( Box :: new( Type :: Int64 ) ) , & Type :: Universe ) ) ;
294
+
280
295
// Array with inheritance (will be tested more with ADTs)
281
296
let mut adts_registry = TypeRegistry :: new ( ) ;
282
297
let vehicle = create_product_adt ( "Vehicle" , vec ! [ ] ) ;
@@ -315,6 +330,9 @@ mod type_registry_tests {
315
330
// Empty tuples
316
331
assert ! ( registry. is_subtype( & Type :: Tuple ( vec![ ] ) , & Type :: Tuple ( vec![ ] ) ) ) ;
317
332
333
+ // All tuples are subtypes of Universe
334
+ assert ! ( registry. is_subtype( & Type :: Tuple ( vec![ Type :: Int64 , Type :: Bool ] ) , & Type :: Universe ) ) ;
335
+
318
336
// Nested tuples
319
337
assert ! ( registry. is_subtype(
320
338
& Type :: Tuple ( vec![
@@ -349,6 +367,12 @@ mod type_registry_tests {
349
367
& Type :: Map ( Box :: new( Type :: String ) , Box :: new( Type :: Int64 ) ) ,
350
368
& Type :: Map ( Box :: new( Type :: String ) , Box :: new( Type :: Bool ) )
351
369
) ) ;
370
+
371
+ // All maps are subtypes of Universe
372
+ assert ! ( registry. is_subtype(
373
+ & Type :: Map ( Box :: new( Type :: String ) , Box :: new( Type :: Int64 ) ) ,
374
+ & Type :: Universe
375
+ ) ) ;
352
376
}
353
377
354
378
#[ test]
@@ -367,6 +391,12 @@ mod type_registry_tests {
367
391
& Type :: Closure ( Box :: new( Type :: Float64 ) , Box :: new( Type :: Bool ) )
368
392
) ) ;
369
393
394
+ // All closures are subtypes of Universe
395
+ assert ! ( registry. is_subtype(
396
+ & Type :: Closure ( Box :: new( Type :: Int64 ) , Box :: new( Type :: Bool ) ) ,
397
+ & Type :: Universe
398
+ ) ) ;
399
+
370
400
// Contravariant parameter types - broader param type is a subtype
371
401
let mut adts_registry = TypeRegistry :: new ( ) ;
372
402
let animal = create_product_adt ( "Animal" , vec ! [ ] ) ;
@@ -444,6 +474,9 @@ mod type_registry_tests {
444
474
& Type :: Adt ( "Shapes" . to_string( ) )
445
475
) ) ;
446
476
477
+ // All ADTs are subtypes of Universe
478
+ assert ! ( registry. is_subtype( & Type :: Adt ( "Shapes" . to_string( ) ) , & Type :: Universe ) ) ;
479
+
447
480
// Non-subtypes should return false
448
481
assert ! ( !registry. is_subtype(
449
482
& Type :: Adt ( "Shapes" . to_string( ) ) ,
@@ -456,6 +489,35 @@ mod type_registry_tests {
456
489
) ) ;
457
490
}
458
491
492
+ #[ test]
493
+ fn test_universe_as_top_type ( ) {
494
+ let registry = TypeRegistry :: new ( ) ;
495
+
496
+ // Check that Universe is a supertype of all primitive types
497
+ assert ! ( registry. is_subtype( & Type :: Int64 , & Type :: Universe ) ) ;
498
+ assert ! ( registry. is_subtype( & Type :: String , & Type :: Universe ) ) ;
499
+ assert ! ( registry. is_subtype( & Type :: Bool , & Type :: Universe ) ) ;
500
+ assert ! ( registry. is_subtype( & Type :: Float64 , & Type :: Universe ) ) ;
501
+ assert ! ( registry. is_subtype( & Type :: Unit , & Type :: Universe ) ) ;
502
+
503
+ // Check that Universe is a supertype of all complex types
504
+ assert ! ( registry. is_subtype( & Type :: Array ( Box :: new( Type :: Int64 ) ) , & Type :: Universe ) ) ;
505
+ assert ! ( registry. is_subtype( & Type :: Tuple ( vec![ Type :: Int64 , Type :: Bool ] ) , & Type :: Universe ) ) ;
506
+ assert ! ( registry. is_subtype(
507
+ & Type :: Map ( Box :: new( Type :: String ) , Box :: new( Type :: Int64 ) ) ,
508
+ & Type :: Universe
509
+ ) ) ;
510
+ assert ! ( registry. is_subtype(
511
+ & Type :: Closure ( Box :: new( Type :: Int64 ) , Box :: new( Type :: Bool ) ) ,
512
+ & Type :: Universe
513
+ ) ) ;
514
+
515
+ // But Universe is not a subtype of any other type
516
+ assert ! ( !registry. is_subtype( & Type :: Universe , & Type :: Int64 ) ) ;
517
+ assert ! ( !registry. is_subtype( & Type :: Universe , & Type :: String ) ) ;
518
+ assert ! ( !registry. is_subtype( & Type :: Universe , & Type :: Array ( Box :: new( Type :: Int64 ) ) ) ) ;
519
+ }
520
+
459
521
#[ test]
460
522
fn test_complex_nested_type_hierarchy ( ) {
461
523
let mut registry = TypeRegistry :: new ( ) ;
@@ -521,6 +583,11 @@ mod type_registry_tests {
521
583
& Type :: Adt ( "Vehicles" . to_string( ) )
522
584
) ) ;
523
585
586
+ // All vehicle types are subtypes of Universe
587
+ assert ! ( registry. is_subtype( & Type :: Adt ( "Vehicles" . to_string( ) ) , & Type :: Universe ) ) ;
588
+ assert ! ( registry. is_subtype( & Type :: Adt ( "Cars" . to_string( ) ) , & Type :: Universe ) ) ;
589
+ assert ! ( registry. is_subtype( & Type :: Adt ( "SportsCar" . to_string( ) ) , & Type :: Universe ) ) ;
590
+
524
591
// Test negative cases
525
592
assert ! ( !registry. is_subtype(
526
593
& Type :: Adt ( "Vehicles" . to_string( ) ) ,
@@ -614,5 +681,20 @@ mod type_registry_tests {
614
681
] ) ) ) )
615
682
)
616
683
) ) ;
684
+
685
+ // Any complex nested type is a subtype of Universe
686
+ assert ! ( registry. is_subtype(
687
+ & Type :: Map (
688
+ Box :: new( Type :: String ) ,
689
+ Box :: new( Type :: Array ( Box :: new( Type :: Tuple ( vec![
690
+ Type :: Adt ( "Dog" . to_string( ) ) ,
691
+ Type :: Closure (
692
+ Box :: new( Type :: Adt ( "Animals" . to_string( ) ) ) ,
693
+ Box :: new( Type :: Adt ( "Cat" . to_string( ) ) )
694
+ )
695
+ ] ) ) ) )
696
+ ) ,
697
+ & Type :: Universe
698
+ ) ) ;
617
699
}
618
700
}
0 commit comments