Mercurial > hg > Members > atton > similar_monad
annotate similer.hs @ 10:7c7efee7891f
Define Monad style Similer
author | Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 02 Sep 2014 16:12:34 +0900 |
parents | 41c71f67c103 |
children | e8a5df54480e |
rev | line source |
---|---|
10
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
1 {-# LANGUAGE GADTs, MultiParamTypeClasses #-} |
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
2 |
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
3 data Similer a = (Eq a) => Similer a (a -> a) a |
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
4 |
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
5 instance (Eq a) => Eq (Similer a) where |
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
6 s == ss = same s == same ss |
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
7 |
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
8 same :: Similer a -> a |
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
9 same (Similer x f y) = if (f x) == y then y else undefined |
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
10 |
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
11 mu :: (Eq a) => Similer (Similer a) -> Similer a |
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
12 mu (Similer a f b) = if ((f a) == b) then b else undefined |
0
7a82a5e50499
Initial commit. define to Functor for Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
13 |
10
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
14 class EqFunctor f where |
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
15 eqmap :: (Eq a, Eq b) => (a -> b) -> f a -> f b |
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
16 |
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
17 instance EqFunctor Similer where |
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
18 eqmap f s = Similer fs id fs |
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
19 where fs = f $ same s |
0
7a82a5e50499
Initial commit. define to Functor for Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
20 |
10
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
21 class EqMonad m where |
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
22 (>>=) :: (Eq a, Eq b) => m a -> (a -> m b) -> m b |
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
23 return ::(Eq a) => a -> m a |
3
3c5fbce357af
Define >>= for Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
2
diff
changeset
|
24 |
10
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
25 instance EqMonad Similer where |
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
26 return x = Similer x id x |
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
27 s >>= f = mu (eqmap f s) |
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
28 |
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
29 {- |
2 | 30 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
|
31 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
|
32 |
3
3c5fbce357af
Define >>= for Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
2
diff
changeset
|
33 |
8
6e0285628ead
Define similer function
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
34 double :: Int -> Int |
6e0285628ead
Define similer function
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
35 double x = (2 * x) |
4
66609010d477
Define Similer example as Functor
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
36 |
8
6e0285628ead
Define similer function
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
37 twicePlus :: Int -> Int |
6e0285628ead
Define similer function
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
38 twicePlus x = x + x |
4
66609010d477
Define Similer example as Functor
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
39 |
8
6e0285628ead
Define similer function
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
40 plusTwo :: Int -> Int |
6e0285628ead
Define similer function
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
41 plusTwo x = x + 2 |
4
66609010d477
Define Similer example as Functor
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
42 |
6 | 43 |
9 | 44 similer :: (Show b, Eq b) => (a -> b) -> (a -> b) -> a -> b |
8
6e0285628ead
Define similer function
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
45 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
|
46 |
6 | 47 |
48 -- samples | |
8
6e0285628ead
Define similer function
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
49 sameExample = map (similer twicePlus double) [1..10] |
6e0285628ead
Define similer function
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
50 nonSameExample = map (similer twicePlus plusTwo) [1..10] |
6e0285628ead
Define similer function
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
51 nonSameExampleSpecific = map (similer twicePlus plusTwo) [2] |
10
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
52 -} |