Function 在合同中声明函数的类型

Function 在合同中声明函数的类型,function,haskell,types,Function,Haskell,Types,所以我对函数的类型有点纠结 我有一个函数 prop_merge_check xs ys = length (merge xs ys) == length (sort (xs ++ ys)) 如何为函数的每个元素分配类型 我试过这样做 prop_merge_check :: Eq a => [a] -> [a] -> Bool 也是这样 prop_merge_check xs ys = length (merge (xs::[a]) (ys::[a])) == length

所以我对函数的类型有点纠结

我有一个函数

prop_merge_check xs ys = length (merge xs ys) == length (sort (xs ++ ys))
如何为函数的每个元素分配类型

我试过这样做

prop_merge_check :: Eq a => [a] -> [a] -> Bool
也是这样

prop_merge_check xs ys = length (merge (xs::[a]) (ys::[a])) == length (sort ((xs::[a]) ++ (ys::[a])))
但这似乎对我不起作用

需要帮助吗

错误是


正如我所怀疑的,类型不是
Eq
type类的一部分,而是
Ord
type类的一部分
Eq
指定如何等于两个事物,而
Ord
指定某个事物是小于(
LT
),大于(
GT
)还是等于(
Eq
)某个事物


将您的类型更改为:
prop\u merge\u check::Ord a=>[a]->[a]->Bool
应该可以工作。

您收到的错误消息是什么?@DevNebulae它说无法匹配预期的类型。请发布整个错误消息。然后我们可以检查它应该是什么。我看没问题。发布一条错误消息,请在tio.run或repl.it上运行完整代码。你的第二行在第一行之上,然后就可以了(你不需要第三行)。您可能将其用于不兼容的参数。
• Ambiguous type variable ‘a0’ arising from a use of ‘merge’
  prevents the constraint ‘(Ord a0)’ from being solved.
  Probable fix: use a type annotation to specify what ‘a0’ should be.
  These potential instances exist:
    instance (Ord a, Ord b) => Ord (Either a b)
      -- Defined in ‘Data.Either’
    instance Ord Ordering -- Defined in ‘GHC.Classes’
    instance Ord Integer
      -- Defined in ‘integer-gmp-1.0.2.0:GHC.Integer.Type’
    ...plus 23 others
    ...plus 98 instances involving out-of-scope types
    (use -fprint-potential-instances to see them all)
• In the first argument of ‘length’, namely
    ‘(merge (xs :: [a]) (ys :: [a]))’
  In the first argument of ‘(==)’, namely
    ‘length (merge (xs :: [a]) (ys :: [a]))’
  In the expression:
    length (merge (xs :: [a]) (ys :: [a]))
      == length (sort ((xs :: [a]) ++ (ys :: [a])))
• Ambiguous type variable ‘a0’ arising from a use of ‘merge’
  prevents the constraint ‘(Ord a0)’ from being solved.
  Probable fix: use a type annotation to specify what ‘a0’ should be.
  These potential instances exist:
    instance (Ord a, Ord b) => Ord (Either a b)
      -- Defined in ‘Data.Either’
    instance Ord Ordering -- Defined in ‘GHC.Classes’
    instance Ord Integer
      -- Defined in ‘integer-gmp-1.0.2.0:GHC.Integer.Type’
    ...plus 23 others
    ...plus 98 instances involving out-of-scope types
    (use -fprint-potential-instances to see them all)
• In the first argument of ‘length’, namely
    ‘(merge (xs :: [a]) (ys :: [a]))’
  In the first argument of ‘(==)’, namely
    ‘length (merge (xs :: [a]) (ys :: [a]))’
  In the expression:
    length (merge (xs :: [a]) (ys :: [a]))
      == length (sort ((xs :: [a]) ++ (ys :: [a])))