Generic interfaces and object type aliases lose their type parameters in Dafny
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 49/100
- Issue type
- Feature
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- typescript
- Domain
- backend-api-design, tooling
Research direction
Start with tools/src/extract.ts at extractRecord, then compare its type parameter handling with parseDeclareType; inspect the signature scan around line 2821 and the lemma call generation in tools/src/transform.ts around line 2528. Add fixtures for the Stack/Popped example, generic empty, and the type table in SPEC.md. Done when the interface and alias examples and empty<T>() verify, and generic records are documented as retaining unbounded type parameters.
Written by the indexing model from the issue text.
Description
Context
DESIGN_LS_IN_LS.md §5.1 and §9 step 3 deferred generic datatype work on 2026-07-21. The note says erasure is uneven ("record decls erase, Dafny union decls stay parametric"), to be picked up "if a case study ever hits it", and that today "no case study hits the broken path".
I hit it in my production code. An undo/redo history module is generic over the snapshot it stores (HistoryState<TSnapshot>, HistoryEntry<TSnapshot>). Its eight helper functions have no extends bound on TSnapshot, so SPEC.md's type table applies its <T> / <T extends U> row: "T kept as type param (bound dropped)". The functions do keep it. The record declarations do not, so the model does not resolve.
Problem
export interface Entry<T> {
value: T;
label: string;
}
export interface Stack<T> {
items: readonly Entry<T>[];
}
export interface Popped<T> {
stack: Stack<T>;
top: Entry<T>;
}
export function push<T>(s: Stack<T>, e: Entry<T>): Stack<T> {
//@ verify
//@ ensures \result.items.length === s.items.length + 1
return { items: [...s.items, e] };
}
export function pop<T>(s: Stack<T>): Popped<T> | null {
//@ verify
//@ ensures (\result === null) === (s.items.length === 0)
if (s.items.length === 0) return null;
return {
stack: { items: s.items.slice(0, s.items.length - 1) },
top: s.items[s.items.length - 1],
};
}
lsc gen --backend=dafny emits:
type Popped(==)
datatype Stack = Stack(items: seq<Entry<T>>)
datatype Entry = Entry(value: T, label_: string)
function push<T>(s: Stack<T>, e: Entry<T>): Stack<T>
{
Stack((s.items + [e]))
}
function pop<T>(s: Stack<T>): Option<Popped<T>>
{
if (|s.items| == 0) then
None
else
Some((Stack(s.items[0..(|s.items| - 1)]), s.items[(|s.items| - 1)]))
}
Dafny reports 11 resolution errors: Wrong number of type arguments (1 instead of 0) for each use of Stack, Entry and Popped, and Type or type parameter is not declared in this scope: T inside the two datatypes.
There are three separate problems here:
- The record declarations drop
<T>.extractRecordintools/src/extract.ts(around line 1168) returns{ name, kind: "record", fields }without type parameters, while the field types still sayEntry<T>. The same source written astype Stack<T> = { … }aliases fails the same way (14 errors). - A generic type used only in a return type becomes opaque.
Popped<T>appears only inPopped<T> | null, and it is emitted astype Popped(==), with the result built as a tuple. The signature scan around line 2821 marks a type as needed only when its base name matches^[A-Za-z_]\w*(?:\.\w+)*$, whichPopped<T>does not. A zero-argument function makes this worse:function empty<T>(): Stack<T> { return { items: [] }; }emitstype Stack(==)and the body([]). - A zero-argument generic function cannot be called from its own lemma. Once the datatypes are generic,
ensures (|empty().items| == 0)fails withtype parameter 'T' (inferred to be '?') in the function call to 'empty' could not be determined. The lemma call built intools/src/transform.ts(around line 2528) has no type arguments. Writingempty<T>()verifies.
For comparison, the same records written as //@ declare-type Entry<T> { value: T, label: string } and //@ declare-type Stack<T> { items: Entry<T>[] } emit datatype Entry<T> and datatype Stack<T>, and push verifies (1 verified, 0 errors). That form is our current workaround, but it needs the type written twice.
Checked on main at 6b18f23 (0.6.8) with Dafny 4.11.0. I did not check the Lean backend.
Proposal
Keep the deferred parametricity design out of scope. Make the record path match what declare-type and discriminated unions already do:
extractRecordrecords the declaration's type parameters, asparseDeclareTypedoes.- The signature scan marks the base name of a generic reference such as
Popped<T>, so the record is emitted. - The lemma's call to a pure generic function passes the function's type parameters as explicit type arguments, for example
empty<T>().
Non-generic records keep byte-identical output. Instantiated generics such as Stack<number> stay out of scope, as §5.1 describes.
We have run equivalent changes as a local patch against 0.6.6 in a downstream project, where the history module now verifies (24 Dafny tasks). I can port them to tools/src as a PR with fixtures.
Acceptance criteria
- The
Stack/Poppedexample above verifies, written withinterfaceand withtypealiases. empty<T>()withensures \result.items.length === 0verifies.- SPEC.md's type table says that generic records keep unbounded type parameters.
- Dominant language
- TypeScript
- Stars
- 113
- Forks
- 13
- Avg merge
- 15h 35m
- Merged PRs (30d)
- 56
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from midspiral/LemmaScript
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
midspiral/LemmaScript#355 · 1 comment ·
Maintainers usually reply within 1 day
-
In a pure function body, a `const` declared inside an `if` branch stays in scope after the `if`, so a false `ensures` can verifyPossibly taken @namin claimed this today. Open
Difficulty 4/5 3-5 days Newbie friendliness 25/100
midspiral/LemmaScript#362 ·
Maintainers usually reply within 1 day
-
A `const` or `let` inside a bare `{ }` block stays in scope after the block, so a false `ensures` can verifyPossibly taken @namin claimed this today. Open
Difficulty 4/5 3-5 days Newbie friendliness 15/100
midspiral/LemmaScript#361 ·
Maintainers usually reply within 1 day
-
A closure declared before a same-name `const` or `let` in its block reads the outer variable, so a false `ensures` can verifyPossibly taken @namin claimed this today. Open
Difficulty 4/5 3-5 days Newbie friendliness 18/100
midspiral/LemmaScript#360 ·
Maintainers usually reply within 1 day
-
An assignment used as an expression, such as `(x = 7);` or `const y = (x = 7)`, is extracted as an equality test, so a false `ensures` can verifyPossibly taken @namin claimed this today. Open
Difficulty 3/5 Half a day Newbie friendliness 30/100
midspiral/LemmaScript#356 · 1 reaction ·
Maintainers usually reply within 1 day
All issues in midspiral/LemmaScript
Similar issues
-
bug done
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
neavo/LinguaGacha#1124 ·
Maintainers usually reply within 1 day
-
[fullsend] E2E: backstage-microsoft-auth — catalogQuery missing error handler for network errorsPossibly taken @fullsend-ai-coder claimed this today. Opene2e-failure ready-to-code
Difficulty 2/5 Under an hour Newbie friendliness 78/100
redhat-developer/rhdh-plugin-export-overlays#4821 · 1 comment ·
Maintainers usually reply within 1 day
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
brylie/mission-island#225 ·
Maintainers usually reply within 1 day
-
status/need-triage
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
google-gemini/gemini-cli#30412 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day