Defining TH Lift instances for algebraic data types

Suppose I have an algebraic data type with multiple constructors, like

data Animal a = Mouse a | Beaver a | Rabbit a

How would I create a Lift instance effectively? The easiest way of doing so would be

instance (Lift a) => Lift (Animal a) where
      lift (Mouse  x) = [| Mouse  x |]
      lift (Beaver x) = [| Beaver x |]
      lift (Rabbit x) = [| Rabbit x |]

This is very redundant though. Of course I can't directly abstract the different animals away like lift x = [| x |] lift x = [| x |] , although conceptually that's similar to what I want to achieve. Is there a way of doing this in TH so that I don't have to write the same line again for each data constructor?


这是th-lift包的目的,它提供了这个功能:http://hackage.haskell.org/package/th-lift

链接地址: http://www.djcxy.com/p/70188.html

上一篇: 横向模式下YotubePlayerSupportFragment全屏视图

下一篇: 为代数数据类型定义TH Lift实例