Game programming in Haskell Alexander Berntsen Stian A. Ellingsen - - PowerPoint PPT Presentation

game programming in haskell
SMART_READER_LITE
LIVE PREVIEW

Game programming in Haskell Alexander Berntsen Stian A. Ellingsen - - PowerPoint PPT Presentation

Outline Our project Welcome to the functional paradigm of Haskell Game programming in Haskell Alexander Berntsen Stian A. Ellingsen September 18, 2013 Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell Outline Our project


slide-1
SLIDE 1

Outline Our project Welcome to the functional paradigm of Haskell

Game programming in Haskell

Alexander Berntsen Stian A. Ellingsen September 18, 2013

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-2
SLIDE 2

Outline Our project Welcome to the functional paradigm of Haskell

Our project Current game programming paradigms Solving those problems Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-3
SLIDE 3

Outline Our project Welcome to the functional paradigm of Haskell Current game programming paradigms Solving those problems

Object-Oriented Programming

◮ In OOP we try to represent our game world with object

models

◮ Objects are usually instances of classes that interact with each

  • ther

◮ Objects have methods through which they perform said

interaction

f o r ( std : : vector<E n t i t i e s ∗>:: c o n s t i t e r a t o r ent = e n t i t i e s . begin ( ) ; ent != e n t i t i e s . end ( ) ; (∗ ent)−>handle ( event ) , ++ent ) ; Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-4
SLIDE 4

Outline Our project Welcome to the functional paradigm of Haskell Current game programming paradigms Solving those problems

Component-Entity Systems

◮ components (single purpose bits of state) ◮ entities (unique ids) ◮ systems (single purpose functions that take a set of entities

with specific component(s) and update them) ( defn r e n d e r e r [ r e n d e r a b l e s ] ( doseq [ e r e n d e r a b l e s ] ( l e t [ rend ( as e : r e n d e r a b l e ) ] (( rend : fn ) e ) ) ) )

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-5
SLIDE 5

Outline Our project Welcome to the functional paradigm of Haskell Current game programming paradigms Solving those problems

Some of the problems with these paradigms

◮ no rigorous definitions ◮ used as a herding mechanism to keep mediocre programmers

  • ccupied (which may lead to mediocre programs)

◮ heavy use of state makes refactoring a nightmare ◮ lots of boilerplate code ◮ tight coupling ◮ bloated core code

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-6
SLIDE 6

Outline Our project Welcome to the functional paradigm of Haskell Current game programming paradigms Solving those problems

We think that

◮ functional purity may eliminate these problems ◮ pure functional programming can be used efficiently for game

programming

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-7
SLIDE 7

Outline Our project Welcome to the functional paradigm of Haskell Current game programming paradigms Solving those problems

What we will try to figure out

◮ how to do a by nature heavily stateful computation (games)

in a pure paradigm (Haskell)

◮ whether this results in a higher quality of source code ◮ what types of games are most compatible with PFP

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-8
SLIDE 8

Outline Our project Welcome to the functional paradigm of Haskell Current game programming paradigms Solving those problems

By doing this

◮ analyse Haskell games’ source code (“frag”, “Nikki and the

Robots”)

◮ analyse Haskell game libraries (“Hipmunk”, “SDL”) ◮ analyse other heavily state-reliant Haskell projects (“xmonad”,

“AMuZed and ZooM”, “Yi”)

◮ try to apply what we learn through this

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-9
SLIDE 9

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming

The imperative way is confusing and low-level

a = 2

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-10
SLIDE 10

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming

The imperative way is confusing and low-level

a = 2 a += 1

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-11
SLIDE 11

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming

The imperative way is confusing and low-level

a = 2 # wtf ? why are you changing your # mind about what ’ a ’ i s ? a += 1

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-12
SLIDE 12

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming

The imperative way is confusing and low-level

a = 2 # wtf ? why are you changing your # mind about what ’ a ’ i s ? a += 1

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-13
SLIDE 13

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming

