assura-types 0.4.3

Type checking for the Assura contract language
Documentation
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
//! Expression type inference.
//!
//! Implements `infer_expr` which infers the type of an Assura expression
//! in a given type environment. Covers literals, variables, field access,
//! binary/unary operations, function calls, quantifiers, and more.

use assura_parser::ast::{BinOp, Expr, Literal, SpExpr, Span, UnaryOp};

use crate::clauses::bind_pattern_vars;
use crate::{Type, TypeEnv, TypeError, parse_type_tokens};

// ---------------------------------------------------------------------------
// Expression type inference
// ---------------------------------------------------------------------------

/// Returns `true` if `ty` is a numeric type.
/// Check if an expression is a literal zero (integer 0 or float 0.0).
pub(crate) fn is_literal_zero(expr: &SpExpr) -> bool {
    match &expr.node {
        Expr::Literal(Literal::Int(s)) => s == "0",
        Expr::Literal(Literal::Float(s)) => s == "0.0" || s == "0",
        _ => false,
    }
}

/// Extract the element type from a collection type.
///
/// Used to type quantifier variables: `forall x in xs` where `xs: List<T>`
/// binds `x` to `T`. For range domains (`a..b` parsed as `BinOp::Range`),
/// the element type is `Int`. For non-collection types, returns `Unknown`.
pub(crate) fn element_type_of(domain_ty: &Type) -> Type {
    match domain_ty {
        Type::List(elem) | Type::Set(elem) | Type::Sequence(elem) => *elem.clone(),
        Type::Map(key, _) => *key.clone(),
        Type::Int | Type::Nat => Type::Int, // range domain
        // String indexable by code points
        Type::String => Type::String,
        _ => Type::Unknown,
    }
}

pub(crate) fn is_numeric(ty: &Type) -> bool {
    match ty {
        Type::Int
        | Type::Nat
        | Type::Float
        | Type::U8
        | Type::U16
        | Type::U32
        | Type::U64
        | Type::I8
        | Type::I16
        | Type::I32
        | Type::I64
        | Type::F32
        | Type::F64 => true,
        // A refined type is numeric if its base type is numeric
        Type::Refined { base, .. } => is_numeric(base),
        // Named types may be numeric aliases; be lenient.
        // Error/Unknown suppress diagnostics.
        Type::Named(_) | Type::Unknown | Type::Error => true,
        _ => false,
    }
}

/// Infer the type of an expression given a type environment.
///
/// Returns `Ok(ty)` with the inferred type, or `Err(TypeError)` when a
/// concrete type mismatch is detected (A03001). Unknown types (from
/// unresolved references) are propagated silently; they never trigger
/// errors.
///
/// Errors produced by this overload use `0..0` spans. For proper source
/// locations, use `infer_expr_spanned` with the enclosing clause span.
pub fn infer_expr(expr: &SpExpr, env: &TypeEnv) -> Result<Type, TypeError> {
    infer_expr_spanned(expr, env, 0..0)
}

