Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
8c1071f
fix(examples): move test-only imports into tests blocks
MicroProofs Oct 10, 2026
da4a4d3
fix(solve): unify type applications with transparent aliases by their…
MicroProofs Oct 10, 2026
cc95d39
fix(solve): keep a failed annotated body's annotation for its callers
MicroProofs Oct 10, 2026
aae9116
fix(solve): do not report types stranded by a failed call as ambiguous
MicroProofs Oct 10, 2026
a73ee39
build: optimize the compiler crates in the dev profile
MicroProofs Oct 8, 2026
438d507
refactor(traits): keep the trait search limits in one place
MicroProofs Oct 9, 2026
e58587b
feat: raise the trait search and specialization depth limits to 256
MicroProofs Oct 9, 2026
95afd58
test(codegen): build the deep type from the depth limit
MicroProofs Oct 9, 2026
532e437
perf(solve): index kind contracts, recovery edges, and scheme records
MicroProofs Oct 11, 2026
de1b709
refactor(traits): share impl selection between inference, entailment,…
MicroProofs Oct 12, 2026
f205096
fix(traits): reject contradictory representation contexts on type app…
MicroProofs Oct 12, 2026
ba45767
refactor(types): restore the Elm constraint tree
MicroProofs Oct 12, 2026
37ea487
fix(solve): reject representation givens that contradict an enclosing…
MicroProofs Oct 13, 2026
115db1d
fix(traits): check every impl context through its superclasses
MicroProofs Oct 13, 2026
b7cb3a1
fix(solve): do not select impls for infinite types
MicroProofs Oct 13, 2026
baadec9
feat(traits): select representation-split impls by coverage
MicroProofs Oct 13, 2026
4c4e515
fix(solve): resolve superclass evidence for open phantom types
MicroProofs Oct 14, 2026
b2fd17f
refactor(solve): remove the step 03 scan oracles
MicroProofs Oct 14, 2026
3a13ea5
fix(solve): let a selected impl prove an open type that another impl …
MicroProofs Oct 15, 2026
4dcd4e9
fix(report): break tuple and record entries as Elm's D.cat does
MicroProofs Oct 15, 2026
b613c93
fix(report): build long trait help lines from document parts
MicroProofs Oct 15, 2026
3d86f09
fix(report): align the record after Available fields under its brace
MicroProofs Oct 16, 2026
5655782
refactor(constrain): carry the tests scope on record field constraints
MicroProofs Oct 16, 2026
d47d277
refactor: replace search budgets with one compile fuel counter
MicroProofs Oct 24, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .sampo/changesets/annotated-body-recovery.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
cargo/nash-solve: patch
---

Keep checking callers of an annotated definition against its annotation when its body has a type error. Callers now report their own mismatches and no longer receive spurious ambiguity errors. An annotation whose type variables escape still blocks its uses.
5 changes: 5 additions & 0 deletions .sampo/changesets/available-fields-alignment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
cargo/nash-report: patch
---

Align the record after "Available fields:" under its opening brace. Its later lines started at the left margin.
16 changes: 16 additions & 0 deletions .sampo/changesets/compile-fuel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
cargo/nash-ast: minor
cargo/nash-can: minor
cargo/nash-cli: minor
cargo/nash-codegen: minor
cargo/nash-constrain: minor
cargo/nash-docs: patch
cargo/nash-driver: minor
cargo/nash-language-server: minor
cargo/nash-report: minor
cargo/nash-solve: minor
---

Replace every separate search budget with one compile fuel counter, `nash_ast::fuel`. Trait resolution, impl overlap checks, superclass entailment, evidence resolution and code generation share it: each search step takes a `tick`, and the driver installs a fresh `Fuel` for each module's compile. `nash check`, `build`, `test` and `docs` run with unlimited fuel, so programs that the old fixed limits rejected now compile, such as an impl context that rewraps its argument used at 200 nested types. The language server runs with 10 million steps per module, so an editor gives up on a looping program after a second or two instead of freezing.

Running out of fuel is one error kind, `COMPILATION STEP LIMIT` (`nash::type::out_of_fuel`), replacing `ImplResolutionLimit` (`nash::type::impl_resolution_limit`), `ImplPatternLimit` (`nash::names::impl_pattern_limit`), the superclass search limit, and the code generation specialization, evidence and recursion normalization limits. The solver reports it at the use that needed the search, canonicalization at the impl being checked, and code generation as a plain error. `nash_driver::build`, `build_with` and `test_with` take a `Fuel` argument, re-exported as `nash_driver::Fuel`.
8 changes: 8 additions & 0 deletions .sampo/changesets/contradictory-application-contexts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
cargo/nash-ast: minor
cargo/nash-can: patch
cargo/nash-report: patch
cargo/nash-solve: patch
---

