我有這種類型的包裝功能如何爲封裝函數的類型編寫任意實例?
newtype Transaction d e r = Transaction (d -> Either e (d,r))
...我想爲它的函子&應用型實例做快速檢查測試,但編譯器抱怨說,它不具有任意實例。
我試圖這樣做,但我堅持生成隨機函數。
謝謝!
的快速檢查屬性這樣
type IdProperty f a = f a -> Bool
functorIdProp :: (Functor f, Eq (f a)) => IdProperty f a
functorIdProp x = (fmap id x) == id x
type CompositionProperty f a b c = f a -> Fun a b -> Fun b c -> Bool
functorCompProp :: (Functor f, Eq (f c)) => CompositionProperty f a b c
functorCompProp x (apply -> f) (apply -> g) = (fmap (g . f) x) == (fmap g . fmap f $ x)
instance (Arbitrary ((->) d (Either e (d, a)))) => Arbitrary (DbTr d e a) where
arbitrary = do
f <- ...???
return $ Transaction f
...和測試定義== == UPDATE看起來是這樣的:
spec = do
describe "Functor properties for (Transaction Int String)" $ do
it "IdProperty (Transaction Int String) Int" $ do
property (functorIdProp :: IdProperty (Transaction Int String) Int)
it "CompositionProperty (Transaction Int String) Int String Float" $ do
property (functorCompProp :: CompositionProperty (Transaction Int String) Int String Float)
[?也許這將是有用的(https://mail.haskell.org/pipermail/haskell- cafe/2010-September/083735.html)但是,我想想你會從這些隨機函數中獲得什麼。我會說,一些邊緣案例是這需要的一切。 –