0

I'm new to Haskell. This is the code which I made:

transition_world world = case world of
   (Head,(x,y)):cs-> (Tail,(x,y)): transition_world cs
   (Tail,(x,y)):cs -> (Conductor, (x,y)): transition_world cs
   (Empty, (x,y)):cs -> (Empty, (x,y)): transition_world cs 
   (Conductor, (x,y)):cs
     | element_occurrence Head == 1 || element_occurrence Head == 2
                  -> (Head, (x,y)): transition_world cs
     | otherwise  -> (Conductor, (x,y)): transition_world cs
   [] -> []

While trying to compile, GHCi gives me this error:

Sources\Transitions\For_List_2D.hs:23:33:
No instance for (Eq (List_2D Cell -> Data.Integer_Subtypes.Nat))
  arising from a use of `=='
Possible fix:
  add an instance declaration for
  (Eq (List_2D Cell -> Data.Integer_Subtypes.Nat))
In the first argument of `(||)', namely
  `element_occurrence Head == 1'
In the expression:
  element_occurrence Head == 1 || element_occurrence Head == 2
In a stmt of a pattern guard for
               a case alternative:
  element_occurrence Head == 1 || element_occurrence Head == 2

Sources\Transitions\For_List_2D.hs:23:36:
No instance for (Num (List_2D Cell -> Data.Integer_Subtypes.Nat))
  arising from the literal `1'
Possible fix:
  add an instance declaration for
  (Num (List_2D Cell -> Data.Integer_Subtypes.Nat))
In the second argument of `(==)', namely `1'
In the first argument of `(||)', namely
  `element_occurrence Head == 1'
In the expression:
  element_occurrence Head == 1 || element_occurrence Head == 2

Can't understand what does it want from me... Where did I make a mistake? And how can I fix it?

Extra 1:

element_occurrence :: Eq e => e -> List_2D e -> Nat
element_occurrence element list = case list of
    (local_element, _): cs
       | local_element == element -> 1 + element_occurrence element cs
       | otherwise                ->     element_occurrence element cs
    [] -> 0

Extra 2:

local_elements :: Coord -> List_2D e -> List_2D e
local_elements (x, y) list = read_neighbours (x, y) 1 list
  where
    read_neighbours :: Coord -> Distance -> List_2D e -> List_2D e
    read_neighbours (x, y) dist list = case list of
        (element, (x_e, y_e)): cs
            |    abs (x_e - x) <= dist
            && abs (y_e - y) <= dist -> (element, (x_e, y_e)): read_neighbours (x, y) dist cs
            | otherwise              ->                        read_neighbours (x, y) dist cs
        [] -> []

local_elements_list :: Coord -> List_2D e -> [e]
local_elements_list (x, y) list = read_neighbours_list (x, y) 1 list
  where
    read_neighbours_list :: Coord -> Distance -> List_2D e -> [e]
    read_neighbours_list (x, y) dist list = case list of
        (element, (x_e, y_e)): cs
            |  abs (x_e - x) <= dist
            && abs (y_e - y) <= dist -> element: read_neighbours_list (x, y) dist cs
            |  otherwise             ->          read_neighbours_list (x, y) dist cs
        [] -> []

size :: List_2D e -> Nat
size list = case list of
   []    -> 0
   _: xs -> 1 + size xs
4
  • 1
    Welcome to StackOverflow. Cloud you please add the code for element_occurrence? Anyway, the error tells you that ghc don't know how to compare a value of type List_2D Cell -> Data.Integer_Subtypes.Nat (which is a function) to 1. The expression element_occurrence Head is probably missing an argument. Commented Apr 3, 2014 at 12:31
  • Just added the extra code Commented Apr 3, 2014 at 13:19
  • I think I understood what I miss. I also have to specify that element_occurence should look for 8 adjacent cells, not the whole picture. I still can't understand how I specify it. I guess I spent so much time staring at code that I can't notice obvious thing. I added some extra bits of code. Hope it will help. Commented Apr 3, 2014 at 15:00
  • @user3493630 I think you are from reddit. In general we don't like doing other people's homework here, either. Maybe try #haskell on IRC (Freenode), and let them know it is homework up front. It'll also be more interactive. Commented Apr 3, 2014 at 19:40

2 Answers 2

3

"Add an instance declaration for..." is one of GHC's most misleading error messages. Generally you should ignore that advice.

If the type it wants an instance for has a -> in it, this basically shows you're trying to do something with a function that's only possible with the result, i.e. you've forgotten to apply the function to all its arguments. Evidently, element_occurence has a type something like Item -> List_2D Cell -> Nat, but you have only element_occurrence Head in your code, which is a partially applied function, it still has type List_2D Cell -> Nat. By applying that to a List_2D Cell, you'll get a Nat which you can then compare to a number literal.

Sign up to request clarification or add additional context in comments.

1 Comment

I think I understood what I miss. I also have to specify that element_occurence should look for 8 adjacent cells, not the whole picture. I still can't understand how I specify it. I guess I spent so much time staring at code that I can't notice obvious thing. I added some extra bits of code. Hope it will help.
1

You've defined element_occurence as a function of 2 arguments:

element_occurrence :: Eq e => e -> List_2D e -> Nat
element_occurrence element list = -- more stuff

You've called element_occurence with only one argument:

transition_world world = case world of
{- other cases -}
(Conductor, (x,y)):cs
  | element_occurrence Head == 1 || element_occurrence Head == 2 -> -- stuff

But, that gives you a value of type "List_2D Cell -> Nat", a function type, which you try to test equality against "1" and "2".

What you need to do is provide both arguments to element_occurence.

GHC is way too subservient here, and assume you really meant to only call with one argument. (==) has type "Eq a => a -> a -> Bool", so the call to (==) will be okay if you provided an instance "Eq (List_2D Cell -> Nat)", but it can't find that instance. So, it prints the first error message.

GHC figures, you are awesome so you'll fix that error right away, and proceeds under the assumption that it is fixed. But now it sees "1" and "2" in a place where it is expecting a "List_2D Cell -> Nat", the other side of (==). Now, numeric literals are overloaded to have type "Num a => a", so that could be a "List_2D Cell -> Nat" if you provide an instance "Num (List_2D Cell -> Nat)". It can't find that instance, and so it spits out the second error message.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.