/// Infer the type of an expression with a context/fallback span for error reporting.
///
/// When the expression has a real (non-zero) span from lowering (post 11.04),
/// errors use the expression's own span for precision (e.g. pointing to the
/// bad sub-expression rather than the whole clause/decl).
/// The passed `span` is used as fallback for recovery nodes (span 0..0) or
/// top-level clause context.
pub fn infer_expr_spanned(expr: &SpExpr, env: &TypeEnv, span: Span) -> Result<Type, TypeError> {
    match &expr.node {
        // --- Literals ---
        Expr::Literal(Literal::Int(_)) => Ok(Type::Int),
        Expr::Literal(Literal::Float(_)) => Ok(Type::Float),
        Expr::Literal(Literal::Str(_)) => Ok(Type::String),
        Expr::Literal(Literal::Bool(_)) => Ok(Type::Bool),

        // --- Identifiers ---
        Expr::Ident(name) => {
            // Boolean literals
            if name == "true" || name == "false" {
                return Ok(Type::Bool);
            }
            // Look up in environment (includes `result` when bound by
            // ensures clause context, and `self` if added by method context)
            Ok(env.lookup(name).cloned().unwrap_or(Type::Unknown))
        }

        // --- Binary operations ---
        Expr::BinOp { lhs, op, rhs } => infer_binop(lhs, op, rhs, env, span.clone()),

        // --- Unary operations ---
        Expr::UnaryOp { op, expr: inner } => {
            let inner_ty = infer_expr_spanned(inner, env, inner.span.clone())?;
            match op {
                UnaryOp::Neg => {
                    if inner_ty.is_indeterminate() || is_numeric(&inner_ty) {
                        Ok(inner_ty)
                    } else {
                        Err(TypeError {
                            code: "A03001".into(),
                            message: format!(
                                "unary `-` requires a numeric type, found `{inner_ty}`"
                            ),
                            span: inner.span.clone(),
                            secondary: None,
                            suggestion: None,
                        })
                    }
                }
                UnaryOp::Not => {
                    if inner_ty.is_indeterminate() || inner_ty == Type::Bool {
                        Ok(Type::Bool)
                    } else {
                        Err(TypeError {
                            code: "A03001".into(),
                            message: format!("unary `!` requires Bool, found `{inner_ty}`"),
                            span: inner.span.clone(),
                            secondary: None,
                            suggestion: None,
                        })
                    }
                }
            }
        }

        // --- If-then-else ---
        Expr::If {
            cond,
            then_branch,
            else_branch,
        } => {
            let cond_ty = infer_expr_spanned(cond, env, cond.span.clone())?;
            if !cond_ty.is_indeterminate() && cond_ty != Type::Bool {
                return Err(TypeError {
                    code: "A03001".into(),
                    message: format!("if condition must be Bool, found `{cond_ty}`"),
                    span: cond.span.clone(),
                    secondary: None,
                    suggestion: None,
                });
            }
            let then_ty = infer_expr_spanned(then_branch, env, then_branch.span.clone())?;
            if let Some(else_br) = else_branch {
                let else_ty = infer_expr_spanned(else_br, env, else_br.span.clone())?;
                if then_ty.is_indeterminate() {
                    Ok(else_ty)
                } else if else_ty.is_indeterminate() || types_compatible(&then_ty, &else_ty) {
                    Ok(then_ty)
                } else {
                    Err(TypeError {
                        code: "A03001".into(),
                        message: format!(
                            "if branches have different types: `{then_ty}` vs `{else_ty}`"
                        ),
                        span: then_branch.span.clone(), // or whole if; using then for now
                        secondary: None,
                        suggestion: None,
                    })
                }
            } else {
                Ok(then_ty)
            }
        }

        // --- Quantifiers ---
        Expr::Forall { var, domain, body } | Expr::Exists { var, domain, body } => {
            // Infer the domain type to determine the quantified variable's type.
            let domain_ty = infer_expr_spanned(domain, env, domain.span.clone())?;
            let elem_ty = element_type_of(&domain_ty);
            // Bind the quantified variable in a child environment so the
            // body can reference it with the correct type.
            let mut child_env = env.clone();
            child_env.insert(var.clone(), elem_ty);
            let _body_ty = infer_expr_spanned(body, &child_env, body.span.clone())?;
            Ok(Type::Bool)
        }

        // --- old(expr) ---
        Expr::Old(inner) => infer_expr_spanned(inner, env, inner.span.clone()),

        // --- List literal ---
        Expr::List(items) => {
            if items.is_empty() {
                return Ok(Type::List(Box::new(Type::Unknown)));
            }
            let first_ty = infer_expr_spanned(&items[0], env, items[0].span.clone())?;
            // Check remaining items match the first
            for item in &items[1..] {
                let item_ty = infer_expr_spanned(item, env, item.span.clone())?;
                if !item_ty.is_indeterminate()
                    && !first_ty.is_indeterminate()
                    && item_ty != first_ty
                {
                    return Err(TypeError {
                        code: "A03001".into(),
                        message: format!(
                            "list element type mismatch: expected `{first_ty}`, found `{item_ty}`"
                        ),
                        span: item.span.clone(),
                        secondary: None,
                        suggestion: None,
                    });
                }
            }
            Ok(Type::List(Box::new(first_ty)))
        }

        // --- Field access ---
        Expr::Field(receiver, field) => {
            let recv_ty = infer_expr_spanned(receiver, env, receiver.span.clone())?;
            // Try to resolve the field on the receiver's type
            let struct_name = match &recv_ty {
                Type::Named(name) => Some(name.as_str()),
                Type::Refined { base, .. } => {
                    if let Type::Named(name) = base.as_ref() {
                        Some(name.as_str())
                    } else {
                        None
                    }
                }
                _ => None,
            };
            if let Some(sname) = struct_name
                && let Some(field_ty) = env.lookup_field(sname, field)
            {
                return Ok(field_ty.clone());
            }
            // Built-in field resolution on known types
            match &recv_ty {
                // List/Sequence: len/length/size -> Nat, head/first/last -> Option<T>
                Type::List(elem) | Type::Sequence(elem) => match field.as_str() {
                    "len" | "length" | "size" | "capacity" => return Ok(Type::Nat),
                    "is_empty" => return Ok(Type::Bool),
                    "head" | "first" | "last" => {
                        return Ok(Type::Option(elem.clone()));
                    }
                    "tail" | "rest" => return Ok(recv_ty),
                    _ => {}
                },
                // String/Bytes: len/length/size -> Nat
                Type::Bytes | Type::String => match field.as_str() {
                    "len" | "length" | "size" | "capacity" => return Ok(Type::Nat),
                    "is_empty" => return Ok(Type::Bool),
                    _ => {}
                },
                // Option<T>: value -> T, is_some/is_none -> Bool
                Type::Option(inner) => match field.as_str() {
                    "value" | "unwrap" => return Ok(*inner.clone()),
                    "is_some" | "is_none" => return Ok(Type::Bool),
                    _ => {}
                },
                // Result<T, E>: value/ok -> T, error/err -> E, is_ok/is_err -> Bool
                Type::Result(ok_ty, err_ty) => match field.as_str() {
                    "value" | "ok" | "unwrap" => return Ok(*ok_ty.clone()),
                    "error" | "err" => return Ok(*err_ty.clone()),
                    "is_ok" | "is_err" => return Ok(Type::Bool),
                    _ => {}
                },
                // Map: len/size -> Nat, is_empty -> Bool, keys/values
                Type::Map(key_ty, val_ty) => match field.as_str() {
                    "len" | "size" => return Ok(Type::Nat),
                    "is_empty" => return Ok(Type::Bool),
                    "keys" => return Ok(Type::List(key_ty.clone())),
                    "values" => return Ok(Type::List(val_ty.clone())),
                    _ => {}
                },
                // Set: len/size -> Nat, is_empty -> Bool
                Type::Set(_) => match field.as_str() {
                    "len" | "size" => return Ok(Type::Nat),
                    "is_empty" => return Ok(Type::Bool),
                    _ => {}
                },
                // Tuple: numeric field access (0, 1, 2, ...)
                Type::Tuple(elems) => {
                    if let Ok(idx) = field.parse::<usize>() {
                        if idx < elems.len() {
                            return Ok(elems[idx].clone());
                        }
                        return Err(TypeError {
                            code: "A03005".into(),
                            message: format!(
                                "tuple index `{field}` out of range for type `{recv_ty}` (arity {})",
                                elems.len()
                            ),
                            span: span.clone(),
                            secondary: None,
                            suggestion: None,
                        });
                    }
                    return Err(TypeError {
                        code: "A03005".into(),
                        message: format!(
                            "unknown field `{field}` on tuple type `{recv_ty}` (use 0, 1, …)"
                        ),
                        span: span.clone(),
                        secondary: None,
                        suggestion: None,
                    });
                }
                _ => {}
            }
            // If the receiver type is known and concrete (struct with
            // registered fields, or a built-in type), emit A03005.
            // For Named types without registered fields we stay lenient.
            if let Some(sname) = struct_name
                && env.struct_fields.contains_key(sname)
            {
                return Err(TypeError {
                    code: "A03005".into(),
                    message: format!("unknown field `{field}` in type `{recv_ty}`"),
                    span: span.clone(),
                    secondary: None,
                    suggestion: None,
                });
            }
            match &recv_ty {
                Type::List(_)
                | Type::Sequence(_)
                | Type::Bytes
                | Type::Set(_)
                | Type::Option(_)
                | Type::Result(_, _)
                | Type::Map(_, _) => {
                    return Err(TypeError {
                        code: "A03005".into(),
                        message: format!("unknown field `{field}` on type `{recv_ty}`"),
                        span: span.clone(),
                        secondary: None,
                        suggestion: None,
                    });
                }
                _ => {}
            }
            // Suppress cascading on Error
            if recv_ty == Type::Error {
                return Ok(Type::Error);
            }
            Ok(Type::Unknown)
        }

        // --- Method call ---
        Expr::MethodCall {
            receiver,
            method,
            args,
        } => {
            let recv_ty = infer_expr_spanned(receiver, env, receiver.span.clone())?;
            // Infer argument types to surface errors inside them
            for arg in args {
                let _ = infer_expr_spanned(arg, env, arg.span.clone())?;
            }
            // Try to resolve the method as a known function in the env
            if let Some(Type::Fn { ret, .. }) = env.lookup(method) {
                return Ok(*ret.clone());
            }
            // Common collection methods with known return types
            match &recv_ty {
                Type::List(elem) | Type::Sequence(elem) => match method.as_str() {
                    "len" | "length" | "size" | "count" => return Ok(Type::Nat),
                    "get" | "first" | "last" | "index" => {
                        return Ok(Type::Option(elem.clone()));
                    }
                    "pop" => return Ok(Type::Option(elem.clone())),
                    "set" | "push" | "append" | "remove" | "clear" | "insert_at" | "remove_at" => {
                        return Ok(Type::Unit);
                    }
                    "contains" | "is_empty" | "any" | "all" => return Ok(Type::Bool),
                    "map" | "filter" | "take" | "skip" | "reverse" | "sort" | "slice" | "dedup"
                    | "flatten" => {
                        return Ok(recv_ty);
                    }
                    "fold" | "reduce" => {
                        // fold/reduce: return type from accumulator (first arg or elem)
                        let acc_ty = if let Some(first_arg) = args.first() {
                            infer_expr_spanned(first_arg, env, first_arg.span.clone())
                                .unwrap_or(*elem.clone())
                        } else {
                            *elem.clone()
                        };
                        return Ok(acc_ty);
                    }
                    "zip" => {
                        // zip(other_list) -> List<(T, U)>
                        let other_elem = if let Some(first_arg) = args.first() {
                            let arg_ty = infer_expr_spanned(first_arg, env, first_arg.span.clone())
                                .unwrap_or(Type::Unknown);
                            if let Type::List(inner) | Type::Sequence(inner) = arg_ty {
                                *inner
                            } else {
                                arg_ty
                            }
                        } else {
                            Type::Unknown
                        };
                        return Ok(Type::List(Box::new(Type::Tuple(vec![
                            *elem.clone(),
                            other_elem,
                        ]))));
                    }
                    "enumerate" => {
                        return Ok(Type::List(Box::new(Type::Tuple(vec![
                            Type::Nat,
                            *elem.clone(),
                        ]))));
                    }
                    "find" => return Ok(Type::Option(elem.clone())),
                    "position" | "index_of" => return Ok(Type::Option(Box::new(Type::Nat))),
                    "iter" => return Ok(recv_ty),
                    _ => {}
                },
                Type::Map(key, val) => match method.as_str() {
                    "get" | "get_or_default" => return Ok(Type::Option(val.clone())),
                    "contains_key" | "contains_value" | "is_empty" => return Ok(Type::Bool),
                    "len" | "size" => return Ok(Type::Nat),
                    "keys" => return Ok(Type::Set(key.clone())),
                    "values" => return Ok(Type::List(val.clone())),
                    "entries" => {
                        return Ok(Type::List(Box::new(Type::Tuple(vec![
                            *key.clone(),
                            *val.clone(),
                        ]))));
                    }
                    "insert" | "remove" | "clear" | "update" => return Ok(Type::Unit),
                    "merge" => return Ok(recv_ty),
                    "map_values" => return Ok(recv_ty),
                    _ => {}
                },
                Type::Set(elem) => match method.as_str() {
                    "contains" | "is_empty" | "is_subset" | "is_superset" | "is_disjoint" => {
                        return Ok(Type::Bool);
                    }
                    "len" | "size" => return Ok(Type::Nat),
                    "insert" | "add" | "remove" | "clear" => return Ok(Type::Unit),
                    "union" | "intersection" | "difference" | "symmetric_difference" => {
                        return Ok(recv_ty);
                    }
                    "to_list" | "iter" => return Ok(Type::List(elem.clone())),
                    _ => {}
                },
                Type::String => match method.as_str() {
                    "len" | "length" => return Ok(Type::Nat),
                    "contains" | "starts_with" | "ends_with" | "is_empty" => {
                        return Ok(Type::Bool);
                    }
                    "to_uppercase" | "to_lowercase" | "trim" | "substring" | "replace"
                    | "concat" | "repeat" => {
                        return Ok(Type::String);
                    }
                    "split" | "chars" => return Ok(Type::List(Box::new(Type::String))),
                    "parse_int" => return Ok(Type::Option(Box::new(Type::Int))),
                    _ => {}
                },
                Type::Option(inner) => match method.as_str() {
                    "unwrap" | "unwrap_or" | "unwrap_or_default" | "expect" => {
                        return Ok(*inner.clone());
                    }
                    "is_some" | "is_none" => return Ok(Type::Bool),
                    "map" => {
                        // Option<T>.map(f) -> Option<ReturnType(f)>
                        // If arg is a known function, infer its return type.
                        let mapped_ty = infer_closure_return_from_args(args, env);
                        return Ok(Type::Option(Box::new(mapped_ty)));
                    }
                    "and_then" => {
                        // and_then returns Option<U> where U is from closure
                        let mapped_ty = infer_closure_return_from_args(args, env);
                        if let Type::Option(_) = &mapped_ty {
                            return Ok(mapped_ty);
                        }
                        return Ok(Type::Option(Box::new(mapped_ty)));
                    }
                    "or_else" => return Ok(recv_ty),
                    "filter" => return Ok(recv_ty),
                    "flatten" => {
                        // Option<Option<T>>.flatten() => Option<T>
                        if let Type::Option(inner2) = inner.as_ref() {
                            return Ok(Type::Option(inner2.clone()));
                        }
                        return Ok(recv_ty);
                    }
                    "ok_or" | "ok_or_else" => {
                        // Infer the error type from the first arg if possible
                        let err_ty = if let Some(first_arg) = args.first() {
                            infer_expr_spanned(first_arg, env, first_arg.span.clone())
                                .unwrap_or(Type::Unknown)
                        } else {
                            Type::Unknown
                        };
                        return Ok(Type::Result(inner.clone(), Box::new(err_ty)));
                    }
                    "zip" => {
                        // Option<T>.zip(Option<U>) -> Option<(T, U)>
                        let other_ty = if let Some(first_arg) = args.first() {
                            let arg_ty = infer_expr_spanned(first_arg, env, first_arg.span.clone())
                                .unwrap_or(Type::Unknown);
                            if let Type::Option(other_inner) = arg_ty {
                                *other_inner
                            } else {
                                arg_ty
                            }
                        } else {
                            Type::Unknown
                        };
                        return Ok(Type::Option(Box::new(Type::Tuple(vec![
                            *inner.clone(),
                            other_ty,
                        ]))));
                    }
                    _ => {}
                },
                Type::Bytes => match method.as_str() {
                    "len" | "length" | "size" => return Ok(Type::Nat),
                    "is_empty" => return Ok(Type::Bool),
                    "slice" => return Ok(Type::Bytes),
                    _ => {}
                },
                Type::Result(ok_ty, err_ty) => match method.as_str() {
                    "unwrap" | "unwrap_or" | "unwrap_or_default" | "expect" => {
                        return Ok(*ok_ty.clone());
                    }
                    "unwrap_err" | "expect_err" => return Ok(*err_ty.clone()),
                    "is_ok" | "is_err" => return Ok(Type::Bool),
                    "map" => {
                        // Result<T,E>.map(f) -> Result<ReturnType(f), E>
                        let mapped_ty = infer_closure_return_from_args(args, env);
                        return Ok(Type::Result(Box::new(mapped_ty), err_ty.clone()));
                    }
                    "and_then" => {
                        let mapped_ty = infer_closure_return_from_args(args, env);
                        if let Type::Result(_, _) = &mapped_ty {
                            return Ok(mapped_ty);
                        }
                        return Ok(Type::Result(Box::new(mapped_ty), err_ty.clone()));
                    }
                    "map_err" => {
                        let mapped_ty = infer_closure_return_from_args(args, env);
                        return Ok(Type::Result(ok_ty.clone(), Box::new(mapped_ty)));
                    }
                    "or_else" => {
                        return Ok(Type::Result(ok_ty.clone(), Box::new(Type::Unknown)));
                    }
                    "ok" => return Ok(Type::Option(ok_ty.clone())),
                    "err" => return Ok(Type::Option(err_ty.clone())),
                    _ => {}
                },
                _ => {}
            }
            // For Named types (user-defined), try common standard methods
            // before returning Unknown. The actual type definition may not
            // be registered, but these methods are universally available.
            if matches!(&recv_ty, Type::Named(_)) {
                match method.as_str() {
                    "len" | "length" | "size" | "count" => return Ok(Type::Nat),
                    "is_empty" | "contains" | "any" | "all" => return Ok(Type::Bool),
                    "clone" => return Ok(recv_ty),
                    "to_string" => return Ok(Type::String),
                    _ => {}
                }
            }
            // Emit A03005 for unknown methods on concrete built-in types
            match &recv_ty {
                Type::List(_)
                | Type::Sequence(_)
                | Type::String
                | Type::Bytes
                | Type::Set(_)
                | Type::Option(_)
                | Type::Result(_, _)
                | Type::Map(_, _) => {
                    return Err(TypeError {
                        code: "A03005".into(),
                        message: format!("unknown method `{method}` on type `{recv_ty}`"),
                        span: span.clone(),
                        secondary: None,
                        suggestion: None,
                    });
                }
                _ => {}
            }
            // For Error/Unknown receivers, suppress cascading errors
            if recv_ty.is_indeterminate() {
                return Ok(recv_ty);
            }
            // Named types: genuinely unknown, may have methods we don't know about
            Ok(Type::Unknown)
        }

        // --- Function call ---
        Expr::Call { func, args } => infer_call(func, args, env, func.span.clone()),

        // --- Index access ---
        Expr::Index { expr: base, index } => {
            let base_ty = infer_expr_spanned(base, env, base.span.clone())?;
            // Infer index type to surface errors inside it.
            let _index_ty = infer_expr_spanned(index, env, index.span.clone())?;
            match &base_ty {
                Type::List(elem) => Ok(*elem.clone()),
                Type::Map(_key, val) => Ok(*val.clone()),
                Type::Sequence(elem) => Ok(*elem.clone()),
                Type::Bytes => Ok(Type::U8),
                // Tuple indexing with literal index
                Type::Tuple(elems) => {
                    if let Expr::Literal(Literal::Int(idx_str)) = &index.as_ref().node
                        && let Ok(idx) = idx_str.parse::<usize>()
                        && idx < elems.len()
                    {
                        return Ok(elems[idx].clone());
                    }
                    // Non-literal or out-of-bounds: return Unknown
                    Ok(Type::Unknown)
                }
                // Types that cannot be indexed
                Type::Bool | Type::Unit | Type::Never | Type::Float | Type::F32 | Type::F64 => {
                    Err(TypeError {
                        code: "A03005".into(),
                        message: format!("type `{base_ty}` cannot be indexed"),
                        span: span.clone(),
                        secondary: None,
                        suggestion: None,
                    })
                }
                // Error: suppress cascading errors
                Type::Error => Ok(Type::Error),
                // Unknown, Named, TypeParam, or user-defined: return Unknown.
                _ => Ok(Type::Unknown),
            }
        }

        // --- Cast: infer from target type annotation ---
        Expr::Cast { expr: inner, ty } => {
            // Type-check the inner expression for side effects
            let _ = infer_expr_spanned(inner, env, inner.span.clone())?;
            // Parse the target type from the cast annotation
            Ok(parse_type_tokens(std::slice::from_ref(ty)))
        }

        // --- Apply lemma: type-check args, result is Bool (adds assumption) ---
        Expr::Apply { args, .. } => {
            for arg in args {
                let _ = infer_expr_spanned(arg, env, arg.span.clone())?;
            }
            // apply expressions contribute assumptions; they have Bool type
            // in the verification domain
            Ok(Type::Bool)
        }

        // --- Ghost block: type-check inner, result is Unit (erased at runtime) ---
        Expr::Ghost(inner) => {
            // Type-check the inner expression (it must be valid in the
            // verification domain) but the ghost block itself evaluates
            // to Unit since it is erased at runtime.
            let _inner_ty = infer_expr_spanned(inner, env, inner.span.clone())?;
            Ok(Type::Unit)
        }

        // --- Match: bind pattern variables and infer all arm types ---
        Expr::Match { scrutinee, arms } => {
            let scrut_ty = infer_expr_spanned(scrutinee, env, scrutinee.span.clone())?;
            let mut result_ty = Type::Unknown;
            for arm in arms {
                // Create a new env with pattern bindings
                let mut arm_env = env.clone();
                // Use arm body span for arity errors when pattern span is absent.
                bind_pattern_vars(&arm.pattern, &scrut_ty, &mut arm_env, arm.body.span.clone())?;
                let arm_ty = infer_expr_spanned(&arm.body, &arm_env, arm.body.span.clone())?;
                if arm_ty.is_indeterminate() {
                    continue;
                }
                if result_ty.is_indeterminate() {
                    result_ty = arm_ty;
                } else if !types_compatible(&result_ty, &arm_ty) {
                    return Err(TypeError {
                        code: "A03001".into(),
                        message: format!(
                            "match arm type `{arm_ty}` is incompatible with \
                             previous arm type `{result_ty}`"
                        ),
                        span: span.clone(),
                        secondary: None,
                        suggestion: None,
                    });
                }
            }
            Ok(result_ty)
        }

        // --- Let binding: bind value type, infer body type ---
        Expr::Let { name, value, body } => {
            let val_ty = infer_expr_spanned(value, env, value.span.clone())?;
            let mut inner_env = env.clone();
            inner_env.insert(name.clone(), val_ty);
            infer_expr_spanned(body, &inner_env, body.span.clone())
        }

        // --- Tuple: infer element types ---
        Expr::Tuple(elems) => {
            let mut elem_types = Vec::with_capacity(elems.len());
            for elem in elems {
                elem_types.push(infer_expr_spanned(elem, env, elem.span.clone())?);
            }
            Ok(Type::Tuple(elem_types))
        }

        // --- Block: infer type of last expression ---
        Expr::Block(exprs) => {
            let mut last_ty = Type::Unit;
            for e in exprs {
                last_ty = infer_expr_spanned(e, env, e.span.clone())?;
            }
            Ok(last_ty)
        }

        // --- Raw: cannot infer from unparsed token sequence ---
        Expr::Raw(_) => Ok(Type::Error),
    }
}

