Skip to content

Instantly share code, notes, and snippets.

@jneira
Created November 9, 2016 08:52
Show Gist options
  • Save jneira/529a496d35e7e3fed79b49b805083022 to your computer and use it in GitHub Desktop.
Save jneira/529a496d35e7e3fed79b49b805083022 to your computer and use it in GitHub Desktop.
module All where
import Life
import Grass
import Sheep
import Wolf
main = do
putStrLn "Grass:"
print $ eat Grass [Grass]
print $ eat Grass [Sheep]
print $ eat Grass [Wolf]
putStrLn "Sheep:"
print $ eat Sheep [Grass]
print $ eat Sheep [Sheep]
print $ eat Sheep [Wolf]
putStrLn "Wolf:"
print $ eat Wolf [Grass]
print $ eat Wolf [Sheep]
print $ eat Wolf [Wolf]
{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}
module Grass where
import Life
data Grass = Grass deriving Show
instance Life Grass where
{-# LANGUAGE Rank2Types, FlexibleInstances, MultiParamTypeClasses, FunctionalDependencies,OverlappingInstances #-}
module Life where
type World = (CanEat a b) => [a] -- or something more complex, cells, maps, ..
class Life a where
class (Life a, Life b) => CanEat a b where
canEat :: a -> b -> Bool
instance (Life a, Life b) => CanEat a b where
canEat _ _ = False
eat :: (CanEat a b) => a -> [b] -> [b]
eat _ [] = []
eat a (b:bs) = if (canEat a b) then bs else b : eat a bs
{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}
module Sheep where
import Life
import Grass
data Sheep = Sheep deriving Show
instance Life Sheep where
instance CanEat Sheep Grass where
canEat _ _ = True
{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}
module Wolf where
import Life
import Sheep
data Wolf = Wolf deriving Show
instance Life Wolf where
instance CanEat Wolf Sheep where
canEat _ _ = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment