Mercurial > hg > Members > atton > delta_monad
comparison delta.hs @ 82:1339772b2e36
Define DeltaM. Delta with Monad
author | Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 04 Jan 2015 16:31:17 +0900 |
parents | 0ad0ae7a3cbe |
children | 6635a513f81a |
comparison
equal
deleted
inserted
replaced
81:47317adefa16 | 82:1339772b2e36 |
---|---|
46 | 46 |
47 instance Monad Delta where | 47 instance Monad Delta where |
48 return x = Mono x | 48 return x = Mono x |
49 d >>= f = mu $ fmap f d | 49 d >>= f = mu $ fmap f d |
50 | 50 |
51 | |
52 | |
53 -- utils | 51 -- utils |
54 | 52 |
55 returnS :: (Show s) => s -> Delta s | 53 returnS :: (Show s) => s -> Delta s |
56 returnS x = Mono x | 54 returnS x = Mono x |
57 | 55 |
87 where | 85 where |
88 maximumValue = maximum xs | 86 maximumValue = maximum xs |
89 sortedValueL = maximumValue | 87 sortedValueL = maximumValue |
90 sortedValueR = replicate (length $ filter (== maximumValue) xs) maximumValue | 88 sortedValueR = replicate (length $ filter (== maximumValue) xs) maximumValue |
91 remainValue = filter (/= maximumValue) xs | 89 remainValue = filter (/= maximumValue) xs |
90 | |
91 -- DeltaM Definition (Delta with Monad) | |
92 | |
93 data DeltaM m a = DeltaM (Delta (m a)) deriving (Show) | |
94 | |
95 | |
96 -- DeltaM utils | |
97 | |
98 headDeltaM :: DeltaM m a -> m a | |
99 headDeltaM (DeltaM (Mono x)) = x | |
100 headDeltaM (DeltaM (Delta x _ )) = x | |
101 | |
102 tailDeltaM :: DeltaM m a -> DeltaM m a | |
103 tailDeltaM d@(DeltaM (Mono _)) = d | |
104 tailDeltaM (DeltaM (Delta _ d)) = DeltaM d | |
105 | |
106 appendDeltaM :: DeltaM m a -> DeltaM m a -> DeltaM m a | |
107 appendDeltaM (DeltaM d) (DeltaM dd) = DeltaM (deltaAppend d dd) | |
108 | |
109 | |
110 -- DeltaM instance definitions | |
111 | |
112 instance (Functor m) => Functor (DeltaM m) where | |
113 fmap f (DeltaM d) = DeltaM $ fmap (fmap f) d | |
114 | |
115 instance (Applicative m) => Applicative (DeltaM m) where | |
116 pure f = DeltaM $ Mono $ pure f | |
117 (DeltaM (Mono f)) <*> (DeltaM (Mono x)) = DeltaM $ Mono $ f <*> x | |
118 df@(DeltaM (Mono f)) <*> (DeltaM (Delta x d)) = appendDeltaM (DeltaM $ Mono $ f <*> x) (df <*> (DeltaM d)) | |
119 (DeltaM (Delta f df)) <*> dx@(DeltaM (Mono x)) = appendDeltaM (DeltaM $ Mono $ f <*> x) ((DeltaM df) <*> dx) | |
120 (DeltaM (Delta f df)) <*> (DeltaM (Delta x dx)) = appendDeltaM (DeltaM $ Mono $ f <*> x) ((DeltaM df) <*> (DeltaM dx)) | |
121 | |
122 instance (Monad m) => Monad (DeltaM m) where | |
123 return x = DeltaM $ Mono $ return x | |
124 (DeltaM (Mono x)) >>= f = DeltaM $ Mono $ (x >>= headDeltaM . f) | |
125 (DeltaM (Delta x d)) >>= f = appendDeltaM ((DeltaM $ Mono x) >>= f) | |
126 ((DeltaM d) >>= tailDeltaM . f) | |
127 | |
128 | |
129 -- DeltaM examples | |
130 | |
131 val :: DeltaM [] Int | |
132 val = DeltaM $ deltaFromList [[10, 20], [1, 2, 3], [100,200,300], [0]] | |
133 | |
134 func :: Int -> DeltaM [] Int | |
135 func x = DeltaM $ deltaFromList [[x+1, x+2, x+3], | |
136 [x*x], | |
137 [x, x, x, x]] |