SlideShare a Scribd company logo
1 of 12
Download to read offline
Conway’s Game of Life with Repa

                     worked example using “Stencil Convolution”
         from Illustrated Haskell <http://illustratedhaskell.org>
Preliminaries
   Short tutorial from HaskellWiki
         http://www.haskell.org/haskellwiki/Numeric_Haskell:_A_Repa_Tutorial
   What is this Game of Life?
         http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life


   This example demonstrates how to use Repa’s
      “Stencil Convolution” to solve a simple problem.
   You should be totally familiar with Repa and Game
      of Life, otherwise what follows will not make any
      sense!


Illustrated Haskell <http://illustratedhaskell.org>
Repa refresher
-- RepaGOL.hs
{-# LANGUAGE TypeOperators #-}

import Data.Array.Repa (Z(..), (:.)(..))
import qualified Data.Array.Repa as R

-- a 2D array (the "Toad" pattern)
toad :: R.Array R.DIM2 Int
toad = R.fromList (Z :. 6 :. 6)
    [ 0, 0, 0, 0, 0, 0
    , 0, 0, 0, 0, 0, 0
    , 0, 0, 1, 1, 1, 0
    , 0, 1, 1, 1, 0, 0
    , 0, 0, 0, 0, 0, 0
    , 0, 0, 0, 0, 0, 0
    ]


 Illustrated Haskell <http://illustratedhaskell.org>
“Stencil Convolution”
 This is one of those things better explained by
    example than in words, and is way simpler than it
    sounds.
 Here is how a stencil looks like in Repa

{-# LANGUAGE TemplateHaskell, QuasiQuotes #-}
sten :: Stencil DIM2 Int
sten = [stencil2| 1 1 1
                     1 0 1
                     1 1 1 |]
 This is a 3x3 stencil


Illustrated Haskell <http://illustratedhaskell.org>
So what is “Stencil Convolution”?
   Applying a stencil to an “image” means
          1.      Put the stencil kernel on every pixel
          2.      Grab all neighbors and multiply with the
                      corresponding stencil value
          3.      Add them all and it becomes the new value of the
                      pixel


   Doesn’t that sound like how you would implement
     GoL using imperative loops?




Illustrated Haskell <http://illustratedhaskell.org>
Stencil Convolution example
sten :: Stencil DIM2 Int                                  Since the image is the
sten = [stencil2| 1 1 1
                  1 0 1                                      same size of the stencil
                  1 1 1 |]                                   kernel, it can only be
                                                             applied on the central
x1 :: R.Array DIM2 Int                                       pixel.
x1 = R.formList (Z :. 3 :. 3)
    [ 1, 2, 3                                             1 + 2 + 3 + 4 + 5 = 15 is the
    , 4, 9, 5                                                new value of the central
    , 0, 0, 0 ]                                              pixel
conv1 = mapStencil2 (BoundConst                           The 9 is not included
0) sten x1                                                   because the center
-- Result: [ 0, 0, 0
--         , 0, 15, 0
                                                             pixel of the kernel is 0
--         , 0, 0, 0 ]


 Illustrated Haskell <http://illustratedhaskell.org>
How to use stencil convolution in Game of
Life
   It should now become pretty obvious
   If we represent live cells as 1, dead cells as 0
   The stencil we just defined be used to count the
      number of neighbors for each cell!




Illustrated Haskell <http://illustratedhaskell.org>
Visualization
             [ 0, 0, 0, 0, 0, 0                         [ 0, 0, 0, 0, 0, 0
             , 0, 0, 0, 0, 0, 0                         , 0, 1, 2, 3, 2, 0
             , 0, 0, 1, 1, 1, 0                         , 0, 3, 4, 4, 2, 0
             , 0, 1, 1, 1, 0, 0                         , 0, 2, 4, 4, 3, 0
             , 0, 0, 0, 0, 0, 0                         , 0, 2, 3, 2, 1, 0
             , 0, 0, 0, 0, 0, 0                         , 0, 0, 0, 0, 0, 0
             ]                                          ]




                                mapStencil2 (BoundConst 0) sten


Illustrated Haskell <http://illustratedhaskell.org>
And what does the rule say?
-- A literal translation of the rules
-- from Wikipedia
transit 1 2 = 1
transit 1 3 = 1
transit 1 _ = 0
transit 0 3 = 1
transit 0 _ = 0




Illustrated Haskell <http://illustratedhaskell.org>
We’re almost done
       Original                       Neighbors                 Original            New state!

[ 0, 0, 0, 0, 0, 0                [ 0, 0, 0, 0, 0, 0       [ 0, 0, 0, 0, 0, 0   [ 0, 0, 0, 0, 0, 0
, 0, 0, 0, 0, 0, 0                , 0, 1, 2, 3, 2, 0       , 0, 0, 0, 0, 0, 0   , 0, 0, 0, 1, 0, 0
, 0, 0, 1, 1, 1, 0                , 0, 3, 4, 4, 2, 0       , 0, 0, 1, 1, 1, 0   , 0, 1, 0, 0, 1, 0
, 0, 1, 1, 1, 0, 0                , 0, 2, 4, 4, 3, 0       , 0, 1, 1, 1, 0, 0
                                                                                , 0, 1, 0, 0, 1, 0
                                  , 0, 2, 3, 2, 1, 0       , 0, 0, 0, 0, 0, 0
, 0, 0, 0, 0, 0, 0                                                              , 0, 0, 1, 0, 0, 0
                                  , 0, 0, 0, 0, 0, 0       , 0, 0, 0, 0, 0, 0
, 0, 0, 0, 0, 0, 0                                                              , 0, 0, 0, 0, 0, 0
                                  ]                        ]
]                                                                               ]



                                                                 zipWith
                    mapStencil2



     Illustrated Haskell <http://illustratedhaskell.org>
How about codifying it?
tick world = R.force $ R.zipWith transit world neighbors
    where neighbors = mapStencil 2 (BoundConst 0) sten world




Illustrated Haskell <http://illustratedhaskell.org>
And putting it in a program…
gameOfLife world = do
    show2D 6 world
    input <- getLine
    case input of
        "q" -> return ()
        _ -> program $ tick world

main = program toad

   That’s it! Hopefully you are now as fluent with Repa
      as with lists!

Illustrated Haskell <http://illustratedhaskell.org>

More Related Content

Similar to Conway's Game of Life with Repa

[This sheet must be completed and attached to the last page of.docx
[This sheet must be completed and attached to the last page of.docx[This sheet must be completed and attached to the last page of.docx
[This sheet must be completed and attached to the last page of.docx
hanneloremccaffery
 
Python for High School Programmers
Python for High School ProgrammersPython for High School Programmers
Python for High School Programmers
Siva Arunachalam
 
International Journal of Computer Science and Security Volume (3) Issue (4)
International Journal of Computer Science and Security Volume (3) Issue (4)International Journal of Computer Science and Security Volume (3) Issue (4)
International Journal of Computer Science and Security Volume (3) Issue (4)
CSCJournals
 

Similar to Conway's Game of Life with Repa (20)

RNN, LSTM and Seq-2-Seq Models
RNN, LSTM and Seq-2-Seq ModelsRNN, LSTM and Seq-2-Seq Models
RNN, LSTM and Seq-2-Seq Models
 
Maze solving app listing
Maze solving app listingMaze solving app listing
Maze solving app listing
 
[This sheet must be completed and attached to the last page of.docx
[This sheet must be completed and attached to the last page of.docx[This sheet must be completed and attached to the last page of.docx
[This sheet must be completed and attached to the last page of.docx
 
Esoteric, Obfuscated, Artistic Programming in Ruby
Esoteric, Obfuscated, Artistic Programming in RubyEsoteric, Obfuscated, Artistic Programming in Ruby
Esoteric, Obfuscated, Artistic Programming in Ruby
 
Families of Triangular Norm Based Kernel Function and Its Application to Kern...
Families of Triangular Norm Based Kernel Function and Its Application to Kern...Families of Triangular Norm Based Kernel Function and Its Application to Kern...
Families of Triangular Norm Based Kernel Function and Its Application to Kern...
 
RESEARCH ON WIND ENERGY INVESTMENT DECISION MAKING: A CASE STUDY IN JILIN
RESEARCH ON WIND ENERGY INVESTMENT DECISION MAKING: A CASE STUDY IN JILINRESEARCH ON WIND ENERGY INVESTMENT DECISION MAKING: A CASE STUDY IN JILIN
RESEARCH ON WIND ENERGY INVESTMENT DECISION MAKING: A CASE STUDY IN JILIN
 
The Magic Of Elixir
The Magic Of ElixirThe Magic Of Elixir
The Magic Of Elixir
 
Lecture 6: Stochastic Hydrology (Estimation Problem-Kriging-, Conditional Sim...
Lecture 6: Stochastic Hydrology (Estimation Problem-Kriging-, Conditional Sim...Lecture 6: Stochastic Hydrology (Estimation Problem-Kriging-, Conditional Sim...
Lecture 6: Stochastic Hydrology (Estimation Problem-Kriging-, Conditional Sim...
 
"Deep Learning" Chap.6 Convolutional Neural Net
"Deep Learning" Chap.6 Convolutional Neural Net"Deep Learning" Chap.6 Convolutional Neural Net
"Deep Learning" Chap.6 Convolutional Neural Net
 
Python for High School Programmers
Python for High School ProgrammersPython for High School Programmers
Python for High School Programmers
 
Deep learning study 2
Deep learning study 2Deep learning study 2
Deep learning study 2
 
Introducing R
Introducing RIntroducing R
Introducing R
 
International Journal of Computer Science and Security Volume (3) Issue (4)
International Journal of Computer Science and Security Volume (3) Issue (4)International Journal of Computer Science and Security Volume (3) Issue (4)
International Journal of Computer Science and Security Volume (3) Issue (4)
 
C0531519
C0531519C0531519
C0531519
 
07 practice paper_4_h_set_a
07 practice paper_4_h_set_a07 practice paper_4_h_set_a
07 practice paper_4_h_set_a
 
05 practice paper_3_h_set_a
05 practice paper_3_h_set_a05 practice paper_3_h_set_a
05 practice paper_3_h_set_a
 
Transformer xl
Transformer xlTransformer xl
Transformer xl
 
The Ring programming language version 1.9 book - Part 69 of 210
The Ring programming language version 1.9 book - Part 69 of 210The Ring programming language version 1.9 book - Part 69 of 210
The Ring programming language version 1.9 book - Part 69 of 210
 
Deep learning simplified
Deep learning simplifiedDeep learning simplified
Deep learning simplified
 
Serpent sboxes
Serpent sboxesSerpent sboxes
Serpent sboxes
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 

Conway's Game of Life with Repa

  • 1. Conway’s Game of Life with Repa worked example using “Stencil Convolution” from Illustrated Haskell <http://illustratedhaskell.org>
  • 2. Preliminaries  Short tutorial from HaskellWiki  http://www.haskell.org/haskellwiki/Numeric_Haskell:_A_Repa_Tutorial  What is this Game of Life?  http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life  This example demonstrates how to use Repa’s “Stencil Convolution” to solve a simple problem.  You should be totally familiar with Repa and Game of Life, otherwise what follows will not make any sense! Illustrated Haskell <http://illustratedhaskell.org>
  • 3. Repa refresher -- RepaGOL.hs {-# LANGUAGE TypeOperators #-} import Data.Array.Repa (Z(..), (:.)(..)) import qualified Data.Array.Repa as R -- a 2D array (the "Toad" pattern) toad :: R.Array R.DIM2 Int toad = R.fromList (Z :. 6 :. 6) [ 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0 , 0, 0, 1, 1, 1, 0 , 0, 1, 1, 1, 0, 0 , 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0 ] Illustrated Haskell <http://illustratedhaskell.org>
  • 4. “Stencil Convolution”  This is one of those things better explained by example than in words, and is way simpler than it sounds.  Here is how a stencil looks like in Repa {-# LANGUAGE TemplateHaskell, QuasiQuotes #-} sten :: Stencil DIM2 Int sten = [stencil2| 1 1 1 1 0 1 1 1 1 |]  This is a 3x3 stencil Illustrated Haskell <http://illustratedhaskell.org>
  • 5. So what is “Stencil Convolution”?  Applying a stencil to an “image” means 1. Put the stencil kernel on every pixel 2. Grab all neighbors and multiply with the corresponding stencil value 3. Add them all and it becomes the new value of the pixel  Doesn’t that sound like how you would implement GoL using imperative loops? Illustrated Haskell <http://illustratedhaskell.org>
  • 6. Stencil Convolution example sten :: Stencil DIM2 Int  Since the image is the sten = [stencil2| 1 1 1 1 0 1 same size of the stencil 1 1 1 |] kernel, it can only be applied on the central x1 :: R.Array DIM2 Int pixel. x1 = R.formList (Z :. 3 :. 3) [ 1, 2, 3  1 + 2 + 3 + 4 + 5 = 15 is the , 4, 9, 5 new value of the central , 0, 0, 0 ] pixel conv1 = mapStencil2 (BoundConst  The 9 is not included 0) sten x1 because the center -- Result: [ 0, 0, 0 -- , 0, 15, 0 pixel of the kernel is 0 -- , 0, 0, 0 ] Illustrated Haskell <http://illustratedhaskell.org>
  • 7. How to use stencil convolution in Game of Life  It should now become pretty obvious  If we represent live cells as 1, dead cells as 0  The stencil we just defined be used to count the number of neighbors for each cell! Illustrated Haskell <http://illustratedhaskell.org>
  • 8. Visualization [ 0, 0, 0, 0, 0, 0 [ 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0 , 0, 1, 2, 3, 2, 0 , 0, 0, 1, 1, 1, 0 , 0, 3, 4, 4, 2, 0 , 0, 1, 1, 1, 0, 0 , 0, 2, 4, 4, 3, 0 , 0, 0, 0, 0, 0, 0 , 0, 2, 3, 2, 1, 0 , 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0 ] ] mapStencil2 (BoundConst 0) sten Illustrated Haskell <http://illustratedhaskell.org>
  • 9. And what does the rule say? -- A literal translation of the rules -- from Wikipedia transit 1 2 = 1 transit 1 3 = 1 transit 1 _ = 0 transit 0 3 = 1 transit 0 _ = 0 Illustrated Haskell <http://illustratedhaskell.org>
  • 10. We’re almost done Original Neighbors Original New state! [ 0, 0, 0, 0, 0, 0 [ 0, 0, 0, 0, 0, 0 [ 0, 0, 0, 0, 0, 0 [ 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0 , 0, 1, 2, 3, 2, 0 , 0, 0, 0, 0, 0, 0 , 0, 0, 0, 1, 0, 0 , 0, 0, 1, 1, 1, 0 , 0, 3, 4, 4, 2, 0 , 0, 0, 1, 1, 1, 0 , 0, 1, 0, 0, 1, 0 , 0, 1, 1, 1, 0, 0 , 0, 2, 4, 4, 3, 0 , 0, 1, 1, 1, 0, 0 , 0, 1, 0, 0, 1, 0 , 0, 2, 3, 2, 1, 0 , 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0 , 0, 0, 1, 0, 0, 0 , 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0 ] ] ] ] zipWith mapStencil2 Illustrated Haskell <http://illustratedhaskell.org>
  • 11. How about codifying it? tick world = R.force $ R.zipWith transit world neighbors where neighbors = mapStencil 2 (BoundConst 0) sten world Illustrated Haskell <http://illustratedhaskell.org>
  • 12. And putting it in a program… gameOfLife world = do show2D 6 world input <- getLine case input of "q" -> return () _ -> program $ tick world main = program toad  That’s it! Hopefully you are now as fluent with Repa as with lists! Illustrated Haskell <http://illustratedhaskell.org>