/// Check if two types are compatible for comparison/arithmetic purposes.
///
/// Types are compatible if:
/// - They are equal
/// - Either side is `Unknown`
/// - Either side is a `Named` type (user-defined, not yet resolved)
/// - A `Refined` type's base matches the other type
/// - Both are numeric
pub(crate) fn types_compatible(a: &Type, b: &Type) -> bool {
    if a == b {
        return true;
    }
    if a.is_indeterminate() || b.is_indeterminate() {
        return true;
    }
    // Named types are unresolved user-defined; be lenient
    if matches!(a, Type::Named(_)) || matches!(b, Type::Named(_)) {
        return true;
    }
    // Refined types are compatible with their base type
    if let Type::Refined { base, .. } = a {
        return types_compatible(base, b);
    }
    if let Type::Refined { base, .. } = b {
        return types_compatible(a, base);
    }
    // TypeParams are unresolved; be lenient
    if matches!(a, Type::TypeParam(_)) || matches!(b, Type::TypeParam(_)) {
        return true;
    }
    // Nat is a subtype of Int; they are compatible in arithmetic/comparison
    if (matches!(a, Type::Nat) && matches!(b, Type::Int))
        || (matches!(a, Type::Int) && matches!(b, Type::Nat))
    {
        return true;
    }
    // Both numeric types are compatible (e.g., U32 vs Int in mixed arithmetic)
    if is_numeric(a) && is_numeric(b) {
        return true;
    }
    // Tuple types are compatible if they have the same arity and element types match
    if let (Type::Tuple(a_elems), Type::Tuple(b_elems)) = (a, b) {
        return a_elems.len() == b_elems.len()
            && a_elems
                .iter()
                .zip(b_elems.iter())
                .all(|(ea, eb)| types_compatible(ea, eb));
    }
    false
}

