【发布时间】:2017-01-02 00:23:42
【问题描述】:
我定义了以下类:
class Configuration c where
input :: (PortIn pin, Show pin) => c -> pin
startSt :: (SysState st, Show st) => c -> st
class (Eq pin, Show pin) => PortIn pin
class (Eq st, Show st) => SysState st
以下类型和实例:
--inputs
data PIn = PIn { _clk :: Bit
, _reset :: Bool
, _start :: Bool
, _stop :: Bool
} deriving (Eq)
instance PortIn PIn
instance Show PIn where
show PIn {..} =
"PIn\n\t _clk = " P.++ show _clk
P.++ "\n\t _reset = " P.++ show _reset
P.++ "\n\t _start = " P.++ show _start
P.++ "\n\t _stop = " P.++ show _stop
--Outputs and state data
data St = St { _cnt_en :: Bool
, _count_us :: BitVector 4
, _stop_d1 :: Bool
, _stop_d2 :: Bool
, _count :: BitVector 4
} deriving (Eq)
instance SysState St
instance Show St where
show St {..} =
"St\n\t _cnt_en = " P.++ show _cnt_en
P.++ "\n\t _count_us = " P.++ show _count_us
P.++ "\n\t _stop_d1 = " P.++ show _stop_d1
P.++ "\n\t _stop_d2 = " P.++ show _stop_d2
P.++ "\n\t _count = " P.++ show _count
为什么我无法在此处创建配置实例:
data Config = Config { input' :: PIn
, startSt' :: St
} deriving (Eq)
instance Show Config where
show Config {..} =
"Config:\n input = " P.++ show input'
P.++ "\n startSt = " P.++ show startSt'
instance Configuration Config where
input = input'
startSt = startSt'
我收到以下错误:
Couldn't match type ‘pin’ with ‘PIn’
‘pin’ is a rigid type variable bound by
the type signature for
input :: (PortIn pin, Show pin) => Config -> pin
at Clks_n_regs_4.hs:101:3
Expected type: Config -> pin
Actual type: Config -> PIn
Relevant bindings include
input :: Config -> pin (bound at Clks_n_regs_4.hs:101:3)
In the expression: input'
In an equation for ‘input’: input = input'
我认为我可以使用 input',因为它会生成一个 PIn,它是 pin 的一个实例。
我在某处有误解,希望有人能解释我所缺少的。
【问题讨论】:
-
这看起来很像您试图将 OO 类压缩到 Haskell 中。你为什么需要这个(或认为你需要它)?
-
我有几个版本的类型:Config、PIn、St 和一个名为
runOneTest的函数。使用类型和函数的代码是相同的。runOneTest的类型签名将始终为runOneTest :: Config -> Signal TestResult我希望能够重用所有其他调用runOneTest函数的代码。我昨天在 CodeReview 上发布了这个,但还没有结果。 [codereview.stackexchange.com/questions/151278/…
标签: haskell