iter 22b.1.2: workspace registry skeleton + coherence checks

Adds the workspace-global typeclass instance registry per Decision 11
"Resolution and monomorphisation". Built at the end of load_workspace
after the DFS over imports completes; keyed by (class-name, type-hash)
where type-hash uses the new canonical::type_hash (16-hex prefix,
parallel to def_hash and module_hash).

Three coherence checks fire from build_registry, each surfacing as a
distinct WorkspaceLoadError variant:

- OrphanInstance: `instance C T` not in C's or T's defining module.
- DuplicateInstance: two entries share the same (class, type-hash) key.
- MissingMethod: instance omits a required (non-default) method.

The CLI's workspace_error_to_diagnostic is extended with the three new
codes (orphan-instance / duplicate-instance / missing-method).

All existing Workspace { ... } construction sites get the new
`registry` field, defaulting to Registry::default() at synthetic /
test-only paths and threading through ws.registry.clone() at codepath
sites that already hold a real workspace.

Includes hash-stability regression tests (iter22b1_schema_extension_*
and iter22b1_classdef_empty_optionals_hash_stable) and the empty-
registry positive test against examples/sum.ail.json. Test count:
288 → 291 (3 new tests, all pass).
This commit is contained in:
2026-05-09 12:37:00 +02:00
parent f25d7b6cd6
commit eb4db9dafc
8 changed files with 442 additions and 2 deletions
+17
View File
@@ -46,6 +46,23 @@ pub fn to_bytes<T: serde::Serialize>(value: &T) -> Vec<u8> {
out
}
/// Iter 22b.1: 16-hex-char hash of a [`crate::ast::Type`] in isolation.
///
/// Used by [`crate::workspace::Registry`] to key `InstanceDef`s by
/// their target type. Parallel in shape to [`crate::def_hash`] and
/// [`crate::workspace::module_hash`]: BLAKE3 over canonical-JSON
/// bytes, truncated to 16 hex chars.
///
/// The key property is determinism — two `Type` values that are
/// canonically equal MUST hash identically — so the workspace
/// registry's "is `instance C T` already declared?" check is
/// representation-independent.
pub fn type_hash(t: &crate::ast::Type) -> String {
let bytes = to_bytes(t);
let h = blake3::hash(&bytes);
h.to_hex().as_str()[..16].to_string()
}
fn write_value(v: &serde_json::Value, out: &mut Vec<u8>) -> std::io::Result<()> {
use serde_json::Value;
match v {