在Haskell中显式键入绑定中键入错误

时间:2014-06-07 19:15:45

标签: haskell

我的Haskell代码出现类型错误。 termEnVoc如果TrueTerm(词汇表)的一部分,则Vocabulario会返回 type Cte = Simbolo type Funcion = (Simbolo,Aridad) type Predicado = (Simbolo, Aridad) type Vocabulario = ([Cte], [Funcion], [Predicado]) data Term = C Simbolo | L Var | F Simbolo [Term] deriving (Show, Eq) termEnVoc :: Term -> Vocabulario -> Bool --This is line 38, the one with the error termEnVoc = \t -> \(cs,fs,ps)-> (or(map (\x ->(x==t))cs) || or(map (\x ->(x==t))f) || or(map (\x ->(x==t))p)); ,我不完全确定它是否有效,但无论如何我可以& #39;了解为什么会出现类型错误。

这是代码:

ERROR file:.\tarea3.hs:38 - Type error in explicitly typed binding
*** Term           : termEnVoc
*** Type           : [Char] -> ([[Char]],[([Char],Int)],[([Char],Int)]) -> Bool
*** Does not match : Term -> Vocabulario -> Bool

这里有错误:

{{1}}

2 个答案:

答案 0 :(得分:2)

正如chi所说,主要问题似乎是你试图将Term s与其他类型的值进行比较。很难看到你正在尝试做什么(具体来说,应该代表什么样的不同类型),但这里是你可能想要构建函数定义的一般方法:

termEnVoc (C simbolo) (cs, fs, ps) = cte `elem` cs
termEnVoc (F simbolo termList) (cs, fs, ps) = head $ filter ((== f) . fst) fs
termEnVoc (L var) (cs, fs, ps) = head $ filter ((== var) . fst) ps

正如我所指出的,一些(甚至大多数)细节可能是错误的,但这应该让您了解如何构建定义。上面的代码使用了以下代码:

(== x)  =  (\y -> y == x)

您实际上可以通过以下操作员执行此操作:

(/ 3) = (\x -> x/3)

(3 /) = (\x -> 3/x)

唯一不好的是减法,我总是要查看规则。

elem a as = or $ map (== a) as

a `elem` b = elem a b

filter p [] = []
filter p (x:xs)
  | p x        = x : filter p xs
  | otherwise  = filter p xs

请注意,出于效率原因,上述的实际定义可能不同。

答案 1 :(得分:0)

我最终决定问题是因为dfeuer说我正在将术语与其他类型的值进行比较。 我最终得到了这个方法:

esTerm :: Vocabulario -> Term -> Bool
esTerm = \(c,f,p)-> \t -> case t of {
        C x -> elem x c;
        L x -> True;
        F n ts -> case (lookup n f) of {
            Nothing -> False;
            Just x -> x==(length ts)&& and(map (esTerm (c,f,p)) ts);
        }
}

感谢您的帮助,这对于解决我在项目中犯下的其他错误非常有用。

相关问题