有谁知道如何创建 Data.Derive 实例生成器的分步教程?似乎有大量关于如何使用现有派生实例生成器的文档。
对于那些不熟悉 Data.Derive 的人(或者如果我只是特别不清楚),这就是我想要做的。给定一个简单的 newtype 示例,例如,
newtype WrapperT α = WrapperT (StateT Integer α)
deriving (Applicative, Functor, Monad)
instance HzMonadBase => HzMonadBase (WrapperT ) where
data ExprTyp (WrapperT ) α = WrapperTE (ExprTyp (StateT Integer ) α)
我想生成所有这些实例(和约束数据系列)。它们几乎由字符串“WrapperT”、“WrapperTE”和“StateT Integer α”参数化。
data instance Constraints (ExprTyp (WrapperT )) α =
(Suitable (ExprTyp ) α) => WrapperT_Constr
instance (Suitable (ExprTyp ) α) => Suitable (ExprTyp (WrapperT )) α where
constraints = WrapperT_Constr
instance HzMonadBase => HzExprTrans (ExprTyp (WrapperT )) where
type PrimExpr (ExprTyp (WrapperT )) = ExprTyp (StateT Integer )
e_lift = fmap WrapperTE
e_lower = fmap (\(WrapperTE x) -> x)
e_lower_suitably x@(ExprT v) f =
withConstraintsOf v $ \WrapperT_Constr -> f (e_lower x)
instance HzMonadBase => BinaryCmpExpr (ExprTyp (WrapperT ))
instance HzMonadBase => BinaryOpExpr (ExprTyp (WrapperT ))
instance HzMonadBase => SketchConstrExpr (ExprTyp (WrapperT ))
instance HzMonadBase => Arithmetic (ExprTyp (WrapperT ))
instance HzMonadBase => VariablesExpr (ExprTyp (WrapperT ))
instance HzMonadBase => HzMonadTrans (WrapperT ) where
type BaseMonad (WrapperT ) = StateT Integer
lift = WrapperT
instance HzMonadBase => HzMonadUnliftTrans (WrapperT ) where
unlift (WrapperT x) = x