There is only one possible type of sum in Haskell, and because of the finished class instances and helper functions, it is useful in many cases, but it becomes significantly clunkier when you insert it.
The best approach for the analyzer is to create your own data type, which reflects the structure that you analyze and analyze it directly. Let me give you an example of a partial toy about a toy language.
data Statement = TypeDec String Type DataDec String [Constructor] FunctionDec String LambdaExpression statement :: Parser Statement statement = TypeDec <$> string "type " *> identifier <*> string " = " *> type <|> DataDec <$> string "data " *> identifier <*> string " = " *> many constructor <|> FunctionDec <$> identifier <*> string " = " *> lambdaExpression
Thus, both your data structure and your code reflect production processes in the grammar that you analyze. The great benefit of this is that your data is safe, understandable and ready to use as soon as it is analyzed.
(I never remember the corrections *> and <* , so I probably did it the way you need brackets or something like that, but hopefully you get this idea.)
source share