/// Infer the result type of a binary operation.
fn infer_binop(
    lhs: &SpExpr,
    op: &BinOp,
    rhs: &SpExpr,
    env: &TypeEnv,
    span: Span,
) -> Result<Type, TypeError> {
    let lhs_ty = infer_expr_spanned(lhs, env, lhs.span.clone())?;
    let rhs_ty = infer_expr_spanned(rhs, env, rhs.span.clone())?;

    // If either side is indeterminate, be lenient
    if lhs_ty.is_indeterminate() || rhs_ty.is_indeterminate() {
        return match op {
            _ if op.is_arithmetic() || *op == BinOp::Concat => {
                // Return whichever side is known, or Unknown
                if !lhs_ty.is_indeterminate() {
                    Ok(lhs_ty)
                } else if !rhs_ty.is_indeterminate() {
                    Ok(rhs_ty)
                } else {
                    Ok(Type::Unknown)
                }
            }
            _ if op.is_comparison() || op.is_logical() || op.is_membership() => Ok(Type::Bool),
            BinOp::Range => Ok(Type::List(Box::new(Type::Int))),
            _ => unreachable!("BinOp should be covered by guards or Range"),
        };
    }

    match op {
        // Arithmetic: both operands same numeric type, result same type
        _ if op.is_arithmetic() => {
            // Division/modulo by literal zero
            if op.is_division_like() && is_literal_zero(rhs) {
                return Err(TypeError {
                    code: "A03010".into(),
                    message: format!(
                        "{} by zero",
                        if *op == BinOp::Div {
                            "division"
                        } else {
                            "modulo"
                        }
                    ),
                    span: span.clone(),
                    secondary: None,
                    suggestion: None,
                });
            }
            if !is_numeric(&lhs_ty) {
                return Err(TypeError {
                    code: "A03001".into(),
                    message: format!(
                        "arithmetic operator requires numeric types, found `{lhs_ty}`"
                    ),
                    span: lhs.span.clone(),
                    secondary: None,
                    suggestion: None,
                });
            }
            if !types_compatible(&lhs_ty, &rhs_ty) {
                // Prefer the rhs span for the "bad" operand in mismatch (for precise
                // sub-expr diagnostics per 11.04/333).
                return Err(TypeError {
                    code: "A03001".into(),
                    message: format!("type mismatch in arithmetic: `{lhs_ty}` vs `{rhs_ty}`"),
                    span: rhs.span.clone(),
                    secondary: None,
                    suggestion: None,
                });
            }
            Ok(lhs_ty)
        }

        // Comparison: operands compatible types, result Bool
        _ if op.is_comparison() => {
            if !types_compatible(&lhs_ty, &rhs_ty) {
                return Err(TypeError {
                    code: "A03001".into(),
                    message: format!(
                        "comparison requires same types, found `{lhs_ty}` vs `{rhs_ty}`"
                    ),
                    span: rhs.span.clone(),
                    secondary: None,
                    suggestion: None,
                });
            }
            Ok(Type::Bool)
        }

        // Logical: both Bool, result Bool
        _ if op.is_logical() => {
            if lhs_ty != Type::Bool {
                return Err(TypeError {
                    code: "A03001".into(),
                    message: format!("logical operator requires Bool, found `{lhs_ty}`"),
                    span: span.clone(),
                    secondary: None,
                    suggestion: None,
                });
            }
            if rhs_ty != Type::Bool {
                return Err(TypeError {
                    code: "A03001".into(),
                    message: format!("logical operator requires Bool, found `{rhs_ty}`"),
                    span: span.clone(),
                    secondary: None,
                    suggestion: None,
                });
            }
            Ok(Type::Bool)
        }

        // Concat: both same type or compatible (String, List, Bytes), result same type
        BinOp::Concat => {
            if types_compatible(&lhs_ty, &rhs_ty) {
                Ok(lhs_ty)
            } else {
                Err(TypeError {
                    code: "A03001".into(),
                    message: format!(
                        "concat requires compatible types, found `{lhs_ty}` vs `{rhs_ty}`"
                    ),
                    span: span.clone(),
                    secondary: None,
                    suggestion: None,
                })
            }
        }

        // Range: both Int/Nat, result is a List<Int> (iterable range)
        BinOp::Range => {
            if !is_numeric(&lhs_ty) {
                return Err(TypeError {
                    code: "A03001".into(),
                    message: format!("range requires numeric operands, found `{lhs_ty}`"),
                    span: span.clone(),
                    secondary: None,
                    suggestion: None,
                });
            }
            if !is_numeric(&rhs_ty) {
                return Err(TypeError {
                    code: "A03001".into(),
                    message: format!("range requires numeric operands, found `{rhs_ty}`"),
                    span: span.clone(),
                    secondary: None,
                    suggestion: None,
                });
            }
            Ok(Type::List(Box::new(Type::Int)))
        }

        // In/NotIn: rhs should be a collection type, result Bool
        BinOp::In | BinOp::NotIn => {
            match &rhs_ty {
                Type::List(_)
                | Type::Set(_)
                | Type::Sequence(_)
                | Type::Map(_, _)
                | Type::String
                | Type::Named(_)
                | Type::Unknown
                | Type::Error => {}
                _ => {
                    return Err(TypeError {
                        code: "A03001".into(),
                        message: format!(
                            "`in` requires a collection on the right side, found `{rhs_ty}`"
                        ),
                        span: span.clone(),
                        secondary: None,
                        suggestion: None,
                    });
                }
            }
            Ok(Type::Bool)
        }

        _ => unreachable!(
            "all BinOp variants should be handled by category guards or specific arms: {:?}",
            op
        ),
    }
}

