Last active
December 19, 2016 09:56
-
-
Save boj/96a99d5f09b9414f7550f92cac6a624e to your computer and use it in GitHub Desktop.
GADT Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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