title: OO-Like interface implementation in Haskell
body: <p>Despite the title I'm not going to ask about a mere translation between OO world and Haskell, but I can't figure out a better title. This discussion is similar, but not equal, to <a href="https://stackoverflow.com/questions/5474171/oo-interface-translation-to-haskell">this one</a>.</p>
  <p>I've started a toy project just to expand my limited knowledge of Haskell while reading &quot;Learn You a Haskell for a Great Good&quot;, and I've decided to implement a very basic &quot;Elemental Type System&quot;, which is a subset of a typical battle system in games like Final Fantasy et simila.
  I'm skipping most of the details, but this is in a nutshell my problem:</p>
  <p>I want to model a spell, a magic you can cast on the player or on a monster. In the OO world you usually go for a &quot;Castable&quot; interface with a method &quot;onCast(Player)&quot;, a &quot;Spell&quot; class so you can define thing like this</p>
  <pre><code>Spell myNewSpell = Spell(&quot;Fire&quot;, 100, 20);
  myNewSpell.onCast(Player p); //models the behaviour for the Fire spell
  </code></pre>
  <p>In Haskell I thought this in terms of Types and Classes (I know that Classes in Haskell are a different concept!). I've encountered some difficulties, because my first attempt was to create this:</p>
  <pre><code>--A type synonim, a tuple (HP,MP)
  type CastResult = (Integer,Integer)
  
  
  --A castable spell can either deal damage (or restore) or
  --inflict a status
  class Castable s where
    onCast :: s -&gt; Either (Maybe Status) CastResult
  
  
  data Spell = Spell{spellName :: String,
                     spellCost :: Integer,
                     spellHpDmg :: Integer,
                     spellMpDmg :: Integer,
                     spellElem :: Maybe Element} deriving (Eq,Show,Read)
  </code></pre>
  <p>Now suppose I create some spell using the Record Syntax</p>
  <pre><code>bio = Spell{spellName = &quot;Bio&quot;, ...etc..}
  </code></pre>
  <p>I would like be able to do something like this</p>
  <pre><code>instance Castable bio where
    onCast bio = Left (Just Poison)
  </code></pre>
  <p>There are many problems here:</p>
  <ol>
  <li><p>I can't do &quot;Castable bio&quot; since bio must be a concrete type, not a value of the Type (It should be Castable Spell)</p>
  </li>
  <li><p>bio isn't in scope, inside the instance block is seen just as a value to pattern match against</p>
  </li>
  </ol>
  <p>Overall, I feel this choice of design is pretty poor, but I'm still learning and I don't grasp such advanced topics like Functors, just to name one.</p>
  <p><strong>In a nutshell, which is the idiomatic way to dealing with situation like this? I mean situation which requires &quot;one definition, multiple implementation for multiple instances&quot;, just to use the OO terminology.</strong></p>