You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PureScript中Eq类型类实例实现报错,请求排查解决

Fixing Your PureScript TypeClasses Module Compilation Error

Let's walk through the issues in your code and get it compiling smoothly:

1. The Root Cause of the Compilation Error

The critical mistake here is a typo in your Eq instance's eq function:

  • You wrote p1.y1 == p2.y2, but your Point data type only has fields x and y—there's no y1 or y2 defined. This is exactly what's triggering the line 17 compilation error.

2. Bonus: Clean Up Redundant Code

Beyond the typo, you can simplify the eq function: PureScript lets you return boolean expressions directly, so the if ... then true else false wrapper is unnecessary and adds extra noise.

Corrected Full Code

module TypeClasses where
import Prelude
import Data.Array
import Data.Number.Format(toString)

data Point = Point { x :: Number, y :: Number }

instance showPoint :: Show Point where
  show (Point { x, y }) = toString x <> ", " <> toString y

instance eqPoint :: Eq Point where
  eq p1 p2 = p1.x == p2.x && p1.y == p2.y

Key Changes Explained

  • Fixed the typo: Changed p1.y1 == p2.y2 to p1.y == p2.y to match the Point type's actual field names.
  • Removed redundant logic: The boolean comparison result can be returned directly, making the code cleaner and more aligned with idiomatic PureScript practices.

内容的提问来源于stack exchange,提问作者Knows Not Much

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:08:49