# Ticket #3335: ApplicativeMethods.2.patch

File ApplicativeMethods.2.patch, 7.2 KB (added by , 8 years ago) |
---|

Line | |
---|---|

1 | Mon Jun 29 12:34:36 BST 2009 Ross Paterson <ross@soi.city.ac.uk> |

2 | * make some Applicative functions into methods, and split off Data.Functor |

3 | |

4 | The following functions |

5 | |

6 | (<$) :: Functor f => a -> f b -> f a |

7 | (*>) :: Applicative f => f a -> f b -> f b |

8 | (<*) :: Applicative f => f a -> f b -> f a |

9 | some :: Alternative f => f a -> f [a] |

10 | many :: Alternative f => f a -> f [a] |

11 | |

12 | are moved into the corresponding classes, with the existing implementations |

13 | as default definitions. This gives people creating instances the option of |

14 | defining specialized implementations of these functions, though they should |

15 | be equivalent to the default definitions. |

16 | |

17 | Although (<$) is now a method of the Functor class, it is hidden in the |

18 | re-export by the Prelude, Control.Monad and Monad. The new module |

19 | Data.Functor exposes the full class, plus the function (<$>). These are |

20 | also re-exported by Control.Applicative. |

21 | |

22 | New patches: |

23 | |

24 | [make some Applicative functions into methods, and split off Data.Functor |

25 | Ross Paterson <ross@soi.city.ac.uk>**20090629113436 |

26 | Ignore-this: f9525a9ed3e44ec844448abafe32e15e |

27 | |

28 | The following functions |

29 | |

30 | (<$) :: Functor f => a -> f b -> f a |

31 | (*>) :: Applicative f => f a -> f b -> f b |

32 | (<*) :: Applicative f => f a -> f b -> f a |

33 | some :: Alternative f => f a -> f [a] |

34 | many :: Alternative f => f a -> f [a] |

35 | |

36 | are moved into the corresponding classes, with the existing implementations |

37 | as default definitions. This gives people creating instances the option of |

38 | defining specialized implementations of these functions, though they should |

39 | be equivalent to the default definitions. |

40 | |

41 | Although (<$) is now a method of the Functor class, it is hidden in the |

42 | re-export by the Prelude, Control.Monad and Monad. The new module |

43 | Data.Functor exposes the full class, plus the function (<$>). These are |

44 | also re-exported by Control.Applicative. |

45 | ] { |

46 | addfile ./Data/Functor.hs |

47 | hunk ./Control/Applicative.hs 33 |

48 | -- * Instances |

49 | Const(..), WrappedMonad(..), WrappedArrow(..), ZipList(..), |

50 | -- * Utility functions |

51 | - (<$>), (<$), (*>), (<*), (<**>), |

52 | + (<$>), (<$), (<**>), |

53 | liftA, liftA2, liftA3, |

54 | hunk ./Control/Applicative.hs 35 |

55 | - optional, some, many |

56 | + optional, |

57 | ) where |

58 | |

59 | import Prelude hiding (id,(.)) |

60 | hunk ./Control/Applicative.hs 46 |

61 | (Arrow(arr, (&&&)), ArrowZero(zeroArrow), ArrowPlus((<+>))) |

62 | import Control.Monad (liftM, ap, MonadPlus(..)) |

63 | import Control.Monad.Instances () |

64 | +import Data.Functor ((<$>), (<$)) |

65 | import Data.Monoid (Monoid(..)) |

66 | |

67 | infixl 3 <|> |

68 | hunk ./Control/Applicative.hs 50 |

69 | -infixl 4 <$>, <$ |

70 | infixl 4 <*>, <*, *>, <**> |

71 | |

72 | -- | A functor with application. |

73 | hunk ./Control/Applicative.hs 68 |

74 | -- [/interchange/] |

75 | -- @u '<*>' 'pure' y = 'pure' ('$' y) '<*>' u@ |

76 | -- |

77 | +-- [/ignore left value/] |

78 | +-- @u '*>' v = 'pure' ('const' 'id') '<*>' u '<*>' v@ |

79 | +-- |

80 | +-- [/ignore right value/] |

81 | +-- @u '<*' v = 'pure' 'const' '<*>' u '<*>' v@ |

82 | +-- |

83 | -- The 'Functor' instance should satisfy |

84 | -- |

85 | -- @ |

86 | hunk ./Control/Applicative.hs 81 |

87 | -- @ |

88 | -- |

89 | -- If @f@ is also a 'Monad', define @'pure' = 'return'@ and @('<*>') = 'ap'@. |

90 | +-- |

91 | +-- Minimal complete definition: 'pure' and '<*>'. |

92 | |

93 | class Functor f => Applicative f where |

94 | -- | Lift a value. |

95 | hunk ./Control/Applicative.hs 91 |

96 | -- | Sequential application. |

97 | (<*>) :: f (a -> b) -> f a -> f b |

98 | |

99 | + -- | Sequence actions, discarding the value of the first argument. |

100 | + (*>) :: f a -> f b -> f b |

101 | + (*>) = liftA2 (const id) |

102 | + |

103 | + -- | Sequence actions, discarding the value of the second argument. |

104 | + (<*) :: f a -> f b -> f a |

105 | + (<*) = liftA2 const |

106 | + |

107 | -- | A monoid on applicative functors. |

108 | hunk ./Control/Applicative.hs 100 |

109 | +-- |

110 | +-- Minimal complete definition: 'empty' and '<|>'. |

111 | +-- |

