GHC/Error messages

From HaskellWiki
< GHC
Revision as of 00:44, 4 June 2018 by DebugSteven (talk | contribs) ("Parse error in pattern")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

GHC error messages and their meaning.

"`foo' is not applied to enough type arguments"

TODO

Example: TODO

"`foo' is not a (visible) method of class `Bar'"

This error message occurs when one tries to instantiate a class, but did not import the functions one tries to implement.

Example:

import Prelude hiding ((==))

data Foo = Foo

instance Eq Foo where 
    (==) a b = True

"Cannot match a monotype with `Foo'"

See this Haskell-Cafe thread.


"Parse error in pattern"

A "parse error in pattern" error occurs when you haven't matched on a data constructor in the way you meant to.

Example:

merge [] bs rest = bs ++ rest
merge as [] rest = as ++ rest
merge a:as b:bs rest
    | a > b     = merge as (b:bs) (a:rest)
    | otherwise = merge (a:as) bs (b:rest)


λ> :load
[1 of 1] Compiling Main             ( merge.hs, interpreted )
merge.hs:3:1: error: Parse error in pattern: merge
   |
 3 | merge a:as (b:bs) rest
   | ^^^^^^^
Failed, no modules loaded.

This error occurs because we haven’t properly pattern matched on the data constructor in the parameter list for our last definition of the merge function. The type constructor here is a list, which means our options for data constructors are [] and a : [a]. The way we match on a list with multiple elements is by indicating it’s 1 argument and we construct the type with the data constructors. We construct a list of `a` elements is by writing it as `a` cons’ed onto a list of `as`, (a:as).

merge [] bs rest = bs ++ rest
merge as [] rest = as ++ rest
merge (a:as) (b:bs) rest
    | a > b     = merge as (b:bs) (a:rest)
    | otherwise = merge (a:as) bs (b:rest)