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

Haskell中Int类型约束报错咨询:Eq Int约束引发FlexibleContexts提示

Understanding the "Non type-variable argument in the constraint" Error in Your Haskell union Function

Hey there! Let's break down this error you're hitting when defining your union function.

Why the Error Happens

Your type signature looks like this:

union :: (Eq (Int)) => [Int] -> [Int] -> [Int]

The problem here is the Eq (Int) constraint. In standard Haskell (Haskell98), type class constraints require the argument to be a type variable (like a, b, etc.), not a concrete type like Int.

To put it simply:

  • Valid constraint: Eq a (where a is a type variable that can stand in for any type that implements Eq)
  • Invalid (in standard Haskell): Eq (Int) (you're specifying a concrete type here, which the default type system doesn't allow)

On top of that, this constraint is totally redundant! Int already implements the Eq type class by default—you don't need to explicitly state that.

How to Fix It

You have a couple of straightforward options:

  1. Remove the constraint entirely (best for your specific [Int] use case)
    Since Int is already an Eq instance, your type signature can just be:

    union :: [Int] -> [Int] -> [Int]
    

    This works perfectly and avoids the error.

  2. Make it generic (for any Eq type)
    If you want your union function to work with lists of any type that implements Eq (not just Int), use a type variable instead:

    union :: Eq a => [a] -> [a] -> [a]
    

    This is the idiomatic Haskell approach—it makes your function reusable across more types while still enforcing the necessary constraint.

  3. Enable the FlexibleContexts extension (not recommended here)
    If you really wanted to keep the Eq (Int) syntax (even though it's unnecessary), you could enable the FlexibleContexts language extension by adding this line at the very top of your file:

    {-# LANGUAGE FlexibleContexts #-}
    

    But again, this is overkill here because the constraint is redundant. Stick with option 1 or 2 instead.

内容的提问来源于stack exchange,提问作者Dipesh Desai

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 08:46:16