/∗ P r i n t ” h e l l o ” 3 times ∗/ main () { int i ; for ( i = 0; i < 3; ++i ) p r i n t f ( ”%s ” , i != 3 ? ” h e l l o ” : ” h e l l o \n” ) ; }

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-14
SLIDE 14

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming /∗ ∗ = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ∗ Filename : l o l . c ∗ D e s c r i p t i o n : This program p r i n t s ” h e l l o h e l l o h e l l o ” ∗ Version : 1.0 ∗ Created : 17/09/13 11:59:11 ∗ R e v i s i o n : none ∗ Compiler : gcc ∗ Author : Foo Bar ( foo@bar . com ) , ∗ O r g a n i z a t i o n : Fubaz , Inc . ∗ = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ∗/ #i n c l u d e <s t d i o . h> #i f d e f GNUC # d e f i n e UNUSED( x ) UNUSED # # x a t t r i b u t e ( ( u n u s e d )) #e l s e # d e f i n e UNUSED( x ) UNUSED # # x #e n d i f #i f d e f GNUC # d e f i n e UNUSED FUNCTION( x ) a t t r i b u t e ( ( u n u s e d ) ) UNUSED # # x #e l s e # d e f i n e UNUSED FUNCTION( x ) UNUSED # # x #e n d i f i n t main ( i n t ∗UNUSED( argc ) , const char UNUSED( argv ) ) { i n t i ; char msg [ 6 ] = ” h e l l o ” ; f o r ( i = 0; i < 3 ; ++i ) { p r i n t f ( ”%s ” , msg ) ; } p r i n t f ( ”\n” ) ; } Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-15
SLIDE 15

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming

So we describe what we want, instead of how to achieve it

” h e l l o ”

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-16
SLIDE 16

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming

So we describe what we want, instead of how to achieve it

r e p l i c a t e 3 ” h e l l o ”

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-17
SLIDE 17

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming

So we describe what we want, instead of how to achieve it

unwords $ r e p l i c a t e 3 ” h e l l o ”

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-18
SLIDE 18

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming

Which often leads to . . .

◮ looser coupling ◮ a more high-level approach ◮ parallellisation and optimisation out of the box ◮ rapid implementation, freeing up development time

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-19
SLIDE 19

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming

Which means . . .

◮ less bloated code ◮ less code, period (LOC) ◮ fewer bugs/errors ◮ faster debugging cycles ◮ source code maintainability and reusability

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-20
SLIDE 20

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming

Which makes imperative programming look like this in comparison

OBTW THIS PROGRAM PRINTS HELLO HELLO HELLO TLDR HAI 1.2 CAN HAS STDIO? IM IN YR LOOP UPPIN YR VAR BOTH SAEM VAR AN 3 , O RLY? YA RLY, GTFO NO WAI, VAR, WTF? OMG 2 VISIBLE ”HELLO” GTFO OMGWTF VISIBLE ”HELLO ”! OIC OIC IM OUTTA YR LOOP KTHXBYE Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-21
SLIDE 21

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming

So there’s this thing called Haskell

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-22
SLIDE 22

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming

And it’s the Right Thing

◮ Haskell is a general-purpose purely functional programming

language, with lazy evaluation and strong static typing

◮ Purely functional means there are no side effects, a function

predictably returns the same thing when given the same input

◮ All IO, state and related features are provided in controlled

monadic contexts

◮ With lazy evaluation nothing is computed until it needs to be

evaluated (take 1 [0..] will not overflow, as only the first element is computed)

◮ Haskell has notably strong static-typing that catches type

errors at compile time and improves code readability – particularly for legacy code

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-23
SLIDE 23

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming

Typing

r e p l i c a t e : : I n t −> a −> [ a ] unwords : : [ S t r i n g ] −> S t r i n g

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-24
SLIDE 24

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming

Typing

