Functor 的常見例項
也許
Maybe
是一個包含可能不存在的值的 Functor
:
instance Functor Maybe where
fmap f Nothing = Nothing
fmap f (Just x) = Just (f x)
Maybe
的 Functor
例項將函式應用於包含在 Just
中的值。如果計算先前失敗了(因此 Maybe
值是 Nothing
),那麼將函式應用於沒有值,因此 fmap
是無操作。
> fmap (+ 3) (Just 3)
Just 6
> fmap length (Just "mousetrap")
Just 9
> fmap sqrt Nothing
Nothing
我們可以使用等式推理檢查此例項的仿函式定律。對於身份法,
fmap id Nothing
Nothing -- definition of fmap
id Nothing -- definition of id
fmap id (Just x)
Just (id x) -- definition of fmap
Just x -- definition of id
id (Just x) -- definition of id
對於組成法,
(fmap f . fmap g) Nothing
fmap f (fmap g Nothing) -- definition of (.)
fmap f Nothing -- definition of fmap
Nothing -- definition of fmap
fmap (f . g) Nothing -- because Nothing = fmap f Nothing, for all f
(fmap f . fmap g) (Just x)
fmap f (fmap g (Just x)) -- definition of (.)
fmap f (Just (g x)) -- definition of fmap
Just (f (g x)) -- definition of fmap
Just ((f . g) x) -- definition of (.)
fmap (f . g) (Just x) -- definition of fmap
清單
列表的 Functor
例項將該函式應用於列表中的每個值。
instance Functor [] where
fmap f [] = []
fmap f (x:xs) = f x : fmap f xs
這也可以寫成列表理解:fmap f xs = [f x | x <- xs]
。
這個例子表明 fmap
概括了 map
。map
只在列表上執行,而 fmap
只在任意的時間執行 15。
身份法可以通過歸納來證明:
-- base case
fmap id []
[] -- definition of fmap
id [] -- definition of id
-- inductive step
fmap id (x:xs)
id x : fmap id xs -- definition of fmap
x : fmap id xs -- definition of id
x : id xs -- by the inductive hypothesis
x : xs -- definition of id
id (x : xs) -- definition of id
同樣,組成法:
-- base case
(fmap f . fmap g) []
fmap f (fmap g []) -- definition of (.)
fmap f [] -- definition of fmap
[] -- definition of fmap
fmap (f . g) [] -- because [] = fmap f [], for all f
-- inductive step
(fmap f . fmap g) (x:xs)
fmap f (fmap g (x:xs)) -- definition of (.)
fmap f (g x : fmap g xs) -- definition of fmap
f (g x) : fmap f (fmap g xs) -- definition of fmap
(f . g) x : fmap f (fmap g xs) -- definition of (.)
(f . g) x : fmap (f . g) xs -- by the inductive hypothesis
fmap (f . g) xs -- definition of fmap
功能
不是每個 Functor
看起來像一個容器。函式的 Functor
例項將函式應用於另一個函式的返回值。
instance Functor ((->) r) where
fmap f g = \x -> f (g x)
請注意,此定義等同於 fmap = (.)
。所以 fmap
概括了功能組成。
再次檢查身份法:
fmap id g
\x -> id (g x) -- definition of fmap
\x -> g x -- definition of id
g -- eta-reduction
id g -- definition of id
和組成法:
(fmap f . fmap g) h
fmap f (fmap g h) -- definition of (.)
fmap f (\x -> g (h x)) -- definition of fmap
\y -> f ((\x -> g (h x)) y) -- definition of fmap
\y -> f (g (h y)) -- beta-reduction
\y -> (f . g) (h y) -- definition of (.)
fmap (f . g) h -- definition of fmap