Can i create data haskell with a help of Template Haskell. Data like :
data Shape = Circle [Float] Double Int
I want write a program, which create Data and then use it
Can i create data haskell with a help of Template Haskell. Data like :
data Shape = Circle [Float] Double Int
I want write a program, which create Data and then use it
Here is a minimal example (sorry, I've never figured out how to make template haskell look pretty)....
First, create a library file (this isn't optional, TH requires you to define expressions in a separate file).
module DataDefinition where
import Language.Haskell.TH
dataDef::DecsQ
dataDef = do
return $ --the following is the definition "data Shape = Circle Float | Square"
[DataD
[]
(mkName "shape")
[]
[
NormalC (mkName "circle")
[(NotStrict, ConT (mkName "Float"))],
NormalC (mkName "Square") []
]
[]
]
Then, use it like this
module Main where
import DataDefinition
$(dataDef) --This defines Shape
main = do
let x = Circle 1.0 --Now you can define and use Shape like any other data def....
return ()
extra-
You can fill in that last empty array in the DataD creation with deriving instances.... Try changing the []
to
[mkName "Show"]
and you can print the output in main....
main = do
let x = Circle 1.0
print x