Haskell类型导致“输入中的解析错误:'”

时间:2014-02-28 04:56:38

标签: haskell functional-programming

最后三行产生错误。如何在不出错的情况下获取这些表达式的类型?拜托,谢谢你。

--
-- pairs of Int
--

data Pair = P Int Int deriving (Show, Eq)

pairFst (P x y ) = x
pairSnd (P x y ) = y

instance Ord Pair where
  compare (P x1 y1) (P x2 y2) = 
    case compare x1 x2 of EQ -> compare y1 y2
                          LT -> LT
                          GT -> GT
--
-- pairs of things of type a
--

data Pair a = P a a deriving (Show, Eq)

pairFst (P x y ) = x
pairSnd (P x y ) = y

-- ordering on same kinds of pairs pairs of the same type of thing

instance Ord a => Ord (Pair a) where
  compare (P x1 y1) (P x2 y2) = 
    case compare x1 x2 of EQ -> compare y1 y2
                          LT -> LT
                          GT -> GT
--
-- pairs of things of type a and b
--

data Pair a b = P a b deriving (Show, Eq)

pairFst (P x y ) = x
pairSnd (P x y ) = y

-- ordering on same kinds of pairs pairs of the same type of thing

instance (Ord a, Ord b) => Ord (Pair a b) where
  compare (P x1 y1) (P x2 y2) = 
    case compare x1 x2 of EQ -> compare y1 y2
                          LT -> LT
                          GT -> GT
--
-- pairs of things of type a and b, automatic deriving
--

data Pair a b = P a b deriving (Show, Eq, Ord)

pairFst (P x y ) = x
pairSnd (P x y ) = y

:t (<) P 1 2
:t (<) P 1 'a'
:t (<) P 1 "a"

1 个答案:

答案 0 :(得分:7)

您无法让编译器本身在编译时轻松地显示类型,并且类型已在运行时擦除。相反,使用GHCi。从文件中删除有问题的:t行,然后运行

$ ghci WhateverFile.hs
 *> :t (<) P 1 2
 ...

作为一个提示,答案是“没什么,这是一个类型错误”,因为你忘记了parens。我想你想要

(<) (P 1 2)

应该具有

类型
 (Num a, Num b) => P a b -> Bool

或它的一些不太通用的版本。