module Data.Derive.Set(makeSet) where
import Language.Haskell
import Data.Derive.Internal.Derivation
import Data.Maybe
makeSet :: Derivation
makeSet = derivationCustom "Set" $ \(_,d) -> Right $ concatMap (makeSetField d) $ dataDeclFields d
makeSetField :: DataDecl -> String -> [Decl ()]
makeSetField d field = [TypeSig () [name set] typ, bind set [pVar "v",pVar "x"] bod]
where
set = "set" ++ title field
tyFun = TyFun ()
typ = typField `tyFun` (dataDeclType d `tyFun` dataDeclType d)
typField = fromJust $ lookup field $ concatMap ctorDeclFields $ dataDeclCtors d
bod = RecUpdate () (var "x") [FieldUpdate () (qname field) (var "v")]