Yasutaka Higa
data Similar a b = Similar a (a -> b) b
data Similar a a = Similar a (a -> a) a
initObj = Similar 100 id 100
initObj >>= \x -> Similar x f (g x) >>= ...
f と g の diff があったら »= の時点で怒って欲しい
data Similar a b = Similar a (a -> b) b
instance Functor (Similar a) where
fmap g (Similar a f b) = Similar a (g . f) $ g b
instance Monad (Similar a) where
return x = Similar x id x -- NG
s >>= f = mu (eqmap f s)
similar :: (Show b, Eq b) => (a -> b) -> (a -> b) -> a -> b
similar f g x = same $ Similar x g (f x)
-- samples
ok = map (similar twicePlus double) [1..10]
notOk = map (similar twicePlus plusTwo) [1..10]
okSpecific = map (similar twicePlus plusTwo) [2]
data Similar a b = Similar a (a -> b) b
instance EqMonad Similar where
return x = Similar x id x
s >>= f = mu (eqmap f s)
class EqFunctor f where
eqmap :: (Eq a, Eq b) => (a -> b) -> f a -> f b
instance EqFunctor Similar where
eqmap f s = Similar fs id fs
where fs = f $ same s
same $ return 100 >>= (\x -> Similar x twicePlus $ double x)
200
same $ return 2 >>= (\x -> Similar x plusTwo $ double x)
4
same $ return 100 >>= (\x -> Similar x plusTwo $ double x)
*** Exception: Prelude.undefined