Andriy Drozdyuk
Andriy Drozdyuk

Reputation: 61041

Invalid type signature in recursive data constructor

So, following along with a sample code on streams, this fails to be loaded into ghci:

data MyStream a = MyStream a (MyStream a)

intsFrom n :: MyStream Integer
intsFrom n = MyStream n $ intsFrom $ n + 1

Getting error:

stream.hs:3:1:
    Invalid type signature: intsFrom n :: MyStream Integer
    Should be of form <variable> :: <type>
Failed, modules loaded: none.

Any ideas? Thanks!

Update: If I just type intsFrom :: MyStream Integer I get error:

stream.hs:4:1:
    The equation(s) for `intsFrom' have one argument,
    but its type `MyStream Integer' has none
Failed, modules loaded: none.

Upvotes: 1

Views: 1665

Answers (2)

Adam Wagner
Adam Wagner

Reputation: 16097

It looks like you want your signature to be something like this:

intsFrom :: Integer -> MyStream Integer

Integer here is your argument, and MyStream Integer is the result of intsFrom.

Upvotes: 6

sth
sth

Reputation: 229583

You should just use the function name in the line with the type signature, not add parameter names. So instead of

intsFrom n :: MyStream Integer

use

intsFrom :: MyStream Integer

You also have to make sure that the type you declare matches the function. Since the function takes an Integer parameter, the correct signature would be:

intsFrom :: Integer -> MyStream Integer

Upvotes: 4

Related Questions