Skip to content

stdlib: the FSharp.Core audit, and the effect-arrow bug it found - #51

Merged
simontreanor merged 2 commits into
mainfrom
feat/fsharp-core-audit
Jul 31, 2026
Merged

stdlib: the FSharp.Core audit, and the effect-arrow bug it found#51
simontreanor merged 2 commits into
mainfrom
feat/fsharp-core-audit

Conversation

@simontreanor

Copy link
Copy Markdown
Owner

The audit pass that closes dogfooding finding #5 (see #34). Depends on nothing else; #50 (the import diagnostic) is independent.

Comparing the surface member by member against FSharp.Core turned up 21 additions — and one bug that matters more than all of them.

The bug

Every scheme with a multi-argument callback put the effect variable on every arrow. A function's latent effect lives on its innermost arrow (DESIGN §4), so an impure two-argument callback wants pure outside and io inside, while (b ->{e} a ->{e} b) demands one answer for both. Nothing effectful could ever unify, which silently made every such member pure-only:

extern log : int -> int = builtins.print
let step acc x =
    let shown = log x
    acc + x
let total = List.fold step 0 [1, 2]

Before: effect mismatch: cannot unify 'a -> 'b -> 'a with int -> int ->{io} int. List.fold could not print. After: it checks, and let pure on it correctly reports performs `io` .

Thirteen schemes across List, Seq, Set, Map, Option and Result now carry the effect on the innermost arrow alone — strictly more permissive, so nothing that compiled before stops. There's a test asserting one shape per module so it cannot regress.

The 21 members

Nearly all from internal asymmetry rather than F# parity:

  • List (4): takeWhile dropWhileSeq had them; plus sortByDescending (which sortBy and sortDescending implied) and countBy.
  • Seq (9): empty distinctBy replicate get last sumBy max min reduce — all of which List already had.
  • Set/Map (2): iter, which existed on List, Seq, Option and Result but neither of these.
  • Option/Result (5): Option.forall/contains; Result.exists/forall/contains. forall passes on None and on Error — the empty case, as List.forall passes on [].
  • Global (1): sign, the one F# global with no equivalent.

Two smaller finds while wiring it up

  • Seq.empty lowered to a bare iter(), which is a TypeError, not an empty iterator. Now iter([]), with a test.
  • My first sign used a num_vars entry on a plain type variable, which constrains nothing — sign "x" type-checked. The numeric constraint is the dedicated Ty::Num, as abs uses. Caught by its own test.

Deliberately not added: foldBack/reduceBack/scanBack (no reverse-order idiom here), transpose/permute/splitInto/allPairs/zip3 (niche), mapi (indexed plus a tuple parameter now covers it), singleton ([x]), Seq.cache (the single-pass caveat is documented rather than papered over).

Tests: 7 typecheck cases including the multi-argument-effect regression across all nine affected members, 2 lowering assertions, and an end-to-end program covering all 21. Full suite, clippy, fmt and the 23 doc lessons clean.

Comparing the surface member by member against FSharp.Core turned up 21
additions, nearly all from *internal* asymmetry rather than parity alone:
takeWhile and dropWhile existed on Seq but not List; nine List members had
no Seq counterpart (empty, distinctBy, replicate, get, last, sumBy, max,
min, reduce); iter existed on List, Seq, Option and Result but not on Set
or Map; Option.exists had no forall; and sign was the one F# global with no
equivalent. List also gains sortByDescending, which sortBy and
sortDescending implied, and countBy.

The more valuable half is a bug. Every scheme with a multi-argument
callback put the effect variable on *every* arrow, but a function's latent
effect lives on its innermost arrow, so an impure two-argument callback
could never unify: it wants pure outside and io inside, while the scheme
demands one answer for both. That silently made every such member pure-only
— List.fold could not print. Thirteen schemes across List, Seq, Set, Map,
Option and Result now carry the effect on the innermost arrow alone, which
accepts pure and impure callbacks alike and lets the effect flow out to the
call, exactly as the one-argument members always did.

Two smaller finds while wiring it up: Seq.empty lowered to a bare `iter()`,
which is a TypeError rather than an empty iterator, and my first `sign`
used a `num_vars` entry on a plain type variable, which constrains nothing
— the numeric constraint is the dedicated `Ty::Num`, as `abs` uses.
@simontreanor
simontreanor merged commit 18c05f2 into main Jul 31, 2026
11 checks passed
@simontreanor
simontreanor deleted the feat/fsharp-core-audit branch July 31, 2026 18:25
@simontreanor simontreanor mentioned this pull request Jul 31, 2026
simontreanor added a commit that referenced this pull request Jul 31, 2026
Two dogfooding reports from real programs, and the standard-library sweep
they triggered.

Language:

* a `type` declaration can name an imported type, bare or module-qualified
  (#36) — the one gap that changed a program's architecture rather than its
  phrasing, forcing two modules into one file
* field access resolves from the base's type when it is known, so two
  records may share a field name without prefixes (#37)
* parameters destructure: tuples (#38), records (#40), and `_`
* a direct self tail call lowers to a loop, so an interactive turn loop no
  longer walks the stack (#39, #41)

Standard library — about 115 new members, taking every module to the F#
core set: List (#42), Seq (#44), Set and Map (#46), String (#47), Option
and Result (#48), then a member-by-member FSharp.Core audit (#51). Every
built-in member now carries a one-line description and its complexity in
hover and completion (#43, #49), enforced by tests.

Fixes:

* `pyfun run` on a single file gives the program its own stdin, so an
  interactive program is runnable by the command whose job is running
  programs (#35)
* a partially applied lambda closes over its argument instead of being
  wrapped, so `List.map ((+) 2)` emits `lambda b: 2 + b` (#52)
* every multi-argument callback's scheme put the effect variable on the
  wrong arrows, so `List.fold` could never accept an effectful folder (#51)
* `Seq.empty` lowered to a bare `iter()`, a TypeError (#51)

One source-incompatible change, which is why this is 0.4.0 and not 0.3.1:
a dotted `extern` target whose module prefix cannot be decided from the
text is now a compile error naming the `extern import` to add (#50).
`sys.stdout.flush` used to emit `import sys.stdout` and fail at runtime;
declaring `extern import sys` fixes it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant