Skip to content

Implement target-polymorphic comprehensions incl. map form [k: v for ...] (§13.6) #390

Closed

Description

@ehartford

Summary

Implement the full §13.6 comprehension surface: target-polymorphic element comprehensions, map comprehensions, nested for, optional filters, and expected-type based collection selection. Current parser/lowering covers only a Vec-shaped array-comprehension path.

Spec

docs/with-specification.md:5990-5998 says comprehensions are one polymorphic family over collection targets: element comprehensions default to Vec, map comprehensions default to HashMap, and expected types select targets such as HashSet, BTreeSet, or BTreeMap. docs/with-specification.md:6000-6017 gives required examples, including [x*x for x in xs], HashSet = [...], HashMap = [name: age for ...], and BTreeMap = [...]. docs/with-specification.md:6019-6031 defines desugaring through collect[C]() or collect[M](). docs/with-specification.md:6044-6045 requires duplicate map keys to follow the target map's normal insertion semantics, last win for standard maps. The grammar includes both element and map comprehension heads at docs/with-specification.md:11387-11390.

Current state

src/Parser.w:6279-6313 parses only [expr for pattern in iter ... if filter] into NK_ARRAY_COMPREHENSION. Because it starts by parsing a single expression and then looks for for, [key: value for ...] has no map-head path. src/MirLower.w:7978-7979 lowers only NK_ARRAY_COMPREHENSION, so there is no map-comprehension lowering node or target-directed lowering path.

Existing coverage is element-only. test/spec/spec_ss13_6_comprehensions.w covers simple, nested, filtered, membership, and tuple-pattern element comprehensions, but there is no HashSet/BTreeSet target test and no HashMap/BTreeMap map comprehension test. test/behavior/behav_list_comp.w:1-4 is check-only and still contains an old TODO comment, so it is not useful as the source of truth.

Implementation plan

  1. Extend AST/parser support with a representation that distinguishes element comprehensions from map comprehensions while preserving the existing element behavior.
  2. Reuse parse_pattern() for each for pattern in iterable clause so tuple, struct, enum, and slice patterns share the normal pattern implementation.
  3. Teach sema to infer the comprehension target from expected type. With no expected type, default element comprehensions to Vec[T] and map comprehensions to HashMap[K, V].
  4. Lower element comprehensions through the same map/filter/collect[C] machinery required by §13.6, not a Vec-only special case.
  5. Lower map comprehensions by inserting key/value pairs into the selected map target and preserving normal duplicate-key semantics.
  6. Keep in disambiguation local to the comprehension parser as required by docs/with-specification.md:6047-6055.

Test plan

Add behavior/spec tests for default Vec, expected HashSet, expected BTreeSet, default HashMap, expected BTreeMap, nested generators, filters, tuple-pattern bindings, and duplicate-key last-win behavior. Add compile-error tests for malformed map heads and incompatible expected collection targets.

Acceptance criteria

  • All §13.6 examples compile and behave as specified.
  • [k: v for ...] works for default and expected map targets.
  • [expr for ...] is target-polymorphic across at least Vec, HashSet, and BTreeSet.
  • Comprehension desugaring shares the standard collection target machinery rather than hardcoding Vec.
  • Existing element-comprehension tests continue to pass.

Dependencies

Depends on #388 for target-polymorphic collect[C]() and overlaps #389 for collection literal target inference and map syntax.

Activity

  1. ehartford commented on Jun 13, 2026

    @ehartford
    ContributorAuthor

    Close-out from the docs/finish.md audit:

    This is the right tracker for the remaining list-comprehension work. The old checklist entry was too broad/outdated:

    • Basic bracket element comprehensions are implemented and covered by test/spec/spec_ss13_6_comprehensions.w.
    • test/behavior/behav_list_comp.w is stale: it is //! check-only and still says [expr for x in iter if cond] is not implemented.
    • The remaining substantive gap is the one this issue already describes: target-polymorphic comprehensions and map forms like [k: v for ...].

    Acceptance addition from this audit: either delete/replace test/behavior/behav_list_comp.w or turn it into real executable coverage. It should not continue to claim ordinary element list comprehensions are unimplemented.

  2. ehartford commented on Jun 15, 2026

    @ehartford
    ContributorAuthor

    Implemented in 68f9eeb. Added target-polymorphic element comprehensions for Vec/HashSet, map comprehensions with default and expected HashMap targets, duplicate-key last-write semantics, parser/AST/walker support for [key: value for ...], and positive/negative tests. BTree targets remain the explicit #414 collection work and continue to fail loudly until that issue lands. Verification passed: WITH=$PWD/out/bin/with ./out/release/bin/with build; build :fixpoint; build :test; build :test-green.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions