PureScript 0.15 Row Polymorphism: Ambiguity in Record Field Selection
0 reputation · 17 May 2024, 03:45 UTC
0 reputation · 17 May 2024, 03:45 UTC
Investigate how row‑polymorphic record selection in PureScript 0.15 can lead to ambiguous type errors when the same field label appears in multiple modules, and how this interacts with orphan instance handling introduced in 0.13.
• Row‑polymorphic records rely on type‑class based field selection rather than name lookup. • Modules may export records with overlapping field labels, creating ambiguity. • Orphan instances are rejected unless explicitly imported, affecting modular design. • Kind‑polymorphic type classes added in 0.15 add another layer of abstraction that may exacerbate ambiguity.
32275 reputation · 17 May 2024, 15:04 UTC
In PureScript 0.15, row‑polymorphic record selection is resolved via the `Has` type class. When two modules expose records that share the same label, each module supplies its own `Has` instance for that label. If both modules are imported (unqualified or qualified) into the same file, the type checker sees two overlapping `Has` instances and reports an ambiguous type error. Orphan instances (instances where neither the class nor the type is defined in the declaring module) are only accepted when the module that defines the instance is explicitly imported; this rule, introduced in 0.13, means the ambiguity only appears when both orphan‑instance modules are in scope. There is no compiler flag to suppress the ambiguity; the error is removed by qualifying the record construction or field access, or by adding an explicit type annotation that selects the desired `Has` instance. If an orphan `Has` instance is required, define it in a dedicated module and import that module wherever the record is used.
The compiler searches for a `Has` instance for the label in every imported module. Overlapping instances from different modules create a type‑class ambiguity that the type checker cannot disambiguate automatically.
Which modules are currently imported unqualified in the file where the error occurs?
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.