[英]Add an instance declaration mistake in Haskell
我是Haskell的新手。 這是我編寫的代碼:
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
[] -> []
嘗試編譯時,GHCi給了我這個錯誤:
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
無法理解我想要什么...我在哪里犯了錯誤? 我該如何解決?
額外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
額外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
“為...添加實例聲明”是GHC最具誤導性的錯誤消息之一。 通常,您應該忽略該建議。
如果它想要實例的類型在其中帶有->
,這基本上表明您正在嘗試使用只能通過result實現的函數,例如,您已經忘記將該函數應用於其所有參數。 顯然, element_occurence
的類型類似於Item -> List_2D Cell -> Nat
,但是您的代碼中只有element_occurrence Head
,這是部分應用的函數 ,它仍然具有List_2D Cell -> Nat
類型。 通過將其應用於List_2D Cell
,您將獲得一個Nat
,然后可以將其與數字文字進行比較。
您已將element_occurence定義為2個參數的函數:
element_occurrence :: Eq e => e -> List_2D e -> Nat
element_occurrence element list = -- more stuff
您僅使用一個參數調用了element_occurence:
transition_world world = case world of
{- other cases -}
(Conductor, (x,y)):cs
| element_occurrence Head == 1 || element_occurrence Head == 2 -> -- stuff
但是,這為您提供了一個類型為“ List_2D Cell-> Nat”的函數類型的值,您嘗試對“ 1”和“ 2”進行相等性測試。
您需要做的是為element_occurence提供兩個參數。
GHC在這里太屈從了,假設您真的打算只用一個參數調用。 (==)的類型為“ Eq a => a-> a-> Bool”,因此,如果您提供了實例“ Eq(List_2D Cell-> Nat)”,則對(==)的調用是可以的,但是可以找不到那個實例。 因此,它將打印第一條錯誤消息。
GHC的數字,你真棒 ,所以你會解決這個錯誤馬上和假設下的收益,它是固定的。 但是現在它在期望“ List_2D單元格-> Nat”(另一邊是==)的位置看到了“ 1”和“ 2”。 現在,數字文字被重載為類型“ Num a => a”,因此如果您提供實例“ Num(List_2D Cell-> Nat)”,則該數字文字可能是“ List_2D Cell-> Nat”。 它找不到該實例,因此它吐出第二條錯誤消息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.