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

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 ModelsEmory NLP
 
Maze solving app listing
Maze solving app listingMaze solving app listing
Maze solving app listingChris Worledge
 
[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.docxhanneloremccaffery
 
Esoteric, Obfuscated, Artistic Programming in Ruby
Esoteric, Obfuscated, Artistic Programming in RubyEsoteric, Obfuscated, Artistic Programming in Ruby
Esoteric, Obfuscated, Artistic Programming in Rubymametter
 
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 JILINWireilla
 
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...Amro Elfeki
 
"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 NetKen'ichi Matsui
 
Python for High School Programmers
Python for High School ProgrammersPython for High School Programmers
Python for High School ProgrammersSiva Arunachalam
 
Deep learning study 2
Deep learning study 2Deep learning study 2
Deep learning study 2San Kim
 
Introducing R
Introducing RIntroducing R
Introducing Rnzfauna
 
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
 
Transformer xl
Transformer xlTransformer xl
Transformer xlSan Kim
 
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 210Mahmoud Samir Fayed
 
Deep learning simplified
Deep learning simplifiedDeep learning simplified
Deep learning simplifiedLovelyn Rose
 

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

#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
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 WorkerThousandEyes
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
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 organizationRadu Cotescu
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
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 RobisonAnna Loughnan Colquhoun
 

Recently uploaded (20)

#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
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
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
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
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
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
 

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>