Difference between revisions of "GHC/Error messages"

From HaskellWiki
< GHC
Jump to navigation Jump to search
(will fill this out once i find a small example)
 
(9 intermediate revisions by one other user not shown)
Line 1: Line 1:
 
GHC error messages and their meaning.
 
GHC error messages and their meaning.
   
  +
== "`foo' is not applied to enough type arguments" ==
== Parse error in pattern ==
 
   
 
TODO
 
TODO
   
 
Example: 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:
  +
<haskell>
  +
import Prelude hiding ((==))
  +
  +
data Foo = Foo
  +
  +
instance Eq Foo where
  +
(==) a b = True
  +
</haskell>
  +
  +
== "Cannot match a monotype with `Foo'" ==
  +
  +
See [http://www.haskell.org/pipermail/haskell-cafe/2009-October/067806.html 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)

Revision as of 00:44, 4 June 2018

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)