haskell: deriving (Eq, Show) for ADTs (+11 tests, 565/565)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 34s

Parser parses optional deriving clause; only appended to AST when non-empty.
hk-bind-decls! data arm generates dictShow_Con / dictEq_Con per constructor.
hk-binop == and /= now deep-force both sides (SX dict equality is by
reference — two thunks wrapping the same value compared as not-equal without
this). Three token-type fixes in the deriving parser (lparen/rparen/comma,
not "special").

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-06 12:25:51 +00:00
parent 6c1a953c80
commit 1c45262577
4 changed files with 195 additions and 5 deletions

View File

@@ -102,7 +102,7 @@ Key mappings:
- [x] `class` / `instance` declarations
- [x] Dictionary-passing elaborator: inserts dict args at call sites
- [x] Standard classes: `Eq`, `Ord`, `Show`, `Num`, `Functor`, `Monad`, `Applicative`
- [ ] `deriving (Eq, Show)` for ADTs
- [x] `deriving (Eq, Show)` for ADTs
### Phase 6 — real IO + Prelude completion
- [ ] Real `IO` monad backed by `perform`/`resume`
@@ -114,6 +114,22 @@ Key mappings:
_Newest first._
- **2026-05-06** — Phase 5 `deriving (Eq, Show)`. Parser: `hk-parse-data` now
optionally parses a `deriving (Class1, Class2)` or `deriving Class` clause
after constructor definitions; result appended as 5th element only when
non-empty (no AST churn for existing decls). Three token-type fixes: the
deriving clause used `"special"` for `(`, `)`, `,` but the tokenizer
produces `"lparen"`, `"rparen"`, `"comma"`. Eval: `hk-bind-decls!` `data`
arm generates `dictShow_{Con}` and `dictEq_{Con}` dicts for each constructor
that appears in a `deriving` list. `Show` delegates to `hk-show-val` (lazy).
`Eq` needed structural equality — `hk-binop "=="` and `/=` now call
`hk-deep-force` on both sides before `=` (SX dict equality is by reference,
so two thunks wrapping the same number compared as not-equal without this).
11 new tests in `lib/haskell/tests/deriving.sx`: nullary Show, constructor
with arg, nested, second constructor, Eq same/different constructor, `/=`
same/different, combined `(Eq, Show)`, Eq with args, different constructors
with args. 565/565 green.
- **2026-05-06** — Phase 5 standard classes. Prelude extended: `foldr`, `foldl`,
`foldl1`, `foldr1`, `zip`, `reverse`, `elem`, `notElem`, `any`, `all`, `and`,
`or`, `sum`, `product`, `maximum`, `minimum`, `compare`, `min`, `max`,