2015-01-26 23 views
7

Esiste un equivalente di makeLenses per GADT? Se ho un semplice GADT come:makeLenses per GADT (Haskell)

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

C'è un modo per generare automaticamente le lenti passando in un costruttore e un elenco di nomi di campo?

+2

Devo chiedere: è possibile scriverli a mano? – dfeuer

risposta

6

non credo che possa essere fatto automaticamente, ma scrivere alcune lenti a mano non è così difficile in questo caso particolare:

{-# 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 
-} 

Sembra che ci sia un po 'rilevante GitHub issue, in cui Kmett afferma che non possono creare obiettivi per campi quantificati in modo esistenziale.