r e p l i c a t e : : I n t −> a −> [ a ] unwords : : [ S t r i n g ] −> S t r i n g type S t r i n g = [ Char ] unwords : : [ [ Char ] ] −> [ Char ]

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-25
SLIDE 25

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming

Typing

r e p l i c a t e : : I n t −> a −> [ a ] unwords : : [ S t r i n g ] −> S t r i n g type S t r i n g = [ Char ] unwords : : [ [ Char ] ] −> [ Char ] type PhoneBook = [ ( [ Char ] , [ Char ] ) ] type PhoneBook = [ ( String , S t r i n g ) ] type PhoneNumber = S t r i n g type Name = S t r i n g type PhoneBook = [ ( Name , PhoneNumber ) ]

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-26
SLIDE 26

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming

Abstracting type signatures with type synonyms

inPhoneBook : : [ Char ] − > [ Char ] − > [ ( [ Char ] , [ Char ] ) ] − > Bool inPhoneBook : : S t r i n g − > S t r i n g − > [ ( String , S t r i n g ) ] − > Bool inPhoneBook : : Name − > PhoneNumber − > PhoneBook − > Bool inPhoneBook n p b = elem (n , p ) b Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-27
SLIDE 27

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming

Currying

max : : (Ord a ) = > a −> a −> a max : : (Ord a ) = > a −> ( a −> a )

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-28
SLIDE 28

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming

Currying

max 1 2 (max 1) 2

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-29
SLIDE 29

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming

High-order functions and partial application

add x y = x + y

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-30
SLIDE 30

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming

High-order functions and partial application

add x y = x + y add2 x = add 2 x

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-31
SLIDE 31

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming

Eta-reduction

add = (+) add2 = add 2

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-32
SLIDE 32

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming

map

map : : ( a −> b) −> [ a ] −> [ b ] map [ ] = [ ] map f ( x : xs ) = f x : map f xs

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-33
SLIDE 33

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming

Function composition

f xs = negate (sum ( t a i l xs )) map f [ [ 1 . . 1 0 ] , [ 5 . . 8 ] , [ 0 . . 3 ] ] −− [−54,−21,−6]

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-34
SLIDE 34

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming

Function composition

