iter 22b.2.1: add Constraint struct, Type::Forall.constraints

This commit is contained in:
2026-05-09 17:34:39 +02:00
parent e1d748d64e
commit b636b883a3
14 changed files with 94 additions and 20 deletions
+24 -3
View File
@@ -331,6 +331,21 @@ pub struct InstanceMethod {
pub body: Term,
}
/// Iter 22b.2: a class constraint on a polymorphic function (Decision
/// 11). `(class, type)` pair where `class` is a class name and `type`
/// is a `Type` expression — typically a single `Type::Var` (e.g.
/// `(Eq, a)` for `Eq a => ...`). Concrete-type constraints are legal
/// schema-wise but fired as `no-instance` at typecheck time if no
/// matching registry entry exists.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Constraint {
/// Class name.
pub class: String,
/// Type the class is applied to.
#[serde(rename = "type")]
pub type_: Type,
}
/// A constant (top-level binding to a value).
///
/// Differs from a zero-arg [`FnDef`] in that it is evaluated once and
@@ -588,6 +603,12 @@ pub enum Type {
/// names, instantiated fresh at each use site.
Forall {
vars: Vec<String>,
/// Iter 22b.2: class constraints quantified together with
/// `vars`. Empty for pre-22b.2 polymorphic types; serialised
/// with `skip_serializing_if = "Vec::is_empty"` so existing
/// canonical-JSON bytes stay bit-identical.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
constraints: Vec<Constraint>,
body: Box<Type>,
},
}
@@ -742,9 +763,9 @@ impl PartialEq for Type {
}
(Type::Var { name: a }, Type::Var { name: b }) => a == b,
(
Type::Forall { vars: a, body: ab },
Type::Forall { vars: b, body: bb },
) => a == b && ab == bb,
Type::Forall { vars: a, constraints: ac, body: ab },
Type::Forall { vars: b, constraints: bc, body: bb },
) => a == b && ac == bc && ab == bb,
_ => false,
}
}