15 KiB
Haskell-on-SX: mini-Haskell with real laziness
Mini-Haskell is the research-paper-worthy demo. Laziness is native to the SX runtime (thunks are already a first-class type); algebraic data types map onto tagged lists; typeclasses map onto dictionary passing; IO maps onto perform/resume. Hindley-Milner inference is the one real piece of new work.
End-state goal: a Haskell 98 subset that runs the small classic programs (sieve of Eratosthenes lazy stream, fibonacci as infinite list, naive quicksort, n-queens, expression evaluator) plus a ~150-test corpus.
Scope decisions (defaults — override)
- Standard: Haskell 98 subset. No GHC extensions (no
DataKinds, noGADTs, noTypeFamilies, noTemplateHaskell). - Phase 1-3 are untyped — we get the evaluator right first with laziness + ADTs, then add HM inference in phase 4. This is deliberate: typing is the hard bit and will take a full phase on its own.
- Typeclasses: dictionary passing, no overlap, no orphan instances. Added in phase 5.
- Layout rule: yes — phase 1 implements Haskell's indentation-sensitive parsing (painful but required).
- Test corpus: custom. No GHC test suite. Bundle classic programs + ~100 hand-written expression-level tests + mini Prelude tests.
Ground rules
- Scope: only
lib/haskell/**andplans/haskell-on-sx.md. No edits tospec/,hosts/,shared/, or other language dirs. - SX files:
sx-treeMCP tools only. - Architecture: Haskell source → AST → desugared-core → SX AST → CEK. Thunks on the SX side provide laziness natively.
- Commits: one feature per commit. Keep
## Progress logupdated.
Architecture sketch
Haskell source
│
▼
lib/haskell/tokenizer.sx — idents, operators, layout-sensitive indentation
│
▼
lib/haskell/parser.sx — AST: modules, data decls, type sigs, fn clauses, expressions
│
▼
lib/haskell/desugar.sx — surface → core: case-of-case, do-notation, list comp, guards
│
▼
lib/haskell/transpile.sx — core → SX AST, wrapping everything in thunks for laziness
│
▼
lib/haskell/runtime.sx — force, ADT constructors, Prelude, typeclass dicts (phase 5+)
│
▼
existing CEK / VM
Key mappings:
- Laziness = every function argument is an SX thunk;
forceis WHNF reduction. SX already hasmake-thunkfrom the trampolining evaluator — we reuse it. - Pattern match = forces the scrutinee to WHNF, then structural match on the tag
- ADT =
data Maybe a = Nothing | Just acompiles to tagged lists:(:Nothing)and(:Just <thunk>) - Typeclass = each class becomes a record type; each instance becomes a record value; each method becomes a projection; the elaborator inserts the dict at each call site (phase 5)
- IO =
IO ais a functionWorld -> (a, World)internally; in practice usesperform/resumefor actual side effects - Layout = offside rule; inserted virtual braces + semis during a lexer-parser feedback pass
Roadmap
Phase 1 — tokenizer + parser + layout rule
- Tokenizer: reserved words, qualified names, operators, numbers (int, float, Rational later), chars/strings, comments (
--and{-nested) - Layout algorithm: turn indentation into virtual
{,;,}tokens per Haskell 98 §10.3 - Parser (split into sub-items — implement one per iteration):
- Expressions: atoms, parens, tuples, lists, ranges, application, infix with full Haskell-98 precedence table, unary
-, backtick operators, lambdas,if,let case … ofanddo-notation expressions (plus minimal patterns needed for arms/binds: var, wildcard, literal, 0-arity and applied constructor, tuple, list)- Patterns — full:
aspatterns, nested, negative literal,~lazy, infix constructor (:/ consym), extend lambdas/let with non-var patterns - Top-level decls: function clauses (simple — no guards/where yet), pattern bindings, multi-name type signatures,
datawith type vars and recursive constructors,typesynonyms,newtype, fixity (infix/infixl/infixrwith optional precedence, comma-separated ops, backtick names). Types: vars / constructors / application /->(right-assoc) / tuples / lists.hk-parse-topentry. whereclauses + guards- Module header + imports (stub)
- List comprehensions + operator sections
- Expressions: atoms, parens, tuples, lists, ranges, application, infix with full Haskell-98 precedence table, unary
- AST design modelled on GHC's HsSyn at a surface level
- Unit tests in
lib/haskell/tests/parse.sx(43 tokenizer tests, all green)
Phase 2 — desugar + eager-ish eval + ADTs (untyped)
- Desugar: guards → nested
ifs;where→let; list comp →concatMap-based; do-notation stays for now (desugared in phase 3) datadeclarations register constructors in runtime- Pattern match (tag-based, value-level): atoms, vars, wildcards, constructor patterns,
aspatterns, nested - Evaluator (still strict internally — laziness in phase 3):
let,lambda, application,case, literals, constructors - 30+ eval tests in
lib/haskell/tests/eval.sx
Phase 3 — laziness + classic programs
- Transpile to thunk-wrapped SX: every application arg becomes
(make-thunk (lambda () <arg>)) force= SX eval-thunk-to-WHNF primitive- Pattern match forces scrutinee before matching
- Infinite structures:
repeat x,iterate f x,[1..], Fibonacci stream, sieve of Eratosthenes seq,deepseqfrom Prelude- Do-notation for a stub
IOmonad (just threading, no real side effects yet) - Classic programs in
lib/haskell/tests/programs/:fib.hs— infinite Fibonacci streamsieve.hs— lazy sieve of Eratosthenesquicksort.hs— naive QSnqueens.hscalculator.hs— parser combinator style expression evaluator
lib/haskell/conformance.sh+ runner;scoreboard.json+scoreboard.md- Target: 5/5 classic programs passing
Phase 4 — Hindley-Milner inference
- Algorithm W: unification + type schemes + generalisation + instantiation
- Report type errors with meaningful positions
- Reject untypeable programs that phase 3 was accepting
- Type-sig checking: user writes
f :: Int -> Int; verify - Let-polymorphism
- Unit tests: inference for 50+ expressions
Phase 5 — typeclasses (dictionary passing)
class/instancedeclarations- Dictionary-passing elaborator: inserts dict args at call sites
- Standard classes:
Eq,Ord,Show,Num,Functor,Monad,Applicative deriving (Eq, Show)for ADTs
Phase 6 — real IO + Prelude completion
- Real
IOmonad backed byperform/resume putStrLn,getLine,readFile,writeFile,print- Full-ish Prelude:
Maybe,Either,Listfunctions,Map-lite - Drive scoreboard toward 150+ passing
Progress log
Newest first.
-
2026-04-24 — Phase 1: top-level decls. Refactored
hk-parse-exprinto ahk-parser tokens modewith:expr/:moduledispatch so the big lexical state is shared (peek/advance/pat/expr helpers all reachable); added public wrappershk-parse-expr,hk-parse-module, and source-level entryhk-parse-top. New type parser (hk-parse-type/hk-parse-btype/hk-parse-atype): type variables (:t-var), type constructors (:t-con), type application (:t-app, left-assoc), right-associative function arrow (:t-fun), unit/tuples (:t-tuple), and lists (:t-list). New decl parser (hk-parse-decl/hk-parse-program) producing a(:program DECLS)shell::type-sig NAMES TYPE— comma-separated multi-name support:fun-clause NAME APATS BODY— patterns for args, body via existing expr:pat-bind PAT BODY— top-level pattern bindings like(a, b) = pair:data NAME TVARS CONSwith:con-def CNAME FIELDSfor nullary and multi-arg constructors, including recursive references:type-syn NAME TVARS TYPE,:newtype NAME TVARS CNAME FIELD:fixity ASSOC PREC OPS— assoc one of"l"/"r"/"n", default prec 9, comma-separated operator names, including backtick-quoted varids. Sig vs fun-clause disambiguated by a paren-balanced top-level scan for::before the next;/}(hk-has-top-dcolon?). 24 new tests inlib/haskell/tests/parser-decls.sxcover all decl forms, signatures with application / tuples / lists / right-assoc arrows, nullary and recursive data types, multi-clause functions, and a mixed program with data + type- synonym + signature + two function clauses. Not yet: guards, where clauses, module header, imports, deriving, contexts, GADTs. 162/162 green.
-
2026-04-24 — Phase 1: full patterns. Added
aspatterns (name@apat→(:p-as NAME PAT)), lazy patterns (~apat→(:p-lazy PAT)), negative literal patterns (-N/-Fresolving eagerly in the parser so downstream passes see a plain(:p-int -1)), and infix constructor patterns via a right-associative single-band layer on top ofhk-parse-pat-lhsfor anyconsymor reservedop:(sox : xsparses as(:p-con ":" [x, xs]),a :+: blikewise). Extendedhk-apat-start?with-and~so the pattern-argument loops in lambdas and constructor applications pick these up. Lambdas now parse apat parameters instead of bare varids — so the:lambdaAST is(:lambda APATS BODY)with apats as pattern nodes.hk-parse-bindbecame a plainpat = exprform, so:bindnow has a pattern LHS throughout (simplex = 1→(:bind (:p-var "x") …)); this picks uplet (x, y) = pair in …andlet Just x = m in xautomatically, and flows throughdo-notation lets. Eight existing tests updated to the pattern-flavoured AST. Also fixed a pragmatic layout issue that surfaced in multi-linelets: when a layout-indent would emit a spurious;just before anintoken (because the let block had already been closed by dedent),hk-peek-next-reservednow lets the layout pass skip that indent and leave closing to the existinginhandler. 18 new tests inlib/haskell/tests/parser-patterns.sxcover every pattern variant, lambda with mixed apats, let pattern-bindings (tuple / constructor / cons), and do-bind with a tuple pattern. 138/138 green. -
2026-04-24 — Phase 1:
case … ofanddo-notation parsers. Addedhk-parse-case/hk-parse-alt,hk-parse-do/hk-parse-do-stmt/hk-parse-do-let, plus the minimal pattern language needed to make arms and binds meaningful:hk-parse-apat(var, wildcard_, int/float/string/char literal, 0-arity conid/qconid, paren+tuple, list) andhk-parse-pat(conid applied to apats greedily). AST nodes::case SCRUT ALTS,:alt PAT BODY,:do STMTSwith stmts:do-expr E/:do-bind PAT E/:do-let BINDS, and pattern tags:p-wild/:p-int/:p-float/:p-string/:p-char/:p-var/:p-con NAME ARGS/:p-tuple/:p-list.do-stmts disambiguatepat <- evs bare expression with a forward paren/bracket/brace-balanced scan for<-before the next;/}— no backtracking, no AST rewrite.caseanddoaccept both implicit (vlbrace/vsemi/vrbrace) and explicit braces. Added tohk-parse-lexpso they participate fully in operator-precedence expressions. 19 new tests inlib/haskell/tests/parser-case-do.sxcover every pattern variant, explicit-bracecase, expression scrutinees, do with bind/let/expr, multi-bindingletindo, constructor patterns in binds, andcase/donested insideletand lambda. The full pattern item (as patterns, negative literals,~lazy, lambda/let pattern extension) remains a separate sub-item. 119/119 green. -
2026-04-24 — Phase 1: expression parser (
lib/haskell/parser.sx, ~380 lines). Pratt-style precedence climbing against a Haskell-98-default op table (24 operators across precedence 0–9, left/right/non assoc, default infixl 9 for anything unlisted). Supports literals (int/float/string/char), varid/conid (qualified variants folded into:var/:con), parens / unit / tuples, list literals, ranges[a..b]and[a,b..c], left-associative application, unary-, backtick operators (x \mod` 3), lambdas,if-then-else, andlet … inconsuming both virtual and explicit braces. AST uses keyword tags (:var,:op,:lambda,:let,:bind,:tuple,:range,:range-step,:app,:neg,:if,:list,:int,:float,:string,:char,:con). The parser skips a leadingvlbrace/lbraceso it can be called on full post-layout output, and uses araise-based error channel with location-lite messages. 42 new tests inlib/haskell/tests/parser-expr.sxcover literals, identifiers, parens/tuple/unit, list + range, app associativity, operator precedence (mul over add, cons right-assoc, function-composition right-assoc,$lowest), backtick ops, unary-, lambda multi-param,ifwith infix condition, single- and multi-bindinglet` (both implicit and explicit braces), plus a few mixed nestings. 100/100 green. -
2026-04-24 — Phase 1: layout algorithm (
lib/haskell/layout.sx, ~260 lines) implementing Haskell 98 §10.3. Two-pass design: a pre-pass augments the raw token stream with explicitlayout-open/layout-indentmarkers (suppressing<n>when{n}already applies, per note 3), then an L pass consumes the augmented stream against a stack of implicit/explicit layout contexts and emitsvlbrace/vsemi/vrbracetokens; newlines are dropped. Supports the initial module-level implicit open (skipped when the first token ismoduleor{), the four layout keywords (let/where/do/of), explicit braces disabling layout, dedent closing nested implicit blocks while also emittingvsemiat the enclosing level, and the pragmatic single-linelet … inrule (emit}wheninmeets an implicit let). 15 new tests inlib/haskell/tests/layout.sxcover module-start, do/let/where/case/of, explicit braces, multi-level dedent, line continuation, and EOF close-down. Shared test helpers moved tolib/haskell/testlib.sxso both test files can share onehk-test.test.shpreloads tokenizer + layout + testlib. 58/58 green. -
2026-04-24 — Phase 1: Haskell 98 tokenizer (
lib/haskell/tokenizer.sx, 490 lines) covering idents (lower/upper/qvarid/qconid), 23 reserved words, 11 reserved ops, varsym/consym operator chains, integer/hex/octal/float literals incl. exponent notation, char + string literals with escape sequences, nested{- ... -}block comments with depth counter,-- ... EOLline comments (respecting the "followed by symbol = not a comment" Haskell 98 rule), backticks, punctuation, and explicitnewlinetokens for the upcoming layout pass. 43 structural tests inlib/haskell/tests/parse.sx, a lightweighthk-deep=?equality helper and a customlib/haskell/test.shrunner (pipes through the OCaml epoch protocol, falls back to the main-repo build when run from a worktree). 43/43 green.Also peeked at
/root/rose-ash/sx-haskell/per briefing: that directory is a Haskell program implementing an SX interpreter (Types.hs, Eval.hs, Primitives.hs, etc. — ~2800 lines of .hs) — the opposite direction from this project. Nothing to fold in.Gotchas hit:
emit!andpeekare SX evaluator special forms, so every local helper uses thehk-prefix.cond/when/letclauses evaluate ONLY the last expression; multi-expression bodies MUST be wrapped in(do ...). These two together account for all the tokenizer's early crashes.
Blockers
- (none yet)