title: Parser with multiple return types
body: <p>I have problem with parsers that can manage multiple types, e.g., the parser for arithmetic expressions. At the moment my parser takes in input only integers:</p>
  
  <pre class="lang-hs prettyprint-override"><code>aexpr :: Parser Int
  aexpr = aterm `chainl` addOp
  
  aterm :: Parser Int
  aterm = afactor `chainl` mulOp
  
  afactor :: Parser Int
  afactor = parenP '(' aexpr ')'
            &lt;|&gt;
            do
              s &lt;- sign 
              a &lt;- aexpr 
              return (s * a)
            &lt;|&gt;  
            token int_const
  </code></pre>
  
  <p>This works well for two integers, but I am introducing the float type, so the parser for arithmetic expressions can have in input two <code>Int</code>, two <code>Float</code> or two mixed types of <code>Int</code> and <code>Float</code>. The result type of the parser should be <code>Int</code> or <code>Float</code> based on the inputs (i.e., <code>Int</code> if both inputs are <code>Int</code>, <code>Float</code> otherwise).</p>
  
  <p>My question is: what is the best way to manage this? A simple way is to just manage all the inputs as <code>Float</code>, so integer values are converted into float values. But I don't like this solution: the end result will be always <code>Float</code>.</p>
  
  <p>Should I create two different parsers, i.e., one for <code>Int</code> and one for <code>Float</code>? But the two parsers would be pretty much the same.</p>
  
  <p>There is a better solution?</p>