14
Frontier Developers 6:00: Pizza & Networking 6:20: Presentation https://github.com/jdegoes/fun-with-automata WIFI: Scriblove / scribble Thursday, May 24, 12

Fun with automata

Embed Size (px)

DESCRIPTION

Deck used for the Fun with Automata talk at Frontier Developers, May 24 2012. Accompanying material at: https://github.com/jdegoes/fun-with-automata

Citation preview

Page 2: Fun with automata

Fun with Automata!

John A. De Goes@jdegoes http://precog.com

https://github.com/jdegoes/fun-with-automata

Thursday, May 24, 12

Page 3: Fun with automata

AutomataAbstract machines that transition between states based on reading of input.

TransducersAutomata that read and write.

Deterministic TransducersOutput deterministically depends on current state and prior input.

Thursday, May 24, 12

Page 4: Fun with automata

DTs vs Functions?

A B

Function

A (B, DT)

DT

Thursday, May 24, 12

Page 5: Fun with automata

“Functions with a memory.”

Thursday, May 24, 12

Page 6: Fun with automata

Benefits• Purely-functional yet stateful

• Incredibly composable

• Safely concurrent

• Recoverable

Thursday, May 24, 12

Page 7: Fun with automata

trait DT[A, B] {

def ! (input: A): (B, DT[A, B])

}

object DT {

def apply[A, B](f: A => (B, DT[A, B])) =

new DT[A, B] { def ! (v: A) = f(v) }

}

Thursday, May 24, 12

Page 8: Fun with automata

val d1 = {

def f(b: Boolean): DT[Int, Int] =

DT { v =>

val output = if (b) v * 2 else v

(output, f(!b))

}

f(false)

}

Thursday, May 24, 12

Page 9: Fun with automata

val (v1, d2) = d1 ! 1

// v1 = 1

val (v2, d3) = d2 ! 1

// v2 = 2

...

Thursday, May 24, 12

Page 10: Fun with automata

trait DT[A, B] {

def ! (input: A): (B, DT[A, B])

def !! (inputs: A*): (List[B], DT[A, B])

def >>> [C] (that: DT[B, C]): DT[A, C]

def map[C](f: B => C): DT[A, C]

def contramap[C](f: C => A): DT[C, B]

def & [C, D](that: DT[C, D]):

DT[(A, C), (B, D)]

}

read many

compose

output transform

input transform

parallelize

Thursday, May 24, 12

Page 11: Fun with automata

def identity[A]: DT[A, A]

def constant[A, B](b: B): DT[A, B]

def rep[A]: DT[A, (A, A)]

def rep3[A]: DT[A, ((A, A), A)]

def merge[A, B, C](f: (A, B) => C): DT[(A, B), C]

def merge3[A, B, C, D](f: (A, B, C) => D): DT[((A, B), C), D]

Helpers

Thursday, May 24, 12

Page 12: Fun with automata

Exercise 1

Develop DT and some of its helper methods in a language of your choice.

Thursday, May 24, 12

Page 13: Fun with automata

Exercise 2

Develop a simple streaming analytics framework, with aggregation and windowing.

Thursday, May 24, 12

Page 14: Fun with automata

Exercise 3

Create a recognizer for the language defined by the grammar ab* (that is, one ‘a’ character followed by zero or more ‘b’ characters). The recognizer emits true’s until a character breaks the pattern, then emits false’s continuously.

Thursday, May 24, 12