112 | +-- 'some' and 'many' should be the least solutions of the equations: |

113 | +-- |

114 | +-- * @some v = (:) '<$>' v '<*>' many v@ |

115 | +-- |

116 | +-- * @many v = some v '<|>' 'pure' []@ |

117 | class Applicative f => Alternative f where |

118 | -- | The identity of '<|>' |

119 | empty :: f a |

120 | hunk ./Control/Applicative.hs 114 |

121 | -- | An associative binary operation |

122 | (<|>) :: f a -> f a -> f a |

123 | |

124 | + -- | One or more. |

125 | + some :: f a -> f [a] |

126 | + some v = some_v |

127 | + where many_v = some_v <|> pure [] |

128 | + some_v = (:) <$> v <*> many_v |

129 | + |

130 | + -- | Zero or more. |

131 | + many :: f a -> f [a] |

132 | + many v = many_v |

133 | + where many_v = some_v <|> pure [] |

134 | + some_v = (:) <$> v <*> many_v |

135 | + |

136 | -- instances for Prelude types |

137 | |

138 | instance Applicative Maybe where |

139 | hunk ./Control/Applicative.hs 209 |

140 | |

141 | -- extra functions |

142 | |

143 | --- | A synonym for 'fmap'. |

144 | -(<$>) :: Functor f => (a -> b) -> f a -> f b |

145 | -f <$> a = fmap f a |

146 | - |

147 | --- | Replace the value. |

148 | -(<$) :: Functor f => a -> f b -> f a |

149 | -(<$) = (<$>) . const |

150 | - |

151 | --- | Sequence actions, discarding the value of the first argument. |

152 | -(*>) :: Applicative f => f a -> f b -> f b |

153 | -(*>) = liftA2 (const id) |

154 | - |

155 | --- | Sequence actions, discarding the value of the second argument. |

156 | -(<*) :: Applicative f => f a -> f b -> f a |

157 | -(<*) = liftA2 const |

158 | - |

159 | -- | A variant of '<*>' with the arguments reversed. |

160 | (<**>) :: Applicative f => f a -> f (a -> b) -> f b |

161 | (<**>) = liftA2 (flip ($)) |

162 | hunk ./Control/Applicative.hs 229 |

163 | -- | One or none. |

164 | optional :: Alternative f => f a -> f (Maybe a) |

165 | optional v = Just <$> v <|> pure Nothing |

166 | - |

167 | --- | One or more. |

168 | -some :: Alternative f => f a -> f [a] |

169 | -some v = some_v |

170 | - where many_v = some_v <|> pure [] |

171 | - some_v = (:) <$> v <*> many_v |

172 | - |

173 | --- | Zero or more. |

174 | -many :: Alternative f => f a -> f [a] |

175 | -many v = many_v |

176 | - where many_v = some_v <|> pure [] |

177 | - some_v = (:) <$> v <*> many_v |

178 | hunk ./Data/Functor.hs 1 |

179 | +----------------------------------------------------------------------------- |

180 | +-- | |

181 | +-- Module : Data.Functor |

182 | +-- Copyright : (c) The University of Glasgow 2001 |

183 | +-- License : BSD-style (see the file libraries/base/LICENSE) |

184 | +-- |

185 | +-- Maintainer : libraries@haskell.org |

186 | +-- Stability : provisional |

187 | +-- Portability : portable |

188 | +-- |

189 | +-- Functors: uniform action over a parameterized type, generalizing the |

190 | +-- 'map' function on lists. |

191 | + |

192 | +module Data.Functor |

193 | + ( |

194 | + Functor(fmap, (<$)), |

195 | + (<$>), |

196 | + ) where |

197 | + |

198 | +#ifdef __GLASGOW_HASKELL__ |

199 | +import GHC.Base (Functor(..)) |

200 | +#endif |

201 | + |

202 | +infixl 4 <$> |

203 | + |

204 | +-- | An infix synonym for 'fmap'. |

205 | +(<$>) :: Functor f => (a -> b) -> f a -> f b |

206 | +(<$>) = fmap |

207 | hunk ./GHC/Base.lhs 114 |

208 | |

209 | infixr 9 . |

210 | infixr 5 ++ |

211 | +infixl 4 <$ |

212 | infixl 1 >>, >>= |

213 | infixr 0 $ |

214 | |

215 | hunk ./GHC/Base.lhs 178 |

216 | class Functor f where |

217 | fmap :: (a -> b) -> f a -> f b |

218 | |

219 | + -- | Replace all locations in the input with the same value. |

220 | + -- The default definition is @'fmap' . 'const'@, but this may be |

221 | + -- overridden with a more efficient version. |

222 | + (<$) :: a -> f b -> f a |

223 | + (<$) = fmap . const |

224 | + |

225 | {- | The 'Monad' class defines the basic operations over a /monad/, |

226 | a concept from a branch of mathematics known as /category theory/. |

227 | From the perspective of a Haskell programmer, however, it is best to |

228 | hunk ./base.cabal 127 |

229 | Data.Fixed, |

230 | Data.Foldable |

231 | Data.Function, |

232 | + Data.Functor, |

233 | Data.HashTable, |

234 | Data.IORef, |

235 | Data.Int, |

236 | } |

237 | |

238 | Context: |

239 | |

240 | [TAG 2009-06-25 |

241 | Ian Lynagh <igloo@earth.li>**20090625160056] |

242 | Patch bundle hash: |

243 | d7d9bd4f8b5727799df364649de0acf0a027d0a8 |