Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Illegal type signature in instance declaration

Tags:

haskell

ghc

I'm getting the Illegal type signature in instance declaration error and I have no idea why it is popping up for my program. Indentation seems right, etc. I hope you can help me.

class Game g s | g -> s where
  findPossibleMoves :: Player -> g -> [(s,g)]
  identifyWinner :: g -> Player -> Maybe Player

instance Game HForest HStrategy where
  identifyWinner :: HForest -> Player -> Maybe Player
  identifyWinner ts p = getWinner $ getLeaves ts

  findPossibleMoves :: Player -> HForest -> [(HStrategy, HForest)]
  findPossibleMoves p ts = map (\s -> (s,move s ts)) $ getStrategies p ts

The error is :

Illegal type signature in instance declaration:
  findPossibleMoves :: Player -> HForest -> [(HStrategy, HForest)]
(Use InstanceSigs to allow this)
In the instance declaration for `Game HForest HStrategy'
like image 247
user3657850 Avatar asked Jan 09 '16 22:01

user3657850


People also ask

How can I write a type signature in an instance declaration?

In Haskell, you can’t write a type signature in an instance declaration, but it is sometimes convenient to do so, and the language extension InstanceSigs allows you to do so. For example:

Should you allow type signatures for members in instance definitions?

Allow type signatures for members in instance definitions. In Haskell, you can’t write a type signature in an instance declaration, but it is sometimes convenient to do so, and the language extension InstanceSigs allows you to do so. For example:

What are the legal and illegal declarations and initializations in C?

Let’s see some of the examples of legal and illegal declarations and initializations in C. It is a legal statement because we can initialize a variable with a constant. It is an illegal statement because static variable has to be initialized by a constant but here q is not initialized with a constant.

What is the head of an instance declaration in Haskell?

In Haskell 98 the head of an instance declaration must be of the form C (T a1 ... an), where C is the class, T is a data type constructor, and the a1 ... an are distinct type variables.


1 Answers

You have a type signature in an instance declaration. That's illegal in standard Haskell. You can enable the InstanceSigs extension (put {-# LANGUAGE InstanceSigs #-} at the top of your file) to allow it. Or just delete the type signature.

like image 66
Reid Barton Avatar answered Oct 06 '22 21:10

Reid Barton