Reject contradictory representation contexts on type applications, such as `(Big ('f 'a), Little ('f 'a))`, with the existing contradictory-representation error. They used to reach impl selection, where an empty proven set matched every disjoint impl. Export `nash_ast::same_type` for location-free type comparison.
6 changes: 6 additions & 0 deletions .sampo/changesets/field-constraint-test-scope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
cargo/nash-constrain: minor
cargo/nash-solve: patch
---

Record on each record-field constraint whether it comes from a module test, in place of the `Constraint::TestScope` wrapper. `Constraint::Record` and `Constraint::Field` gain a `tests` field, `TestScope` is removed, and `pattern::State` and `pattern::empty_state` take the flag. Field visibility is unchanged: fields that only a tests-block import makes visible are visible in tests and nowhere else.
5 changes: 5 additions & 0 deletions .sampo/changesets/impl-context-superclass-consistency.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
cargo/nash-can: patch
---

Check every impl context together with its superclasses for contradictory representations. An impl that defines no method, such as `impl (Foo 'a, Little 'a) => Sub 'a` where `Foo` requires `Big`, skipped this check and crashed the superclass entailment check; it now gets the contradictory-representation error.
5 changes: 5 additions & 0 deletions .sampo/changesets/infinite-type-impl-selection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
cargo/nash-solve: patch
---

Do not select an impl for a trait constraint whose argument is an infinite type; the occurs check of the binding reports the type, as in Elm. Two impls that are disjoint on finite types can both match an infinite type, which crashed the compiler, and an impl that matches it at every depth gave a false step-limit error.
5 changes: 5 additions & 0 deletions .sampo/changesets/long-trait-help-lines.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
cargo/nash-report: patch
---

Stop `nash check` from crashing with "Doc::text must not contain newlines" when a missing-constraint or missing-impl predicate is wider than 80 columns. The help line is now part of the report layout instead of pre-rendered text. When a sentence with a predicate does not fit the line, the predicate moves to its own line, indented by 4. Sentences that fit render as before.
5 changes: 5 additions & 0 deletions .sampo/changesets/nested-representation-contexts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
cargo/nash-solve: patch
---

Report a local annotation whose representation constraints contradict an enclosing annotation, such as `Little 'a` inside `Big 'a => ...`, as a contradictory-representation error. The two annotations share `'a`, and their combined constraints used to reach impl selection and crash the compiler. The error no longer lists a requirement that another listed requirement implies, such as `Storable` next to `Big`.
7 changes: 7 additions & 0 deletions .sampo/changesets/open-superclass-types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
cargo/nash-ast: minor
cargo/nash-solve: minor
cargo/nash-codegen: minor
---

Resolve superclass evidence when a type variable stays open. A phantom parameter that no use fixes, as in `wrapper : tag 'p -> bool; wrapper t = same t t` with `same : Ord 'a => 'a -> 'a -> bool`, passed type checking but stopped `nash test` with "superclass resolution requires a ground type". `nash_solve::evidence::resolve` now keeps open type variables. An open variable equals only itself. An impl that matches whatever the variable is proves the predicate, and resolution reports `NonGround` only when no impl does and impl selection, a representation test, or the requirements of a type need the variable. The new `nash_ast::head::Open` matches impl heads against open types. Under `SelectPolicy::Exact`, `select_impl` now returns `Deferred` when no impl is selected and a candidate is undecided. `nash_codegen::evidence::Error::NonGround` is removed.
5 changes: 5 additions & 0 deletions .sampo/changesets/report-tuple-record-entries.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
cargo/nash-report: minor
---

Break a tuple or record type that does not fit the line with one entry per line, as Elm does. The entries were joined without break points, so only the last entries broke and the type drifted to the right. Add `Doc::pcat`, Elm's `D.cat`.
7 changes: 7 additions & 0 deletions .sampo/changesets/representation-coverage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
cargo/nash-ast: minor
cargo/nash-solve: minor
cargo/nash-codegen: minor
---

Add the representation coverage rule. When a rigid type variable keeps impl selection from deciding, because the impls are split by its representation class (for example `Eq (list ('a : Big))` and `Eq 'a => Eq (list ('a : Little))`), inference accepts the predicate if the annotation states the trait at that variable and every admitted representation selects an impl whose context holds. `same : Eq 'a => list 'a -> list 'a -> bool` now compiles. The evidence is the new `Evidence::Specialize`; code generation selects the impl for each use and keeps the selecting types in the specialization key. A missing element constraint is now reported as a missing constraint, such as `Eq 'a`, instead of a missing impl for the container. Superclass proofs do not use the rule: an impl names a split requirement in its context. nash-ast also gains `head::cover_impls` and `ReprTrait::exactly`.
6 changes: 6 additions & 0 deletions .sampo/changesets/restore-constraint-tree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
cargo/nash-constrain: minor
cargo/nash-solve: patch
---