/// Infer the result type of a function call expression.
fn infer_call(
    func: &SpExpr,
    args: &[SpExpr],
    env: &TypeEnv,
    span: Span,
) -> Result<Type, TypeError> {
    let func_ty = infer_expr_spanned(func, env, func.span.clone())?;

    // Infer argument types eagerly so errors inside arguments are surfaced
    // even when the callee type is Unknown.
    let mut arg_types = Vec::with_capacity(args.len());
    for arg in args {
        arg_types.push(infer_expr_spanned(arg, env, arg.span.clone())?);
    }

    match func_ty {
        Type::Fn { params, ret } => {
            // If params is non-empty, check argument count.
            // (params may be empty when the function was registered with
            // placeholder params from the symbol table.)
            if !params.is_empty() && params.len() != arg_types.len() {
                return Err(TypeError {
                    code: "A03002".into(),
                    message: format!(
                        "function expects {} argument(s), but {} were provided",
                        params.len(),
                        arg_types.len()
                    ),
                    span: span.clone(),
                    secondary: None,
                    suggestion: None,
                });
            }
            Ok(*ret)
        }
        // Unknown callee: try to infer from function name or argument types.
        Type::Unknown => {
            if let Expr::Ident(name) = &func.node
                && let Some(ty) = infer_builtin_call_type(name, &arg_types)
            {
                return Ok(ty);
            }
            Ok(Type::Unknown)
        }
        // Error: suppress cascading
        Type::Error => Ok(Type::Error),
        // Named type: could be a constructor. Return the Named type itself.
        Type::Named(name) => Ok(Type::Named(name)),
        // TypeParam: calling a type param (e.g. `T(args)`) is a
        // constructor-style pattern; return the type param itself.
        Type::TypeParam(name) => Ok(Type::TypeParam(name)),
        // Definitely not callable (A03001 type mismatch; A03005 is unknown field).
        other => Err(TypeError {
            code: "A03001".into(),
            message: format!("type `{other}` is not callable"),
            span: span.clone(),
            secondary: None,
            suggestion: Some(
                "Ensure you are calling a function, not a type or variable. \
                 If you meant to construct a value, use struct literal syntax."
                    .into(),
            ),
        }),
    }
}

