我正在嘗試使用鏡頭欄位為資料型別生成鏡頭。
data St st l = St {
_st_s :: String,
_st_lens :: Lens' st l
}
st_lens :: forall st l. Lens' (St st l) (Lens' st l)
st_lens = lens _st_lens (\s a -> s { _st_lens = a })
GHC 8.10.7 給我一個錯誤:
Illegal polymorphic type:
forall (f1 :: * -> *). Functor f1 => (l -> f1 l) -> st -> f1 st
GHC doesn't yet support impredicative polymorphism
? In the expansion of type synonym ‘Lens’
uj5u.com熱心網友回復:
{-# Language ImpredicativeTypes #-}
{-# Language TypeApplications #-}
-- ..
st_lens :: forall st l. Lens' (St st l) (Lens' st l)
st_lens = lens @_ @(Lens' st l) _st_lens \s a -> s { _st_lens = a }
uj5u.com熱心網友回復:
使用 GHC 9.2 或更高版本,您可以打開ImpredicativeTypes,如Iceland_jack's answer 所示。如果不能切換GHC版本,現在,一個9.2版之前的替代方案是存盤您的鏡頭一個ALens,然后要么使用cloneLens或改編組合程式,以利用它:
data St st l = St {
_st_s :: String,
_st_lens :: ALens' st l
}
st_lens :: forall st l. Lens' (St st l) (ALens' st l)
st_lens = lens _st_lens (\s a -> s { _st_lens = a })
ghci> ("foo", 15) ^# (St "bar" _1 ^. st_lens)
"foo"
ghci> ("foo", 15) ^. cloneLens (St "bar" _1 ^. st_lens)
"foo"
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/370609.html
