Devitect
Devitect

Reputation: 31

Structuring a Haskell program

Im trying to structure a haskell module as best i can and this is what i have come up with.

The inputs can be Fix or Twitter and the output from the parsers can be maybe output, I want to tag the output from the parsers as either Structured or Unstructured.

Is this the right type signatures? Am i using the maybe monad instance class correctly? Is this the right way to handle side effects in haskell?

module Parsers.Parser(
      handle
    ) where

data Input = FIX Text
        | Twitter Text

data Output = StructuredDataEvent Quote 
            | UnStructuredDataEvent  Text

handle :: Input -> Output
handle (FIX r)      = fixParser  r >>=   StructuredDataEvent  
handle (Twitter r)  = twitterParser  r >>=  UnStructuredDataEvent

sideEffects:: Output -> Output
sideEffects   a = LogEventToDatabase a

instance Monad Maybe where  
    return x = Just $ sideEffects x  
    Nothing >>= f = Nothing  
    Just x >>= f  = f $ sideEffects x  
    fail _ =  Nothing  

Upvotes: 0

Views: 140

Answers (1)

mb21
mb21

Reputation: 39189

handle should be of type Input -> IO Output if you really want to do side-effects.

But it's better to write a pure parser that keeps a list of all your error messages around. If they are just Strings, and for example using the Writer monad:

handle :: Input -> Writer [String] Output

Also, have you considered using Parsec, which is monad transformer?

Upvotes: 1

Related Questions