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                self.u.unify_effects(ea, eb).map_err(|_| UnifyError::Mismatch { a: a.clone(), b: b.clone() })?;
645                self.unify_coerce_inner((**ra).clone(), (**rb).clone())
646            }
647            _ => self.u.unify(&a, &b),
648        }
649    }
650
651    fn check_fn(&mut self, fd: &a::FnDecl) -> Result<Scheme, Vec<TypeError>> {
652        // Instantiate fn's signature with fresh vars for its type params.
653        let scheme = function_scheme(fd, &self.type_env);
654        let (param_tys, declared_effects, ret_ty) = match instantiate(&scheme, &mut self.u) {
655            Ty::Function { params, effects, ret } => (params, effects, *ret),
656            _ => unreachable!(),
657        };
658
659        let mut locals: IndexMap<String, Ty> = IndexMap::new();
660        for (p, t) in fd.params.iter().zip(param_tys.iter()) {
661            locals.insert(p.name.clone(), t.clone());
662        }
663
664        let mut inferred_effects = EffectSet::empty();
665        let body_ty = self.check_expr(&fd.body, "n_0", &mut locals, &mut inferred_effects)
666            .map_err(|e| vec![e])?;
667
668        // The body may produce an anonymous record literal where the
669        // signature expects a nominal record alias (and vice-versa,
670        // and at any nested level). `unify_with_record_coercion`
671        // handles that asymmetry while keeping nominal-vs-nominal
672        // mismatches strict.
673        if let Err(e) = self.unify_with_record_coercion(&body_ty, &ret_ty) {
674            return Err(vec![mismatch_err("n_0", e, &self.u, vec![format!("in function `{}`", fd.name)])]);
675        }
676
677        if !inferred_effects.is_subset(&declared_effects) {
678            // Pick the first undeclared effect for the error.
679            for e in inferred_effects.concrete.iter() {
680                if !declared_effects.concrete.iter().any(|d| d.subsumes(e)) {
681                    return Err(vec![TypeError::EffectNotDeclared {
682                        at_node: "n_0".into(),
683                        effect: e.pretty(),
684                    }]);
685                }
686            }
687        }
688
689        // #369: signature-level examples. Pure-only in v1; arg arity
690        // must match params; each arg type-checks against its param,
691        // each expected type-checks against the return type. Behavioral
692        // equivalence (run the body, compare to expected) is a follow-up
693        // — this slice catches type-level drift, which is already a
694        // common source of stale examples.
695        if !fd.examples.is_empty() {
696            if !declared_effects.concrete.is_empty() {
697                return Err(vec![TypeError::ExamplesOnEffectfulFn {
698                    at_node: "n_0".into(),
699                    fn_name: fd.name.clone(),
700                }]);
701            }
702            for (case_index, ex) in fd.examples.iter().enumerate() {
703                if ex.args.len() != param_tys.len() {
704                    return Err(vec![TypeError::ExampleArityMismatch {
705                        at_node: "n_0".into(),
706                        fn_name: fd.name.clone(),
707                        case_index,
708                        expected: param_tys.len(),
709                        got: ex.args.len(),
710                    }]);
711                }
712                let mut example_locals: IndexMap<String, Ty> = IndexMap::new();
713                let mut example_effects = EffectSet::empty();
714                for (i, (arg, expected_ty)) in
715                    ex.args.iter().zip(param_tys.iter()).enumerate()
716                {
717                    let arg_ty = self
718                        .check_expr(arg, "n_0", &mut example_locals, &mut example_effects)
719                        .map_err(|e| vec![e])?;
720                    if let Err(e) = self.unify_with_record_coercion(&arg_ty, expected_ty) {
721                        return Err(vec![mismatch_err(
722                            "n_0",
723                            e,
724                            &self.u,
725                            vec![format!(
726                                "in example #{} for `{}`, argument {}",
727                                case_index + 1,
728                                fd.name,
729                                i + 1
730                            )],
731                        )]);
732                    }
733                }
734                let expected_ty = self
735                    .check_expr(&ex.expected, "n_0", &mut example_locals, &mut example_effects)
736                    .map_err(|e| vec![e])?;
737                if let Err(e) = self.unify_with_record_coercion(&expected_ty, &ret_ty) {
738                    return Err(vec![mismatch_err(
739                        "n_0",
740                        e,
741                        &self.u,
742                        vec![format!(
743                            "in example #{} for `{}`, expected value",
744                            case_index + 1,
745                            fd.name
746                        )],
747                    )]);
748                }
749                // The example's args/expected are expected to be pure
750                // by construction (literals in the common case); if
751                // they invoked effects, they'd break the pure-only
752                // discipline. Reject the first one via the same effect rule.
753                if let Some(e) = example_effects.concrete.iter().next() {
754                    return Err(vec![TypeError::EffectNotDeclared {
755                        at_node: "n_0".into(),
756                        effect: e.pretty(),
757                    }]);
758                }
759            }
760        }
761
762        Ok(scheme)
763    }
764
765    fn check_expr(
766        &mut self,
767        e: &a::CExpr,
768        node_id: &str,
769        locals: &mut IndexMap<String, Ty>,
770        effs: &mut EffectSet,
771    ) -> Result<Ty, TypeError> {
772        match e {
773            a::CExpr::Literal { value } => Ok(lit_type(value)),
774            a::CExpr::Var { name } => {
775                if let Some(t) = locals.get(name) {
776                    return Ok(t.clone());
777                }
778                if let Some(scheme) = self.globals.get(name).cloned() {
779                    return Ok(instantiate(&scheme, &mut self.u));
780                }
781                Err(TypeError::UnknownIdentifier { at_node: node_id.into(), name: name.clone() })
782            }
783            a::CExpr::Constructor { name, args } => self.check_constructor(name, args, node_id, locals, effs),
784            a::CExpr::Call { callee, args } => self.check_call(e, callee, args, node_id, locals, effs),
785            a::CExpr::Let { name, ty, value, body } => {
786                let v_ty = self.check_expr(value, node_id, locals, effs)?;
787                if let Some(declared) = ty {
788                    let d = ty_from_canon_env(declared, &[], &self.type_env);
789                    if let Err(err) = self.unify_with_record_coercion(&v_ty, &d) {
790                        return Err(mismatch_err(node_id, err, &self.u, vec![format!("in let `{}`", name)]));
791                    }
792                }
793                let prev = locals.insert(name.clone(), v_ty);
794                let body_ty = self.check_expr(body, node_id, locals, effs)?;
795                match prev {
796                    Some(p) => { locals.insert(name.clone(), p); }
797                    None => { locals.shift_remove(name); }
798                }
799                Ok(body_ty)
800            }
801            a::CExpr::Match { scrutinee, arms } => {
802                let scrut_ty = self.check_expr(scrutinee, node_id, locals, effs)?;
803                if arms.is_empty() {
804                    return Err(TypeError::NonExhaustiveMatch {
805                        at_node: node_id.into(), missing: vec!["_".into()]
806                    });
807                }
808                let result_ty = self.u.fresh();
809                for arm in arms {
810                    let mut arm_locals = locals.clone();
811                    self.bind_pattern(&arm.pattern, &scrut_ty, &mut arm_locals, node_id)?;
812                    let arm_ty = self.check_expr(&arm.body, node_id, &mut arm_locals, effs)?;
813                    if let Err(err) = self.unify_with_record_coercion(&arm_ty, &result_ty) {
814                        return Err(mismatch_err(node_id, err, &self.u, vec!["in match arm".into()]));
815                    }
816                }
817                Ok(result_ty)
818            }
819            a::CExpr::Block { statements, result } => {
820                for s in statements {
821                    self.check_expr(s, node_id, locals, effs)?;
822                }
823                self.check_expr(result, node_id, locals, effs)
824            }
825            a::CExpr::RecordLit { fields } => {
826                let mut tys = IndexMap::new();
827                for f in fields {
828                    if tys.contains_key(&f.name) {
829                        return Err(TypeError::DuplicateField {
830                            at_node: node_id.into(), field: f.name.clone()
831                        });
832                    }
833                    let ft = self.check_expr(&f.value, node_id, locals, effs)?;
834                    tys.insert(f.name.clone(), ft);
835                }
836                Ok(Ty::Record(tys))
837            }
838            a::CExpr::TupleLit { items } => {
839                let mut ts = Vec::new();
840                for it in items { ts.push(self.check_expr(it, node_id, locals, effs)?); }
841                Ok(Ty::Tuple(ts))
842            }
843            a::CExpr::ListLit { items } => {
844                let elem = self.u.fresh();
845                for it in items {
846                    let t = self.check_expr(it, node_id, locals, effs)?;
847                    if let Err(err) = self.unify_with_record_coercion(&t, &elem) {
848                        return Err(mismatch_err(node_id, err, &self.u, vec!["in list literal".into()]));
849                    }
850                }
851                Ok(Ty::List(Box::new(elem)))
852            }
853            a::CExpr::FieldAccess { value, field } => {
854                let vt = self.check_expr(value, node_id, locals, effs)?;
855                let resolved = self.u.resolve(&vt);
856                // Unfold a Record-aliased Con (e.g. `type Request = { ... }`
857                // or `type Box[T] = { value :: T }`). For parametric aliases
858                // the helper substitutes the actual args for the formal
859                // params; the post-unfold shape is only a Record when the
860                // alias body was a record, so non-record aliases (e.g.
861                // `type UserId = Int`) fall through to the
862                // "expected record" error below.
863                let resolved = if let Ty::Con(_, _) = &resolved {
864                    let unfolded = self.unfold_record_alias(resolved.clone());
865                    if matches!(unfolded, Ty::Record(_)) {
866                        unfolded
867                    } else {
868                        resolved
869                    }
870                } else {
871                    resolved
872                };
873                match resolved {
874                    Ty::Record(fields) => fields.get(field).cloned()
875                        .ok_or_else(|| TypeError::UnknownField {
876                            at_node: node_id.into(),
877                            record_type: Ty::Record(fields.clone()).pretty(),
878                            field: field.clone(),
879                        }),
880                    other => Err(TypeError::TypeMismatch {
881                        at_node: node_id.into(),
882                        expected: "record".into(),
883                        got: other.pretty(),
884                        context: vec![format!("field access `.{}`", field)],
885                    }),
886                }
887            }
888            a::CExpr::Lambda { params, return_type, effects: l_effects, body } => {
889                let param_tys: Vec<Ty> = params.iter().map(|p| ty_from_canon_env(&p.ty, &[], &self.type_env)).collect();
890                let ret_ty = ty_from_canon_env(return_type, &[], &self.type_env);
891                let declared = EffectSet {
892                    concrete: {
893                        let mut s = std::collections::BTreeSet::new();
894                        for e in l_effects {
895                            let arg = e.arg.as_ref().map(|a| match a {
896                                a::EffectArg::Str { value } => crate::types::EffectArg::Str(value.clone()),
897                                a::EffectArg::Int { value } => crate::types::EffectArg::Int(*value),
898                                a::EffectArg::Ident { value } => crate::types::EffectArg::Ident(value.clone()),
899                            });
900                            s.insert(crate::types::EffectKind { name: e.name.clone(), arg });
901                        }
902                        s
903                    },
904                    var: None,
905                };
906                let mut inner_locals = locals.clone();
907                for (p, t) in params.iter().zip(param_tys.iter()) {
908                    inner_locals.insert(p.name.clone(), t.clone());
909                }
910                let mut inner_effs = EffectSet::empty();
911                let body_ty = self.check_expr(body, node_id, &mut inner_locals, &mut inner_effs)?;
912                if let Err(err) = self.unify_with_record_coercion(&body_ty, &ret_ty) {
913                    return Err(mismatch_err(node_id, err, &self.u, vec!["in lambda body".into()]));
914                }
915                if !inner_effs.is_subset(&declared) {
916                    for e in inner_effs.concrete.iter() {
917                        if !declared.concrete.iter().any(|d| d.subsumes(e)) {
918                            return Err(TypeError::EffectNotDeclared {
919                                at_node: node_id.into(),
920                                effect: e.pretty(),
921                            });
922                        }
923                    }
924                }
925                Ok(Ty::function(param_tys, declared, ret_ty))
926            }
927            a::CExpr::BinOp { op, lhs, rhs } => self.check_binop(op, lhs, rhs, node_id, locals, effs),
928            a::CExpr::UnaryOp { op, expr } => {
929                let t = self.check_expr(expr, node_id, locals, effs)?;
930                match op.as_str() {
931                    "-" => {
932                        // Either Int or Float; we pick Int by default if unconstrained.
933                        let r = self.u.resolve(&t);
934                        match r {
935                            Ty::Prim(Prim::Int) | Ty::Prim(Prim::Float) => Ok(t),
936                            Ty::Var(_) => {
937                                // default to Int.
938                                self.u.unify(&t, &Ty::int()).map_err(|e| mismatch_err(node_id, e, &self.u, vec![]))?;
939                                Ok(Ty::int())
940                            }
941                            other => Err(TypeError::TypeMismatch {
942                                at_node: node_id.into(),
943                                expected: "Int or Float".into(),
944                                got: other.pretty(),
945                                context: vec!["unary `-`".into()],
946                            }),
947                        }
948                    }
949                    "not" => {
950                        self.u.unify(&t, &Ty::bool()).map_err(|e| mismatch_err(node_id, e, &self.u, vec!["unary `not`".into()]))?;
951                        Ok(Ty::bool())
952                    }
953                    other => panic!("unknown unary op: {other}"),
954                }
955            }
956            a::CExpr::Return { value } => {
957                // For now treat Return as having type Never; the surrounding
958                // context will unify with the actual return type.
959                self.check_expr(value, node_id, locals, effs)?;
960                Ok(Ty::Never)
961            }
962        }
963    }
964
965    fn check_binop(
966        &mut self,
967        op: &str,
968        lhs: &a::CExpr,
969        rhs: &a::CExpr,
970        node_id: &str,
971        locals: &mut IndexMap<String, Ty>,
972        effs: &mut EffectSet,
973    ) -> Result<Ty, TypeError> {
974        let lt = self.check_expr(lhs, node_id, locals, effs)?;
975        let rt = self.check_expr(rhs, node_id, locals, effs)?;
976        match op {
977            "+" => {
978                // #308: `+` is overloaded over Int, Float, and Str.
979                // Str concatenation dispatches at the VM layer
980                // (Op::NumAdd in bytecode handles all three).
981                // #323: unfold one-step type aliases on the resolved
982                // type so `type UserId = Int; id + id` works under
983                // Option-A transparency. Same below for the other
984                // numeric operator groups.
985                self.u.unify(&lt, &rt).map_err(|e| mismatch_err(node_id, e, &self.u, vec![format!("operator `{op}`")]))?;
986                let r = self.unfold_record_alias(self.u.resolve(&lt));
987                match r {
988                    Ty::Prim(Prim::Int) | Ty::Prim(Prim::Float) | Ty::Prim(Prim::Str) => Ok(lt),
989                    Ty::Var(_) => {
990                        self.u.unify(&lt, &Ty::int()).map_err(|e| mismatch_err(node_id, e, &self.u, vec![format!("operator `{op}`")]))?;
991                        Ok(Ty::int())
992                    }
993                    other => Err(TypeError::TypeMismatch {
994                        at_node: node_id.into(),
995                        expected: "Int, Float, or Str".into(),
996                        got: other.pretty(),
997                        context: vec![format!("operator `{op}`")],
998                    }),
999                }
1000            }
1001            "-" | "*" | "/" | "%" => {
1002                self.u.unify(&lt, &rt).map_err(|e| mismatch_err(node_id, e, &self.u, vec![format!("operator `{op}`")]))?;
1003                let r = self.unfold_record_alias(self.u.resolve(&lt));
1004                match r {
1005                    Ty::Prim(Prim::Int) | Ty::Prim(Prim::Float) => Ok(lt),
1006                    Ty::Var(_) => {
1007                        self.u.unify(&lt, &Ty::int()).map_err(|e| mismatch_err(node_id, e, &self.u, vec![format!("operator `{op}`")]))?;
1008                        Ok(Ty::int())
1009                    }
1010                    other => Err(TypeError::TypeMismatch {
1011                        at_node: node_id.into(),
1012                        expected: "Int or Float".into(),
1013                        got: other.pretty(),
1014                        context: vec![format!("operator `{op}`")],
1015                    }),
1016                }
1017            }
1018            "==" | "!=" => {
1019                self.u.unify(&lt, &rt).map_err(|e| mismatch_err(node_id, e, &self.u, vec![format!("operator `{op}`")]))?;
1020                Ok(Ty::bool())
1021            }
1022            "<" | "<=" | ">" | ">=" => {
1023                self.u.unify(&lt, &rt).map_err(|e| mismatch_err(node_id, e, &self.u, vec![format!("operator `{op}`")]))?;
1024                let r = self.unfold_record_alias(self.u.resolve(&lt));
1025                match r {
1026                    Ty::Prim(Prim::Int) | Ty::Prim(Prim::Float) | Ty::Prim(Prim::Str) => Ok(Ty::bool()),
1027                    Ty::Var(_) => {
1028                        self.u.unify(&lt, &Ty::int()).map_err(|e| mismatch_err(node_id, e, &self.u, vec![format!("operator `{op}`")]))?;
1029                        Ok(Ty::bool())
1030                    }
1031                    other => Err(TypeError::TypeMismatch {
1032                        at_node: node_id.into(),
1033                        expected: "Int, Float, or Str".into(),
1034                        got: other.pretty(),
1035                        context: vec![format!("operator `{op}`")],
1036                    }),
1037                }
1038            }
1039            "and" | "or" => {
1040                self.u.unify(&lt, &Ty::bool()).map_err(|e| mismatch_err(node_id, e, &self.u, vec![format!("operator `{op}`")]))?;
1041                self.u.unify(&rt, &Ty::bool()).map_err(|e| mismatch_err(node_id, e, &self.u, vec![format!("operator `{op}`")]))?;
1042                Ok(Ty::bool())
1043            }
1044            other => panic!("unknown binop: {other}"),
1045        }
1046    }
1047
1048    fn check_call(
1049        &mut self,
1050        call_expr: &a::CExpr,
1051        callee: &a::CExpr,
1052        args: &[a::CExpr],
1053        node_id: &str,
1054        locals: &mut IndexMap<String, Ty>,
1055        effs: &mut EffectSet,
1056    ) -> Result<Ty, TypeError> {
1057        // #168: snapshot the call's address before the recursive
1058        // descent so we can later rewrite this exact node. Pointer
1059        // identity is only meaningful while the AST stays put,
1060        // which it does until check_program returns and the AST
1061        // is handed back to the caller. `is_module_parse_call`
1062        // recognises `<alias>.parse` where alias was bound to one
1063        // of {json, toml, yaml} during the import pass.
1064        let parse_call_ptr = if self.is_module_parse_call(callee) {
1065            Some(call_expr as *const a::CExpr as usize)
1066        } else {
1067            None
1068        };
1069        let callee_ty = self.check_expr(callee, node_id, locals, effs)?;
1070        let resolved = self.u.resolve(&callee_ty);
1071        match resolved {
1072            Ty::Function { params, effects, ret } => {
1073                if params.len() != args.len() {
1074                    return Err(TypeError::ArityMismatch {
1075                        at_node: node_id.into(),
1076                        expected: params.len(),
1077                        got: args.len(),
1078                    });
1079                }
1080                for (i, (a, p)) in args.iter().zip(params.iter()).enumerate() {
1081                    let at = self.check_expr(a, node_id, locals, effs)?;
1082                    if let Err(err) = self.unify_with_record_coercion(&at, p) {
1083                        return Err(mismatch_err(node_id, err, &self.u, vec![format!("argument {} of call", i + 1)]));
1084                    }
1085                }
1086                // #209 slice 2: refinement discharge for direct named
1087                // calls. Look up the callee's original params (kept
1088                // pre-strip in `fn_params`), and for each refined
1089                // param attempt static discharge against the call
1090                // arg. Refuted = type error; Deferred = pass (slice
1091                // 3 will add a runtime residual check).
1092                if let a::CExpr::Var { name: callee_name } = callee {
1093                    if let Some(callee_params) = self.fn_params.get(callee_name).cloned() {
1094                        for (i, (param, arg)) in callee_params.iter().zip(args.iter()).enumerate() {
1095                            if let a::TypeExpr::Refined { binding, predicate, .. } = &param.ty {
1096                                let outcome = crate::discharge::try_discharge(
1097                                    predicate, binding, arg);
1098                                if let crate::discharge::DischargeOutcome::Refuted { reason } = outcome {
1099                                    return Err(TypeError::RefinementViolation {
1100                                        at_node: node_id.into(),
1101                                        fn_name: callee_name.clone(),
1102                                        param_index: i,
1103                                        binding: binding.clone(),
1104                                        reason,
1105                                    });
1106                                }
1107                            }
1108                        }
1109                    }
1110                }
1111                // Re-resolve effects after unifying args: an effect-row
1112                // variable on the function type may have been bound by
1113                // an argument's closure type, and we want the
1114                // *post-binding* set when propagating to the caller.
1115                let resolved_effects = self.u.resolve_effects(&effects);
1116                effs.extend(&resolved_effects);
1117                // #168: snapshot the post-arg-unification return type
1118                // for stdlib parse calls. Resolution to the eventual
1119                // `Result[Record{...}, _]` shape happens at the end
1120                // of `check_program` once the whole program's
1121                // unification has settled — match-pattern annotations
1122                // and let-type-annotations may bind T after this
1123                // point.
1124                if let Some(ptr) = parse_call_ptr {
1125                    self.pending_parse_calls.push((ptr, (*ret).clone()));
1126                }
1127                Ok(*ret)
1128            }
1129            Ty::Var(_) => {
1130                // Build a function type and unify.
1131                let mut p_tys = Vec::new();
1132                for a in args { p_tys.push(self.check_expr(a, node_id, locals, effs)?); }
1133                let r = self.u.fresh();
1134                let f = Ty::function(p_tys, EffectSet::empty(), r.clone());
1135                self.u.unify(&callee_ty, &f).map_err(|e| mismatch_err(node_id, e, &self.u, vec!["in call".into()]))?;
1136                Ok(r)
1137            }
1138            other => Err(TypeError::TypeMismatch {
1139                at_node: node_id.into(),
1140                expected: "function".into(),
1141                got: other.pretty(),
1142                context: vec!["in call".into()],
1143            }),
1144        }
1145    }
1146
1147    fn check_constructor(
1148        &mut self,
1149        name: &str,
1150        args: &[a::CExpr],
1151        node_id: &str,
1152        locals: &mut IndexMap<String, Ty>,
1153        effs: &mut EffectSet,
1154    ) -> Result<Ty, TypeError> {
1155        let owning = self.type_env.ctor_to_type.get(name).cloned()
1156            .ok_or_else(|| TypeError::UnknownVariant {
1157                at_node: node_id.into(),
1158                constructor: name.to_string(),
1159            })?;
1160        let def = self.type_env.types.get(&owning).cloned()
1161            .expect("ctor_to_type points to a real type");
1162        let variants = match &def.kind {
1163            TypeDefKind::Union(v) => v.clone(),
1164            _ => return Err(TypeError::UnknownVariant {
1165                at_node: node_id.into(),
1166                constructor: name.to_string(),
1167            }),
1168        };
1169        // Instantiate the type's params with fresh vars; substitute into
1170        // both the variant's payload type and the resulting Con(...).
1171        let mut subst = IndexMap::new();
1172        let mut con_args = Vec::with_capacity(def.params.len());
1173        for (i, _p) in def.params.iter().enumerate() {
1174            let fresh = self.u.fresh();
1175            subst.insert(i as u32, fresh.clone());
1176            con_args.push(fresh);
1177        }
1178        let payload = variants.get(name).cloned().flatten();
1179        match (payload, args) {
1180            (None, []) => Ok(Ty::Con(owning, con_args)),
1181            (Some(payload), args) => {
1182                let inst_payload = subst_vars(&payload, &subst, &IndexMap::new());
1183                let arg_count = match &inst_payload {
1184                    Ty::Tuple(items) => items.len(),
1185                    _ => 1,
1186                };
1187                if arg_count != args.len() {
1188                    return Err(TypeError::ArityMismatch {
1189                        at_node: node_id.into(),
1190                        expected: arg_count,
1191                        got: args.len(),
1192                    });
1193                }
1194                if args.len() == 1 {
1195                    let at = self.check_expr(&args[0], node_id, locals, effs)?;
1196                    self.unify_with_record_coercion(&at, &inst_payload).map_err(|e| mismatch_err(node_id, e, &self.u, vec![format!("constructor `{}`", name)]))?;
1197                } else if let Ty::Tuple(items) = inst_payload {
1198                    for (i, (a, t)) in args.iter().zip(items.iter()).enumerate() {
1199                        let at = self.check_expr(a, node_id, locals, effs)?;
1200                        self.unify_with_record_coercion(&at, t).map_err(|e| mismatch_err(node_id, e, &self.u, vec![format!("constructor `{}` arg {}", name, i + 1)]))?;
1201                    }
1202                }
1203                Ok(Ty::Con(owning, con_args))
1204            }
1205            (None, _) => Err(TypeError::ArityMismatch {
1206                at_node: node_id.into(), expected: 0, got: args.len(),
1207            }),
1208        }
1209    }
1210
1211    fn bind_pattern(
1212        &mut self,
1213        pat: &a::Pattern,
1214        ty: &Ty,
1215        locals: &mut IndexMap<String, Ty>,
1216        node_id: &str,
1217    ) -> Result<(), TypeError> {
1218        match pat {
1219            a::Pattern::PWild => Ok(()),
1220            a::Pattern::PVar { name } => {
1221                locals.insert(name.clone(), ty.clone());
1222                Ok(())
1223            }
1224            a::Pattern::PLiteral { value } => {
1225                let lt = lit_type(value);
1226                self.unify_with_record_coercion(&lt, ty).map_err(|e| mismatch_err(node_id, e, &self.u, vec!["in pattern".into()]))?;
1227                Ok(())
1228            }
1229            a::Pattern::PConstructor { name, args } => {
1230                // Re-use constructor logic but in pattern position.
1231                let owning = self.type_env.ctor_to_type.get(name).cloned()
1232                    .ok_or_else(|| TypeError::UnknownVariant {
1233                        at_node: node_id.into(), constructor: name.clone(),
1234                    })?;
1235                let def = self.type_env.types.get(&owning).cloned().unwrap();
1236                let mut subst = IndexMap::new();
1237                let mut con_args = Vec::new();
1238                for (i, _) in def.params.iter().enumerate() {
1239                    let fresh = self.u.fresh();
1240                    subst.insert(i as u32, fresh.clone());
1241                    con_args.push(fresh);
1242                }
1243                let con_ty = Ty::Con(owning.clone(), con_args);
1244                self.unify_with_record_coercion(&con_ty, ty).map_err(|e| mismatch_err(node_id, e, &self.u, vec![format!("constructor pattern `{}`", name)]))?;
1245                let payload = match &def.kind {
1246                    TypeDefKind::Union(v) => v.get(name).cloned().flatten(),
1247                    _ => None,
1248                };
1249                match (payload, args.as_slice()) {
1250                    (None, []) => Ok(()),
1251                    (Some(payload), args) => {
1252                        let inst = subst_vars(&payload, &subst, &IndexMap::new());
1253                        if args.len() == 1 {
1254                            self.bind_pattern(&args[0], &inst, locals, node_id)?;
1255                        } else if let Ty::Tuple(items) = inst {
1256                            for (a, t) in args.iter().zip(items.iter()) {
1257                                self.bind_pattern(a, t, locals, node_id)?;
1258                            }
1259                        }
1260                        Ok(())
1261                    }
1262                    (None, _) => Err(TypeError::ArityMismatch {
1263                        at_node: node_id.into(), expected: 0, got: args.len(),
1264                    }),
1265                }
1266            }
1267            a::Pattern::PRecord { fields } => {
1268                // Unfold a record-aliased Con (`type Bands = { ... }`)
1269                // so a structural `{ idea: pat, ... }` pattern can match
1270                // a nominal-typed scrutinee, mirror of #79's literal
1271                // coercion at every position.
1272                let resolved = self.unfold_record_alias(self.u.resolve(ty));
1273                let rec = match resolved {
1274                    Ty::Record(r) => r,
1275                    _ => return Err(TypeError::TypeMismatch {
1276                        at_node: node_id.into(),
1277                        expected: "record".into(),
1278                        got: ty.pretty(),
1279                        context: vec!["in record pattern".into()],
1280                    }),
1281                };
1282                for f in fields {
1283                    let ft = rec.get(&f.name).cloned()
1284                        .ok_or_else(|| TypeError::UnknownField {
1285                            at_node: node_id.into(),
1286                            record_type: Ty::Record(rec.clone()).pretty(),
1287                            field: f.name.clone(),
1288                        })?;
1289                    self.bind_pattern(&f.pattern, &ft, locals, node_id)?;
1290                }
1291                Ok(())
1292            }
1293            a::Pattern::PTuple { items } => {
1294                let resolved = self.u.resolve(ty);
1295                let tup = match resolved {
1296                    Ty::Tuple(t) => t,
1297                    Ty::Var(_) => {
1298                        let fresh: Vec<Ty> = items.iter().map(|_| self.u.fresh()).collect();
1299                        let tup_ty = Ty::Tuple(fresh.clone());
1300                        self.unify_with_record_coercion(&tup_ty, ty).map_err(|e| mismatch_err(node_id, e, &self.u, vec!["in tuple pattern".into()]))?;
1301                        fresh
1302                    }
1303                    other => return Err(TypeError::TypeMismatch {
1304                        at_node: node_id.into(),
1305                        expected: "tuple".into(),
1306                        got: other.pretty(),
1307                        context: vec!["in tuple pattern".into()],
1308                    }),
1309                };
1310                if tup.len() != items.len() {
1311                    return Err(TypeError::ArityMismatch {
1312                        at_node: node_id.into(), expected: tup.len(), got: items.len(),
1313                    });
1314                }
1315                for (p, t) in items.iter().zip(tup.iter()) {
1316                    self.bind_pattern(p, t, locals, node_id)?;
1317                }
1318                Ok(())
1319            }
1320        }
1321    }
1322}
1323
1324fn lit_type(l: &a::CLit) -> Ty {
1325    match l {
1326        a::CLit::Int { .. } => Ty::int(),
1327        a::CLit::Float { .. } => Ty::float(),
1328        a::CLit::Str { .. } => Ty::str(),
1329        a::CLit::Bytes { .. } => Ty::bytes(),
1330        a::CLit::Bool { .. } => Ty::bool(),
1331        a::CLit::Unit => Ty::Unit,
1332    }
1333}
1334
1335fn instantiate(s: &Scheme, u: &mut Unifier) -> Ty {
1336    let mut ty_subst = IndexMap::new();
1337    for v in &s.vars { ty_subst.insert(*v, u.fresh()); }
1338    let mut eff_subst = IndexMap::new();
1339    for v in &s.eff_vars { eff_subst.insert(*v, u.fresh_eff_id()); }
1340    subst_vars(&s.ty, &ty_subst, &eff_subst)
1341}
1342
1343fn subst_vars(
1344    t: &Ty,
1345    subst: &IndexMap<TyVarId, Ty>,
1346    eff_subst: &IndexMap<u32, u32>,
1347) -> Ty {
1348    match t {
1349        Ty::Var(v) => subst.get(v).cloned().unwrap_or_else(|| Ty::Var(*v)),
1350        Ty::Prim(_) | Ty::Unit | Ty::Never => t.clone(),
1351        Ty::List(inner) => Ty::List(Box::new(subst_vars(inner, subst, eff_subst))),
1352        Ty::Tuple(items) => Ty::Tuple(items.iter().map(|t| subst_vars(t, subst, eff_subst)).collect()),
1353        Ty::Record(fs) => {
1354            let mut out = IndexMap::new();
1355            for (k, v) in fs { out.insert(k.clone(), subst_vars(v, subst, eff_subst)); }
1356            Ty::Record(out)
1357        }
1358        Ty::Con(n, args) => Ty::Con(n.clone(),
1359            args.iter().map(|t| subst_vars(t, subst, eff_subst)).collect()),
1360        Ty::Function { params, effects, ret } => {
1361            // Refresh the effect-row variable if it's quantified in the
1362            // scheme; concrete kinds carry through unchanged.
1363            let new_effects = EffectSet {
1364                concrete: effects.concrete.clone(),
1365                var: effects.var.and_then(|v| eff_subst.get(&v).copied()).or(effects.var),
1366            };
1367            Ty::Function {
1368                params: params.iter().map(|t| subst_vars(t, subst, eff_subst)).collect(),
1369                effects: new_effects,
1370                ret: Box::new(subst_vars(ret, subst, eff_subst)),
1371            }
1372        }
1373    }
1374}
1375
1376fn mismatch_err(node_id: &str, e: UnifyError, u: &Unifier, context: Vec<String>) -> TypeError {
1377    match e {
1378        UnifyError::Mismatch { a, b } => TypeError::TypeMismatch {
1379            at_node: node_id.into(),
1380            expected: u.resolve(&b).pretty(),
1381            got: u.resolve(&a).pretty(),
1382            context,
1383        },
1384        UnifyError::Infinite { .. } => TypeError::InfiniteType { at_node: node_id.into() },
1385        UnifyError::EffectMismatch { a, b } => {
1386            // Render effect mismatches as a type-mismatch in compact
1387            // form, e.g. `[net]` vs `[]`. Avoids inventing a new
1388            // TypeError variant + wire format right now.
1389            let render = |e: &EffectSet| -> String {
1390                let mut parts: Vec<String> = e.concrete.iter()
1391                    .map(crate::types::EffectKind::pretty).collect();
1392                if let Some(v) = e.var { parts.push(format!("?e{}", v)); }
1393                if parts.is_empty() { "[]".into() } else { format!("[{}]", parts.join(", ")) }
1394            };
1395            TypeError::TypeMismatch {
1396                at_node: node_id.into(),
1397                expected: render(&b),
1398                got: render(&a),
1399                context,
1400            }
1401        }
1402    }
1403}