Skip to main content

lex_types/
env.rs

1//! Type environment: type-decl info and value-binding scopes.
2
3use crate::types::*;
4use indexmap::IndexMap;
5
6#[derive(Debug, Clone)]
7pub struct TypeDef {
8    pub params: Vec<String>,
9    pub kind: TypeDefKind,
10}
11
12#[derive(Debug, Clone)]
13pub enum TypeDefKind {
14    /// A union: variant name → optional payload.
15    Union(IndexMap<String, Option<Ty>>),
16    /// A record alias: `type Foo = { x :: Int }` etc.
17    Alias(Ty),
18    /// Built-in opaque (Map, Set, ...).
19    Opaque,
20}
21
22#[derive(Debug, Clone, Default)]
23pub struct TypeEnv {
24    /// Type-name → definition.
25    pub types: IndexMap<String, TypeDef>,
26    /// Constructor name → owning type-name.
27    pub ctor_to_type: IndexMap<String, String>,
28}
29
30impl TypeEnv {
31    pub fn new_with_builtins() -> Self {
32        let mut e = TypeEnv::default();
33        // Result[T, E] = Ok(T) | Err(E)
34        let mut r_variants = IndexMap::new();
35        r_variants.insert("Ok".into(), Some(Ty::Var(0))); // T
36        r_variants.insert("Err".into(), Some(Ty::Var(1))); // E
37        e.types.insert("Result".into(), TypeDef {
38            params: vec!["T".into(), "E".into()],
39            kind: TypeDefKind::Union(r_variants),
40        });
41        e.ctor_to_type.insert("Ok".into(), "Result".into());
42        e.ctor_to_type.insert("Err".into(), "Result".into());
43
44        // Option[T] = Some(T) | None
45        let mut o_variants = IndexMap::new();
46        o_variants.insert("Some".into(), Some(Ty::Var(0))); // T
47        o_variants.insert("None".into(), None);
48        e.types.insert("Option".into(), TypeDef {
49            params: vec!["T".into()],
50            kind: TypeDefKind::Union(o_variants),
51        });
52        e.ctor_to_type.insert("Some".into(), "Option".into());
53        e.ctor_to_type.insert("None".into(), "Option".into());
54
55        // Nil = Unit (alias)
56        e.types.insert("Nil".into(), TypeDef {
57            params: vec![],
58            kind: TypeDefKind::Alias(Ty::Unit),
59        });
60
61        // Map, Set: opaque-ish. We just register the names so they parse as Cons.
62        e.types.insert("Map".into(), TypeDef { params: vec!["K".into(), "V".into()], kind: TypeDefKind::Opaque });
63        e.types.insert("Set".into(), TypeDef { params: vec!["T".into()], kind: TypeDefKind::Opaque });
64
65        // SqlParam = PStr(Str) | PInt(Int) | PFloat(Float) | PBool(Bool) | PNull
66        // Typed parameter binding for std.sql (#362). Replaces the v1 List[Str]
67        // approach so callers don't have to stringify non-string values.
68        let mut sp_variants = IndexMap::new();
69        sp_variants.insert("PStr".into(),   Some(Ty::str()));
70        sp_variants.insert("PInt".into(),   Some(Ty::int()));
71        sp_variants.insert("PFloat".into(), Some(Ty::Con("Float".into(), vec![])));
72        sp_variants.insert("PBool".into(),  Some(Ty::Con("Bool".into(), vec![])));
73        sp_variants.insert("PNull".into(),  None);
74        e.types.insert("SqlParam".into(), TypeDef {
75            params: vec![],
76            kind: TypeDefKind::Union(sp_variants),
77        });
78        for ctor in &["PStr", "PInt", "PFloat", "PBool", "PNull"] {
79            e.ctor_to_type.insert((*ctor).into(), "SqlParam".into());
80        }
81
82        // SqlTx: opaque transaction handle (#362). Backed by the same
83        // Int registry key as Db; the type system enforces that commit/
84        // rollback can only be called on a value from sql.begin, not on
85        // a raw Db connection.
86        e.types.insert("SqlTx".into(), TypeDef { params: vec![], kind: TypeDefKind::Opaque });
87
88        // SqlError = { message :: Str, code :: Option[Str], detail :: Option[Str] }
89        // Structured error shape returned by every `std.sql` op (#380).
90        // `code` carries the SQLSTATE (Postgres) or the symbolic SQLite
91        // error name (`SQLITE_BUSY`, `SQLITE_CONSTRAINT_UNIQUE`, …) so
92        // dialect-aware retry / conflict-handling can avoid string
93        // parsing. `message` is always populated; `detail` carries a
94        // driver-side detail string when present.
95        let mut se_fields = IndexMap::new();
96        se_fields.insert("message".into(), Ty::str());
97        se_fields.insert("code".into(), Ty::Con("Option".into(), vec![Ty::str()]));
98        se_fields.insert("detail".into(), Ty::Con("Option".into(), vec![Ty::str()]));
99        e.types.insert("SqlError".into(), TypeDef {
100            params: vec![],
101            kind: TypeDefKind::Alias(Ty::Record(se_fields)),
102        });
103
104        // AeadResult = { ciphertext :: Bytes, tag :: Bytes } — return
105        // shape for every AEAD seal op in `std.crypto` (#382 AEAD slice).
106        // The auth tag is split out from the ciphertext so callers don't
107        // have to know each algorithm's tag length: AES-GCM and
108        // ChaCha20-Poly1305 both happen to be 16 bytes today, but the
109        // shape keeps that detail encapsulated.
110        let mut ar_fields = IndexMap::new();
111        ar_fields.insert("ciphertext".into(), Ty::bytes());
112        ar_fields.insert("tag".into(), Ty::bytes());
113        e.types.insert("AeadResult".into(), TypeDef {
114            params: vec![],
115            kind: TypeDefKind::Alias(Ty::Record(ar_fields)),
116        });
117
118        // Iter[T]: lazy positional iterator (#364). Backed at runtime by a
119        // (List[T], Int) tuple; the Int is the current cursor index. All
120        // iter.* operations are compiler-inlined so no effect is needed.
121        e.types.insert("Iter".into(), TypeDef { params: vec!["T".into()], kind: TypeDefKind::Opaque });
122
123        // Stream[T]: opaque streaming iterator (#305 slice 3).
124        // Built and consumed exclusively through the `stream.*` and
125        // `agent.cloud_stream` effect builtins; the runtime
126        // represents a Stream value as an opaque variant carrying a
127        // handle id. Registered as Opaque so type-checking knows
128        // `Stream[Str]` parses but doesn't unwrap it structurally.
129        e.types.insert("Stream".into(), TypeDef { params: vec!["T".into()], kind: TypeDefKind::Opaque });
130
131        // Tz = Utc | Local | Offset(Int) | Iana(Str).
132        // Used by std.datetime; the variant-typed alternative to the
133        // pre-v1 stringly Tz ("UTC" / "Local" / "+05:30" / IANA name).
134        // Registered globally so users don't have to import a module
135        // to mention `Utc` / `Iana("America/New_York")` etc.
136        let mut tz_variants = IndexMap::new();
137        tz_variants.insert("Utc".into(), None);
138        tz_variants.insert("Local".into(), None);
139        tz_variants.insert("Offset".into(), Some(Ty::int()));
140        tz_variants.insert("Iana".into(), Some(Ty::str()));
141        e.types.insert("Tz".into(), TypeDef {
142            params: vec![],
143            kind: TypeDefKind::Union(tz_variants),
144        });
145        for ctor in &["Utc", "Local", "Offset", "Iana"] {
146            e.ctor_to_type.insert((*ctor).into(), "Tz".into());
147        }
148
149        // HttpError = NetworkError(Str) | TimeoutError | TlsError(Str)
150        //           | DecodeError(Str)
151        // Used by std.http; structured failure shape so callers can
152        // discriminate transport vs. timeout vs. TLS vs. body-decode
153        // errors without parsing strings.
154        let mut http_err_variants = IndexMap::new();
155        http_err_variants.insert("NetworkError".into(), Some(Ty::str()));
156        http_err_variants.insert("TimeoutError".into(), None);
157        http_err_variants.insert("TlsError".into(), Some(Ty::str()));
158        http_err_variants.insert("DecodeError".into(), Some(Ty::str()));
159        e.types.insert("HttpError".into(), TypeDef {
160            params: vec![],
161            kind: TypeDefKind::Union(http_err_variants),
162        });
163        for ctor in &["NetworkError", "TimeoutError", "TlsError", "DecodeError"] {
164            e.ctor_to_type.insert((*ctor).into(), "HttpError".into());
165        }
166
167        // HttpRequest = { method, url, headers, body, timeout_ms }.
168        // The std.http request shape. Anonymous record literals coerce
169        // to this nominal alias at every position (per the §3.13
170        // record-coercion rules), so users write
171        // `{ method: "GET", url: u, headers: map.new(), body: None,
172        // timeout_ms: None }` rather than a dedicated constructor —
173        // builders (`http.with_header` etc.) are pure transforms over
174        // the same shape.
175        let mut req_fields = IndexMap::new();
176        req_fields.insert("method".into(), Ty::str());
177        req_fields.insert("url".into(), Ty::str());
178        req_fields.insert("headers".into(), Ty::Con("Map".into(), vec![Ty::str(), Ty::str()]));
179        req_fields.insert("body".into(), Ty::Con("Option".into(), vec![Ty::bytes()]));
180        req_fields.insert("timeout_ms".into(), Ty::Con("Option".into(), vec![Ty::int()]));
181        e.types.insert("HttpRequest".into(), TypeDef {
182            params: vec![],
183            kind: TypeDefKind::Alias(Ty::Record(req_fields)),
184        });
185
186        // HttpResponse = { status, headers, body }. Returned by every
187        // `http.{send,get,post}` happy path; also the input to
188        // `http.{json_body,text_body}`.
189        let mut resp_fields = IndexMap::new();
190        resp_fields.insert("status".into(), Ty::int());
191        resp_fields.insert("headers".into(), Ty::Con("Map".into(), vec![Ty::str(), Ty::str()]));
192        resp_fields.insert("body".into(), Ty::bytes());
193        e.types.insert("HttpResponse".into(), TypeDef {
194            params: vec![],
195            kind: TypeDefKind::Alias(Ty::Record(resp_fields)),
196        });
197
198        // Matrix = { rows :: Int, cols :: Int, data :: List[Float] }.
199        // Used by std.math; runtime values are the F64Array fast lane,
200        // not a real record. The alias makes math.* signatures readable
201        // (`:: Matrix` instead of an inline record) and lets call sites
202        // unify nominally. Field access via `m.rows` would type-check
203        // but fail at runtime — use `math.rows / math.cols / math.get`.
204        let mut mat_fields = IndexMap::new();
205        mat_fields.insert("rows".into(), Ty::int());
206        mat_fields.insert("cols".into(), Ty::int());
207        mat_fields.insert("data".into(), Ty::List(Box::new(Ty::float())));
208        e.types.insert("Matrix".into(), TypeDef {
209            params: vec![],
210            kind: TypeDefKind::Alias(Ty::Record(mat_fields)),
211        });
212
213        // Request = { method :: Str, path :: Str, query :: Str, body :: Str,
214        //             headers :: Map[Str, Str], path_params :: Map[Str, Str] }
215        // Inbound request shape used by net.serve_fn handlers.
216        // `path_params` is populated by `net.serve_routed` from `:name`
217        // segments in the route pattern; empty under `net.serve_fn`.
218        let mut net_req_fields = IndexMap::new();
219        net_req_fields.insert("method".into(), Ty::str());
220        net_req_fields.insert("path".into(), Ty::str());
221        net_req_fields.insert("query".into(), Ty::str());
222        net_req_fields.insert("body".into(), Ty::str());
223        net_req_fields.insert("headers".into(), Ty::Con("Map".into(), vec![Ty::str(), Ty::str()]));
224        net_req_fields.insert("path_params".into(), Ty::Con("Map".into(), vec![Ty::str(), Ty::str()]));
225        e.types.insert("Request".into(), TypeDef {
226            params: vec![],
227            kind: TypeDefKind::Alias(Ty::Record(net_req_fields)),
228        });
229
230        // Response = { status :: Int, body :: ResponseBody, headers :: Map[Str, Str] }
231        // Outbound response shape returned by net.serve_fn handlers.
232        // #375: `body` is now an ADT instead of a bare Str. Streaming
233        // variants (BodyStream / BodyBytes) carry an `Iter[T]` that the
234        // server drains chunk-by-chunk under chunked transfer-encoding.
235        let mut rb_variants = IndexMap::new();
236        rb_variants.insert("BodyStr".into(),    Some(Ty::str()));
237        rb_variants.insert(
238            "BodyStream".into(),
239            Some(Ty::Con("Iter".into(), vec![Ty::str()])),
240        );
241        rb_variants.insert(
242            "BodyBytes".into(),
243            Some(Ty::Con("Iter".into(), vec![Ty::List(Box::new(Ty::int()))])),
244        );
245        e.types.insert("ResponseBody".into(), TypeDef {
246            params: vec![],
247            kind: TypeDefKind::Union(rb_variants),
248        });
249        for ctor in &["BodyStr", "BodyStream", "BodyBytes"] {
250            e.ctor_to_type.insert((*ctor).into(), "ResponseBody".into());
251        }
252
253        let mut net_resp_fields = IndexMap::new();
254        net_resp_fields.insert("status".into(), Ty::int());
255        net_resp_fields.insert("body".into(), Ty::Con("ResponseBody".into(), vec![]));
256        net_resp_fields.insert("headers".into(), Ty::Con("Map".into(), vec![Ty::str(), Ty::str()]));
257        e.types.insert("Response".into(), TypeDef {
258            params: vec![],
259            kind: TypeDefKind::Alias(Ty::Record(net_resp_fields)),
260        });
261
262        // WsConn = { id :: Str, path :: Str, subprotocol :: Str }
263        // Passed to every net.serve_ws_fn message handler.
264        let mut ws_conn_fields = IndexMap::new();
265        ws_conn_fields.insert("id".into(), Ty::str());
266        ws_conn_fields.insert("path".into(), Ty::str());
267        ws_conn_fields.insert("subprotocol".into(), Ty::str());
268        e.types.insert("WsConn".into(), TypeDef {
269            params: vec![],
270            kind: TypeDefKind::Alias(Ty::Record(ws_conn_fields)),
271        });
272
273        // WsMessage = WsText(Str) | WsBinary(List[Int]) | WsPing | WsClose
274        let mut ws_msg_variants = IndexMap::new();
275        ws_msg_variants.insert("WsText".into(), Some(Ty::str()));
276        ws_msg_variants.insert("WsBinary".into(), Some(Ty::List(Box::new(Ty::int()))));
277        ws_msg_variants.insert("WsPing".into(), None);
278        ws_msg_variants.insert("WsClose".into(), None);
279        e.types.insert("WsMessage".into(), TypeDef {
280            params: vec![],
281            kind: TypeDefKind::Union(ws_msg_variants),
282        });
283        for ctor in &["WsText", "WsBinary", "WsPing", "WsClose"] {
284            e.ctor_to_type.insert((*ctor).into(), "WsMessage".into());
285        }
286
287        // WsAction = WsSend(Str) | WsSendBinary(List[Int]) | WsNoOp
288        // Handlers return this to tell the runtime what to send back.
289        // Connection close is handled automatically when the runtime receives
290        // an incoming WsClose frame; handlers do not need to emit a close action.
291        let mut ws_act_variants = IndexMap::new();
292        ws_act_variants.insert("WsSend".into(), Some(Ty::str()));
293        ws_act_variants.insert("WsSendBinary".into(), Some(Ty::List(Box::new(Ty::int()))));
294        ws_act_variants.insert("WsNoOp".into(), None);
295        e.types.insert("WsAction".into(), TypeDef {
296            params: vec![],
297            kind: TypeDefKind::Union(ws_act_variants),
298        });
299        for ctor in &["WsSend", "WsSendBinary", "WsNoOp"] {
300            e.ctor_to_type.insert((*ctor).into(), "WsAction".into());
301        }
302
303        // ConcError = AlreadyRegistered(Str) | NotRegistered(Str)
304        // Returned by `conc.register` / `conc.unregister` (#444). A
305        // third `TypeMismatch` variant is reserved for when the
306        // SigId-tagged registry lands — see `conc_registry.rs` in
307        // lex-bytecode for the deferred-design note.
308        let mut ce_variants = IndexMap::new();
309        ce_variants.insert("AlreadyRegistered".into(), Some(Ty::str()));
310        ce_variants.insert("NotRegistered".into(), Some(Ty::str()));
311        e.types.insert("ConcError".into(), TypeDef {
312            params: vec![],
313            kind: TypeDefKind::Union(ce_variants),
314        });
315        for ctor in &["AlreadyRegistered", "NotRegistered"] {
316            e.ctor_to_type.insert((*ctor).into(), "ConcError".into());
317        }
318
319        e
320    }
321
322    pub fn add_user_type(&mut self, name: &str, decl: lex_ast::TypeDecl) -> Result<(), String> {
323        match &decl.definition {
324            lex_ast::TypeExpr::Union { variants } => {
325                let mut vmap = IndexMap::new();
326                for v in variants {
327                    let payload = v.payload.as_ref().map(|p| ty_from_canon(p, &decl.params));
328                    vmap.insert(v.name.clone(), payload);
329                    self.ctor_to_type.insert(v.name.clone(), name.to_string());
330                }
331                self.types.insert(name.to_string(), TypeDef {
332                    params: decl.params.clone(),
333                    kind: TypeDefKind::Union(vmap),
334                });
335            }
336            other => {
337                let ty = ty_from_canon_env(other, &decl.params, self);
338                self.types.insert(name.to_string(), TypeDef {
339                    params: decl.params.clone(),
340                    kind: TypeDefKind::Alias(ty),
341                });
342            }
343        }
344        Ok(())
345    }
346}
347
348/// Convert canonical TypeExpr to internal Ty, treating type params as
349/// fresh-numbered Vars (0..n in declaration order). When instantiating, we
350/// substitute these out.
351pub fn ty_from_canon(t: &lex_ast::TypeExpr, params: &[String]) -> Ty {
352    match t {
353        lex_ast::TypeExpr::Named { name, args } => {
354            // type param?
355            if let Some(idx) = params.iter().position(|p| p == name) {
356                if !args.is_empty() {
357                    // Type params don't take args.
358                    return Ty::Con(name.clone(), args.iter().map(|a| ty_from_canon(a, params)).collect());
359                }
360                return Ty::Var(idx as u32);
361            }
362            // Primitives.
363            match name.as_str() {
364                "Int" => return Ty::int(),
365                "Float" => return Ty::float(),
366                "Bool" => return Ty::bool(),
367                "Str" => return Ty::str(),
368                "Bytes" => return Ty::bytes(),
369                "Unit" | "Nil" => return Ty::Unit,
370                "Never" => return Ty::Never,
371                "List" if args.len() == 1 => return Ty::List(Box::new(ty_from_canon(&args[0], params))),
372                // `Tuple[T0, T1, ...]` is the constructor surface for
373                // tuples; canonicalize to the structural Ty::Tuple so
374                // it unifies with `(T0, T1)` literal-tuple syntax and
375                // with std.tuple's signatures.
376                "Tuple" => return Ty::Tuple(args.iter().map(|a| ty_from_canon(a, params)).collect()),
377                _ => {}
378            }
379            Ty::Con(name.clone(), args.iter().map(|a| ty_from_canon(a, params)).collect())
380        }
381        lex_ast::TypeExpr::Record { fields } => {
382            let mut m = IndexMap::new();
383            for f in fields { m.insert(f.name.clone(), ty_from_canon(&f.ty, params)); }
384            Ty::Record(m)
385        }
386        lex_ast::TypeExpr::Tuple { items } => Ty::Tuple(items.iter().map(|t| ty_from_canon(t, params)).collect()),
387        lex_ast::TypeExpr::Function { params: ps, effects, ret } => {
388            // Plumb effect args (#207).
389            let effs = EffectSet {
390                concrete: {
391                    let mut s = std::collections::BTreeSet::new();
392                    for e in effects {
393                        let arg = e.arg.as_ref().map(|a| match a {
394                            lex_ast::EffectArg::Str { value } => crate::types::EffectArg::Str(value.clone()),
395                            lex_ast::EffectArg::Int { value } => crate::types::EffectArg::Int(*value),
396                            lex_ast::EffectArg::Ident { value } => crate::types::EffectArg::Ident(value.clone()),
397                        });
398                        s.insert(crate::types::EffectKind { name: e.name.clone(), arg });
399                    }
400                    s
401                },
402                var: None,
403            };
404            Ty::Function {
405                params: ps.iter().map(|t| ty_from_canon(t, params)).collect(),
406                effects: effs,
407                ret: Box::new(ty_from_canon(ret, params)),
408            }
409        }
410        lex_ast::TypeExpr::Union { .. } => {
411            // Unions on the RHS of type-decls; not in arbitrary positions.
412            Ty::Unit
413        }
414        lex_ast::TypeExpr::Refined { base, .. } => {
415            // #209 slice 1: refinement types unify structurally as
416            // their base type. The predicate is parsed and stored in
417            // the AST (so `lex-vcs` content-addressing picks up
418            // refinement edits), but static discharge and runtime
419            // residual checks land in slices 2 and 3 of #209. The
420            // unification behavior here means a function declaring
421            // `Int{x | x > 0}` interoperates with plain `Int` callers
422            // — the predicate is informational until discharge is
423            // wired up.
424            ty_from_canon(base, params)
425        }
426        lex_ast::TypeExpr::RecordWithSpreads { .. } => {
427            // Caller should use ty_from_canon_env for spread resolution.
428            Ty::Unit
429        }
430    }
431}
432
433/// Like `ty_from_canon` but resolves `RecordWithSpreads` by looking up base
434/// type names in `env`. Called from `add_user_type` and `function_scheme` so
435/// that `{ ...Post, extra :: Int }` expands to a flat `Ty::Record`.
436pub fn ty_from_canon_env(t: &lex_ast::TypeExpr, params: &[String], env: &TypeEnv) -> Ty {
437    match t {
438        lex_ast::TypeExpr::RecordWithSpreads { spreads, fields } => {
439            let mut m = IndexMap::new();
440            for spread_name in spreads {
441                if let Some(td) = env.types.get(spread_name.as_str()) {
442                    if let TypeDefKind::Alias(Ty::Record(spread_fields)) = &td.kind {
443                        for (k, v) in spread_fields {
444                            m.insert(k.clone(), v.clone());
445                        }
446                    }
447                }
448            }
449            for f in fields {
450                m.insert(f.name.clone(), ty_from_canon_env(&f.ty, params, env));
451            }
452            Ty::Record(m)
453        }
454        other => ty_from_canon(other, params),
455    }
456}