title: Is it possible to define a function in Haskell that has an input argument of two possible types?
body: <p>For my own understanding, I want to define a function in Haskell that takes two arguments- either both Integers, or both Chars. It does some trivial examination of the arguments, like so:</p>
  
  <pre><code>foo 1 2 = 1
  foo 2 1 = 0
  foo 'a' 'b' = -1
  foo _ _ = -10
  </code></pre>
  
  <p>This I know won't compile, because it doesn't know whether its args are of type Num or Char. But I can't make its arguments polymorphic, like:</p>
  
  <pre><code>foo :: a -&gt; a -&gt; Int
  </code></pre>
  
  <p>Because then we are saying it must be a Char (or Int) in the body.</p>
  
  <p>Is it possible to do this in Haskell? I thought of maybe creating a custom type? Something like:</p>
  
  <pre><code>data Bar = Int | Char
  foo :: Bar -&gt; Bar -&gt; Int
  </code></pre>
  
  <p>But I don't think this is valid either. In general, I'm confused about if there's a middle ground between a function in Haskell being either explicitly of ONE type, or polymorphic to a typeclass, prohibiting any usage of a specific type in the function body.</p>