/derive-2.5.8/Data/Derive/Data.hs

# · Haskell · 155 lines · 102 code · 8 blank · 45 comment · 1 complexity · fb979bb6da241b60569bf77449e4e95a MD5 · raw file

  1. module Data.Derive.Data(makeData) where
  2. {-
  3. import Data.Data
  4. example :: Custom
  5. instance (Data a, Typeable a) => Data (Sample a) where
  6. gfoldl k r (First) = r First
  7. gfoldl k r (Second x1 x2) = r Second `k` x1 `k` x2
  8. gfoldl k r (Third x1) = r Third `k` x1
  9. gunfold k z c = case constrIndex c - 1 of
  10. 0 -> z First
  11. 1 -> const k 1 $ const k 2 $ z Second
  12. 2 -> const k 1 $ z Third
  13. i -> error $ ("Data.gunfold for Sample" ++ ", unknown index: ") ++ show i
  14. toConstr x@First{} = indexConstr (dataTypeOf x) (0+1)
  15. toConstr x@Second{} = indexConstr (dataTypeOf x) (1+1)
  16. toConstr x@Third{} = indexConstr (dataTypeOf x) (2+1)
  17. dataTypeOf _ = ty
  18. where ty = mkDataType $(dataName)
  19. [mkConstr ty "First" $(ctorFields 0) $(ctorFixity 0)
  20. ,mkConstr ty "Second" $(ctorFields 1) $(ctorFixity 1)
  21. ,mkConstr ty "Third" $(ctorFields 2) $(ctorFixity 2)]
  22. test :: Computer
  23. instance Data Computer where
  24. gfoldl k r (Laptop x1 x2) = r Laptop `k` x1 `k` x2
  25. gfoldl k r (Desktop x1) = r Desktop `k` x1
  26. gunfold k z c = case constrIndex c - 1 of
  27. 0 -> k $ k $ z Laptop
  28. 1 -> k $ z Desktop
  29. i -> error $ "Data.gunfold for Computer, unknown index: " ++ show i
  30. toConstr x@Laptop{} = indexConstr (dataTypeOf x) 1
  31. toConstr x@Desktop{} = indexConstr (dataTypeOf x) 2
  32. dataTypeOf _ = ty
  33. where ty = mkDataType "Example.Computer"
  34. [mkConstr ty "Laptop" ["weight", "speed"] Prefix
  35. ,mkConstr ty "Desktop" ["speed"] Prefix]
  36. -}
  37. import Data.Derive.DSL.HSE
  38. import qualified Language.Haskell as H
  39. -- GENERATED START
  40. import Data.Derive.DSL.DSL
  41. import Data.Derive.Internal.Derivation
  42. makeData :: Derivation
  43. makeData = derivationCustomDSL "Data" custom $
  44. List [Instance ["Data","Typeable"] "Data" (List [App "InsDecl" (
  45. List [App "FunBind" (List [MapCtor (App "Match" (List [App "Ident"
  46. (List [String "gfoldl"]),List [App "PVar" (List [App "Ident" (List
  47. [String "k"])]),App "PVar" (List [App "Ident" (List [String "r"])]
  48. ),App "PParen" (List [App "PApp" (List [App "UnQual" (List [App
  49. "Ident" (List [CtorName])]),MapField (App "PVar" (List [App
  50. "Ident" (List [Concat (List [String "x",ShowInt FieldIndex])])]))]
  51. )])],App "Nothing" (List []),App "UnGuardedRhs" (List [Fold (App
  52. "InfixApp" (List [Tail,App "QVarOp" (List [App "UnQual" (List [App
  53. "Ident" (List [String "k"])])]),Head])) (Concat (List [Reverse (
  54. MapField (App "Var" (List [App "UnQual" (List [App "Ident" (List [
  55. Concat (List [String "x",ShowInt FieldIndex])])])]))),List [App
  56. "App" (List [App "Var" (List [App "UnQual" (List [App "Ident" (
  57. List [String "r"])])]),App "Con" (List [App "UnQual" (List [App
  58. "Ident" (List [CtorName])])])])]]))]),App "BDecls" (List [List []]
  59. )]))])]),App "InsDecl" (List [App "FunBind" (List [List [App
  60. "Match" (List [App "Ident" (List [String "gunfold"]),List [App
  61. "PVar" (List [App "Ident" (List [String "k"])]),App "PVar" (List [
  62. App "Ident" (List [String "z"])]),App "PVar" (List [App "Ident" (
  63. List [String "c"])])],App "Nothing" (List []),App "UnGuardedRhs" (
  64. List [App "Case" (List [App "InfixApp" (List [App "App" (List [App
  65. "Var" (List [App "UnQual" (List [App "Ident" (List [String
  66. "constrIndex"])])]),App "Var" (List [App "UnQual" (List [App
  67. "Ident" (List [String "c"])])])]),App "QVarOp" (List [App "UnQual"
  68. (List [App "Symbol" (List [String "-"])])]),App "Lit" (List [App
  69. "Int" (List [Int 1])])]),Concat (List [MapCtor (App "Alt" (List [
  70. App "PLit" (List [App "Int" (List [CtorIndex])]),App
  71. "UnGuardedAlt" (List [Fold (App "InfixApp" (List [Head,App
  72. "QVarOp" (List [App "UnQual" (List [App "Symbol" (List [String "$"
  73. ])])]),Tail])) (Concat (List [MapField (Application (List [App
  74. "Var" (List [App "UnQual" (List [App "Ident" (List [String "const"
  75. ])])]),App "Var" (List [App "UnQual" (List [App "Ident" (List [
  76. String "k"])])]),App "Lit" (List [App "Int" (List [FieldIndex])])]
  77. )),List [App "App" (List [App "Var" (List [App "UnQual" (List [App
  78. "Ident" (List [String "z"])])]),App "Con" (List [App "UnQual" (
  79. List [App "Ident" (List [CtorName])])])])]]))]),App "BDecls" (List
  80. [List []])])),List [App "Alt" (List [App "PVar" (List [App "Ident"
  81. (List [String "i"])]),App "UnGuardedAlt" (List [App "InfixApp" (
  82. List [App "Var" (List [App "UnQual" (List [App "Ident" (List [
  83. String "error"])])]),App "QVarOp" (List [App "UnQual" (List [App
  84. "Symbol" (List [String "$"])])]),App "InfixApp" (List [App "Paren"
  85. (List [App "InfixApp" (List [App "Lit" (List [App "String" (List [
  86. Concat (List [String "Data.gunfold for ",DataName])])]),App
  87. "QVarOp" (List [App "UnQual" (List [App "Symbol" (List [String
  88. "++"])])]),App "Lit" (List [App "String" (List [String
  89. ", unknown index: "])])])]),App "QVarOp" (List [App "UnQual" (List
  90. [App "Symbol" (List [String "++"])])]),App "App" (List [App "Var"
  91. (List [App "UnQual" (List [App "Ident" (List [String "show"])])]),
  92. App "Var" (List [App "UnQual" (List [App "Ident" (List [String "i"
  93. ])])])])])])]),App "BDecls" (List [List []])])]])])]),App "BDecls"
  94. (List [List []])])]])]),App "InsDecl" (List [App "FunBind" (List [
  95. MapCtor (App "Match" (List [App "Ident" (List [String "toConstr"])
  96. ,List [App "PAsPat" (List [App "Ident" (List [String "x"]),App
  97. "PRec" (List [App "UnQual" (List [App "Ident" (List [CtorName])]),
  98. List []])])],App "Nothing" (List []),App "UnGuardedRhs" (List [
  99. Application (List [App "Var" (List [App "UnQual" (List [App
  100. "Ident" (List [String "indexConstr"])])]),App "Paren" (List [App
  101. "App" (List [App "Var" (List [App "UnQual" (List [App "Ident" (
  102. List [String "dataTypeOf"])])]),App "Var" (List [App "UnQual" (
  103. List [App "Ident" (List [String "x"])])])])]),App "Paren" (List [
  104. App "InfixApp" (List [App "Lit" (List [App "Int" (List [CtorIndex]
  105. )]),App "QVarOp" (List [App "UnQual" (List [App "Symbol" (List [
  106. String "+"])])]),App "Lit" (List [App "Int" (List [Int 1])])])])])
  107. ]),App "BDecls" (List [List []])]))])]),App "InsDecl" (List [App
  108. "FunBind" (List [List [App "Match" (List [App "Ident" (List [
  109. String "dataTypeOf"]),List [App "PWildCard" (List [])],App
  110. "Nothing" (List []),App "UnGuardedRhs" (List [App "Var" (List [App
  111. "UnQual" (List [App "Ident" (List [String "ty"])])])]),App
  112. "BDecls" (List [List [App "PatBind" (List [App "PVar" (List [App
  113. "Ident" (List [String "ty"])]),App "Nothing" (List []),App
  114. "UnGuardedRhs" (List [Application (List [App "Var" (List [App
  115. "UnQual" (List [App "Ident" (List [String "mkDataType"])])]),App
  116. "SpliceExp" (List [App "ParenSplice" (List [App "Var" (List [App
  117. "UnQual" (List [App "Ident" (List [String "dataName"])])])])]),App
  118. "List" (List [MapCtor (Application (List [App "Var" (List [App
  119. "UnQual" (List [App "Ident" (List [String "mkConstr"])])]),App
  120. "Var" (List [App "UnQual" (List [App "Ident" (List [String "ty"])]
  121. )]),App "Lit" (List [App "String" (List [CtorName])]),App
  122. "SpliceExp" (List [App "ParenSplice" (List [App "App" (List [App
  123. "Var" (List [App "UnQual" (List [App "Ident" (List [String
  124. "ctorFields"])])]),App "Lit" (List [App "Int" (List [CtorIndex])])
  125. ])])]),App "SpliceExp" (List [App "ParenSplice" (List [App "App" (
  126. List [App "Var" (List [App "UnQual" (List [App "Ident" (List [
  127. String "ctorFixity"])])]),App "Lit" (List [App "Int" (List [
  128. CtorIndex])])])])])]))])])]),App "BDecls" (List [List []])])]])])]
  129. ])])])]
  130. -- GENERATED STOP
  131. custom d = customContext context d . customSplice splice d
  132. splice :: FullDataDecl -> Exp -> Exp
  133. splice d x | x ~= "dataName" = H.Lit $ H.String $ prettyPrint (fst d) ++ "." ++ dataDeclName (snd d)
  134. splice d (H.App x (H.Lit (H.Int y)))
  135. | x ~= "ctorFields" = H.List $ [H.Lit $ H.String a | (a,_) <- ctorDeclFields ctor, a /= ""]
  136. | x ~= "ctorFixity" = Con (UnQual (Ident "Prefix"))
  137. where ctor = dataDeclCtors (snd d) !! fromInteger y
  138. context :: FullDataDecl -> Context -> Context
  139. context d _ = [ClassA (qname t) [tyVar x] | x <- dataDeclVars $ snd d, t <- ["Typeable","Data"]]