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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
//! DOL 2.0 Type Checker
//!
//! This module implements type inference and checking for DOL 2.0 expressions.
//! It validates type consistency, infers types for expressions, and ensures
//! type annotations match actual types.
//!
//! # Architecture
//!
//! The type checker uses bidirectional type checking:
//! - **Inference mode**: Synthesizes types from expressions
//! - **Checking mode**: Verifies expressions against expected types
//!
//! # Example
//!
//! ```rust
//! use metadol::typechecker::{TypeChecker, Type};
//! use metadol::ast::{Expr, Literal};
//!
//! let mut checker = TypeChecker::new();
//! let expr = Expr::Literal(Literal::Int(42));
//! let ty = checker.infer(&expr).unwrap();
//! assert_eq!(ty, Type::Int64);
//! ```
#[cfg(test)]
use crate::ast::Span;
use crate::ast::{BinaryOp, Block, Expr, Literal, Pattern, Stmt, TypeExpr, UnaryOp};
use std::collections::HashMap;
/// Semantic types used during type checking.
///
/// Unlike `TypeExpr` which represents syntax, `Type` represents
/// the actual semantic types used for type checking.
#[derive(Debug, Clone, PartialEq)]
pub enum Type {
/// Void type (no value)
Void,
/// Boolean type
Bool,
/// 8-bit signed integer
Int8,
/// 16-bit signed integer
Int16,
/// 32-bit signed integer
Int32,
/// 64-bit signed integer
Int64,
/// 8-bit unsigned integer
UInt8,
/// 16-bit unsigned integer
UInt16,
/// 32-bit unsigned integer
UInt32,
/// 64-bit unsigned integer
UInt64,
/// 32-bit floating point
Float32,
/// 64-bit floating point
Float64,
/// String type
String,
/// Function type
Function {
/// Parameter types
params: Vec<Type>,
/// Return type
return_type: Box<Type>,
},
/// Tuple type
Tuple(Vec<Type>),
/// Generic/parametric type
Generic {
/// Type constructor name
name: String,
/// Type arguments
args: Vec<Type>,
},
/// Type variable (for inference)
Var(usize),
/// Unknown type (inference placeholder)
Unknown,
/// Any type (compatible with everything - for gradual typing)
Any,
/// Never type (function never returns)
Never,
/// Error type (propagates type errors)
Error,
}
impl Type {
/// Returns true if this is a numeric type.
pub fn is_numeric(&self) -> bool {
matches!(
self,
Type::Int8
| Type::Int16
| Type::Int32
| Type::Int64
| Type::UInt8
| Type::UInt16
| Type::UInt32
| Type::UInt64
| Type::Float32
| Type::Float64
)
}
/// Returns true if this is an integer type.
pub fn is_integer(&self) -> bool {
matches!(
self,
Type::Int8
| Type::Int16
| Type::Int32
| Type::Int64
| Type::UInt8
| Type::UInt16
| Type::UInt32
| Type::UInt64
)
}
/// Returns true if this is a floating point type.
pub fn is_float(&self) -> bool {
matches!(self, Type::Float32 | Type::Float64)
}
/// Returns true if this is a signed type.
pub fn is_signed(&self) -> bool {
matches!(
self,
Type::Int8 | Type::Int16 | Type::Int32 | Type::Int64 | Type::Float32 | Type::Float64
)
}
/// Returns the bit width of numeric types.
pub fn bit_width(&self) -> Option<usize> {
match self {
Type::Int8 | Type::UInt8 => Some(8),
Type::Int16 | Type::UInt16 => Some(16),
Type::Int32 | Type::UInt32 | Type::Float32 => Some(32),
Type::Int64 | Type::UInt64 | Type::Float64 => Some(64),
_ => None,
}
}
/// Creates a type from a TypeExpr.
pub fn from_type_expr(expr: &TypeExpr) -> Type {
match expr {
TypeExpr::Named(name) => match name.as_str() {
"Void" => Type::Void,
"Bool" => Type::Bool,
"Int8" => Type::Int8,
"Int16" => Type::Int16,
"Int32" => Type::Int32,
"Int64" => Type::Int64,
"UInt8" => Type::UInt8,
"UInt16" => Type::UInt16,
"UInt32" => Type::UInt32,
"UInt64" => Type::UInt64,
"Float32" => Type::Float32,
"Float64" => Type::Float64,
"String" => Type::String,
_ => Type::Generic {
name: name.clone(),
args: vec![],
},
},
TypeExpr::Generic { name, args } => Type::Generic {
name: name.clone(),
args: args.iter().map(Type::from_type_expr).collect(),
},
TypeExpr::Function {
params,
return_type,
} => Type::Function {
params: params.iter().map(Type::from_type_expr).collect(),
return_type: Box::new(Type::from_type_expr(return_type)),
},
TypeExpr::Tuple(types) => Type::Tuple(types.iter().map(Type::from_type_expr).collect()),
TypeExpr::Never => Type::Never,
TypeExpr::Enum { variants } => Type::Generic {
name: "Enum".to_string(),
args: variants
.iter()
.map(|v| Type::Generic {
name: v.name.clone(),
args: vec![],
})
.collect(),
},
}
}
}
impl std::fmt::Display for Type {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Type::Void => write!(f, "Void"),
Type::Bool => write!(f, "Bool"),
Type::Int8 => write!(f, "Int8"),
Type::Int16 => write!(f, "Int16"),
Type::Int32 => write!(f, "Int32"),
Type::Int64 => write!(f, "Int64"),
Type::UInt8 => write!(f, "UInt8"),
Type::UInt16 => write!(f, "UInt16"),
Type::UInt32 => write!(f, "UInt32"),
Type::UInt64 => write!(f, "UInt64"),
Type::Float32 => write!(f, "Float32"),
Type::Float64 => write!(f, "Float64"),
Type::String => write!(f, "String"),
Type::Function {
params,
return_type,
} => {
write!(f, "(")?;
for (i, p) in params.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{}", p)?;
}
write!(f, ") -> {}", return_type)
}
Type::Tuple(types) => {
write!(f, "(")?;
for (i, t) in types.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{}", t)?;
}
write!(f, ")")
}
Type::Generic { name, args } => {
write!(f, "{}", name)?;
if !args.is_empty() {
write!(f, "<")?;
for (i, arg) in args.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{}", arg)?;
}
write!(f, ">")?;
}
Ok(())
}
Type::Var(id) => write!(f, "?{}", id),
Type::Unknown => write!(f, "?"),
Type::Any => write!(f, "Any"),
Type::Never => write!(f, "!"),
Type::Error => write!(f, "Error"),
}
}
}
/// Effect context for tracking purity during type checking.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum EffectContext {
/// Pure context - no side effects allowed (default)
#[default]
Pure,
/// Sex context - side effects permitted
Sex,
}
/// Type checking error.
#[derive(Debug, Clone, PartialEq)]
pub struct TypeError {
/// Error message
pub message: String,
/// Expected type (if applicable)
pub expected: Option<Type>,
/// Actual type (if applicable)
pub actual: Option<Type>,
}
impl TypeError {
/// Creates a new type error.
pub fn new(message: impl Into<String>) -> Self {
Self {
message: message.into(),
expected: None,
actual: None,
}
}
/// Creates a type mismatch error.
pub fn mismatch(expected: Type, actual: Type) -> Self {
Self {
message: format!("type mismatch: expected {}, found {}", expected, actual),
expected: Some(expected),
actual: Some(actual),
}
}
/// Creates an undefined variable error.
pub fn undefined(name: &str) -> Self {
Self {
message: format!("undefined variable: {}", name),
expected: None,
actual: None,
}
}
}
impl std::fmt::Display for TypeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.message)
}
}
impl std::error::Error for TypeError {}
/// Type environment for tracking variable bindings.
#[derive(Debug, Clone, Default)]
pub struct TypeEnv {
/// Variable bindings in current scope
bindings: HashMap<String, Type>,
/// Parent scope (for nested scopes)
parent: Option<Box<TypeEnv>>,
}
impl TypeEnv {
/// Creates a new empty environment.
pub fn new() -> Self {
Self::default()
}
/// Creates a child environment with this as parent.
pub fn child(&self) -> Self {
Self {
bindings: HashMap::new(),
parent: Some(Box::new(self.clone())),
}
}
/// Binds a variable to a type.
pub fn bind(&mut self, name: impl Into<String>, ty: Type) {
self.bindings.insert(name.into(), ty);
}
/// Looks up a variable's type.
pub fn lookup(&self, name: &str) -> Option<&Type> {
self.bindings
.get(name)
.or_else(|| self.parent.as_ref().and_then(|p| p.lookup(name)))
}
}
/// The type checker.
#[derive(Debug)]
pub struct TypeChecker {
/// Current type environment
env: TypeEnv,
/// Counter for generating fresh type variables
var_counter: usize,
/// Collected type errors
errors: Vec<TypeError>,
/// Current effect context
effect_context: EffectContext,
/// Effect context stack for nested contexts
effect_stack: Vec<EffectContext>,
}
impl Default for TypeChecker {
fn default() -> Self {
Self::new()
}
}
impl TypeChecker {
/// Creates a new type checker.
pub fn new() -> Self {
Self {
env: TypeEnv::new(),
var_counter: 0,
errors: Vec::new(),
effect_context: EffectContext::Pure,
effect_stack: Vec::new(),
}
}
/// Returns collected errors.
pub fn errors(&self) -> &[TypeError] {
&self.errors
}
/// Returns true if type checking passed without errors.
pub fn is_ok(&self) -> bool {
self.errors.is_empty()
}
/// Clears collected errors.
pub fn clear_errors(&mut self) {
self.errors.clear();
}
/// Generates a fresh type variable.
fn fresh_var(&mut self) -> Type {
let id = self.var_counter;
self.var_counter += 1;
Type::Var(id)
}
/// Adds a type error.
fn error(&mut self, err: TypeError) {
self.errors.push(err);
}
/// Enter a sex context (e.g., for sex blocks or sex functions)
pub fn enter_sex_context(&mut self) {
self.effect_stack.push(self.effect_context);
self.effect_context = EffectContext::Sex;
}
/// Exit the current effect context
pub fn exit_sex_context(&mut self) {
self.effect_context = self.effect_stack.pop().unwrap_or(EffectContext::Pure);
}
/// Check if currently in a sex context
pub fn in_sex_context(&self) -> bool {
self.effect_context == EffectContext::Sex
}
/// Get the current effect context
pub fn current_effect_context(&self) -> EffectContext {
self.effect_context
}
/// Infers the type of an expression.
pub fn infer(&mut self, expr: &Expr) -> Result<Type, TypeError> {
match expr {
// Literals
Expr::Literal(lit) => match lit {
Literal::Int(_) => Ok(Type::Int64),
Literal::Float(_) => Ok(Type::Float64),
Literal::Bool(_) => Ok(Type::Bool),
Literal::String(_) => Ok(Type::String),
Literal::Char(_) => Ok(Type::String), // Char treated as String
Literal::Null => Ok(Type::Unknown), // Null is polymorphic
},
// Identifiers
Expr::Identifier(name) => self
.env
.lookup(name)
.cloned()
.ok_or_else(|| TypeError::undefined(name)),
// Unary expressions
Expr::Unary { op, operand } => self.infer_unary(op, operand),
// Binary expressions
Expr::Binary { op, left, right } => self.infer_binary(op, left, right),
// Function calls
Expr::Call { callee, args } => self.infer_call(callee, args),
// Lambdas
Expr::Lambda {
params,
body,
return_type,
} => self.infer_lambda(params, body, return_type.as_ref()),
// If expressions
Expr::If {
condition,
then_branch,
else_branch,
} => self.infer_if(condition, then_branch, else_branch.as_deref()),
// Match expressions
Expr::Match { scrutinee, arms } => self.infer_match(scrutinee, arms),
// Block expressions
Expr::Block(Block {
statements,
final_expr,
..
}) => self.infer_block(statements, final_expr.as_deref()),
// Member/Field access
Expr::Member { object, field } => {
let _obj_type = self.infer(object)?;
// For now, field access returns Unknown (structural typing TBD)
let _ = field;
Ok(Type::Unknown)
}
// Quote/Eval/Reflect - return special types
Expr::Quote(inner) => {
let inner_type = self.infer(inner)?;
Ok(Type::Generic {
name: "Quoted".to_string(),
args: vec![inner_type],
})
}
Expr::Eval(inner) => {
let inner_type = self.infer(inner)?;
match inner_type {
Type::Generic { name, args } if name == "Quoted" && !args.is_empty() => {
// Eval of Quoted<T> returns T
Ok(args.into_iter().next().unwrap())
}
Type::Generic { name, .. } if name == "Quoted" => {
// Quoted without type param - return Unknown
Ok(Type::Unknown)
}
Type::Unknown => Ok(Type::Unknown),
_ => {
self.error(TypeError::new(format!(
"cannot eval non-quoted type: {}",
inner_type
)));
Ok(Type::Error)
}
}
}
Expr::Reflect(_type_expr) => {
// Type reflection returns type metadata
Ok(Type::Generic {
name: "TypeInfo".to_string(),
args: vec![],
})
}
Expr::IdiomBracket { func, args } => {
// Idiom brackets [| f a b |] desugar to f <$> a <*> b
// For typing, we check that func is a function and args are applicative contexts
let func_type = self.infer(func)?;
// Infer types of all arguments
for arg in args {
self.infer(arg)?;
}
// The result type depends on the function's return type wrapped in the applicative
// For now, return the function's return type (simplified)
match func_type {
Type::Function {
ref return_type, ..
} => Ok((**return_type).clone()),
_ => Ok(Type::Unknown),
}
}
Expr::Unquote(inner) => {
// Unquote inside a quote evaluates the inner expression
self.infer(inner)
}
Expr::QuasiQuote(inner) => {
// QuasiQuote is like Quote but allows splicing via Unquote
let inner_type = self.infer(inner)?;
Ok(Type::Generic {
name: "Quoted".to_string(),
args: vec![inner_type],
})
}
// Logic expressions
Expr::Forall(forall_expr) => {
// Forall expressions have type Bool (they are propositions)
self.infer(&forall_expr.body)?;
Ok(Type::Bool)
}
Expr::Exists(exists_expr) => {
// Exists expressions have type Bool (they are propositions)
self.infer(&exists_expr.body)?;
Ok(Type::Bool)
}
Expr::Implies { left, right, .. } => {
// Implication requires both sides to be Bool
let left_type = self.infer(left)?;
let right_type = self.infer(right)?;
if left_type != Type::Bool || right_type != Type::Bool {
self.error(TypeError::new(format!(
"implication requires Bool, found {} and {}",
left_type, right_type
)));
}
Ok(Type::Bool)
}
// Sex block - enter sex context, infer type, exit context
Expr::SexBlock(Block {
statements,
final_expr,
..
}) => {
self.enter_sex_context();
let result = self.infer_block(statements, final_expr.as_deref());
self.exit_sex_context();
result
}
// List literal
Expr::List(elements) => {
if elements.is_empty() {
// Empty list: List<Unknown>
Ok(Type::Generic {
name: "List".to_string(),
args: vec![Type::Unknown],
})
} else {
// Infer element type from first element
let elem_type = self.infer(&elements[0])?;
// Check that all elements have the same type
for elem in elements.iter().skip(1) {
let t = self.infer(elem)?;
if t != elem_type {
self.error(TypeError::new(format!(
"list elements have inconsistent types: {} vs {}",
elem_type, t
)));
}
}
Ok(Type::Generic {
name: "List".to_string(),
args: vec![elem_type],
})
}
}
// Tuple literal
Expr::Tuple(elements) => {
let mut elem_types = Vec::new();
for elem in elements {
elem_types.push(self.infer(elem)?);
}
Ok(Type::Tuple(elem_types))
}
// Type cast - the result is the target type
Expr::Cast { expr, target_type } => {
// Type-check the expression being cast
let _expr_type = self.infer(expr)?;
// The result type is the target type
Ok(Type::from_type_expr(target_type))
}
// Struct literal - the result is the struct type
Expr::StructLiteral { type_name, fields } => {
// Type-check all field expressions
for (_, field_expr) in fields {
self.infer(field_expr)?;
}
// The result type is the struct type (represented as a generic with no args)
Ok(Type::Generic {
name: type_name.clone(),
args: vec![],
})
}
// Try expression - propagates errors, returns inner type on success
Expr::Try(inner) => {
// Type-check the inner expression
let inner_type = self.infer(inner)?;
// For now, just return the inner type (proper Result handling would be more complex)
Ok(inner_type)
}
// This - reference to current instance
Expr::This => {
// Look up 'this' type in the environment
self.env.lookup("this").cloned().ok_or_else(|| {
TypeError::new("'this' used outside of method context".to_string())
})
}
}
}
/// Infers type for unary expressions.
fn infer_unary(&mut self, op: &UnaryOp, operand: &Expr) -> Result<Type, TypeError> {
let operand_type = self.infer(operand)?;
match op {
UnaryOp::Neg => {
if !operand_type.is_numeric() {
self.error(TypeError::new(format!(
"cannot negate non-numeric type {}",
operand_type
)));
Ok(Type::Error)
} else {
Ok(operand_type)
}
}
UnaryOp::Not => {
if operand_type != Type::Bool {
self.error(TypeError::new(format!(
"logical not requires Bool, found {}",
operand_type
)));
Ok(Type::Error)
} else {
Ok(Type::Bool)
}
}
UnaryOp::Quote => {
// The operand was already inferred, pass its type to Quoted
Ok(Type::Generic {
name: "Quoted".to_string(),
args: vec![operand_type],
})
}
UnaryOp::Reflect => Ok(Type::Generic {
name: "TypeInfo".to_string(),
args: vec![],
}),
UnaryOp::Deref => {
// Dereference - for now just return the operand type
// Full implementation would unwrap pointer/reference types
Ok(operand_type)
}
}
}
/// Infers type for binary expressions.
fn infer_binary(
&mut self,
op: &BinaryOp,
left: &Expr,
right: &Expr,
) -> Result<Type, TypeError> {
let left_type = self.infer(left)?;
let right_type = self.infer(right)?;
match op {
// Arithmetic operators
BinaryOp::Add | BinaryOp::Sub | BinaryOp::Mul | BinaryOp::Div | BinaryOp::Mod => {
if !left_type.is_numeric() || !right_type.is_numeric() {
self.error(TypeError::new(format!(
"arithmetic requires numeric types, found {} and {}",
left_type, right_type
)));
return Ok(Type::Error);
}
// Promote to larger type
Ok(self.promote_numeric(&left_type, &right_type))
}
// Comparison operators
BinaryOp::Eq | BinaryOp::Ne => {
// Equality works on any matching types
if !self.types_compatible(&left_type, &right_type) {
self.error(TypeError::new(format!(
"cannot compare {} with {}",
left_type, right_type
)));
}
Ok(Type::Bool)
}
BinaryOp::Lt | BinaryOp::Le | BinaryOp::Gt | BinaryOp::Ge => {
if !left_type.is_numeric() || !right_type.is_numeric() {
self.error(TypeError::new(format!(
"comparison requires numeric types, found {} and {}",
left_type, right_type
)));
}
Ok(Type::Bool)
}
// Logical operators
BinaryOp::And | BinaryOp::Or => {
if left_type != Type::Bool || right_type != Type::Bool {
self.error(TypeError::new(format!(
"logical operators require Bool, found {} and {}",
left_type, right_type
)));
}
Ok(Type::Bool)
}
// Pipe operator: a |> f means f(a)
BinaryOp::Pipe => {
// Right must be a function that accepts left
match right_type {
Type::Function {
params,
return_type,
} => {
if params.is_empty() {
self.error(TypeError::new(
"pipe target function must accept at least one argument",
));
Ok(Type::Error)
} else if !self.types_compatible(&left_type, ¶ms[0]) {
self.error(TypeError::mismatch(params[0].clone(), left_type));
Ok(Type::Error)
} else {
Ok(*return_type)
}
}
Type::Unknown | Type::Any => Ok(Type::Unknown),
_ => {
// For untyped identifiers, assume it works
Ok(Type::Unknown)
}
}
}
// Compose operator: f >> g means g(f(x))
BinaryOp::Compose => {
// Both must be functions
match (&left_type, &right_type) {
(
Type::Function {
return_type: left_ret,
params: left_params,
},
Type::Function {
params: right_params,
return_type: right_ret,
},
) => {
// f's return type must match g's first param
if !right_params.is_empty()
&& !self.types_compatible(left_ret, &right_params[0])
{
self.error(TypeError::new(format!(
"cannot compose: {} does not match {}",
left_ret, right_params[0]
)));
}
Ok(Type::Function {
params: left_params.clone(),
return_type: right_ret.clone(),
})
}
(Type::Unknown, _) | (_, Type::Unknown) => Ok(Type::Unknown),
_ => {
// Assume identifier composition works
Ok(Type::Unknown)
}
}
}
// Exponentiation
BinaryOp::Pow => {
if !left_type.is_numeric() || !right_type.is_numeric() {
self.error(TypeError::new(format!(
"exponentiation requires numeric types, found {} and {}",
left_type, right_type
)));
return Ok(Type::Error);
}
Ok(self.promote_numeric(&left_type, &right_type))
}
// Function application
BinaryOp::Apply => {
// Similar to pipe but different syntax
match right_type {
Type::Function {
params,
return_type,
} => {
if params.is_empty() {
self.error(TypeError::new(
"apply target function must accept at least one argument",
));
Ok(Type::Error)
} else if !self.types_compatible(&left_type, ¶ms[0]) {
self.error(TypeError::mismatch(params[0].clone(), left_type));
Ok(Type::Error)
} else {
Ok(*return_type)
}
}
Type::Unknown | Type::Any => Ok(Type::Unknown),
_ => Ok(Type::Unknown),
}
}
// Bind operator (assignment-like)
BinaryOp::Bind => {
// Bind returns the right-hand side type
Ok(right_type)
}
// Member access operator
BinaryOp::Member => {
// Member access returns unknown (structural typing TBD)
Ok(Type::Unknown)
}
// Functor map operator <$>
BinaryOp::Map => {
// Map applies a function to a value inside a functor
// Type: (a -> b) -> f a -> f b
// For now, return Unknown (full functor support TBD)
Ok(Type::Unknown)
}
// Applicative apply operator <*>
BinaryOp::Ap => {
// Ap applies a wrapped function to a wrapped value
// Type: f (a -> b) -> f a -> f b
// For now, return Unknown (full applicative support TBD)
Ok(Type::Unknown)
}
// Logical implication
BinaryOp::Implies => {
if left_type != Type::Bool || right_type != Type::Bool {
self.error(TypeError::new(format!(
"implication requires Bool, found {} and {}",
left_type, right_type
)));
}
Ok(Type::Bool)
}
// Range operator
BinaryOp::Range => {
if !left_type.is_numeric() || !right_type.is_numeric() {
self.error(TypeError::new(format!(
"range requires numeric types, found {} and {}",
left_type, right_type
)));
}
// Return a Range type (for now, use a tuple representation)
Ok(Type::Tuple(vec![left_type, right_type]))
}
}
}
/// Infers type for function calls.
fn infer_call(&mut self, function: &Expr, args: &[Expr]) -> Result<Type, TypeError> {
let func_type = self.infer(function)?;
match func_type {
Type::Function {
params,
return_type,
} => {
// Check argument count
if args.len() != params.len() {
self.error(TypeError::new(format!(
"expected {} arguments, found {}",
params.len(),
args.len()
)));
return Ok(*return_type);
}
// Check argument types
for (i, (arg, param)) in args.iter().zip(params.iter()).enumerate() {
let arg_type = self.infer(arg)?;
if !self.types_compatible(&arg_type, param) {
self.error(TypeError::new(format!(
"argument {} has type {}, expected {}",
i, arg_type, param
)));
}
}
Ok(*return_type)
}
Type::Unknown | Type::Any => {
// Infer all arguments for side effects, return unknown
for arg in args {
let _ = self.infer(arg)?;
}
Ok(Type::Unknown)
}
_ => {
self.error(TypeError::new(format!(
"cannot call non-function type {}",
func_type
)));
Ok(Type::Error)
}
}
}
/// Infers type for lambda expressions.
fn infer_lambda(
&mut self,
params: &[(String, Option<TypeExpr>)],
body: &Expr,
return_type: Option<&TypeExpr>,
) -> Result<Type, TypeError> {
// Create child environment with parameters
let old_env = std::mem::take(&mut self.env);
self.env = old_env.child();
let param_types: Vec<Type> = params
.iter()
.map(|(name, ty_expr)| {
let ty = ty_expr
.as_ref()
.map(Type::from_type_expr)
.unwrap_or_else(|| self.fresh_var());
self.env.bind(name.clone(), ty.clone());
ty
})
.collect();
// Infer body type
let body_type = self.infer(body)?;
// Restore environment
self.env = old_env;
// Check return type annotation if present
let ret_type = if let Some(ret_expr) = return_type {
let expected = Type::from_type_expr(ret_expr);
if !self.types_compatible(&body_type, &expected) {
self.error(TypeError::mismatch(expected.clone(), body_type));
}
expected
} else {
body_type
};
Ok(Type::Function {
params: param_types,
return_type: Box::new(ret_type),
})
}
/// Infers type for if expressions.
fn infer_if(
&mut self,
condition: &Expr,
then_branch: &Expr,
else_branch: Option<&Expr>,
) -> Result<Type, TypeError> {
// Condition must be bool
let cond_type = self.infer(condition)?;
if cond_type != Type::Bool && cond_type != Type::Unknown {
self.error(TypeError::mismatch(Type::Bool, cond_type));
}
// Infer branch types
let then_type = self.infer(then_branch)?;
if let Some(else_expr) = else_branch {
let else_type = self.infer(else_expr)?;
// Both branches should have compatible types
if !self.types_compatible(&then_type, &else_type) {
self.error(TypeError::new(format!(
"if branches have incompatible types: {} and {}",
then_type, else_type
)));
}
Ok(then_type)
} else {
// No else branch, result is Void
Ok(Type::Void)
}
}
/// Infers type for match expressions.
fn infer_match(
&mut self,
scrutinee: &Expr,
arms: &[crate::ast::MatchArm],
) -> Result<Type, TypeError> {
let scrutinee_type = self.infer(scrutinee)?;
let _ = scrutinee_type; // Used for pattern checking (TODO)
if arms.is_empty() {
return Ok(Type::Void);
}
// Infer first arm's type
let first_type = self.infer_match_arm(&arms[0])?;
// Check all arms have compatible types
for arm in arms.iter().skip(1) {
let arm_type = self.infer_match_arm(arm)?;
if !self.types_compatible(&first_type, &arm_type) {
self.error(TypeError::new(format!(
"match arms have incompatible types: {} and {}",
first_type, arm_type
)));
}
}
Ok(first_type)
}
/// Infers type for a single match arm.
fn infer_match_arm(&mut self, arm: &crate::ast::MatchArm) -> Result<Type, TypeError> {
// Create child env for pattern bindings
let old_env = std::mem::take(&mut self.env);
self.env = old_env.child();
// Bind pattern variables
self.bind_pattern(&arm.pattern);
// Check guard if present
if let Some(guard) = &arm.guard {
let guard_type = self.infer(guard)?;
if guard_type != Type::Bool && guard_type != Type::Unknown {
self.error(TypeError::mismatch(Type::Bool, guard_type));
}
}
// Infer body type
let body_type = self.infer(&arm.body)?;
// Restore environment
self.env = old_env;
Ok(body_type)
}
/// Binds pattern variables to types.
fn bind_pattern(&mut self, pattern: &Pattern) {
match pattern {
Pattern::Identifier(name) => {
self.env.bind(name.clone(), Type::Unknown);
}
Pattern::Tuple(patterns) => {
for p in patterns {
self.bind_pattern(p);
}
}
Pattern::Constructor { fields, .. } => {
for p in fields {
self.bind_pattern(p);
}
}
Pattern::Or(patterns) => {
// For or-patterns, bind variables from first pattern
// (all patterns should bind the same variables)
if let Some(p) = patterns.first() {
self.bind_pattern(p);
}
}
Pattern::Literal(_) | Pattern::Wildcard => {}
}
}
/// Infers type for block expressions.
fn infer_block(
&mut self,
statements: &[Stmt],
final_expr: Option<&Expr>,
) -> Result<Type, TypeError> {
let old_env = std::mem::take(&mut self.env);
self.env = old_env.child();
// Type check statements
for stmt in statements {
self.check_stmt(stmt)?;
}
// Infer final expression type
let result_type = if let Some(expr) = final_expr {
self.infer(expr)?
} else {
Type::Void
};
self.env = old_env;
Ok(result_type)
}
/// Type checks a statement.
fn check_stmt(&mut self, stmt: &Stmt) -> Result<(), TypeError> {
match stmt {
Stmt::Let { name, value, .. } => {
let ty = self.infer(value)?;
self.env.bind(name.clone(), ty);
}
Stmt::Expr(expr) => {
let _ = self.infer(expr)?;
}
Stmt::For {
binding,
iterable,
body,
..
} => {
// Infer iterator type
let iter_type = self.infer(iterable)?;
// Create child env with binding
let old_env = std::mem::take(&mut self.env);
self.env = old_env.child();
// Bind element type (extract from iterator)
let elem_type = match iter_type {
Type::Generic { name, args } if name == "Array" && !args.is_empty() => {
args[0].clone()
}
_ => Type::Unknown,
};
self.env.bind(binding.clone(), elem_type);
// Check body
for s in body {
self.check_stmt(s)?;
}
self.env = old_env;
}
Stmt::While { condition, body } => {
let cond_type = self.infer(condition)?;
if cond_type != Type::Bool && cond_type != Type::Unknown {
self.error(TypeError::mismatch(Type::Bool, cond_type));
}
for s in body {
self.check_stmt(s)?;
}
}
Stmt::Loop { body } => {
for s in body {
self.check_stmt(s)?;
}
}
Stmt::Break | Stmt::Continue => {}
Stmt::Return(Some(e)) => {
let _ = self.infer(e)?;
}
Stmt::Return(None) => {}
_ => {} // Other statements (DOL-specific)
}
Ok(())
}
/// Promotes two numeric types to their common supertype.
fn promote_numeric(&self, left: &Type, right: &Type) -> Type {
// Float > Int, larger width wins, signed > unsigned at same width
let left_width = left.bit_width().unwrap_or(64);
let right_width = right.bit_width().unwrap_or(64);
if left.is_float() || right.is_float() {
if left_width >= 64 || right_width >= 64 {
Type::Float64
} else {
Type::Float32
}
} else {
let max_width = left_width.max(right_width);
let signed = left.is_signed() || right.is_signed();
match (max_width, signed) {
(8, true) => Type::Int8,
(8, false) => Type::UInt8,
(16, true) => Type::Int16,
(16, false) => Type::UInt16,
(32, true) => Type::Int32,
(32, false) => Type::UInt32,
(_, true) => Type::Int64,
(_, false) => Type::UInt64,
}
}
}
/// Checks if two types are compatible.
#[allow(clippy::only_used_in_recursion)]
fn types_compatible(&self, ty1: &Type, ty2: &Type) -> bool {
match (ty1, ty2) {
(Type::Unknown, _) | (_, Type::Unknown) => true,
(Type::Any, _) | (_, Type::Any) => true,
(Type::Error, _) | (_, Type::Error) => true,
(Type::Var(a), Type::Var(b)) => a == b,
(a, b) if a == b => true,
// Numeric coercion
(a, b) if a.is_numeric() && b.is_numeric() => true,
// Generic types
(Type::Generic { name: n1, args: a1 }, Type::Generic { name: n2, args: a2 }) => {
n1 == n2
&& a1.len() == a2.len()
&& a1.iter().zip(a2).all(|(x, y)| self.types_compatible(x, y))
}
// Function types
(
Type::Function {
params: p1,
return_type: r1,
},
Type::Function {
params: p2,
return_type: r2,
},
) => {
p1.len() == p2.len()
&& p1.iter().zip(p2).all(|(x, y)| self.types_compatible(x, y))
&& self.types_compatible(r1, r2)
}
// Tuples
(Type::Tuple(t1), Type::Tuple(t2)) => {
t1.len() == t2.len() && t1.iter().zip(t2).all(|(x, y)| self.types_compatible(x, y))
}
_ => false,
}
}
/// Checks an expression against an expected type.
pub fn check(&mut self, expr: &Expr, expected: &Type) -> Result<(), TypeError> {
let actual = self.infer(expr)?;
if !self.types_compatible(&actual, expected) {
self.error(TypeError::mismatch(expected.clone(), actual));
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
fn int_lit(n: i64) -> Expr {
Expr::Literal(Literal::Int(n))
}
fn float_lit(n: f64) -> Expr {
Expr::Literal(Literal::Float(n))
}
fn bool_lit(b: bool) -> Expr {
Expr::Literal(Literal::Bool(b))
}
fn string_lit(s: &str) -> Expr {
Expr::Literal(Literal::String(s.to_string()))
}
#[test]
fn test_infer_literals() {
let mut checker = TypeChecker::new();
assert_eq!(checker.infer(&int_lit(42)).unwrap(), Type::Int64);
assert_eq!(checker.infer(&float_lit(1.5)).unwrap(), Type::Float64);
assert_eq!(checker.infer(&bool_lit(true)).unwrap(), Type::Bool);
assert_eq!(checker.infer(&string_lit("hello")).unwrap(), Type::String);
}
#[test]
fn test_infer_arithmetic() {
let mut checker = TypeChecker::new();
let expr = Expr::Binary {
op: BinaryOp::Add,
left: Box::new(int_lit(1)),
right: Box::new(int_lit(2)),
};
assert!(checker.infer(&expr).unwrap().is_integer());
}
#[test]
fn test_infer_comparison() {
let mut checker = TypeChecker::new();
let expr = Expr::Binary {
op: BinaryOp::Lt,
left: Box::new(int_lit(1)),
right: Box::new(int_lit(2)),
};
assert_eq!(checker.infer(&expr).unwrap(), Type::Bool);
}
#[test]
fn test_infer_lambda() {
let mut checker = TypeChecker::new();
let lambda = Expr::Lambda {
params: vec![("x".to_string(), Some(TypeExpr::Named("Int32".to_string())))],
body: Box::new(Expr::Identifier("x".to_string())),
return_type: None,
};
let ty = checker.infer(&lambda).unwrap();
match ty {
Type::Function { params, .. } => {
assert_eq!(params.len(), 1);
assert_eq!(params[0], Type::Int32);
}
_ => panic!("Expected function type"),
}
}
#[test]
fn test_infer_if() {
let mut checker = TypeChecker::new();
let if_expr = Expr::If {
condition: Box::new(bool_lit(true)),
then_branch: Box::new(int_lit(1)),
else_branch: Some(Box::new(int_lit(2))),
};
let ty = checker.infer(&if_expr).unwrap();
assert!(ty.is_integer());
}
#[test]
fn test_type_mismatch_error() {
let mut checker = TypeChecker::new();
let if_expr = Expr::If {
condition: Box::new(int_lit(42)), // Should be Bool!
then_branch: Box::new(int_lit(1)),
else_branch: None,
};
let _ = checker.infer(&if_expr);
assert!(!checker.is_ok());
}
#[test]
fn test_variable_binding() {
let mut checker = TypeChecker::new();
checker.env.bind("x", Type::Int32);
let expr = Expr::Identifier("x".to_string());
assert_eq!(checker.infer(&expr).unwrap(), Type::Int32);
}
#[test]
fn test_undefined_variable() {
let mut checker = TypeChecker::new();
let expr = Expr::Identifier("undefined".to_string());
assert!(checker.infer(&expr).is_err());
}
#[test]
fn test_type_from_type_expr() {
let type_expr = TypeExpr::Function {
params: vec![TypeExpr::Named("Int32".to_string())],
return_type: Box::new(TypeExpr::Named("Bool".to_string())),
};
let ty = Type::from_type_expr(&type_expr);
match ty {
Type::Function {
params,
return_type,
} => {
assert_eq!(params.len(), 1);
assert_eq!(params[0], Type::Int32);
assert_eq!(*return_type, Type::Bool);
}
_ => panic!("Expected function type"),
}
}
#[test]
fn test_quote_preserves_inner_type() {
let mut checker = TypeChecker::new();
// Quote of Int64 should return Quoted<Int64>
let expr = Expr::Quote(Box::new(int_lit(42)));
let ty = checker.infer(&expr).unwrap();
match ty {
Type::Generic { name, args } => {
assert_eq!(name, "Quoted");
assert_eq!(args.len(), 1);
assert_eq!(args[0], Type::Int64);
}
_ => panic!("Expected Quoted<Int64>, found {}", ty),
}
}
#[test]
fn test_quote_bool() {
let mut checker = TypeChecker::new();
// Quote of Bool should return Quoted<Bool>
let expr = Expr::Quote(Box::new(bool_lit(true)));
let ty = checker.infer(&expr).unwrap();
match ty {
Type::Generic { name, args } => {
assert_eq!(name, "Quoted");
assert_eq!(args.len(), 1);
assert_eq!(args[0], Type::Bool);
}
_ => panic!("Expected Quoted<Bool>, found {}", ty),
}
}
#[test]
fn test_eval_unwraps_quoted_type() {
let mut checker = TypeChecker::new();
// Eval of Quoted<Int64> should return Int64
let quoted_expr = Expr::Quote(Box::new(int_lit(42)));
let eval_expr = Expr::Eval(Box::new(quoted_expr));
let ty = checker.infer(&eval_expr).unwrap();
assert_eq!(ty, Type::Int64);
}
#[test]
fn test_eval_of_non_quoted_type_errors() {
let mut checker = TypeChecker::new();
// Eval of Int64 (non-quoted) should produce error
let eval_expr = Expr::Eval(Box::new(int_lit(42)));
let ty = checker.infer(&eval_expr).unwrap();
assert_eq!(ty, Type::Error);
assert!(!checker.is_ok());
assert!(!checker.errors().is_empty());
assert!(checker.errors()[0]
.message
.contains("cannot eval non-quoted type"));
}
#[test]
fn test_unary_quote_preserves_type() {
let mut checker = TypeChecker::new();
// Unary quote operator should also preserve type
let expr = Expr::Unary {
op: UnaryOp::Quote,
operand: Box::new(string_lit("hello")),
};
let ty = checker.infer(&expr).unwrap();
match ty {
Type::Generic { name, args } => {
assert_eq!(name, "Quoted");
assert_eq!(args.len(), 1);
assert_eq!(args[0], Type::String);
}
_ => panic!("Expected Quoted<String>, found {}", ty),
}
}
#[test]
fn test_quasiquote_preserves_type() {
let mut checker = TypeChecker::new();
// QuasiQuote should work like Quote
let expr = Expr::QuasiQuote(Box::new(float_lit(1.5)));
let ty = checker.infer(&expr).unwrap();
match ty {
Type::Generic { name, args } => {
assert_eq!(name, "Quoted");
assert_eq!(args.len(), 1);
assert_eq!(args[0], Type::Float64);
}
_ => panic!("Expected Quoted<Float64>, found {}", ty),
}
}
#[test]
fn test_unquote_evaluates_inner_expr() {
let mut checker = TypeChecker::new();
// Unquote should return the type of the inner expression
let expr = Expr::Unquote(Box::new(int_lit(42)));
let ty = checker.infer(&expr).unwrap();
assert_eq!(ty, Type::Int64);
}
#[test]
fn test_nested_quote_eval() {
let mut checker = TypeChecker::new();
// Quote(Eval(Quote(42))) should be Quoted<Int64>
let inner_quote = Expr::Quote(Box::new(int_lit(42)));
let eval_expr = Expr::Eval(Box::new(inner_quote));
let outer_quote = Expr::Quote(Box::new(eval_expr));
let ty = checker.infer(&outer_quote).unwrap();
match ty {
Type::Generic { name, args } => {
assert_eq!(name, "Quoted");
assert_eq!(args.len(), 1);
assert_eq!(args[0], Type::Int64);
}
_ => panic!("Expected Quoted<Int64>, found {}", ty),
}
}
#[test]
fn test_effect_context_default() {
let checker = TypeChecker::new();
assert_eq!(checker.current_effect_context(), EffectContext::Pure);
assert!(!checker.in_sex_context());
}
#[test]
fn test_effect_context_enter_exit() {
let mut checker = TypeChecker::new();
// Initially pure
assert!(!checker.in_sex_context());
// Enter sex context
checker.enter_sex_context();
assert!(checker.in_sex_context());
assert_eq!(checker.current_effect_context(), EffectContext::Sex);
// Exit sex context
checker.exit_sex_context();
assert!(!checker.in_sex_context());
assert_eq!(checker.current_effect_context(), EffectContext::Pure);
}
#[test]
fn test_nested_effect_contexts() {
let mut checker = TypeChecker::new();
// Pure -> Sex -> Sex -> Pure (stack)
checker.enter_sex_context();
assert!(checker.in_sex_context());
checker.enter_sex_context();
assert!(checker.in_sex_context());
checker.exit_sex_context();
assert!(checker.in_sex_context());
checker.exit_sex_context();
assert!(!checker.in_sex_context());
}
#[test]
fn test_sex_block_inference() {
let mut checker = TypeChecker::new();
// Create a sex block with an integer literal
let sex_block = Expr::SexBlock(Block {
statements: vec![],
final_expr: Some(Box::new(int_lit(42))),
span: Span::default(),
});
let ty = checker.infer(&sex_block).unwrap();
assert_eq!(ty, Type::Int64);
// Should be back in pure context after inference
assert!(!checker.in_sex_context());
}
}