f xs = negate (sum ( t a i l xs )) map f [ [ 1 . . 1 0 ] , [ 5 . . 8 ] , [ 0 . . 3 ] ] −− [−54,−21,−6] l e t f xs = negate (sum ( t a i l xs )) in map f [ [ 1 . . 1 0 ] , [ 5 . . 8 ] , 0 . . 3 ] ] −− a l s o [−54,−21,−6] map f [ [ 1 . . 1 0 ] , [ 5 . . 8 ] , [ 0 . . 3 ] −− a l s o a l s o [−54,−21,−6] where f xs = negate (sum ( t a i l xs )) map (\ xs −> negate (sum ( t a i l xs ) ) ) [ [ 1 . . 1 0 ] , [ 5 . . 8 ] , [ 0 . . 3 ] ] −− s t i l l [−54,−21,−6]

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-35
SLIDE 35

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming

Function composition

f xs = negate (sum ( t a i l xs )) map f [ [ 1 . . 1 0 ] , [ 5 . . 8 ] , [ 0 . . 3 ] ] −− [−54,−21,−6] l e t f xs = negate (sum ( t a i l xs )) in map f [ [ 1 . . 1 0 ] , [ 5 . . 8 ] , 0 . . 3 ] ] −− a l s o [−54,−21,−6] map f [ [ 1 . . 1 0 ] , [ 5 . . 8 ] , [ 0 . . 3 ] −− a l s o a l s o [−54,−21,−6] where f xs = negate (sum ( t a i l xs )) map (\ xs −> negate (sum ( t a i l xs ) ) ) [ [ 1 . . 1 0 ] , [ 5 . . 8 ] , [ 0 . . 3 ] ] −− s t i l l [−54,−21,−6] map ( negate . sum . t a i l ) [ [ 1 . . 1 0 ] , [ 5 . . 8 ] , [ 0 . . 3 ] ] −− tada : [−54,−21,−6]

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-36
SLIDE 36

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming

Don’t worry, general relativity was also difficult at first

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-37
SLIDE 37

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming

Also there’s this thing called FRP1

Programming paradigm that mixes the functional and reactive

  • paradigms. Key traits:

◮ behaviours/signals (values that vary over continuous time) ◮ events (occurences at finitely many points in time) ◮ switching (a means to change the system in response to

events)

◮ separation of evaluation details ◮ synchrony / glitch freedom (consistent event ordering)

1No known relation to any right-wing religious extremist Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-38
SLIDE 38

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming

A wild example appears!

◮ Example: simple GUI label that displays the number of

seconds passed since program start

◮ Quite complicated in an event-based model ◮ Would require making a label and updating it all the time

using a timer/idle event

◮ However, in FRP it becomes easy

Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-39
SLIDE 39

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming − − D i s p l a y c u r r e n t time i n a l a b e l l a b e l = time Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-40
SLIDE 40

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming − − D i s p l a y c u r r e n t time i n a l a b e l l a b e l = time − − D i s p l a y twice the number

  • f

seconds passed + 10 l a b e l = 2∗ time + 10 Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-41
SLIDE 41

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming − − D i s p l a y c u r r e n t time i n a l a b e l l a b e l = time − − D i s p l a y twice the number

  • f

seconds passed + 10 l a b e l = 2∗ time + 10 − − D i s p l a y the s t r i n g ” yes ” i n a l a b e l l a b e l = ” yes ” Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-42
SLIDE 42

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming − − D i s p l a y c u r r e n t time i n a l a b e l l a b e l = time − − D i s p l a y twice the number

  • f

seconds passed + 10 l a b e l = 2∗ time + 10 − − D i s p l a y the s t r i n g ” yes ” i n a l a b e l l a b e l = ” yes ” − − D i s p l a y ” yes ” i f space key i s held down , ”no”

  • t h e r w i s e

l a b e l = ” yes ” . keyDown Space <|> ”no” Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-43
SLIDE 43

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming − − D i s p l a y c u r r e n t time i n a l a b e l l a b e l = time − − D i s p l a y twice the number

  • f

seconds passed + 10 l a b e l = 2∗ time + 10 − − D i s p l a y the s t r i n g ” yes ” i n a l a b e l l a b e l = ” yes ” − − D i s p l a y ” yes ” i f space key i s held down , ”no”

  • t h e r w i s e

l a b e l = ” yes ” . keyDown Space <|> ”no” − − D i s p l a y time w h i l e space i s pressed , ” Press space ”

  • t h e r w i s e

l a b e l = fmap show time . keyDown Space <|> ” Press space ” Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell

slide-44
SLIDE 44

Outline Our project Welcome to the functional paradigm of Haskell Declarative programming Haskell Functional reactive programming − − D i s p l a y c u r r e n t time i n a l a b e l l a b e l = time − − D i s p l a y twice the number

  • f

seconds passed + 10 l a b e l = 2∗ time + 10 − − D i s p l a y the s t r i n g ” yes ” i n a l a b e l l a b e l = ” yes ” − − D i s p l a y ” yes ” i f space key i s held down , ”no”

  • t h e r w i s e

l a b e l = ” yes ” . keyDown Space <|> ”no” − − D i s p l a y time w h i l e space i s pressed , ” Press space ”

  • t h e r w i s e

l a b e l = fmap show time . keyDown Space <|> ” Press space ” − − D i s p l a y ” yes ”/”no” e v e r y

  • ther

second l a b e l = ” yes ” . holdFor 1 ( p e r i o d i c a l l y 2 <|> ”no” ) Alexander Berntsen, Stian A. Ellingsen Game programming in Haskell