tags:

views:

120

answers:

3

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
                                    [Language.Haskell.TH.Syntax.Dec]'
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.

UPDATE

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.

UPDATE 2

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.

+2  A: 

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

ghc -o main --make Main.hs
Jonno_FTW
+4  A: 

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
Dave Hinton
That's right, the question asker seems to think that bare function calls in the Main module will get called in order...
yatima2975
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.
Agent Worm
`putStrLn . show` is the same as `print` btw.
sepp2k
+2  A: 

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

Test.hs:1:0:
    Couldn't match expected type `Language.Haskell.TH.Syntax.Q
                                    [Language.Haskell.TH.Syntax.Dec]'
           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."
Travis Brown
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: http://hackage.haskell.org/trac/ghc/ticket/4042
Reid Barton