Take the 2-minute tour ×

I've defined a function called findPaths in a Haskell Module called BinaryTree and I am trying to call that function in the main module I've created. The type of the function call is

findPaths :: Tree -> [Path]

Where Tree is a data type defined as:

data Tree = Leaf | Node Tree Tree

and Path is defined as:

data Path = LeftTurn Path | RightTurn Path | This

In the main function I'm doing this and only this:

module Main where
import BinaryTree
findPaths (Node Leaf Leaf)

But when I try and compile this with the following command:

ghc -o --make Main Main.hs BinaryTree.hs

I get this error:

Couldn't match expected type `Language.Haskell.TH.Syntax.Q
against inferred type `[Path]'
In the expression: findPaths (Node Leaf Leaf)

I get the same error if I try to export the data types in the BinaryTree module:

module BinaryTree (Tree(..), Path(..), allPaths) where...

I'm at a loss... I don't know what I'm doing wrong. Suggestions, no matter how straight forward and obvious are greatly welcome. Thank you.


Thank you, all of you, for your help.

@Travis Aside from what everyone suggested I ended up doing this last night before I read your message:

import BinaryTree

main = do
    print (findPaths (Node Leaf Leaf))

It works the way I expected it to. But in the future, I'll make sure I follow the proper semantics you referenced me.


I had responded last night with some other answers but apparently there was a power outage and 4 hours worth of answers and questions were lost. Thought maybe I had dreamt answering those questions. Good to know I'm not crazy.

share|improve this question
2. Yes, the show is necessary if you're using putStrLn because putStrLn takes a String and a Tree is not a String. You use show to convert the Tree to a String. And the reason that you derived Show is so that there even is a show function for your Tree type. If you hadn't derived (or manually instantiated) Show, you just wouldn't be able to call show. –  sepp2k Oct 9 '10 at 9:14
add comment

3 Answers

up vote 3 down vote accepted

You're seeing this error because findPaths (Node Leaf Leaf) is an expression at the top level of your Main module, which should only contain declarations.

You can get the same error from GHC by trying to compile a file containing only a string literal, for example:

travis@sidmouth% echo '"Hello world"' > Test.hs
travis@sidmouth% ghc Test.hs

    Couldn't match expected type `Language.Haskell.TH.Syntax.Q
           against inferred type `[Char]'
    In the expression: "Hello world"

I have no idea why GHC gives an error message related to Template Haskell here—it's an arcane and confusing response to a simple mistake. All you actually need to do is change the expression to a declaration. The following should work just fine:

module Main where
import BinaryTree
paths = findPaths (Node Leaf Leaf)
main = putStrLn "Do something here."
share|improve this answer
GHC implicitly splices Template Haskell declarations at top level as of version 6.12.1: if decls is a value of type Q [Dec] you can just write decls rather than the traditional syntax of $(decls). This works even without the TemplateHaskell language extension enabled: a bug, which seems to be fixed in GHC 7: hackage.haskell.org/trac/ghc/ticket/4042 –  Reid Barton Oct 10 '10 at 15:35
add comment

I think the problem here is in your calling of ghc, try this:

ghc -o main --make Main.hs
share|improve this answer
add comment

To add to what Jonno_FTW said, you need a main routine in your Main module and it needs to do IO. So your Main.hs should be something like this:

module Main where
import BinaryTree
main = putStrLn . show . findPaths $ Node Leaf Leaf
share|improve this answer
That's right, the question asker seems to think that bare function calls in the Main module will get called in order... –  yatima2975 Oct 9 '10 at 7:19
I did actually, because I had done something a little similar before, so I figured it'd work. But looking at that code now, I'm seeing that I had main = putStrLn "HelloWorld" on a line followed by insert 5 (Node 1 Leaf Leaf) Where insert is defined in another module. –  Emmanuel F. Oct 9 '10 at 7:34
putStrLn . show is the same as print btw. –  sepp2k Oct 9 '10 at 9:12
add comment

Your Answer


