Illegal binding of built-in syntax: `:`

I need to build custom Stream data type. It's like list data type, but for infinite lists only (without empty list possibility).

I found list data type -

data [] a = a : [a] | []

and I wrote this:

data Stream a = a : (Stream a)

but GHCi gives error message:

Illegal binding of built-in syntax: :

what's wrong this my data type declaration?

Upvotes: 10

Views: 2511

Answers (2)

Edward Kmett
Edward Kmett

Reputation: 29982

Note: you can use the existing Data.Stream.Infinite from the streams package for this purpose and get a ton of instances and combinators for free, but (:) itself is built in syntax.

Upvotes: 6

C. A. McCann
C. A. McCann

Reputation: 77424

The constructor (:) is built-in syntax and specific to the standard list type, unlike much of the standard "built-in" types that are just regular types defined in the standard library.

So, you'll need to use a different constructor for your stream type. (:|) and (:<) are versions I've seen, so something like this:

data Stream a = a :< Stream a

...should work fine.

You could also drop the infix constructor entirely if you prefer:

data Stream a = Stream a (Stream a)

Upvotes: 11

Related Questions