/// Attempt to infer a return type from closure/function arguments passed
/// to higher-order methods like `map`, `and_then`, etc.
///
/// If the first argument is a known function name in the environment, return
/// its declared return type. Otherwise return `Type::Unknown`.
fn infer_closure_return_from_args(args: &[SpExpr], env: &TypeEnv) -> Type {
    if let Some(first_arg) = args.first() {
        // If the argument is an identifier that resolves to a function, use its return type
        if let Expr::Ident(name) = &first_arg.node
            && let Some(Type::Fn { ret, .. }) = env.lookup(name)
        {
            return *ret.clone();
        }
        // Try inferring the expression type
        if let Ok(Type::Fn { ret, .. }) = infer_expr(first_arg, env) {
            return *ret;
        }
    }
    Type::Unknown
}

/// Infer the return type of a well-known builtin function call.
fn infer_builtin_call_type(name: &str, arg_types: &[Type]) -> Option<Type> {
    match name {
        // Collection operations
        "len" | "length" | "size" | "count" => Some(Type::Nat),
        // Type predicates
        "is_empty" | "contains" | "is_valid" | "is_some" | "is_none" | "is_ok" | "is_err" => {
            Some(Type::Bool)
        }
        // Numeric
        "abs" => arg_types.first().cloned(),
        "min" | "max" => arg_types.first().cloned(),
        "sqrt" => Some(Type::Float),
        "floor" | "ceil" | "round" => arg_types.first().cloned(),
        "to_string" | "to_str" => Some(Type::String),
        "to_int" | "parse_int" => Some(Type::Int),
        // Option/Result unwrapping
        "unwrap" => {
            if let Some(Type::Option(inner)) = arg_types.first() {
                Some(*inner.clone())
            } else if let Some(Type::Result(ok, _)) = arg_types.first() {
                Some(*ok.clone())
            } else {
                None
            }
        }
        _ => None,
    }
}
#[cfg(test)]
#[path = "inference_tests.rs"]
mod tests;