Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
runarorama committed Nov 20, 2017
0 parents commit 9a00530
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
dist
dist-*
cabal-dev
*.o
*.hi
*.chi
*.chs.h
*.dyn_o
*.dyn_hi
.hpc
.hsenv
.cabal-sandbox/
cabal.sandbox.config
*.prof
*.aux
*.hp
*.eventlog
.stack-work/
cabal.project.local
.HTF/
*~
30 changes: 30 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Copyright (c) 2017, Runar Bjarnason

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.

* Neither the name of Runar Bjarnason nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
70 changes: 70 additions & 0 deletions Life.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
-- Initial Life.cabal generated by cabal init. For further documentation,
-- see http://haskell.org/cabal/users-guide/

-- The name of the package.
name: Life

-- The package version. See the Haskell package versioning policy (PVP)
-- for standards guiding when and how versions should be incremented.
-- https://wiki.haskell.org/Package_versioning_policy
-- PVP summary: +-+------- breaking API changes
-- | | +----- non-breaking API additions
-- | | | +--- code changes with no API change
version: 0.1.0.0

-- A short (one-line) description of the package.
-- synopsis:

-- A longer description of the package.
-- description:

-- The license under which the package is released.
license: BSD3

-- The file containing the license text.
license-file: LICENSE

-- The package author(s).
author: Runar Bjarnason

-- An email address to which users can send suggestions, bug reports, and
-- patches.
maintainer: [email protected]

-- A copyright notice.
-- copyright:

-- category:

build-type: Simple

-- Extra files to be distributed with the package, such as examples or a
-- README.
extra-source-files: ChangeLog.md

-- Constraint on the version of Cabal needed to build this package.
cabal-version: >=1.10


executable Life
-- .hs or .lhs file containing the Main module.
main-is: Main.hs

-- Modules included in this executable, other than Main.
-- other-modules:

-- LANGUAGE extensions used by modules in this package.
-- other-extensions:

-- Other library packages from which modules are imported.
build-depends: base >=4.9 && <4.10, adjunctions >=4.3 && <4.4,
containers >=0.5 && <0.6, comonad >3, random,
MemoTrie, distributive

-- Directories containing source files.
hs-source-dirs: src

-- Base language which the package is written in.
default-language: Haskell2010

ghc-options: -threaded -O2
2 changes: 2 additions & 0 deletions Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain
87 changes: 87 additions & 0 deletions src/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{-# LANGUAGE TypeFamilies #-}

module Main where

import Control.Arrow ((&&&))
import Control.Comonad.Representable.Store
import Control.Comonad
import Control.Monad
import Data.Bifunctor
import Data.Distributive
import Data.Foldable
import Data.Functor.Rep
import Data.Maybe (fromMaybe)
import qualified Data.Map.Lazy as M
import Data.Map.Lazy (Map(..))
import Data.MemoTrie
import System.Random

boardSize :: Int
boardSize = 50

wrap :: Int -> ((Int, Int) -> a) -> (Int, Int) -> a
wrap n f p =
f $ (join bimap) (\x -> if x < 0 then n - (x `mod` n) else x `mod` n) p

newtype Mem a = Mem { unMem :: (Int, Int) -> a }

mem = Mem . wrap boardSize

instance Functor Mem where
fmap f (Mem g) = mem . memo $ f . g

instance Distributive Mem where
distribute x = Mem . distribute $ fmap (\m -> unMem m) x

instance Representable Mem where
type Rep Mem = (Int,Int)
tabulate = mem . tabulate . memo
index (Mem f) k = f k

type Board = Store Mem Bool

alive :: Board -> Bool
alive b = c && n > 1 && n < 4 || not c && n == 3
where
(c,n) = (extract &&& liveNeighbors) b

liveNeighbors :: Board -> Int
liveNeighbors b = length . filter id $ do
x <- [-1..1]
y <- [-1..1]
if x == 0 && y == 0
then []
else
return $ let (cx,cy) = pos b in peek (cx + x, cy + y) b

blankBoard :: Board
blankBoard = store (const False) (0,0)

randomBoard :: Int -> IO Board
randomBoard n = do
rs <- replicateM n $ randoms <$> mkStdGen <$> randomIO
return . mkBoard . M.fromList $ do
(x, row) <- zip [1..n] rs
(y, c) <- zip [1..n] row
return ((x,y), c)

mkBoard :: Map (Int, Int) Bool -> Board
mkBoard m = store (fromMaybe False . flip M.lookup m) (0,0)

lifeStep = extend alive

printBoard :: Int -> Int -> Board -> IO ()
printBoard x y b = do
for_ [1..y] $ \y -> do
for_ [1..x] $ \x ->
putStr (if peek (x,y) b then "x" else ".")
putStrLn ""
putStrLn ""

main :: IO ()
main = randomBoard 50 >>= go
where
go b = do
printBoard 100 50 b
go $ lifeStep b

0 comments on commit 9a00530

Please sign in to comment.