stdlib: give Option and Result their combining half - #48
Merged
Conversation
Both had map, bind, withDefault and the predicates, which handle one value at a time. Neither could combine two, or cross into another type without a match. That is ten members: map2, orElse, flatten, iter, toList and exists for Option; map2, orElse, iter and toList for Result. map2 is the one that earns its place. Two independent lookups that both have to succeed otherwise means nesting two matches, and this is the shape that removes them. Option.map2 is Some only when both are; Result.map2 is Ok only when both are and otherwise reports the *first* Error, so the earliest failure is the one that surfaces. A lowering test pins that ordering rather than trusting the reading. orElse takes the fallback first, matching withDefault, so it reads as `primary |> Option.orElse fallback` under the pipe. Getting that backwards would be silently wrong rather than a type error, since both arguments have the same type, which is why the argument order is documented on the member itself. One test premise of mine was wrong and is worth recording: `Result.map2 f (Ok 1) (Error 2)` type-checks, because `Ok 1` leaves its error type free. Two *concrete* error types are what conflict, so the test pins that instead. Every member of both modules is O(1), which is why none of them state a cost: they hold one value, so there is nothing to traverse.
simontreanor
force-pushed
the
feat/stdlib-option-result-sweep
branch
from
July 31, 2026 16:41
1a14f43 to
9f2687c
Compare
Merged
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Dogfooding finding #5, PR 5 of 5 (see #34) — the last sweep. Branched from
main, independent of #46 (Map/Set) and #47 (String); all three can merge in any order.Both types had
map/bind/withDefaultand the predicates, which handle one value at a time. Neither could combine two, or cross into another type without amatch. Ten members:map2orElseflattenitertoListexistsforOption;map2orElseitertoListforResult.map2is the one that earns its place. Two independent lookups that both have to succeed otherwise means nesting two matches:Option.map2isSomeonly when both are.Result.map2isOkonly when both are, and otherwise reports the firstError, so the earliest failure surfaces. There's a lowering test assertingais checked beforeb, rather than trusting the reading.orElsetakes the fallback first, matchingwithDefault, so it reads asprimary |> Option.orElse fallbackunder the pipe. Worth review: getting the order backwards would be silently wrong rather than a type error, since both arguments have the same type. That's why the argument order is stated on the member's own documentation line, not just here.One test premise of mine was wrong, and the correction is instructive. I expected
Result.map2 f (Ok 1) (Error 2)to be a type error, but it checks:Ok 1leaves its error type free, so it unifies with anything. Two concrete error types are what conflict, and the test pins that instead.Tests: 7 typecheck cases (combining, the
orElseorder, one-levelflatten, the shared error type, effect-carryingiter), 1 lowering assertion (first-error ordering), and 2 end-to-end programs covering all ten members. Full suite, clippy and fmt clean.With this the sweep is complete: 94 members across
List,Seq,Set,Map,String,Option,Result, plusfst/snd, all documented with their complexity. Next is theFSharp.Coreaudit pass, then finding #7's import diagnostic.