Mercurial > hg > Members > atton > delta_monad
annotate similar.hs @ 17:279ebcf670c4
Define Similar as Applicative
author | Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 09 Sep 2014 16:21:22 +0900 |
parents | 4b315cf0edb9 |
children | c77397d0677f |
rev | line source |
---|---|
17
279ebcf670c4
Define Similar as Applicative
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
1 import Control.Applicative |
279ebcf670c4
Define Similar as Applicative
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
2 |
14
116131b196bb
Define fmap and mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
3 data Similar a = Single a | Similar a (Similar a) deriving (Show) |
116131b196bb
Define fmap and mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
4 |
116131b196bb
Define fmap and mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
5 instance (Eq a) => Eq (Similar a) where |
116131b196bb
Define fmap and mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
6 s == ss = (same s) == (same ss) |
10
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
7 |
13
88d6897c391a
Redefine Similar. reject function in data
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
12
diff
changeset
|
8 same :: (Eq a) => Similar a -> a |
88d6897c391a
Redefine Similar. reject function in data
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
12
diff
changeset
|
9 same (Single x) = x |
14
116131b196bb
Define fmap and mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
10 same (Similar x s) = if x == (same s) then x else (error "same") |
10
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
11 |
15 | 12 value :: Similar a -> a |
13 value (Single x) = x | |
14 value (Similar x s) = value s | |
15 | |
16
4b315cf0edb9
Improve mu definition
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
15
diff
changeset
|
16 similar :: Similar a -> Similar a -> Similar a |
4b315cf0edb9
Improve mu definition
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
15
diff
changeset
|
17 similar (Single x) ss = Similar x ss |
4b315cf0edb9
Improve mu definition
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
15
diff
changeset
|
18 similar (Similar x s) ss = Similar x (similar s ss) |
10
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
19 |
13
88d6897c391a
Redefine Similar. reject function in data
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
12
diff
changeset
|
20 instance Functor Similar where |
88d6897c391a
Redefine Similar. reject function in data
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
12
diff
changeset
|
21 fmap f (Single a) = Single (f a) |
88d6897c391a
Redefine Similar. reject function in data
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
12
diff
changeset
|
22 fmap f (Similar a s) = Similar (f a) (fmap f s) |
88d6897c391a
Redefine Similar. reject function in data
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
12
diff
changeset
|
23 |
17
279ebcf670c4
Define Similar as Applicative
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
24 instance Applicative Similar where |
279ebcf670c4
Define Similar as Applicative
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
25 pure = Single |
279ebcf670c4
Define Similar as Applicative
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
26 (Single f) <*> s = fmap f s |
279ebcf670c4
Define Similar as Applicative
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
27 (Similar f s) <*> ss = similar (fmap f ss) (s <*> ss) |
279ebcf670c4
Define Similar as Applicative
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
28 |
15 | 29 mu :: (Similar (Similar a)) -> Similar a |
16
4b315cf0edb9
Improve mu definition
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
15
diff
changeset
|
30 mu (Single s) = s |
4b315cf0edb9
Improve mu definition
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
15
diff
changeset
|
31 mu (Similar s ss) = similar s (mu ss) |
10
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
32 |
14
116131b196bb
Define fmap and mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
33 instance Monad Similar where |
116131b196bb
Define fmap and mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
34 return = Single |
116131b196bb
Define fmap and mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
35 (Single x) >>= f = f x |
116131b196bb
Define fmap and mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
36 (Similar x s) >>= f = mu $ Similar (f x) (fmap f s) |
11
e8a5df54480e
Define sample for Monad style
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
10
diff
changeset
|
37 |
0
7a82a5e50499
Initial commit. define to Functor for Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
38 |
16
4b315cf0edb9
Improve mu definition
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
15
diff
changeset
|
39 -- samples |
3
3c5fbce357af
Define >>= for Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
2
diff
changeset
|
40 |
14
116131b196bb
Define fmap and mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
41 double :: Int -> Similar Int |
116131b196bb
Define fmap and mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
42 double x = Single (2 * x) |
4
66609010d477
Define Similer example as Functor
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
43 |
14
116131b196bb
Define fmap and mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
44 twicePlus :: Int -> Similar Int |
116131b196bb
Define fmap and mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
45 twicePlus x = Similar (x + x) (double x) |
4
66609010d477
Define Similer example as Functor
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
46 |
14
116131b196bb
Define fmap and mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
47 plusTwo :: Int -> Similar Int |
116131b196bb
Define fmap and mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
48 plusTwo x = Similar (x + 2) (double x) |
4
66609010d477
Define Similer example as Functor
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
49 |
11
e8a5df54480e
Define sample for Monad style
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
10
diff
changeset
|
50 -- samples |
6 | 51 |
11
e8a5df54480e
Define sample for Monad style
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
10
diff
changeset
|
52 {- |
15 | 53 - Similar as Functor |
54 *Main> fmap (double ) (Single 1) | |
55 Single (Single 2) | |
56 *Main> fmap (twicePlus) (Single 1) | |
57 Single (Similar 2 (Single 2)) | |
58 *Main> fmap (plusTwo) (Single 1) | |
59 Single (Similar 3 (Single 2)) | |
60 *Main> fmap (fmap double) (fmap (plusTwo ) (Single 1)) | |
61 Single (Similar (Single 6) (Single (Single 4))) | |
62 *Main> same $ fmap same $ fmap (fmap double) (fmap (plusTwo ) (Single 1)) | |
63 *** Exception: same | |
64 *Main> same $ fmap same $ fmap (fmap double) (fmap (plusTwo ) (Single 2)) | |
65 Single 8 | |
6 | 66 |
17
279ebcf670c4
Define Similar as Applicative
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
67 - Similar as Applicative Functor |
279ebcf670c4
Define Similar as Applicative
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
68 *Main> Single (\x -> x * x) <*> Single 100 |
279ebcf670c4
Define Similar as Applicative
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
69 Single 10000 |
279ebcf670c4
Define Similar as Applicative
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
70 *Main> Similar (\x -> x * x) (Single (\x -> x * 3)) <*> Single 100 |
279ebcf670c4
Define Similar as Applicative
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
71 Similar 10000 (Single 300) |
279ebcf670c4
Define Similar as Applicative
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
72 *Main> Similar (\x -> x * x) (Single (\x -> x * 3)) <*> (Similar 100 (Single 200)) |
279ebcf670c4
Define Similar as Applicative
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
73 Similar 10000 (Similar 40000 (Similar 300 (Single 600))) |
279ebcf670c4
Define Similar as Applicative
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
74 |
15 | 75 - Similar as Monad |
76 *Main> return 100 >>= double >>= twicePlus | |
77 Similar 400 (Single 400) | |
78 *Main> return 100 >>= double >>= twicePlus >>= plusTwo | |
79 Similar 402 (Similar 800 (Similar 402 (Single 800))) | |
11
e8a5df54480e
Define sample for Monad style
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
10
diff
changeset
|
80 |
15 | 81 *Main> same $ return 100 >>= double >>= twicePlus >>= plusTwo |
82 *** Exception: same | |
83 *Main> same $ return 100 >>= double >>= twicePlus | |
84 400 | |
85 | |
86 *Main> same $ return 100 >>= double >>= twicePlus | |
87 400 | |
16
4b315cf0edb9
Improve mu definition
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
15
diff
changeset
|
88 *Main> same $ return 100 >>= double >>= twicePlus >>= plusTwo |
15 | 89 *** Exception: same |
16
4b315cf0edb9
Improve mu definition
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
15
diff
changeset
|
90 *Main> value $ return 100 >>= double >>= twicePlus >>= plusTwo |
15 | 91 800 |
92 | |
10
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
93 -} |
12
158ae705cd16
Rename Similer -> Similar
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
11
diff
changeset
|
94 |