Mercurial > hg > Members > atton > similar_monad
annotate similar.hs @ 19:003b6e58d815
Define Similar as Monad by mu
author | Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 22 Sep 2014 23:26:51 +0900 |
parents | c77397d0677f |
children | d4aa70d94352 |
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 |
18
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
2 import Data.Numbers.Primes -- $ cabal install primes |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
3 |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
4 data Similar a = Single [String] a | Similar [String] a [String] a deriving (Show) |
17
279ebcf670c4
Define Similar as Applicative
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
5 |
19
003b6e58d815
Define Similar as Monad by mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
18
diff
changeset
|
6 value :: (Similar a) -> Similar a |
003b6e58d815
Define Similar as Monad by mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
18
diff
changeset
|
7 value (Similar xs x _ _) = Single xs x |
003b6e58d815
Define Similar as Monad by mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
18
diff
changeset
|
8 value s = s |
14
116131b196bb
Define fmap and mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
9 |
116131b196bb
Define fmap and mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
10 instance (Eq a) => Eq (Similar a) where |
19
003b6e58d815
Define Similar as Monad by mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
18
diff
changeset
|
11 s == ss = (value s) == (value ss) |
18
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
12 |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
13 instance Functor Similar where |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
14 fmap f (Single xs x) = Single xs (f x) |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
15 fmap f (Similar xs x ys y) = Similar xs (f x) ys (f y) |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
16 |
19
003b6e58d815
Define Similar as Monad by mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
18
diff
changeset
|
17 mu :: Similar (Similar a) -> Similar a |
003b6e58d815
Define Similar as Monad by mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
18
diff
changeset
|
18 mu (Single ls (Single lx x)) = Single (ls ++ lx) x |
003b6e58d815
Define Similar as Monad by mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
18
diff
changeset
|
19 mu (Single ls (Similar lx x ly y)) = Similar (ls ++ lx) x (ls ++ ly) y |
003b6e58d815
Define Similar as Monad by mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
18
diff
changeset
|
20 mu (Similar lx (Single llx x) ly (Single lly y)) = Similar (lx ++ llx) x (ly ++ lly) y |
003b6e58d815
Define Similar as Monad by mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
18
diff
changeset
|
21 mu (Similar lx (Similar llx x _ _) ly (Similar _ _ lly y)) = Similar (lx ++ llx) x (lx ++ lly) y |
003b6e58d815
Define Similar as Monad by mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
18
diff
changeset
|
22 mu _ = error "Invalid Similar" |
18
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
23 |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
24 instance Monad Similar where |
19
003b6e58d815
Define Similar as Monad by mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
18
diff
changeset
|
25 return = Single [] |
003b6e58d815
Define Similar as Monad by mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
18
diff
changeset
|
26 s >>= f = mu $ fmap f s |
18
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
27 |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
28 |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
29 {- |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
30 |
10
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
31 |
18
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
32 |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
33 -- samples |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
34 {- |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
35 |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
36 generator :: Int -> Similar [Int] |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
37 generator x = return [1..x] |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
38 |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
39 primeFilter :: [Int] -> Similar [Int] |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
40 primeFilter xs = return $ filter isPrime xs |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
41 |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
42 count :: [Int] -> Similar Int |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
43 count xs = return $ length xs |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
44 |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
45 primeCount :: Int -> Int |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
46 primeCount x = value $ generator x >>= primeFilter >>= count |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
47 -} |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
48 |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
49 |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
50 {- |
13
88d6897c391a
Redefine Similar. reject function in data
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
12
diff
changeset
|
51 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
|
52 same (Single x) = x |
14
116131b196bb
Define fmap and mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
53 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
|
54 |
15 | 55 |
16
4b315cf0edb9
Improve mu definition
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
15
diff
changeset
|
56 similar :: Similar a -> Similar a -> Similar a |
4b315cf0edb9
Improve mu definition
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
15
diff
changeset
|
57 similar (Single x) ss = Similar x ss |
4b315cf0edb9
Improve mu definition
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
15
diff
changeset
|
58 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
|
59 |
13
88d6897c391a
Redefine Similar. reject function in data
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
12
diff
changeset
|
60 instance Functor Similar where |
88d6897c391a
Redefine Similar. reject function in data
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
12
diff
changeset
|
61 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
|
62 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
|
63 |
17
279ebcf670c4
Define Similar as Applicative
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
64 instance Applicative Similar where |
279ebcf670c4
Define Similar as Applicative
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
65 pure = Single |
279ebcf670c4
Define Similar as Applicative
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
66 (Single f) <*> s = fmap f s |
279ebcf670c4
Define Similar as Applicative
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
67 (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
|
68 |
15 | 69 mu :: (Similar (Similar a)) -> Similar a |
16
4b315cf0edb9
Improve mu definition
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
15
diff
changeset
|
70 mu (Single s) = s |
4b315cf0edb9
Improve mu definition
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
15
diff
changeset
|
71 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
|
72 |
11
e8a5df54480e
Define sample for Monad style
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
10
diff
changeset
|
73 |
0
7a82a5e50499
Initial commit. define to Functor for Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
74 |
16
4b315cf0edb9
Improve mu definition
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
15
diff
changeset
|
75 -- samples |
3
3c5fbce357af
Define >>= for Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
2
diff
changeset
|
76 |
14
116131b196bb
Define fmap and mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
77 double :: Int -> Similar Int |
116131b196bb
Define fmap and mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
78 double x = Single (2 * x) |
4
66609010d477
Define Similer example as Functor
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
79 |
14
116131b196bb
Define fmap and mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
80 twicePlus :: Int -> Similar Int |
116131b196bb
Define fmap and mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
81 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
|
82 |
14
116131b196bb
Define fmap and mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
83 plusTwo :: Int -> Similar Int |
116131b196bb
Define fmap and mu
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
13
diff
changeset
|
84 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
|
85 |
11
e8a5df54480e
Define sample for Monad style
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
10
diff
changeset
|
86 -- samples |
6 | 87 |
11
e8a5df54480e
Define sample for Monad style
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
10
diff
changeset
|
88 {- |
15 | 89 - Similar as Functor |
90 *Main> fmap (double ) (Single 1) | |
91 Single (Single 2) | |
92 *Main> fmap (twicePlus) (Single 1) | |
93 Single (Similar 2 (Single 2)) | |
94 *Main> fmap (plusTwo) (Single 1) | |
95 Single (Similar 3 (Single 2)) | |
96 *Main> fmap (fmap double) (fmap (plusTwo ) (Single 1)) | |
97 Single (Similar (Single 6) (Single (Single 4))) | |
98 *Main> same $ fmap same $ fmap (fmap double) (fmap (plusTwo ) (Single 1)) | |
99 *** Exception: same | |
100 *Main> same $ fmap same $ fmap (fmap double) (fmap (plusTwo ) (Single 2)) | |
101 Single 8 | |
6 | 102 |
17
279ebcf670c4
Define Similar as Applicative
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
103 - Similar as Applicative Functor |
279ebcf670c4
Define Similar as Applicative
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
104 *Main> Single (\x -> x * x) <*> Single 100 |
279ebcf670c4
Define Similar as Applicative
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
105 Single 10000 |
279ebcf670c4
Define Similar as Applicative
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
106 *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
|
107 Similar 10000 (Single 300) |
279ebcf670c4
Define Similar as Applicative
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
16
diff
changeset
|
108 *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
|
109 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
|
110 |
15 | 111 - Similar as Monad |
112 *Main> return 100 >>= double >>= twicePlus | |
113 Similar 400 (Single 400) | |
114 *Main> return 100 >>= double >>= twicePlus >>= plusTwo | |
115 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
|
116 |
15 | 117 *Main> same $ return 100 >>= double >>= twicePlus >>= plusTwo |
118 *** Exception: same | |
119 *Main> same $ return 100 >>= double >>= twicePlus | |
120 400 | |
121 | |
122 *Main> same $ return 100 >>= double >>= twicePlus | |
123 400 | |
16
4b315cf0edb9
Improve mu definition
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
15
diff
changeset
|
124 *Main> same $ return 100 >>= double >>= twicePlus >>= plusTwo |
15 | 125 *** Exception: same |
16
4b315cf0edb9
Improve mu definition
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
15
diff
changeset
|
126 *Main> value $ return 100 >>= double >>= twicePlus >>= plusTwo |
15 | 127 800 |
128 | |
10
7c7efee7891f
Define Monad style Similer
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
129 -} |
18
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
130 -} |
c77397d0677f
Try define Similar as Monad
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
17
diff
changeset
|
131 -} |