Skip to content

stdlib: give Set and Map their traversal half - #46

Merged
simontreanor merged 1 commit into
mainfrom
feat/stdlib-map-set-sweep
Jul 31, 2026
Merged

stdlib: give Set and Map their traversal half#46
simontreanor merged 1 commit into
mainfrom
feat/stdlib-map-set-sweep

Conversation

@simontreanor

Copy link
Copy Markdown
Owner

Dogfooding finding #5, PR 3 of 5 (see #34). Follows #42 (List), #43 (docs) and #44 (Seq).

Set and Map were containers you could build, query for membership and convert, but not walk. Transforming either meant a round trip through toList — the kind of gap that makes a library feel unfinished even when nothing is strictly missing. This adds 19 members.

  • Set (11): isEmpty map filter fold exists forall partition isSubset isSuperset max min
  • Map (8): isEmpty map filter fold exists forall partition union

Design points worth review:

  • Every Map member takes the key and the value, since a map's element is the pair, and walks m.items() so each entry is read once. A loop over keys with m[k] inside would hash every key twice; there's a test asserting the emitted helper does neither.
  • Map.map keeps the keys and frees the value type (Map k v -> Map k w), while Set.map frees the element type entirely (Set a -> Set b) and can return a smaller set, since two elements may map onto one. Both have tests.
  • Map.union lets the second map win a shared key, matching dict.update and Python's {**a, **b}.
  • Set.max/Set.min answer with Option and carry comparison, following List.max.
  • Set.fold walks in Python's set order, which is not sorted. Its documentation says so rather than leaving it to be discovered by a fold whose answer depends on order.
  • Both partitions test each element once, as List.partition does.

All the higher-order members are effect-polymorphic like List.map, with tests that let pure rejects an impure traversal in each module.

Tests: 8 typecheck cases, 3 lowering assertions (items-walk, single-evaluation partition, Python's own set methods), and 2 end-to-end programs covering all 19 members' runtime results. Full suite, clippy and fmt clean.

Both were containers you could build, query for membership and convert,
but not walk. Transforming either meant a round trip through toList, which
is the kind of gap that makes a standard library feel unfinished even
though nothing is strictly missing.

Set gains isEmpty, map, filter, fold, exists, forall, partition, isSubset,
isSuperset, max and min. Map gains isEmpty, map, filter, fold, exists,
forall, partition and union.

Every Map member takes the key *and* the value, since a map's element is
the pair, and walks m.items() so each entry is read once — a loop over keys
with m[k] inside would hash every key twice. Map.map keeps the keys and
frees the value type (Map k v -> Map k w); Map.union lets the second map
win a shared key.

Set.map can return a smaller set, since two elements may map onto one, so
its result type is free rather than a relabelling. Set.max and Set.min
answer with Option and carry the comparison constraint, like List.max.
Set.fold walks in Python's set order, which is not sorted, and its
documentation says so rather than leaving it to be discovered.

Both partitions test each element once, as List.partition does: the
two-pass form would double the predicate's effects as well as its cost.
@simontreanor
simontreanor merged commit 21197b3 into main Jul 31, 2026
11 checks passed
@simontreanor
simontreanor deleted the feat/stdlib-map-set-sweep branch July 31, 2026 16:32
@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