无法使用数据变量定义实例方法
Can't define instance method with data variable
起初,我试图解决相互递归模块问题。但是,我解决这个问题的努力使代码变得更糟。
原题
我有两个模块像
module W where
import T
data W = W { wName :: String
, queue :: [T W]}`
和
module T where
import W
data T = T { tName :: String
, tUpdate :: W -> IO W}
class Manage a where
update :: a -> W -> IO W
instance Manage T where
update = tUpdate
由于相互递归模块问题,这些模块无法工作。
所以我改了module T
,根据this link
带有数据变量的新解决方案
module T where
data T w = T { tName :: String
, tUpdate :: w -> IO w}
class Manage a where
update :: a -> w -> IO w
instance Manage (T w) where
update = tUpdate
但是,此代码会产生如下错误:
Couldn't match type ‘w’ with ‘w1’
‘w’ is a rigid type variable bound by
the instance declaration at TestModule2.hs:12:10
‘w1’ is a rigid type variable bound by
the type signature for update :: T w -> w1 -> IO w1
at TestModule2.hs:13:5
Expected type: T w -> w1 -> IO w1
Actual type: T w -> w -> IO w
Relevant bindings include
update :: T w -> w1 -> IO w1 (bound at TestModule2.hs:13:5)
In the expression: tUpdate
In an equation for ‘update’: update = tUpdate
所以,我尝试了很多方法来解决这个问题,比如
引入新的class像State w
来代表data W
为了解决rigid type variable ...
,我引入了新的class。
{-# LANGUAGE InstanceSigs, Rank2Types #-}
module T where
.....
data T w = T { tName :: String
, tUpdate :: (State w) => w -> IO w}
class Manage a where
update :: (State w) => a -> (w -> IO w)
instance (State w) => Manage (T w) where
update :: T w -> w -> IO w
update = tUpdate
但是这段代码也会出现如下错误:
Method signature does not match class; it should be
update :: forall w1. State w1 => T w -> w1 -> IO w1
In the instance declaration for ‘Manage (T w)’
我听从了这个建议,但这不起作用。
此外,w1
和 w
应该是同一类型。
我需要用其他方式定义 class 或 class 方法吗?
那么,我需要学习什么,我该如何解决这个问题?
我尝试了更多的东西,但我无法解决这个问题。
我需要研究什么来解决这个问题?
我该如何解决这个问题?
要点:
- 我需要将这两个模块分开。
- 我尽量不要用hs-boot,而是更优雅的方式
根据 user2407038 评论,一种解决方案可以是:
模块 T:
module T where
data T w = T { tName :: String
, tUpdate :: w -> IO w
}
class Manage a where
update :: a w -> w -> IO w
instance Manage T where
update = tUpdate
模块 W:
module W where
import T
data W = W { wName :: String
, queue :: [T W]
}
起初,我试图解决相互递归模块问题。但是,我解决这个问题的努力使代码变得更糟。
原题
我有两个模块像
module W where
import T
data W = W { wName :: String
, queue :: [T W]}`
和
module T where
import W
data T = T { tName :: String
, tUpdate :: W -> IO W}
class Manage a where
update :: a -> W -> IO W
instance Manage T where
update = tUpdate
由于相互递归模块问题,这些模块无法工作。
所以我改了module T
,根据this link
带有数据变量的新解决方案
module T where
data T w = T { tName :: String
, tUpdate :: w -> IO w}
class Manage a where
update :: a -> w -> IO w
instance Manage (T w) where
update = tUpdate
但是,此代码会产生如下错误:
Couldn't match type ‘w’ with ‘w1’
‘w’ is a rigid type variable bound by
the instance declaration at TestModule2.hs:12:10
‘w1’ is a rigid type variable bound by
the type signature for update :: T w -> w1 -> IO w1
at TestModule2.hs:13:5
Expected type: T w -> w1 -> IO w1
Actual type: T w -> w -> IO w
Relevant bindings include
update :: T w -> w1 -> IO w1 (bound at TestModule2.hs:13:5)
In the expression: tUpdate
In an equation for ‘update’: update = tUpdate
所以,我尝试了很多方法来解决这个问题,比如
引入新的class像State w
来代表data W
为了解决rigid type variable ...
,我引入了新的class。
{-# LANGUAGE InstanceSigs, Rank2Types #-}
module T where
.....
data T w = T { tName :: String
, tUpdate :: (State w) => w -> IO w}
class Manage a where
update :: (State w) => a -> (w -> IO w)
instance (State w) => Manage (T w) where
update :: T w -> w -> IO w
update = tUpdate
但是这段代码也会出现如下错误:
Method signature does not match class; it should be
update :: forall w1. State w1 => T w -> w1 -> IO w1
In the instance declaration for ‘Manage (T w)’
我听从了这个建议,但这不起作用。
此外,w1
和 w
应该是同一类型。
我需要用其他方式定义 class 或 class 方法吗?
那么,我需要学习什么,我该如何解决这个问题?
我尝试了更多的东西,但我无法解决这个问题。 我需要研究什么来解决这个问题? 我该如何解决这个问题?
要点:
- 我需要将这两个模块分开。
- 我尽量不要用hs-boot,而是更优雅的方式
根据 user2407038 评论,一种解决方案可以是:
模块 T:
module T where
data T w = T { tName :: String
, tUpdate :: w -> IO w
}
class Manage a where
update :: a w -> w -> IO w
instance Manage T where
update = tUpdate
模块 W:
module W where
import T
data W = W { wName :: String
, queue :: [T W]
}