Mercurial > hg > Members > atton > similar_monad
annotate similer.hs @ 8:6e0285628ead
Define similer function
author | Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 02 Sep 2014 11:27:49 +0900 |
parents | 5e367a167382 |
children | 41c71f67c103 |
rev | line source |
---|---|
3
3c5fbce357af
Define >>= for Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
2
diff
changeset
|
1 {-# LANGUAGE UndecidableInstances #-} |
2 | 2 data Similer a b = Similer a (a -> b) b |
0
7a82a5e50499
Initial commit. define to Functor for Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
3 |
2 | 4 instance Functor (Similer a) where |
0
7a82a5e50499
Initial commit. define to Functor for Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
5 fmap g (Similer a f b) = Similer a (g . f) $ g b |
7a82a5e50499
Initial commit. define to Functor for Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6 |
5
051c663a4af2
Rename some functions
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
7 eq :: (Eq a) => Similer a b -> Similer a b -> Bool |
051c663a4af2
Rename some functions
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
8 eq (Similer a _ _ ) (Similer b _ _) = a == b |
3
3c5fbce357af
Define >>= for Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
2
diff
changeset
|
9 |
2 | 10 eta :: a -> Similer a a |
0
7a82a5e50499
Initial commit. define to Functor for Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
11 eta a = Similer a id a |
7a82a5e50499
Initial commit. define to Functor for Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
12 |
3
3c5fbce357af
Define >>= for Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
2
diff
changeset
|
13 mu :: (Eq b) => Similer a (Similer b c) -> Similer b c |
5
051c663a4af2
Rename some functions
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
14 mu (Similer a f b) = if (eq (f a) b) then b else undefined |
3
3c5fbce357af
Define >>= for Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
2
diff
changeset
|
15 |
8
6e0285628ead
Define similer function
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
16 double :: Int -> Int |
6e0285628ead
Define similer function
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
17 double x = (2 * x) |
4
66609010d477
Define Similer example as Functor
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
18 |
8
6e0285628ead
Define similer function
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
19 twicePlus :: Int -> Int |
6e0285628ead
Define similer function
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
20 twicePlus x = x + x |
4
66609010d477
Define Similer example as Functor
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
21 |
8
6e0285628ead
Define similer function
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
22 plusTwo :: Int -> Int |
6e0285628ead
Define similer function
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
23 plusTwo x = x + 2 |
4
66609010d477
Define Similer example as Functor
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
24 |
5
051c663a4af2
Rename some functions
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
25 same :: Eq b => Similer a b -> b |
051c663a4af2
Rename some functions
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
4
diff
changeset
|
26 same (Similer x f y) = if (f x) == y then y else undefined |
6 | 27 |
8
6e0285628ead
Define similer function
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
28 similer :: Eq b => (a -> b) -> (a -> b) -> a -> b |
6e0285628ead
Define similer function
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
29 similer f g x = same $ Similer x g (f x) |
6e0285628ead
Define similer function
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
30 |
6 | 31 |
32 -- samples | |
8
6e0285628ead
Define similer function
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
33 sameExample = map (similer twicePlus double) [1..10] |
6e0285628ead
Define similer function
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
34 nonSameExample = map (similer twicePlus plusTwo) [1..10] |
6e0285628ead
Define similer function
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
35 nonSameExampleSpecific = map (similer twicePlus plusTwo) [2] |