Skip to content

Instantly share code, notes, and snippets.

@boj
Last active December 19, 2016 09:56
Show Gist options
  • Save boj/96a99d5f09b9414f7550f92cac6a624e to your computer and use it in GitHub Desktop.
Save boj/96a99d5f09b9414f7550f92cac6a624e to your computer and use it in GitHub Desktop.
GADT Example
{-# LANGUAGE GADTs #-}
module Main where
data SpellHeal = SpellHeal Int deriving (Show)
data SpellDamage = SpellDamage Int deriving (Show)
data SpellRitual = SpellRitual Float deriving (Show)
data Creature = Creature Int deriving (Show)
data Spell a where
Heal :: Int -> Spell SpellHeal
Damage :: Int -> Spell SpellDamage
Ritual :: Float -> Spell SpellRitual
useSpell :: Spell a -> Creature -> Creature
useSpell (Heal amount) (Creature hp) = Creature (hp + amount)
useSpell (Damage amount) (Creature hp) = Creature (hp - amount)
doRitual :: Spell a -> Bool
doRitual (Ritual duration) = True
main :: IO ()
main = do
print $ useSpell (Heal 1) (Creature 10)
print $ doRitual (Ritual 10.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment