comparison similer.hs @ 9:41c71f67c103

Show ErrorMessage
author Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
date Tue, 02 Sep 2014 11:48:53 +0900
parents 6e0285628ead
children 7c7efee7891f
comparison
equal deleted inserted replaced
8:6e0285628ead 9:41c71f67c103
1 {-# LANGUAGE UndecidableInstances #-}
2 data Similer a b = Similer a (a -> b) b 1 data Similer a b = Similer a (a -> b) b
3 2
4 instance Functor (Similer a) where 3 instance Functor (Similer a) where
5 fmap g (Similer a f b) = Similer a (g . f) $ g b 4 fmap g (Similer a f b) = Similer a (g . f) $ g b
6 5
20 twicePlus x = x + x 19 twicePlus x = x + x
21 20
22 plusTwo :: Int -> Int 21 plusTwo :: Int -> Int
23 plusTwo x = x + 2 22 plusTwo x = x + 2
24 23
25 same :: Eq b => Similer a b -> b 24 same :: (Show b, Eq b) => Similer a b -> b
26 same (Similer x f y) = if (f x) == y then y else undefined 25 same (Similer x f y) = if (f x) == y then y else (error ("not same :" ++ show y))
27 26
28 similer :: Eq b => (a -> b) -> (a -> b) -> a -> b 27 similer :: (Show b, Eq b) => (a -> b) -> (a -> b) -> a -> b
29 similer f g x = same $ Similer x g (f x) 28 similer f g x = same $ Similer x g (f x)
30 29
31 30
32 -- samples 31 -- samples
33 sameExample = map (similer twicePlus double) [1..10] 32 sameExample = map (similer twicePlus double) [1..10]