Skip to main content

lex_types/
checker.rs

1//! M3: type checker. Walks the canonical AST, infers types via unification,
2//! and checks declared signatures and effects.
3
4use crate::builtins::{module_for_import, module_scope};
5use crate::env::{TypeDefKind, TypeEnv, ty_from_canon_env};
6use crate::error::{PositionedError, TypeError};
7use crate::position::Position;
8use crate::types::*;
9use crate::unifier::{UnifyError, Unifier};
10use indexmap::IndexMap;
11use lex_ast as a;
12use std::collections::{BTreeMap, HashMap};
13
14/// Field names + type-tag schema extracted from a `Result[Record{...}, _]`
15/// return type. Used by the `parse` → `parse_strict_typed` rewrite (#322).
16type FieldSchema = (Vec<String>, Vec<(String, String)>);
17
18/// Result of checking a whole program.
19pub struct ProgramTypes {
20    pub fn_signatures: IndexMap<String, Scheme>,
21    pub type_env: TypeEnv,
22    /// For #168: per-call required-fields map for `module.parse(s)`
23    /// calls whose inferred result type is `Result[Record{...}, _]`.
24    /// Keyed by `&CExpr as *const _ as usize` so callers can do an
25    /// O(1) pointer-equality lookup during a separate AST rewrite
26    /// pass. Empty unless any matching call sites were found.
27    ///
28    /// See [`check_and_rewrite_program`] for the function that
29    /// populates this and applies the rewrite in one step.
30    pub parse_required_fields: HashMap<usize, Vec<String>>,
31    /// For #322: per-call type schema alongside the field names.
32    /// Each entry is a `Vec<(field_name, type_tag)>` parallel to
33    /// `parse_required_fields`. Used by the rewrite pass to inject
34    /// the third argument to `parse_strict`.
35    pub parse_type_schemas: HashMap<usize, Vec<(String, String)>>,
36}
37
38/// Variant of [`check_program`] that stamps a source [`Position`]
39/// onto every emitted error (#306 slice 1).
40///
41/// `positions` is keyed by function name and supplies the position
42/// of each `fn` declaration in the source. Errors from a given
43/// function are tagged with that function's position; errors that
44/// don't map to a single function (e.g. type-decl-level errors)
45/// keep `position = None`.
46///
47/// Slice 1 ships function-level granularity. Slice 1.5 will plumb
48/// per-expression spans through canonicalize so deep-body errors
49/// land on the offending sub-expression rather than its enclosing
50/// function.
51pub fn check_program_with_positions(
52    stages: &[a::Stage],
53    positions: &BTreeMap<String, Position>,
54) -> Result<ProgramTypes, Vec<PositionedError>> {
55    check_program_inner(stages, Some(positions))
56        .map_err(|errs| errs.into_iter().map(|(e, fn_name)| {
57            let pos = fn_name.as_deref().and_then(|n| positions.get(n)).cloned();
58            PositionedError::new(e, pos)
59        }).collect())
60}
61
62pub fn check_program(stages: &[a::Stage]) -> Result<ProgramTypes, Vec<TypeError>> {
63    check_program_inner(stages, None)
64        .map_err(|errs| errs.into_iter().map(|(e, _)| e).collect())
65}
66
67fn check_program_inner(
68    stages: &[a::Stage],
69    _positions: Option<&BTreeMap<String, Position>>,
70) -> Result<ProgramTypes, Vec<(TypeError, Option<String>)>> {
71    let mut tcx = Checker::new();
72    // Each entry is (error, optional fn name the error came from)
73    // so callers can resolve the error to a source position.
74    let mut errors: Vec<(TypeError, Option<String>)> = Vec::new();
75
76    // Pass 1: gather imports → bring module values into scope.
77    for stage in stages {
78        if let a::Stage::Import(i) = stage {
79            if let Some(mod_name) = module_for_import(&i.reference) {
80                if let Some(ty) = module_scope(mod_name, &tcx.type_env) {
81                    tcx.globals.insert(i.alias.clone(), Scheme {
82                        // Module-level signatures use Var(0..n) and
83                        // effect-vars on stdlib HOFs (list.map's `[E]`
84                        // etc.); generalize both.
85                        vars: collect_vars(&ty),
86                        eff_vars: collect_eff_vars(&ty),
87                        ty,
88                    });
89                    tcx.module_aliases.insert(i.alias.clone(), mod_name.to_string());
90                }
91            }
92        }
93    }
94
95    // Pass 2: register user-declared types.
96    for stage in stages {
97        if let a::Stage::TypeDecl(td) = stage {
98            if let Err(e) = tcx.type_env.add_user_type(&td.name, td.clone()) {
99                errors.push((TypeError::RecursiveTypeWithoutConstructor {
100                    at_node: "n_0".into(),
101                    name: e,
102                }, None));
103            }
104        }
105    }
106
107    // Pass 3: register fn signatures (so mutual recursion works).
108    for stage in stages {
109        if let a::Stage::FnDecl(fd) = stage {
110            let scheme = function_scheme(fd, &tcx.type_env);
111            tcx.globals.insert(fd.name.clone(), scheme);
112            // #209 slice 2: keep the original params so call-site
113            // refinement discharge can see the predicate before it
114            // gets stripped to its base type by `ty_from_canon`.
115            tcx.fn_params.insert(fd.name.clone(), fd.params.clone());
116        }
117    }
118
119    // Pass 4: check each fn body. With #306 slice 1, every emitted
120    // error is paired with the source fn it came from so the public
121    // [`check_program_with_positions`] wrapper can stamp the
122    // function's source position onto a [`PositionedError`].
123    let mut signatures = IndexMap::new();
124    for stage in stages {
125        if let a::Stage::FnDecl(fd) = stage {
126            match tcx.check_fn(fd) {
127                Ok(scheme) => { signatures.insert(fd.name.clone(), scheme); }
128                Err(es) => {
129                    errors.extend(es.into_iter().map(|e| (e, Some(fd.name.clone()))));
130                }
131            }
132        }
133    }
134
135    if errors.is_empty() {
136        // #168: walk pending parse-call records and resolve each
137        // call's return type now that all unification has settled.
138        // A call shows up here only if the call site syntactically
139        // looks like `<alias>.parse(s)` for an alias bound to one
140        // of {json, toml, yaml} via the import pass.
141        let mut parse_required_fields = HashMap::new();
142        let mut parse_type_schemas = HashMap::new();
143        for (call_ptr, ret_ty) in &tcx.pending_parse_calls {
144            if let Some((fields, schema)) = extract_record_fields_and_schema(&tcx.u, &tcx.type_env, ret_ty) {
145                parse_required_fields.insert(*call_ptr, fields);
146                parse_type_schemas.insert(*call_ptr, schema);
147            }
148        }
149        Ok(ProgramTypes {
150            fn_signatures: signatures,
151            type_env: tcx.type_env,
152            parse_required_fields,
153            parse_type_schemas,
154        })
155    } else {
156        Err(errors)
157    }
158}
159
160/// Type-check `stages` and rewrite every `module.parse(s)` call
161/// where the inferred T is a Record into the equivalent
162/// `module.parse_strict(s, [field_names])` (#168). Existing
163/// [`check_program`] keeps the old immutable signature for tests
164/// and tools that don't want the AST rewritten.
165pub fn check_and_rewrite_program(
166    stages: &mut [a::Stage],
167) -> Result<ProgramTypes, Vec<TypeError>> {
168    // Borrow as immutable for the type-check pass — the side-table
169    // it produces is keyed by `*const CExpr as usize`, and the Vec
170    // backing storage doesn't move between this borrow and the
171    // mutable one below.
172    let pt = check_program(&*stages)?;
173    if !pt.parse_required_fields.is_empty() {
174        rewrite_parse_calls(stages, &pt.parse_required_fields, &pt.parse_type_schemas);
175    }
176    Ok(pt)
177}
178
179/// Walk `stages` mutably and, for every `CExpr::Call` whose
180/// pointer (cast to `usize`) is a key in `required`, rewrite it
181/// from `module.parse(s)` into `module.parse_strict(s, [...], [...])`.
182///
183/// Assumptions:
184///
185/// - The `usize` keys come from the same physical AST passed
186///   here. This is true when called from
187///   [`check_and_rewrite_program`].
188/// - Every key corresponds to a call whose callee is
189///   `FieldAccess(_, "parse")`. The type-checker only inserts
190///   keys when this holds, so we panic if the assumption is
191///   violated — that's a checker bug, not a user error.
192fn rewrite_parse_calls(
193    stages: &mut [a::Stage],
194    required: &HashMap<usize, Vec<String>>,
195    schemas: &HashMap<usize, Vec<(String, String)>>,
196) {
197    for stage in stages.iter_mut() {
198        if let a::Stage::FnDecl(fd) = stage {
199            rewrite_in_expr(&mut fd.body, required, schemas);
200        }
201    }
202}
203
204fn rewrite_in_expr(
205    expr: &mut a::CExpr,
206    required: &HashMap<usize, Vec<String>>,
207    schemas: &HashMap<usize, Vec<(String, String)>>,
208) {
209    let ptr = expr as *const a::CExpr as usize;
210    let do_rewrite = required.get(&ptr).cloned();
211    let do_schema = schemas.get(&ptr).cloned();
212    // Recurse into children first; rewriting the call itself
213    // doesn't touch the source-arg, so the order doesn't change
214    // semantics — but processing children up front means a
215    // hypothetical nested parse-of-parse still gets rewritten
216    // correctly.
217    match expr {
218        a::CExpr::Call { callee, args } => {
219            rewrite_in_expr(callee, required, schemas);
220            for a in args.iter_mut() { rewrite_in_expr(a, required, schemas); }
221        }
222        a::CExpr::Let { value, body, .. } => {
223            rewrite_in_expr(value, required, schemas);
224            rewrite_in_expr(body, required, schemas);
225        }
226        a::CExpr::Match { scrutinee, arms } => {
227            rewrite_in_expr(scrutinee, required, schemas);
228            for arm in arms.iter_mut() { rewrite_in_expr(&mut arm.body, required, schemas); }
229        }
230        a::CExpr::Block { statements, result } => {
231            for s in statements.iter_mut() { rewrite_in_expr(s, required, schemas); }
232            rewrite_in_expr(result, required, schemas);
233        }
234        a::CExpr::Constructor { args, .. } => {
235            for a in args.iter_mut() { rewrite_in_expr(a, required, schemas); }
236        }
237        a::CExpr::RecordLit { fields } => {
238            for f in fields.iter_mut() { rewrite_in_expr(&mut f.value, required, schemas); }
239        }
240        a::CExpr::TupleLit { items } | a::CExpr::ListLit { items } => {
241            for it in items.iter_mut() { rewrite_in_expr(it, required, schemas); }
242        }
243        a::CExpr::FieldAccess { value, .. } => rewrite_in_expr(value, required, schemas),
244        a::CExpr::Lambda { body, .. } => rewrite_in_expr(body, required, schemas),
245        a::CExpr::BinOp { lhs, rhs, .. } => {
246            rewrite_in_expr(lhs, required, schemas);
247            rewrite_in_expr(rhs, required, schemas);
248        }
249        a::CExpr::UnaryOp { expr, .. } => rewrite_in_expr(expr, required, schemas),
250        a::CExpr::Return { value } => rewrite_in_expr(value, required, schemas),
251        a::CExpr::Literal { .. } | a::CExpr::Var { .. } => {}
252    }
253    if let Some(fields) = do_rewrite {
254        match expr {
255            a::CExpr::Call { callee, args } => {
256                if let a::CExpr::FieldAccess { field, .. } = callee.as_mut() {
257                    debug_assert_eq!(field, "parse",
258                        "rewrite_in_expr: only `.parse` calls should be in the table");
259                    // Use parse_strict_typed (internal, 3-arg) rather than the
260                    // public 2-arg parse_strict so direct callers aren't broken.
261                    *field = "parse_strict_typed".to_string();
262                }
263                // Second argument: List[Str] of required field names.
264                args.push(a::CExpr::ListLit {
265                    items: fields.into_iter()
266                        .map(|f| a::CExpr::Literal {
267                            value: a::CLit::Str { value: f },
268                        })
269                        .collect(),
270                });
271                // Third argument: List[(Str, Str)] type schema (#322).
272                let schema = do_schema.unwrap_or_default();
273                args.push(a::CExpr::ListLit {
274                    items: schema.into_iter()
275                        .map(|(name, tag)| a::CExpr::TupleLit {
276                            items: vec![
277                                a::CExpr::Literal { value: a::CLit::Str { value: name } },
278                                a::CExpr::Literal { value: a::CLit::Str { value: tag } },
279                            ],
280                        })
281                        .collect(),
282                });
283            }
284            _ => unreachable!("rewrite table key must point to a Call expression"),
285        }
286    }
287}
288
289/// Given an inferred return type from a `module.parse(s)` call,
290/// resolve through the unifier and any type aliases, then look
291/// for `Result[Record{...}, _]`. Returns `(field_names, schema)`
292/// where `schema` is a `Vec<(field_name, type_tag)>` for #322.
293/// Returns `None` if the shape doesn't match.
294fn extract_record_fields_and_schema(
295    u: &Unifier,
296    env: &TypeEnv,
297    ty: &Ty,
298) -> Option<FieldSchema> {
299    let resolved = u.resolve(ty);
300    let Ty::Con(ref name, ref args) = resolved else { return None; };
301    if name != "Result" || args.len() != 2 { return None; }
302    let ok_ty = u.resolve(&args[0]);
303    let unfolded = unfold_record_alias_static(env, ok_ty);
304    if let Ty::Record(fields) = unfolded {
305        let names: Vec<String> = fields.keys().cloned().collect();
306        let schema: Vec<(String, String)> = fields.iter()
307            .map(|(k, v)| (k.clone(), ty_to_tag(u, v)))
308            .collect();
309        Some((names, schema))
310    } else {
311        None
312    }
313}
314
315/// Convert a `Ty` to a compact string tag for the type schema
316/// injected by the rewrite pass (#322). The runtime uses these
317/// tags to validate JSON field values against the declared Lex type.
318fn ty_to_tag(u: &Unifier, ty: &Ty) -> String {
319    let resolved = u.resolve(ty);
320    match &resolved {
321        Ty::Prim(Prim::Int)   => "Int".to_string(),
322        Ty::Prim(Prim::Float) => "Float".to_string(),
323        Ty::Prim(Prim::Bool)  => "Bool".to_string(),
324        Ty::Prim(Prim::Str)   => "Str".to_string(),
325        Ty::Con(name, args) if name == "Option" && args.len() == 1 => {
326            format!("Option[{}]", ty_to_tag(u, &args[0]))
327        }
328        Ty::List(inner) => {
329            format!("List[{}]", ty_to_tag(u, inner))
330        }
331        Ty::Record(_) => "Record".to_string(),
332        _ => "Any".to_string(),
333    }
334}
335
336/// Standalone version of `Checker::unfold_record_alias` —
337/// resolves a `Ty::Con` whose definition is a type alias (record
338/// or otherwise) to the underlying type. Module-level helper
339/// because we need it after the `Checker` has been
340/// moved/destructured.
341fn unfold_record_alias_static(env: &TypeEnv, ty: Ty) -> Ty {
342    if let Ty::Con(ref n, ref args) = ty {
343        if let Some(td) = env.types.get(n) {
344            if let TypeDefKind::Alias(inner) = &td.kind {
345                if td.params.len() != args.len() {
346                    return ty;
347                }
348                if td.params.is_empty() {
349                    return inner.clone();
350                }
351                let mut subst = IndexMap::new();
352                for (i, a) in args.iter().enumerate() {
353                    subst.insert(i as u32, a.clone());
354                }
355                return subst_vars(inner, &subst, &IndexMap::new());
356            }
357        }
358    }
359    ty
360}
361
362fn collect_vars(t: &Ty) -> Vec<TyVarId> {
363    let mut out = Vec::new();
364    fn walk(t: &Ty, out: &mut Vec<TyVarId>) {
365        match t {
366            Ty::Var(v) => { if !out.contains(v) { out.push(*v); } }
367            Ty::Prim(_) | Ty::Unit | Ty::Never => {}
368            Ty::List(inner) => walk(inner, out),
369            Ty::Tuple(items) => for it in items { walk(it, out); },
370            Ty::Record(fs) => for v in fs.values() { walk(v, out); },
371            Ty::Con(_, args) => for a in args { walk(a, out); },
372            Ty::Function { params, ret, .. } => {
373                for p in params { walk(p, out); }
374                walk(ret, out);
375            }
376        }
377    }
378    walk(t, &mut out);
379    out
380}
381
382/// Walk a type and collect every effect-row variable id that appears
383/// inside any function-type's effect set. Used to generalize stdlib
384/// HOF schemes alongside ordinary type vars.
385fn collect_eff_vars(t: &Ty) -> Vec<u32> {
386    let mut out = Vec::new();
387    fn walk(t: &Ty, out: &mut Vec<u32>) {
388        match t {
389            Ty::Var(_) | Ty::Prim(_) | Ty::Unit | Ty::Never => {}
390            Ty::List(inner) => walk(inner, out),
391            Ty::Tuple(items) => for it in items { walk(it, out); },
392            Ty::Record(fs) => for v in fs.values() { walk(v, out); },
393            Ty::Con(_, args) => for a in args { walk(a, out); },
394            Ty::Function { params, effects, ret } => {
395                if let Some(v) = effects.var {
396                    if !out.contains(&v) { out.push(v); }
397                }
398                for p in params { walk(p, out); }
399                walk(ret, out);
400            }
401        }
402    }
403    walk(t, &mut out);
404    out
405}
406
407fn function_scheme(fd: &a::FnDecl, env: &TypeEnv) -> Scheme {
408    // Collect type-param ids in order; map their names to fresh Var(idx).
409    let params: Vec<Ty> = fd.params.iter().map(|p| ty_from_canon_env(&p.ty, &fd.type_params, env)).collect();
410    let ret = ty_from_canon_env(&fd.return_type, &fd.type_params, env);
411    // Plumb effect args (#207). A canonical-AST `EffectDecl` already
412    // carries `Option<EffectArg>`; map it into the type-system kind so
413    // subsumption can honor parameterized effects.
414    let effects = EffectSet {
415        concrete: {
416            let mut s = std::collections::BTreeSet::new();
417            for e in &fd.effects {
418                let arg = e.arg.as_ref().map(|a| match a {
419                    a::EffectArg::Str { value } => crate::types::EffectArg::Str(value.clone()),
420                    a::EffectArg::Int { value } => crate::types::EffectArg::Int(*value),
421                    a::EffectArg::Ident { value } => crate::types::EffectArg::Ident(value.clone()),
422                });
423                s.insert(crate::types::EffectKind { name: e.name.clone(), arg });
424            }
425            s
426        },
427        var: None,
428    };
429    let ty = Ty::Function { params, effects, ret: Box::new(ret) };
430    let vars: Vec<TyVarId> = (0..fd.type_params.len() as u32).collect();
431    // User-declared functions don't carry effect-row variables today
432    // (the surface syntax has no `[E]` form for user types). Only
433    // stdlib HOFs do, and those are loaded via module_scope.
434    Scheme { vars, eff_vars: Vec::new(), ty }
435}
436
437struct Checker {
438    u: Unifier,
439    type_env: TypeEnv,
440    globals: IndexMap<String, Scheme>,
441    /// Imported alias → canonical module name (e.g. `cfg` → `toml`).
442    /// Populated during the import pass; consulted by `check_call`
443    /// to recognise `cfg.parse(...)` as a stdlib parse call.
444    module_aliases: IndexMap<String, String>,
445    /// For #168: every `<alias>.parse(s)` call where alias is in
446    /// `module_aliases` and maps to {json, toml, yaml}, recorded
447    /// here as `(call_pointer_as_usize, return_type_var)`. After
448    /// the whole program type-checks, we walk this and resolve
449    /// each return type through the unifier — at that point any
450    /// `Result[Manifest, _]` constraints from match patterns or
451    /// let-annotations have settled.
452    pending_parse_calls: Vec<(usize, Ty)>,
453    /// Per-function param list, retained so call-site discharge can
454    /// see refinement predicates (#209 slice 2). The main `globals`
455    /// scheme strips refinements (`Refined` unifies as its base);
456    /// this side-table keeps the pre-stripped `TypeExpr` available
457    /// for static discharge of literal arguments.
458    fn_params: IndexMap<String, Vec<a::Param>>,
459}
460
461impl Checker {
462    fn new() -> Self {
463        Self {
464            u: Unifier::new(),
465            type_env: TypeEnv::new_with_builtins(),
466            globals: IndexMap::new(),
467            module_aliases: IndexMap::new(),
468            pending_parse_calls: Vec::new(),
469            fn_params: IndexMap::new(),
470        }
471    }
472
473    /// If `ty` is a `Ty::Con(name, args)` whose definition is a type
474    /// alias (record or otherwise), return the aliased type with the
475    /// alias's formal parameters substituted by `args`. For zero-arg
476    /// aliases this is the identity substitution. For parametric
477    /// aliases (#439, e.g. `type Box[T] = { value :: T }`), the
478    /// formal `Ty::Var(i)` for the i-th param is replaced by `args[i]`
479    /// so `Box[Str]` unfolds to `{ value :: Str }` rather than to the
480    /// unsubstituted body. Returns `ty` unchanged when arity doesn't
481    /// match or the name doesn't resolve to an alias.
482    fn unfold_record_alias(&self, ty: Ty) -> Ty {
483        if let Ty::Con(ref n, ref args) = ty {
484            if let Some(td) = self.type_env.types.get(n) {
485                if let TypeDefKind::Alias(inner) = &td.kind {
486                    if td.params.len() != args.len() {
487                        return ty;
488                    }
489                    if td.params.is_empty() {
490                        return inner.clone();
491                    }
492                    let mut subst = IndexMap::new();
493                    for (i, a) in args.iter().enumerate() {
494                        subst.insert(i as u32, a.clone());
495                    }
496                    return subst_vars(inner, &subst, &IndexMap::new());
497                }
498            }
499        }
500        ty
501    }
502
503    /// True iff `ty` is a `Ty::Con(name, args)` whose definition is a
504    /// `TypeDefKind::Alias` and whose arity matches. Used by
505    /// `unify_coerce_inner` to detect the case where both sides are
506    /// nominal aliases and unfolding would collapse the nominal
507    /// distinction (#323 / #439). For parametric aliases the arity
508    /// match guards against `Box[Str]` vs an inconsistent `Box[Str, Int]`.
509    fn is_alias_con(&self, ty: &Ty) -> bool {
510        if let Ty::Con(name, args) = ty {
511            if let Some(td) = self.type_env.types.get(name) {
512                if matches!(td.kind, TypeDefKind::Alias(_))
513                    && td.params.len() == args.len()
514                {
515                    return true;
516                }
517            }
518        }
519        false
520    }
521
522    /// Whether `callee` is a `<alias>.parse` field access where
523    /// `<alias>` was imported from one of the stdlib modules whose
524    /// `parse` returns `Result[T, Str]` and whose `parse_strict`
525    /// shape exists for #168 enforcement (json / toml / yaml).
526    fn is_module_parse_call(&self, callee: &a::CExpr) -> bool {
527        if let a::CExpr::FieldAccess { value, field } = callee {
528            if field != "parse" { return false; }
529            if let a::CExpr::Var { name } = value.as_ref() {
530                if let Some(module) = self.module_aliases.get(name) {
531                    return matches!(module.as_str(), "json" | "toml" | "yaml");
532                }
533            }
534        }
535        false
536    }
537
538    /// Unify two types, asymmetrically coercing an anonymous record
539    /// against a nominal record alias at any level of nesting. So a
540    /// `{ x: 1, y: 2 }` literal can be passed to a fn taking
541    /// `Inner = { x :: Int, y :: Int }`, even when the literal is the
542    /// inner field of an outer record literal.
543    ///
544    /// We deliberately keep nominal-vs-nominal mismatches strict: two
545    /// distinct `Ty::Con` names won't unify just because their record
546    /// shapes match. The coercion fires only when one side is a bare
547    /// `Ty::Record` and the other is a `Ty::Con` whose alias is a
548    /// record.
549    fn unify_with_record_coercion(&mut self, a: &Ty, b: &Ty) -> Result<(), UnifyError> {
550        let a = self.u.resolve(a);
551        let b = self.u.resolve(b);
552        self.unify_coerce_inner(a, b)
553    }
554
555    fn unify_coerce_inner(&mut self, a: Ty, b: Ty) -> Result<(), UnifyError> {
556        // #323: alias unfolding. If exactly one side is an `alias-Con`
557        // — a 0-arg `Ty::Con(name, [])` whose definition is a type
558        // alias (Record or non-record) — unfold both sides so the
559        // structural cases below can match (`Errors` ↔ `List[…]`,
560        // `Path` ↔ `Tuple(…)`, `Maybe` ↔ `Option[…]`,
561        // `UserId` ↔ `Int`, …).
562        //
563        // Three cases intentionally bypass unfolding:
564        //
565        // - **Same-named Cons** (`Test` vs `Test`): preserve nominal
566        //   identity. The Con-Con same-name case below recurses on
567        //   args; eager unfold here would force the nominal name
568        //   to evaporate, breaking unifications elsewhere that
569        //   still see the nominal `Con`.
570        // - **Var on either side**: don't unfold against an unbound
571        //   variable, because the plain unifier would bind the var
572        //   to the unfolded shape and lose the nominal name. The
573        //   var binds to the nominal `Con` instead, and later
574        //   unifications against concrete shapes re-enter this
575        //   function and unfold then.
576        // - **Two distinct alias-Cons** (`Apple` vs `Box`, both
577        //   declared as record aliases with identical shapes):
578        //   preserve nominal distinction between aliases. Unfolding
579        //   both would collapse the test of "same shape, different
580        //   names" into "same shape" and erase the names.
581        let (a, b) = match (&a, &b) {
582            (Ty::Con(n1, _), Ty::Con(n2, _)) if n1 == n2 => (a, b),
583            (Ty::Var(_), _) | (_, Ty::Var(_)) => (a, b),
584            (Ty::Con(_, _), Ty::Con(_, _))
585                if self.is_alias_con(&a) && self.is_alias_con(&b) =>
586            {
587                (a, b)
588            }
589            _ => {
590                let a_u = if let Ty::Con(_, _) = &a {
591                    self.unfold_record_alias(a.clone())
592                } else {
593                    a
594                };
595                let b_u = if let Ty::Con(_, _) = &b {
596                    self.unfold_record_alias(b.clone())
597                } else {
598                    b
599                };
600                (a_u, b_u)
601            }
602        };
603
604        match (&a, &b) {
605            (Ty::Record(fa), Ty::Record(fb)) => {
606                if fa.len() != fb.len() {
607                    return Err(UnifyError::Mismatch { a: a.clone(), b: b.clone() });
608                }
609                for (k, va) in fa.clone() {
610                    match fb.get(&k) {
611                        Some(vb) => self.unify_coerce_inner(va, vb.clone())?,
612                        None => return Err(UnifyError::Mismatch { a: a.clone(), b: b.clone() }),
613                    }
614                }
615                Ok(())
616            }
617            (Ty::List(ta), Ty::List(tb)) => {
618                self.unify_coerce_inner((**ta).clone(), (**tb).clone())
619            }
620            (Ty::Tuple(xs), Ty::Tuple(ys)) if xs.len() == ys.len() => {
621                for (x, y) in xs.clone().into_iter().zip(ys.clone()) {
622                    self.unify_coerce_inner(x, y)?;
623                }
624                Ok(())
625            }
626            // Recurse into Con-Con pairs so record-alias coercion reaches
627            // arbitrary nesting depth (e.g. Result[T, MyAlias]) (#328).
628            (Ty::Con(n1, a1), Ty::Con(n2, a2)) if n1 == n2 && a1.len() == a2.len() => {
629                for (x, y) in a1.clone().into_iter().zip(a2.clone()) {
630                    self.unify_coerce_inner(x, y)?;
631                }
632                Ok(())
633            }
634            // #345: recurse into Function types so alias coercion fires on
635            // closure params / return types. Without this, a closure annotated
636            // `(Errors, Errors) -> Errors` fails to unify with the expected
637            // `(List[?n], ?m) -> List[?n]` even though `Errors = List[Error]`.
638            (Ty::Function { params: pa, effects: ea, ret: ra },
639             Ty::Function { params: pb, effects: eb, ret: rb })
640            if pa.len() == pb.len() => {
641                for (x, y) in pa.clone().into_iter().zip(pb.clone()) {
642                    self.unify_coerce_inner(x, y)?;
643                }
644                // Propagate the EffectMismatch verbatim (rather than
645                // collapsing it into a whole-type Mismatch) so the
646                // invariant-effect-row case surfaces as its own
647                // rule_tag with the narrow-the-body fix (#565).
648                self.u.unify_effects(ea, eb)?;
649                self.unify_coerce_inner((**ra).clone(), (**rb).clone())
650            }
651            _ => self.u.unify(&a, &b),
652        }
653    }
654
655    fn check_fn(&mut self, fd: &a::FnDecl) -> Result<Scheme, Vec<TypeError>> {
656        // Instantiate fn's signature with fresh vars for its type params.
657        let scheme = function_scheme(fd, &self.type_env);
658        let (param_tys, declared_effects, ret_ty) = match instantiate(&scheme, &mut self.u) {
659            Ty::Function { params, effects, ret } => (params, effects, *ret),
660            _ => unreachable!(),
661        };
662
663        let mut locals: IndexMap<String, Ty> = IndexMap::new();
664        for (p, t) in fd.params.iter().zip(param_tys.iter()) {
665            locals.insert(p.name.clone(), t.clone());
666        }
667
668        let mut inferred_effects = EffectSet::empty();
669        let body_ty = self.check_expr(&fd.body, "n_0", &mut locals, &mut inferred_effects)
670            .map_err(|e| vec![e])?;
671
672        // The body may produce an anonymous record literal where the
673        // signature expects a nominal record alias (and vice-versa,
674        // and at any nested level). `unify_with_record_coercion`
675        // handles that asymmetry while keeping nominal-vs-nominal
676        // mismatches strict.
677        if let Err(e) = self.unify_with_record_coercion(&body_ty, &ret_ty) {
678            return Err(vec![mismatch_err("n_0", e, &self.u, vec![format!("in function `{}`", fd.name)])]);
679        }
680
681        if !inferred_effects.is_subset(&declared_effects) {
682            // Pick the first undeclared effect for the error.
683            for e in inferred_effects.concrete.iter() {
684                if !declared_effects.concrete.iter().any(|d| d.subsumes(e)) {
685                    return Err(vec![TypeError::EffectNotDeclared {
686                        at_node: "n_0".into(),
687                        effect: e.pretty(),
688                    }]);
689                }
690            }
691        }
692
693        // #369: signature-level examples. Pure-only in v1; arg arity
694        // must match params; each arg type-checks against its param,
695        // each expected type-checks against the return type. Behavioral
696        // equivalence (run the body, compare to expected) is a follow-up
697        // — this slice catches type-level drift, which is already a
698        // common source of stale examples.
699        if !fd.examples.is_empty() {
700            if !declared_effects.concrete.is_empty() {
701                return Err(vec![TypeError::ExamplesOnEffectfulFn {
702                    at_node: "n_0".into(),
703                    fn_name: fd.name.clone(),
704                }]);
705            }
706            for (case_index, ex) in fd.examples.iter().enumerate() {
707                if ex.args.len() != param_tys.len() {
708                    return Err(vec![TypeError::ExampleArityMismatch {
709                        at_node: "n_0".into(),
710                        fn_name: fd.name.clone(),
711                        case_index,
712                        expected: param_tys.len(),
713                        got: ex.args.len(),
714                    }]);
715                }
716                let mut example_locals: IndexMap<String, Ty> = IndexMap::new();
717                let mut example_effects = EffectSet::empty();
718                for (i, (arg, expected_ty)) in
719                    ex.args.iter().zip(param_tys.iter()).enumerate()
720                {
721                    let arg_ty = self
722                        .check_expr(arg, "n_0", &mut example_locals, &mut example_effects)
723                        .map_err(|e| vec![e])?;
724                    if let Err(e) = self.unify_with_record_coercion(&arg_ty, expected_ty) {
725                        return Err(vec![mismatch_err(
726                            "n_0",
727                            e,
728                            &self.u,
729                            vec![format!(
730                                "in example #{} for `{}`, argument {}",
731                                case_index + 1,
732                                fd.name,
733                                i + 1
734                            )],
735                        )]);
736                    }
737                }
738                let expected_ty = self
739                    .check_expr(&ex.expected, "n_0", &mut example_locals, &mut example_effects)
740                    .map_err(|e| vec![e])?;
741                if let Err(e) = self.unify_with_record_coercion(&expected_ty, &ret_ty) {
742                    return Err(vec![mismatch_err(
743                        "n_0",
744                        e,
745                        &self.u,
746                        vec![format!(
747                            "in example #{} for `{}`, expected value",
748                            case_index + 1,
749                            fd.name
750                        )],
751                    )]);
752                }
753                // The example's args/expected are expected to be pure
754                // by construction (literals in the common case); if
755                // they invoked effects, they'd break the pure-only
756                // discipline. Reject the first one via the same effect rule.
757                if let Some(e) = example_effects.concrete.iter().next() {
758                    return Err(vec![TypeError::EffectNotDeclared {
759                        at_node: "n_0".into(),
760                        effect: e.pretty(),
761                    }]);
762                }
763            }
764        }
765
766        Ok(scheme)
767    }
768
769    fn check_expr(
770        &mut self,
771        e: &a::CExpr,
772        node_id: &str,
773        locals: &mut IndexMap<String, Ty>,
774        effs: &mut EffectSet,
775    ) -> Result<Ty, TypeError> {
776        match e {
777            a::CExpr::Literal { value } => Ok(lit_type(value)),
778            a::CExpr::Var { name } => {
779                if let Some(t) = locals.get(name) {
780                    return Ok(t.clone());
781                }
782                if let Some(scheme) = self.globals.get(name).cloned() {
783                    return Ok(instantiate(&scheme, &mut self.u));
784                }
785                Err(TypeError::UnknownIdentifier { at_node: node_id.into(), name: name.clone() })
786            }
787            a::CExpr::Constructor { name, args } => self.check_constructor(name, args, node_id, locals, effs),
788            a::CExpr::Call { callee, args } => self.check_call(e, callee, args, node_id, locals, effs),
789            a::CExpr::Let { name, ty, value, body } => {
790                let v_ty = self.check_expr(value, node_id, locals, effs)?;
791                if let Some(declared) = ty {
792                    let d = ty_from_canon_env(declared, &[], &self.type_env);
793                    if let Err(err) = self.unify_with_record_coercion(&v_ty, &d) {
794                        return Err(mismatch_err(node_id, err, &self.u, vec![format!("in let `{}`", name)]));
795                    }
796                }
797                let prev = locals.insert(name.clone(), v_ty);
798                let body_ty = self.check_expr(body, node_id, locals, effs)?;
799                match prev {
800                    Some(p) => { locals.insert(name.clone(), p); }
801                    None => { locals.shift_remove(name); }
802                }
803                Ok(body_ty)
804            }
805            a::CExpr::Match { scrutinee, arms } => {
806                let scrut_ty = self.check_expr(scrutinee, node_id, locals, effs)?;
807                if arms.is_empty() {
808                    return Err(TypeError::NonExhaustiveMatch {
809                        at_node: node_id.into(), missing: vec!["_".into()]
810                    });
811                }
812                let result_ty = self.u.fresh();
813                for arm in arms {
814                    let mut arm_locals = locals.clone();
815                    self.bind_pattern(&arm.pattern, &scrut_ty, &mut arm_locals, node_id)?;
816                    let arm_ty = self.check_expr(&arm.body, node_id, &mut arm_locals, effs)?;
817                    if let Err(err) = self.unify_with_record_coercion(&arm_ty, &result_ty) {
818                        return Err(mismatch_err(node_id, err, &self.u, vec!["in match arm".into()]));
819                    }
820                }
821                Ok(result_ty)
822            }
823            a::CExpr::Block { statements, result } => {
824                for s in statements {
825                    self.check_expr(s, node_id, locals, effs)?;
826                }
827                self.check_expr(result, node_id, locals, effs)
828            }
829            a::CExpr::RecordLit { fields } => {
830                let mut tys = IndexMap::new();
831                for f in fields {
832                    if tys.contains_key(&f.name) {
833                        return Err(TypeError::DuplicateField {
834                            at_node: node_id.into(), field: f.name.clone()
835                        });
836                    }
837                    let ft = self.check_expr(&f.value, node_id, locals, effs)?;
838                    tys.insert(f.name.clone(), ft);
839                }
840                Ok(Ty::Record(tys))
841            }
842            a::CExpr::TupleLit { items } => {
843                let mut ts = Vec::new();
844                for it in items { ts.push(self.check_expr(it, node_id, locals, effs)?); }
845                Ok(Ty::Tuple(ts))
846            }
847            a::CExpr::ListLit { items } => {
848                let elem = self.u.fresh();
849                for it in items {
850                    let t = self.check_expr(it, node_id, locals, effs)?;
851                    if let Err(err) = self.unify_with_record_coercion(&t, &elem) {
852                        return Err(mismatch_err(node_id, err, &self.u, vec!["in list literal".into()]));
853                    }
854                }
855                Ok(Ty::List(Box::new(elem)))
856            }
857            a::CExpr::FieldAccess { value, field } => {
858                let vt = self.check_expr(value, node_id, locals, effs)?;
859                let resolved = self.u.resolve(&vt);
860                // Unfold a Record-aliased Con (e.g. `type Request = { ... }`
861                // or `type Box[T] = { value :: T }`). For parametric aliases
862                // the helper substitutes the actual args for the formal
863                // params; the post-unfold shape is only a Record when the
864                // alias body was a record, so non-record aliases (e.g.
865                // `type UserId = Int`) fall through to the
866                // "expected record" error below.
867                let resolved = if let Ty::Con(_, _) = &resolved {
868                    let unfolded = self.unfold_record_alias(resolved.clone());
869                    if matches!(unfolded, Ty::Record(_)) {
870                        unfolded
871                    } else {
872                        resolved
873                    }
874                } else {
875                    resolved
876                };
877                match resolved {
878                    Ty::Record(fields) => fields.get(field).cloned()
879                        .ok_or_else(|| TypeError::UnknownField {
880                            at_node: node_id.into(),
881                            record_type: Ty::Record(fields.clone()).pretty(),
882                            field: field.clone(),
883                        }),
884                    other => Err(TypeError::TypeMismatch {
885                        at_node: node_id.into(),
886                        expected: "record".into(),
887                        got: other.pretty(),
888                        context: vec![format!("field access `.{}`", field)],
889                    }),
890                }
891            }
892            a::CExpr::Lambda { params, return_type, effects: l_effects, body } => {
893                let param_tys: Vec<Ty> = params.iter().map(|p| ty_from_canon_env(&p.ty, &[], &self.type_env)).collect();
894                let ret_ty = ty_from_canon_env(return_type, &[], &self.type_env);
895                let declared = EffectSet {
896                    concrete: {
897                        let mut s = std::collections::BTreeSet::new();
898                        for e in l_effects {
899                            let arg = e.arg.as_ref().map(|a| match a {
900                                a::EffectArg::Str { value } => crate::types::EffectArg::Str(value.clone()),
901                                a::EffectArg::Int { value } => crate::types::EffectArg::Int(*value),
902                                a::EffectArg::Ident { value } => crate::types::EffectArg::Ident(value.clone()),
903                            });
904                            s.insert(crate::types::EffectKind { name: e.name.clone(), arg });
905                        }
906                        s
907                    },
908                    var: None,
909                };
910                let mut inner_locals = locals.clone();
911                for (p, t) in params.iter().zip(param_tys.iter()) {
912                    inner_locals.insert(p.name.clone(), t.clone());
913                }
914                let mut inner_effs = EffectSet::empty();
915                let body_ty = self.check_expr(body, node_id, &mut inner_locals, &mut inner_effs)?;
916                if let Err(err) = self.unify_with_record_coercion(&body_ty, &ret_ty) {
917                    return Err(mismatch_err(node_id, err, &self.u, vec!["in lambda body".into()]));
918                }
919                if !inner_effs.is_subset(&declared) {
920                    for e in inner_effs.concrete.iter() {
921                        if !declared.concrete.iter().any(|d| d.subsumes(e)) {
922                            return Err(TypeError::EffectNotDeclared {
923                                at_node: node_id.into(),
924                                effect: e.pretty(),
925                            });
926                        }
927                    }
928                }
929                Ok(Ty::function(param_tys, declared, ret_ty))
930            }
931            a::CExpr::BinOp { op, lhs, rhs } => self.check_binop(op, lhs, rhs, node_id, locals, effs),
932            a::CExpr::UnaryOp { op, expr } => {
933                let t = self.check_expr(expr, node_id, locals, effs)?;
934                match op.as_str() {
935                    "-" => {
936                        // Either Int or Float; we pick Int by default if unconstrained.
937                        let r = self.u.resolve(&t);
938                        match r {
939                            Ty::Prim(Prim::Int) | Ty::Prim(Prim::Float) => Ok(t),
940                            Ty::Var(_) => {
941                                // default to Int.
942                                self.u.unify(&t, &Ty::int()).map_err(|e| mismatch_err(node_id, e, &self.u, vec![]))?;
943                                Ok(Ty::int())
944                            }
945                            other => Err(TypeError::TypeMismatch {
946                                at_node: node_id.into(),
947                                expected: "Int or Float".into(),
948                                got: other.pretty(),
949                                context: vec!["unary `-`".into()],
950                            }),
951                        }
952                    }
953                    "not" => {
954                        self.u.unify(&t, &Ty::bool()).map_err(|e| mismatch_err(node_id, e, &self.u, vec!["unary `not`".into()]))?;
955                        Ok(Ty::bool())
956                    }
957                    other => panic!("unknown unary op: {other}"),
958                }
959            }
960            a::CExpr::Return { value } => {
961                // For now treat Return as having type Never; the surrounding
962                // context will unify with the actual return type.
963                self.check_expr(value, node_id, locals, effs)?;
964                Ok(Ty::Never)
965            }
966        }
967    }
968
969    fn check_binop(
970        &mut self,
971        op: &str,
972        lhs: &a::CExpr,
973        rhs: &a::CExpr,
974        node_id: &str,
975        locals: &mut IndexMap<String, Ty>,
976        effs: &mut EffectSet,
977    ) -> Result<Ty, TypeError> {
978        let lt = self.check_expr(lhs, node_id, locals, effs)?;
979        let rt = self.check_expr(rhs, node_id, locals, effs)?;
980        match op {
981            "+" => {
982                // #308: `+` is overloaded over Int, Float, and Str.
983                // Str concatenation dispatches at the VM layer
984                // (Op::NumAdd in bytecode handles all three).
985                // #323: unfold one-step type aliases on the resolved
986                // type so `type UserId = Int; id + id` works under
987                // Option-A transparency. Same below for the other
988                // numeric operator groups.
989                self.u.unify(&lt, &rt).map_err(|e| mismatch_err(node_id, e, &self.u, vec![format!("operator `{op}`")]))?;
990                let r = self.unfold_record_alias(self.u.resolve(&lt));
991                match r {
992                    Ty::Prim(Prim::Int) | Ty::Prim(Prim::Float) | Ty::Prim(Prim::Str) => Ok(lt),
993                    Ty::Var(_) => {
994                        self.u.unify(&lt, &Ty::int()).map_err(|e| mismatch_err(node_id, e, &self.u, vec![format!("operator `{op}`")]))?;
995                        Ok(Ty::int())
996                    }
997                    other => Err(TypeError::TypeMismatch {
998                        at_node: node_id.into(),
999                        expected: "Int, Float, or Str".into(),
1000                        got: other.pretty(),
1001                        context: vec![format!("operator `{op}`")],
1002                    }),
1003                }
1004            }
1005            "-" | "*" | "/" | "%" => {
1006                self.u.unify(&lt, &rt).map_err(|e| mismatch_err(node_id, e, &self.u, vec![format!("operator `{op}`")]))?;
1007                let r = self.unfold_record_alias(self.u.resolve(&lt));
1008                match r {
1009                    Ty::Prim(Prim::Int) | Ty::Prim(Prim::Float) => Ok(lt),
1010                    Ty::Var(_) => {
1011                        self.u.unify(&lt, &Ty::int()).map_err(|e| mismatch_err(node_id, e, &self.u, vec![format!("operator `{op}`")]))?;
1012                        Ok(Ty::int())
1013                    }
1014                    other => Err(TypeError::TypeMismatch {
1015                        at_node: node_id.into(),
1016                        expected: "Int or Float".into(),
1017                        got: other.pretty(),
1018                        context: vec![format!("operator `{op}`")],
1019                    }),
1020                }
1021            }
1022            "==" | "!=" => {
1023                self.u.unify(&lt, &rt).map_err(|e| mismatch_err(node_id, e, &self.u, vec![format!("operator `{op}`")]))?;
1024                Ok(Ty::bool())
1025            }
1026            "<" | "<=" | ">" | ">=" => {
1027                self.u.unify(&lt, &rt).map_err(|e| mismatch_err(node_id, e, &self.u, vec![format!("operator `{op}`")]))?;
1028                let r = self.unfold_record_alias(self.u.resolve(&lt));
1029                match r {
1030                    Ty::Prim(Prim::Int) | Ty::Prim(Prim::Float) | Ty::Prim(Prim::Str) => Ok(Ty::bool()),
1031                    Ty::Var(_) => {
1032                        self.u.unify(&lt, &Ty::int()).map_err(|e| mismatch_err(node_id, e, &self.u, vec![format!("operator `{op}`")]))?;
1033                        Ok(Ty::bool())
1034                    }
1035                    other => Err(TypeError::TypeMismatch {
1036                        at_node: node_id.into(),
1037                        expected: "Int, Float, or Str".into(),
1038                        got: other.pretty(),
1039                        context: vec![format!("operator `{op}`")],
1040                    }),
1041                }
1042            }
1043            "and" | "or" => {
1044                self.u.unify(&lt, &Ty::bool()).map_err(|e| mismatch_err(node_id, e, &self.u, vec![format!("operator `{op}`")]))?;
1045                self.u.unify(&rt, &Ty::bool()).map_err(|e| mismatch_err(node_id, e, &self.u, vec![format!("operator `{op}`")]))?;
1046                Ok(Ty::bool())
1047            }
1048            other => panic!("unknown binop: {other}"),
1049        }
1050    }
1051
1052    fn check_call(
1053        &mut self,
1054        call_expr: &a::CExpr,
1055        callee: &a::CExpr,
1056        args: &[a::CExpr],
1057        node_id: &str,
1058        locals: &mut IndexMap<String, Ty>,
1059        effs: &mut EffectSet,
1060    ) -> Result<Ty, TypeError> {
1061        // #168: snapshot the call's address before the recursive
1062        // descent so we can later rewrite this exact node. Pointer
1063        // identity is only meaningful while the AST stays put,
1064        // which it does until check_program returns and the AST
1065        // is handed back to the caller. `is_module_parse_call`
1066        // recognises `<alias>.parse` where alias was bound to one
1067        // of {json, toml, yaml} during the import pass.
1068        let parse_call_ptr = if self.is_module_parse_call(callee) {
1069            Some(call_expr as *const a::CExpr as usize)
1070        } else {
1071            None
1072        };
1073        let callee_ty = self.check_expr(callee, node_id, locals, effs)?;
1074        let resolved = self.u.resolve(&callee_ty);
1075        match resolved {
1076            Ty::Function { params, effects, ret } => {
1077                if params.len() != args.len() {
1078                    return Err(TypeError::ArityMismatch {
1079                        at_node: node_id.into(),
1080                        expected: params.len(),
1081                        got: args.len(),
1082                    });
1083                }
1084                for (i, (a, p)) in args.iter().zip(params.iter()).enumerate() {
1085                    let at = self.check_expr(a, node_id, locals, effs)?;
1086                    if let Err(err) = self.unify_with_record_coercion(&at, p) {
1087                        return Err(mismatch_err(node_id, err, &self.u, vec![format!("argument {} of call", i + 1)]));
1088                    }
1089                }
1090                // #209 slice 2: refinement discharge for direct named
1091                // calls. Look up the callee's original params (kept
1092                // pre-strip in `fn_params`), and for each refined
1093                // param attempt static discharge against the call
1094                // arg. Refuted = type error; Deferred = pass (slice
1095                // 3 will add a runtime residual check).
1096                if let a::CExpr::Var { name: callee_name } = callee {
1097                    if let Some(callee_params) = self.fn_params.get(callee_name).cloned() {
1098                        for (i, (param, arg)) in callee_params.iter().zip(args.iter()).enumerate() {
1099                            if let a::TypeExpr::Refined { binding, predicate, .. } = &param.ty {
1100                                let outcome = crate::discharge::try_discharge(
1101                                    predicate, binding, arg);
1102                                if let crate::discharge::DischargeOutcome::Refuted { reason } = outcome {
1103                                    return Err(TypeError::RefinementViolation {
1104                                        at_node: node_id.into(),
1105                                        fn_name: callee_name.clone(),
1106                                        param_index: i,
1107                                        binding: binding.clone(),
1108                                        reason,
1109                                    });
1110                                }
1111                            }
1112                        }
1113                    }
1114                }
1115                // Re-resolve effects after unifying args: an effect-row
1116                // variable on the function type may have been bound by
1117                // an argument's closure type, and we want the
1118                // *post-binding* set when propagating to the caller.
1119                let resolved_effects = self.u.resolve_effects(&effects);
1120                effs.extend(&resolved_effects);
1121                // #168: snapshot the post-arg-unification return type
1122                // for stdlib parse calls. Resolution to the eventual
1123                // `Result[Record{...}, _]` shape happens at the end
1124                // of `check_program` once the whole program's
1125                // unification has settled — match-pattern annotations
1126                // and let-type-annotations may bind T after this
1127                // point.
1128                if let Some(ptr) = parse_call_ptr {
1129                    self.pending_parse_calls.push((ptr, (*ret).clone()));
1130                }
1131                Ok(*ret)
1132            }
1133            Ty::Var(_) => {
1134                // Build a function type and unify.
1135                let mut p_tys = Vec::new();
1136                for a in args { p_tys.push(self.check_expr(a, node_id, locals, effs)?); }
1137                let r = self.u.fresh();
1138                let f = Ty::function(p_tys, EffectSet::empty(), r.clone());
1139                self.u.unify(&callee_ty, &f).map_err(|e| mismatch_err(node_id, e, &self.u, vec!["in call".into()]))?;
1140                Ok(r)
1141            }
1142            other => Err(TypeError::TypeMismatch {
1143                at_node: node_id.into(),
1144                expected: "function".into(),
1145                got: other.pretty(),
1146                context: vec!["in call".into()],
1147            }),
1148        }
1149    }
1150
1151    fn check_constructor(
1152        &mut self,
1153        name: &str,
1154        args: &[a::CExpr],
1155        node_id: &str,
1156        locals: &mut IndexMap<String, Ty>,
1157        effs: &mut EffectSet,
1158    ) -> Result<Ty, TypeError> {
1159        let owning = self.type_env.ctor_to_type.get(name).cloned()
1160            .ok_or_else(|| TypeError::UnknownVariant {
1161                at_node: node_id.into(),
1162                constructor: name.to_string(),
1163            })?;
1164        let def = self.type_env.types.get(&owning).cloned()
1165            .expect("ctor_to_type points to a real type");
1166        let variants = match &def.kind {
1167            TypeDefKind::Union(v) => v.clone(),
1168            _ => return Err(TypeError::UnknownVariant {
1169                at_node: node_id.into(),
1170                constructor: name.to_string(),
1171            }),
1172        };
1173        // Instantiate the type's params with fresh vars; substitute into
1174        // both the variant's payload type and the resulting Con(...).
1175        let mut subst = IndexMap::new();
1176        let mut con_args = Vec::with_capacity(def.params.len());
1177        for (i, _p) in def.params.iter().enumerate() {
1178            let fresh = self.u.fresh();
1179            subst.insert(i as u32, fresh.clone());
1180            con_args.push(fresh);
1181        }
1182        let payload = variants.get(name).cloned().flatten();
1183        match (payload, args) {
1184            (None, []) => Ok(Ty::Con(owning, con_args)),
1185            (Some(payload), args) => {
1186                let inst_payload = subst_vars(&payload, &subst, &IndexMap::new());
1187                let arg_count = match &inst_payload {
1188                    Ty::Tuple(items) => items.len(),
1189                    _ => 1,
1190                };
1191                if arg_count != args.len() {
1192                    return Err(TypeError::ArityMismatch {
1193                        at_node: node_id.into(),
1194                        expected: arg_count,
1195                        got: args.len(),
1196                    });
1197                }
1198                if args.len() == 1 {
1199                    let at = self.check_expr(&args[0], node_id, locals, effs)?;
1200                    self.unify_with_record_coercion(&at, &inst_payload).map_err(|e| mismatch_err(node_id, e, &self.u, vec![format!("constructor `{}`", name)]))?;
1201                } else if let Ty::Tuple(items) = inst_payload {
1202                    for (i, (a, t)) in args.iter().zip(items.iter()).enumerate() {
1203                        let at = self.check_expr(a, node_id, locals, effs)?;
1204                        self.unify_with_record_coercion(&at, t).map_err(|e| mismatch_err(node_id, e, &self.u, vec![format!("constructor `{}` arg {}", name, i + 1)]))?;
1205                    }
1206                }
1207                Ok(Ty::Con(owning, con_args))
1208            }
1209            (None, _) => Err(TypeError::ArityMismatch {
1210                at_node: node_id.into(), expected: 0, got: args.len(),
1211            }),
1212        }
1213    }
1214
1215    fn bind_pattern(
1216        &mut self,
1217        pat: &a::Pattern,
1218        ty: &Ty,
1219        locals: &mut IndexMap<String, Ty>,
1220        node_id: &str,
1221    ) -> Result<(), TypeError> {
1222        match pat {
1223            a::Pattern::PWild => Ok(()),
1224            a::Pattern::PVar { name } => {
1225                locals.insert(name.clone(), ty.clone());
1226                Ok(())
1227            }
1228            a::Pattern::PLiteral { value } => {
1229                let lt = lit_type(value);
1230                self.unify_with_record_coercion(&lt, ty).map_err(|e| mismatch_err(node_id, e, &self.u, vec!["in pattern".into()]))?;
1231                Ok(())
1232            }
1233            a::Pattern::PConstructor { name, args } => {
1234                // Re-use constructor logic but in pattern position.
1235                let owning = self.type_env.ctor_to_type.get(name).cloned()
1236                    .ok_or_else(|| TypeError::UnknownVariant {
1237                        at_node: node_id.into(), constructor: name.clone(),
1238                    })?;
1239                let def = self.type_env.types.get(&owning).cloned().unwrap();
1240                let mut subst = IndexMap::new();
1241                let mut con_args = Vec::new();
1242                for (i, _) in def.params.iter().enumerate() {
1243                    let fresh = self.u.fresh();
1244                    subst.insert(i as u32, fresh.clone());
1245                    con_args.push(fresh);
1246                }
1247                let con_ty = Ty::Con(owning.clone(), con_args);
1248                self.unify_with_record_coercion(&con_ty, ty).map_err(|e| mismatch_err(node_id, e, &self.u, vec![format!("constructor pattern `{}`", name)]))?;
1249                let payload = match &def.kind {
1250                    TypeDefKind::Union(v) => v.get(name).cloned().flatten(),
1251                    _ => None,
1252                };
1253                match (payload, args.as_slice()) {
1254                    (None, []) => Ok(()),
1255                    (Some(payload), args) => {
1256                        let inst = subst_vars(&payload, &subst, &IndexMap::new());
1257                        if args.len() == 1 {
1258                            self.bind_pattern(&args[0], &inst, locals, node_id)?;
1259                        } else if let Ty::Tuple(items) = inst {
1260                            for (a, t) in args.iter().zip(items.iter()) {
1261                                self.bind_pattern(a, t, locals, node_id)?;
1262                            }
1263                        }
1264                        Ok(())
1265                    }
1266                    (None, _) => Err(TypeError::ArityMismatch {
1267                        at_node: node_id.into(), expected: 0, got: args.len(),
1268                    }),
1269                }
1270            }
1271            a::Pattern::PRecord { fields } => {
1272                // Unfold a record-aliased Con (`type Bands = { ... }`)
1273                // so a structural `{ idea: pat, ... }` pattern can match
1274                // a nominal-typed scrutinee, mirror of #79's literal
1275                // coercion at every position.
1276                let resolved = self.unfold_record_alias(self.u.resolve(ty));
1277                let rec = match resolved {
1278                    Ty::Record(r) => r,
1279                    _ => return Err(TypeError::TypeMismatch {
1280                        at_node: node_id.into(),
1281                        expected: "record".into(),
1282                        got: ty.pretty(),
1283                        context: vec!["in record pattern".into()],
1284                    }),
1285                };
1286                for f in fields {
1287                    let ft = rec.get(&f.name).cloned()
1288                        .ok_or_else(|| TypeError::UnknownField {
1289                            at_node: node_id.into(),
1290                            record_type: Ty::Record(rec.clone()).pretty(),
1291                            field: f.name.clone(),
1292                        })?;
1293                    self.bind_pattern(&f.pattern, &ft, locals, node_id)?;
1294                }
1295                Ok(())
1296            }
1297            a::Pattern::PTuple { items } => {
1298                // An empty-tuple pattern `()` is equivalent to Unit.
1299                if items.is_empty() {
1300                    return self.unify_with_record_coercion(&Ty::Unit, ty)
1301                        .map_err(|e| mismatch_err(node_id, e, &self.u, vec!["in unit pattern".into()]));
1302                }
1303                let resolved = self.u.resolve(ty);
1304                let tup = match resolved {
1305                    Ty::Tuple(t) => t,
1306                    Ty::Var(_) => {
1307                        let fresh: Vec<Ty> = items.iter().map(|_| self.u.fresh()).collect();
1308                        let tup_ty = Ty::Tuple(fresh.clone());
1309                        self.unify_with_record_coercion(&tup_ty, ty).map_err(|e| mismatch_err(node_id, e, &self.u, vec!["in tuple pattern".into()]))?;
1310                        fresh
1311                    }
1312                    other => {
1313                        return Err(TypeError::TypeMismatch {
1314                            at_node: node_id.into(),
1315                            expected: "tuple".into(),
1316                            got: other.pretty(),
1317                            context: vec!["in tuple pattern".into()],
1318                        });
1319                    }
1320                };
1321                if tup.len() != items.len() {
1322                    return Err(TypeError::ArityMismatch {
1323                        at_node: node_id.into(), expected: tup.len(), got: items.len(),
1324                    });
1325                }
1326                for (p, t) in items.iter().zip(tup.iter()) {
1327                    self.bind_pattern(p, t, locals, node_id)?;
1328                }
1329                Ok(())
1330            }
1331        }
1332    }
1333}
1334
1335fn lit_type(l: &a::CLit) -> Ty {
1336    match l {
1337        a::CLit::Int { .. } => Ty::int(),
1338        a::CLit::Float { .. } => Ty::float(),
1339        a::CLit::Str { .. } => Ty::str(),
1340        a::CLit::Bytes { .. } => Ty::bytes(),
1341        a::CLit::Bool { .. } => Ty::bool(),
1342        a::CLit::Unit => Ty::Unit,
1343    }
1344}
1345
1346fn instantiate(s: &Scheme, u: &mut Unifier) -> Ty {
1347    let mut ty_subst = IndexMap::new();
1348    for v in &s.vars { ty_subst.insert(*v, u.fresh()); }
1349    let mut eff_subst = IndexMap::new();
1350    for v in &s.eff_vars { eff_subst.insert(*v, u.fresh_eff_id()); }
1351    subst_vars(&s.ty, &ty_subst, &eff_subst)
1352}
1353
1354fn subst_vars(
1355    t: &Ty,
1356    subst: &IndexMap<TyVarId, Ty>,
1357    eff_subst: &IndexMap<u32, u32>,
1358) -> Ty {
1359    match t {
1360        Ty::Var(v) => subst.get(v).cloned().unwrap_or_else(|| Ty::Var(*v)),
1361        Ty::Prim(_) | Ty::Unit | Ty::Never => t.clone(),
1362        Ty::List(inner) => Ty::List(Box::new(subst_vars(inner, subst, eff_subst))),
1363        Ty::Tuple(items) => Ty::Tuple(items.iter().map(|t| subst_vars(t, subst, eff_subst)).collect()),
1364        Ty::Record(fs) => {
1365            let mut out = IndexMap::new();
1366            for (k, v) in fs { out.insert(k.clone(), subst_vars(v, subst, eff_subst)); }
1367            Ty::Record(out)
1368        }
1369        Ty::Con(n, args) => Ty::Con(n.clone(),
1370            args.iter().map(|t| subst_vars(t, subst, eff_subst)).collect()),
1371        Ty::Function { params, effects, ret } => {
1372            // Refresh the effect-row variable if it's quantified in the
1373            // scheme; concrete kinds carry through unchanged.
1374            let new_effects = EffectSet {
1375                concrete: effects.concrete.clone(),
1376                var: effects.var.and_then(|v| eff_subst.get(&v).copied()).or(effects.var),
1377            };
1378            Ty::Function {
1379                params: params.iter().map(|t| subst_vars(t, subst, eff_subst)).collect(),
1380                effects: new_effects,
1381                ret: Box::new(subst_vars(ret, subst, eff_subst)),
1382            }
1383        }
1384    }
1385}
1386
1387fn mismatch_err(node_id: &str, e: UnifyError, u: &Unifier, context: Vec<String>) -> TypeError {
1388    match e {
1389        UnifyError::Mismatch { a, b } => TypeError::TypeMismatch {
1390            at_node: node_id.into(),
1391            expected: u.resolve(&b).pretty(),
1392            got: u.resolve(&a).pretty(),
1393            context,
1394        },
1395        UnifyError::Infinite { .. } => TypeError::InfiniteType { at_node: node_id.into() },
1396        UnifyError::EffectMismatch { a, b } => {
1397            // Render the two rows in compact form, e.g. `[net]` vs `[]`.
1398            // Effect rows are invariant, so this is its own rule_tag
1399            // (#565) rather than a generic type-mismatch — the
1400            // explanation steers the fix toward narrowing the body.
1401            let render = |e: &EffectSet| -> String {
1402                let mut parts: Vec<String> = e.concrete.iter()
1403                    .map(crate::types::EffectKind::pretty).collect();
1404                if let Some(v) = e.var { parts.push(format!("?e{}", v)); }
1405                if parts.is_empty() { "[]".into() } else { format!("[{}]", parts.join(", ")) }
1406            };
1407            TypeError::EffectRowMismatch {
1408                at_node: node_id.into(),
1409                expected: render(&b),
1410                got: render(&a),
1411                context,
1412            }
1413        }
1414    }
1415}