module CreateTypes where import Language.Haskell.TH import Language.Haskell.TH.Syntax import StringUtil import System.IO (putStr, getLine) import Data.List import Data.Maybe import GenDataTypes import GetTypes {- - generatedType: - - At compile time takes the name of the data file which the types are being generated from. After compilation - generatedType will just be a list of doubles of type representations, e.g.: - - [("String", []), ("Int", []), ("T0", ["Male", "Female"])] -} generatedType = $( let types = putStr "Enter file name: " >> getLine >>= readFile >>= \fc -> return (getTypes fc) -- The types are generated, the type IO [(String, [String])] is the result of the monadic operations. in lift =<< runIO types) -- Run the IO operation in the Q monad, and then lift that to an expression which is then spliced. {- - createTypes: - - Generates the declarations for the new types and type synonym based on the contents of the type representation - list bound to generatedType by the splice. -} createTypes = let dtList = filter ((/=) [] . snd) generatedType -- Gets all of the representation list elements which need a new type declaration. newDataType x = makeType (fst x) (snd x) -- Takes a double and calls the makeType function on the first and second elements. newDataTypes = map newDataType dtList -- Make a list of declarations from the new type representations. newTypeSyn = makeTypeSyn "MainType" (map fst generatedType) -- Generates the new type synonym declaration from the type representation list. in sequenceQ (newDataTypes++[newTypeSyn]) -- Converts from [Q Dec] to Q [Dec] so the declarations can be spliced.