Restore Elm's constraint tree: `nash-constrain` generates it from the canonical AST and `nash-solve` walks it, in place of direct inference. An annotated definition now publishes its declared type: a call with more arguments in the same module no longer replaces an alias in its annotation, so importers find the impls of that alias.
5 changes: 5 additions & 0 deletions .sampo/changesets/shared-impl-selection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
cargo/nash-ast: minor
---

Add `select_impl` with `SelectPolicy` and `ImplSelection`, the one impl selection used by inference, superclass entailment, and evidence. Every candidate is matched, and a second match stops the compiler as a coherence bug instead of picking one.
6 changes: 6 additions & 0 deletions .sampo/changesets/solver-indexed-bookkeeping.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
cargo/nash-constrain: patch
cargo/nash-solve: patch
---

Index kind contracts, recovery dependencies, stranded variables, and scheme records so that type checking does not scan every earlier declaration again.
5 changes: 5 additions & 0 deletions .sampo/changesets/stranded-call-constraints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
cargo/nash-solve: patch
---

Stop reporting ambiguous or unresolved trait constraints on values passed to a definition whose body failed to type check. Their types are unknown because of that failure; the body error is still reported, and fixing it reveals any remaining ambiguity.
5 changes: 5 additions & 0 deletions .sampo/changesets/transparent-alias-application.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
cargo/nash-solve: patch
---

Unify a type-variable application such as `'f 'a` with a transparent alias through the alias body when the alias's own parameters cannot supply the applied arguments. `type alias Xs = List Int` now works with `List.length`; record aliases remain nominal.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,19 @@ xz2 = "0.1"
# Keep zip 2 defaults except unused Bzip2 support.
zip = { version = "2", default-features = false, features = ["aes-crypto", "deflate64", "deflate", "lzma", "time", "zstd", "xz"] }

# Optimize insta/similar in dev for faster diffs
# Optimize the compiler crates and insta/similar in dev for faster tests and diffs
[profile.dev.package]
bumpalo.opt-level = 3
insta.opt-level = 3
nash-ast.opt-level = 3
nash-can.opt-level = 3
nash-codegen.opt-level = 3
nash-constrain.opt-level = 3
nash-ir.opt-level = 3
nash-nitpick.opt-level = 3
nash-parse.opt-level = 3
nash-plutus.opt-level = 3
nash-solve.opt-level = 3
similar.opt-level = 3

# The profile that 'dist' will build with
Expand Down
8 changes: 4 additions & 4 deletions SPEC.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ component specs in [`docs/`](docs/); chunked implementation plans in
## Pipeline

```
parse -> canonicalize -> kinds -> direct inference (+ traits) -> nitpick
parse -> canonicalize -> kinds -> constrain + solve (+ traits) -> nitpick
-> macro expansion (loop) -> Core IR -> optimize -> UPLC
```

Expand All @@ -24,8 +24,8 @@ produce UPLC programs; all dependencies inline into each program.
| `nash-parse` | parser + Elm error hierarchy | extend ([plans/01](plans/01-syntax.md)) |
| `nash-ast` | canonical AST | extend |
| `nash-can` | canonicalization, interfaces | extend |
| `nash-constrain` | union-find types, canonical instantiation, type errors | done |
| `nash-solve` | direct AST inference, traits, defaulting | extend |
| `nash-constrain` | constraint generation, union-find types, canonical instantiation, type errors | done |
| `nash-solve` | constraint solver, traits, defaulting | extend |
| `nash-nitpick` | exhaustiveness and redundancy | done ([plans/05](plans/05-nitpick.md)) |
| `nash-report` | concise diagnostics (terminal, JSON, LSP) | done ([plans/06](plans/06-diagnostics.md)) |
| `nash-ir` | Core IR + passes | new ([plans/07](plans/07-codegen.md), [08](plans/08-optimizer.md)) |
Expand All @@ -46,7 +46,7 @@ Done:

- [x] Parser (Elm `Parse/*` port, full syntax error hierarchy)
- [x] Canonicalization (Elm `Canonicalize/*` port, SCC, interfaces)
- [x] Type inference (direct AST inference, rank-based solver, records, aliases)
- [x] Type inference (constraint generation, rank-based solver, records, aliases)
- [x] Frontend hardening and direct-inference parity ([verification](docs/frontend-hardening-verification.md))
- [x] Project config, driver, dependency-ordered builds, interface cache
- [x] `nash check`
Expand Down
3 changes: 3 additions & 0 deletions crates/nash-ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ license.workspace = true
bumpalo.workspace = true
nash-region = { path = "../nash-region", version = "0.3.0" }
nash-source = { path = "../nash-source", version = "0.10.0" }

