8

makeLensesGADTに相当するものはありますか? 次のような単純なものがある場合GADT:

data D a b where
  D :: (Ord a, Ord b) => !a -> !b -> D a b

コンストラクターとフィールド名のリストを渡してレンズを自動的に生成する方法はありますか?

4

1 に答える 1

7

自動的にできるとは思いませんが、いくつかのレンズを手で書くことは、この特定のケースではそれほど難しくありません:

{-# LANGUAGE GADTs #-}

import Control.Lens

data D a b where
  D :: (Ord a, Ord b) => !a -> !b -> D a b

field1 :: Lens' (D a b) a
field1 f (D x y) = fmap (\x' -> D x' y) (f x)

field2 :: Lens' (D a b) b
field2 f (D x y) = fmap (\y' -> D x y') (f y)

{- If you want type-changing lenses, you can also use these signatures.
 - Note that then the target type Ord constraint has to escape.

field1 :: (Ord a2) => Lens (D a1 b) (D a2 b) a1 a2
field2 :: (Ord b2) => Lens (D a b1) (D a b2) b1 b2
 -}

Kmettは、存在量化されたフィールドのレンズを作成できないと主張しています

于 2015-01-26T06:53:56.963 に答える