[dev-dependencies]
insta.workspace = true
17 changes: 16 additions & 1 deletion crates/nash-ast/src/evidence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ fn same_types(a: &[&Located<Type<'_>>], b: &[&Located<Type<'_>>]) -> bool {
a.len() == b.len() && a.iter().zip(b).all(|(a, b)| same_type(&a.value, &b.value))
}

fn same_type(a: &Type<'_>, b: &Type<'_>) -> bool {
/// Structural type equality that ignores source locations.
pub fn same_type(a: &Type<'_>, b: &Type<'_>) -> bool {
match (a, b) {
(Type::Var(a), Type::Var(b)) => a == b,

Expand Down Expand Up @@ -172,6 +173,16 @@ impl PartialEq for Evidence<'_> {
(Self::Super { of: ao, index: ai }, Self::Super { of: bo, index: bi }) => {
ao == bo && ai == bi
}
(
Self::Specialize {
trait_: a,
args: aa,
},
Self::Specialize {
trait_: b,
args: ba,
},
) => a == b && same_types(aa, ba),
_ => false,
}
}
Expand Down Expand Up @@ -207,6 +218,10 @@ impl Hash for Evidence<'_> {
of.hash(state);
index.hash(state);
}
Self::Specialize { trait_, args } => {
trait_.hash(state);
hash_types(args, state);
}
}
}
}
Expand Down
96 changes: 96 additions & 0 deletions crates/nash-ast/src/fuel.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//! The one step counter every compiler search draws from.
//!
//! Impl contexts need not shrink, so trait resolution, overlap checks and code
//! generation can loop on a program that is accepted syntactically. Instead of
//! a budget per search, the driver installs one `Fuel` around each module's
//! compile, and every search loop calls [`tick`]. A pass over an existing type
//! that cannot run away on its own pays with [`charge`], so the steps track
//! time even when the types grow. The command-line tools run `Unlimited`; the
//! language server runs `Limited` so an editor never freezes on a looping
//! program.
//!
//! The counter is thread-local so no signature carries it. That is sound only
//! while the installed section runs on one thread and never awaits; the driver
//! installs it inside its dedicated compiler thread. With nothing installed the
//! fuel is `Unlimited`.
use std::cell::Cell;

/// How many steps a compile may take.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Fuel {
Unlimited,
Limited(u64),
}

/// The installed fuel ran out.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct OutOfFuel;

thread_local! {
static REMAINING: Cell<Fuel> = const { Cell::new(Fuel::Unlimited) };
}

/// Spend one step.
#[inline]
pub fn tick() -> Result<(), OutOfFuel> {
REMAINING.with(|remaining| match remaining.get() {
Fuel::Unlimited => Ok(()),
Fuel::Limited(0) => Err(OutOfFuel),
Fuel::Limited(steps) => {
remaining.set(Fuel::Limited(steps - 1));
Ok(())
}
})
}

/// Pay for `steps` of work that cannot run away on its own, such as one pass
/// over an existing type. Running out is not reported here: the next `tick`
/// fails, because an empty counter stays empty.
#[inline]
pub fn charge(steps: usize) {
REMAINING.with(|remaining| {
if let Fuel::Limited(left) = remaining.get() {
remaining.set(Fuel::Limited(left.saturating_sub(steps as u64)));
}
});
}

/// The fuel left on this thread.
pub fn remaining() -> Fuel {
REMAINING.with(Cell::get)
}

/// Run `work` with a fresh counter of `fuel` on this thread, then restore the
/// counter that was installed before, even if `work` panics.
pub fn run<T>(fuel: Fuel, work: impl FnOnce() -> T) -> T {
struct Restore(Fuel);
impl Drop for Restore {
fn drop(&mut self) {
REMAINING.with(|remaining| remaining.set(self.0));
}
}
let _restore = Restore(REMAINING.with(|remaining| remaining.replace(fuel)));
work()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn limited_fuel_runs_out_and_the_previous_counter_returns() {
assert_eq!(tick(), Ok(()));
run(Fuel::Limited(2), || {
assert_eq!(tick(), Ok(()));
assert_eq!(tick(), Ok(()));
assert_eq!(tick(), Err(OutOfFuel));
assert_eq!(remaining(), Fuel::Limited(0));
});
run(Fuel::Limited(3), || {
charge(5);
assert_eq!(remaining(), Fuel::Limited(0));
assert_eq!(tick(), Err(OutOfFuel));
});
assert_eq!(remaining(), Fuel::Unlimited);
}
}
Loading
Loading