software craftsmanship nyc meetup: the pony programming language

168
The Pony Programming Language Andrew Turley Software Craftsmanship NYC October 27, 2016

Upload: aturleyslides

Post on 13-Apr-2017

288 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Software Craftsmanship NYC Meetup:  The Pony Programming Language

The Pony Programming Language

Andrew TurleySoftware Craftsmanship NYC

October 27, 2016

Page 2: Software Craftsmanship NYC Meetup:  The Pony Programming Language

About MeName

Andrew Turley

Contact@casio_juarez / [email protected]

Career

Currently at Sendence

Page 3: Software Craftsmanship NYC Meetup:  The Pony Programming Language

What Is Pony?

Page 4: Software Craftsmanship NYC Meetup:  The Pony Programming Language

What Is Pony?“Pony is an open-source, object-oriented, actor-model, capabilities-secure, high performance programming language.” -- ponylang.org

Page 5: Software Craftsmanship NYC Meetup:  The Pony Programming Language

What Is Pony?“Pony is an open-source, object-oriented, actor-model, capabilities-secure, high performance programming language.” -- ponylang.org

BSD licenseCopyright (c) 2014-2015, Causality Ltd.All rights reserved.

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

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.2. 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.

Page 6: Software Craftsmanship NYC Meetup:  The Pony Programming Language

What Is Pony?“Pony is an open-source, object-oriented, actor-model, capabilities-secure, high performance programming language.” -- ponylang.org

Classes, Interfaces, Traits(but they may work a little differently than you expect)

Page 7: Software Craftsmanship NYC Meetup:  The Pony Programming Language

What Is Pony?“Pony is an open-source, object-oriented, actor-model, capabilities-secure, high performance programming language.” -- ponylang.org

Actors communicate by passing messages to other

actors

Page 8: Software Craftsmanship NYC Meetup:  The Pony Programming Language

What Is Pony?“Pony is an open-source, object-oriented, actor-model, capabilities-secure, high performance programming language.” -- ponylang.org

The compiler enforces what you can and cannot do with

an object

Page 9: Software Craftsmanship NYC Meetup:  The Pony Programming Language

What Is Pony?“Pony is an open-source, object-oriented, actor-model, capabilities-secure, high performance programming language.” -- ponylang.org

Uses LLVM to compile to native code

Page 10: Software Craftsmanship NYC Meetup:  The Pony Programming Language

What Is Pony?“Pony is an open-source, object-oriented, actor-model, capabilities-secure, high performance programming language.” -- ponylang.org

Also:

● powerful type system (unions, intersections, parameterized types and functions)

● fast actor-based garbage collection system

Page 11: Software Craftsmanship NYC Meetup:  The Pony Programming Language

hello.pony

Page 12: Software Craftsmanship NYC Meetup:  The Pony Programming Language

hello.ponyactor Main new create(env: Env) => env.out.print(“hello world”)

Page 13: Software Craftsmanship NYC Meetup:  The Pony Programming Language

more-interesting.pony

Page 14: Software Craftsmanship NYC Meetup:  The Pony Programming Language

more-interesting.ponytrait Shape fun area(): F64

interface Named fun name(): String

class Circle is Shape let _radius: F64 new create(radius: F64) => _radius = radius fun name(): String => "circle" fun area(): F64 => 3.14159 * _radius * _radius

class Square is Shape let _side: F64 new create(side: F64) => _side = side fun name(): String => "square" fun area(): F64 => _side * _side

primitive AreaReporter fun report(shape: (Shape & Named)): String => "The area of this " + shape.name() + " is " + shape.area().string()

actor Main new create(env: Env) => let s: F64 = 15.3 var area = AreaReporter.report(Circle(s)) env.out.print(area) area = AreaReporter.report(Square(s)) env.out.print(area)

Page 15: Software Craftsmanship NYC Meetup:  The Pony Programming Language

more-interesting.ponytrait Shape fun area(): F64

interface Named fun name(): String

class Circle is Shape let _radius: F64 new create(radius: F64) => _radius = radius fun name(): String => "circle" fun area(): F64 => 3.14159 * _radius * _radius

class Square is Shape let _side: F64 new create(side: F64) => _side = side fun name(): String => "square" fun area(): F64 => _side * _side

primitive AreaReporter fun report(shape: (Shape & Named)): String => "The area of this " + shape.name() + " is " + shape.area().string()

actor Main new create(env: Env) => let s: F64 = 15.3 var area = AreaReporter.report(Circle(s)) env.out.print(area) area = AreaReporter.report(Square(s)) env.out.print(area)

Page 16: Software Craftsmanship NYC Meetup:  The Pony Programming Language

more-interesting.ponytrait Shape fun area(): F64

interface Named fun name(): String

class Circle is Shape let _radius: F64 new create(radius: F64) => _radius = radius fun name(): String => "circle" fun area(): F64 => 3.14159 * _radius * _radius

class Square is Shape let _side: F64 new create(side: F64) => _side = side fun name(): String => "square" fun area(): F64 => _side * _side

primitive AreaReporter fun report(shape: (Shape & Named)): String => "The area of this " + shape.name() + " is " + shape.area().string()

actor Main new create(env: Env) => let s: F64 = 15.3 var area = AreaReporter.report(Circle(s)) env.out.print(area) area = AreaReporter.report(Square(s)) env.out.print(area)

traits: nominal subtyping

Page 17: Software Craftsmanship NYC Meetup:  The Pony Programming Language

more-interesting.ponytrait Shape fun area(): F64

interface Named fun name(): String

class Circle is Shape let _radius: F64 new create(radius: F64) => _radius = radius fun name(): String => "circle" fun area(): F64 => 3.14159 * _radius * _radius

class Square is Shape let _side: F64 new create(side: F64) => _side = side fun name(): String => "square" fun area(): F64 => _side * _side

primitive AreaReporter fun report(shape: (Shape & Named)): String => "The area of this " + shape.name() + " is " + shape.area().string()

actor Main new create(env: Env) => let s: F64 = 15.3 var area = AreaReporter.report(Circle(s)) env.out.print(area) area = AreaReporter.report(Square(s)) env.out.print(area)

Page 18: Software Craftsmanship NYC Meetup:  The Pony Programming Language

more-interesting.ponytrait Shape fun area(): F64

interface Named fun name(): String

class Circle is Shape let _radius: F64 new create(radius: F64) => _radius = radius fun name(): String => "circle" fun area(): F64 => 3.14159 * _radius * _radius

class Square is Shape let _side: F64 new create(side: F64) => _side = side fun name(): String => "square" fun area(): F64 => _side * _side

primitive AreaReporter fun report(shape: (Shape & Named)): String => "The area of this " + shape.name() + " is " + shape.area().string()

actor Main new create(env: Env) => let s: F64 = 15.3 var area = AreaReporter.report(Circle(s)) env.out.print(area) area = AreaReporter.report(Square(s)) env.out.print(area)

interfaces: structural subtyping

Page 19: Software Craftsmanship NYC Meetup:  The Pony Programming Language

more-interesting.ponytrait Shape fun area(): F64

interface Named fun name(): String

class Circle is Shape let _radius: F64 new create(radius: F64) => _radius = radius fun name(): String => "circle" fun area(): F64 => 3.14159 * _radius * _radius

class Square is Shape let _side: F64 new create(side: F64) => _side = side fun name(): String => "square" fun area(): F64 => _side * _side

primitive AreaReporter fun report(shape: (Shape & Named)): String => "The area of this " + shape.name() + " is " + shape.area().string()

actor Main new create(env: Env) => let s: F64 = 15.3 var area = AreaReporter.report(Circle(s)) env.out.print(area) area = AreaReporter.report(Square(s)) env.out.print(area)

Page 20: Software Craftsmanship NYC Meetup:  The Pony Programming Language

more-interesting.ponytrait Shape fun area(): F64

interface Named fun name(): String

class Circle is Shape let _radius: F64 new create(radius: F64) => _radius = radius fun name(): String => "circle" fun area(): F64 => 3.14159 * _radius * _radius

class Square is Shape let _side: F64 new create(side: F64) => _side = side fun name(): String => "square" fun area(): F64 => _side * _side

primitive AreaReporter fun report(shape: (Shape & Named)): String => "The area of this " + shape.name() + " is " + shape.area().string()

actor Main new create(env: Env) => let s: F64 = 15.3 var area = AreaReporter.report(Circle(s)) env.out.print(area) area = AreaReporter.report(Square(s)) env.out.print(area)

Page 21: Software Craftsmanship NYC Meetup:  The Pony Programming Language

more-interesting.ponytrait Shape fun area(): F64

interface Named fun name(): String

class Circle is Shape let _radius: F64 new create(radius: F64) => _radius = radius fun name(): String => "circle" fun area(): F64 => 3.14159 * _radius * _radius

class Square is Shape let _side: F64 new create(side: F64) => _side = side fun name(): String => "square" fun area(): F64 => _side * _side

primitive AreaReporter fun report(shape: (Shape & Named)): String => "The area of this " + shape.name() + " is " + shape.area().string()

actor Main new create(env: Env) => let s: F64 = 15.3 var area = AreaReporter.report(Circle(s)) env.out.print(area) area = AreaReporter.report(Square(s)) env.out.print(area)

primitive: object with no data and only one instance

Page 22: Software Craftsmanship NYC Meetup:  The Pony Programming Language

more-interesting.ponytrait Shape fun area(): F64

interface Named fun name(): String

class Circle is Shape let _radius: F64 new create(radius: F64) => _radius = radius fun name(): String => "circle" fun area(): F64 => 3.14159 * _radius * _radius

class Square is Shape let _side: F64 new create(side: F64) => _side = side fun name(): String => "square" fun area(): F64 => _side * _side

primitive AreaReporter fun report(shape: (Shape & Named)): String => "The area of this " + shape.name() + " is " + shape.area().string()

actor Main new create(env: Env) => let s: F64 = 15.3 var area = AreaReporter.report(Circle(s)) env.out.print(area) area = AreaReporter.report(Square(s)) env.out.print(area)

Page 23: Software Craftsmanship NYC Meetup:  The Pony Programming Language

more-interesting.ponytrait Shape fun area(): F64

interface Named fun name(): String

class Circle is Shape let _radius: F64 new create(radius: F64) => _radius = radius fun name(): String => "circle" fun area(): F64 => 3.14159 * _radius * _radius

class Square is Shape let _side: F64 new create(side: F64) => _side = side fun name(): String => "square" fun area(): F64 => _side * _side

primitive AreaReporter fun report(shape: (Shape & Named)): String => "The area of this " + shape.name() + " is " + shape.area().string()

actor Main new create(env: Env) => let s: F64 = 15.3 var area = AreaReporter.report(Circle(s)) env.out.print(area) area = AreaReporter.report(Square(s)) env.out.print(area)

> ./ponyc src/more-interesting> ./more-interestingThe area of this circle is 735.415The area of this square is 234.09>

Page 24: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Pony: The Really Interesting Parts

Page 25: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Pony: The Really Interesting PartsPony uses actors and reference capabilities to allow the compiler to guarantee that a program is data-race-free.

Page 26: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Pony: The Really Interesting PartsPony uses actors and reference capabilities to allow the compiler to guarantee that a program is data-race-free.

Page 27: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Pony: The Really Interesting PartsPony uses actors and reference capabilities to allow the compiler to guarantee

that a program is data-race-free.

Page 28: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Pony: The Really Interesting PartsPony uses actors and reference capabilities to allow the compiler to guarantee

that a program is data-race-free.

Page 29: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Pony: The Really Interesting PartsPony uses actors and reference capabilities to allow the compiler to guarantee

that a program is data-race-free.

Page 30: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Pony: The Really Interesting PartsPony uses actors and reference capabilities to allow the compiler to guarantee

that a program is data-race-free.

Page 31: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Pony: The Really Interesting PartsPony uses actors and reference capabilities to allow the compiler to guarantee that a program is

data-race-free.

Page 32: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Pony: The Really Interesting Parts

data-race-free(this is the part you should remember)

Page 33: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Off To The Data Races!

Page 34: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Off To The Data Races!Some pseudo code (not Pony) …

global int a = 0

function inc() { for x in range(0, 1000001) { a = a + 1 }}

function main() { inc() print(“a = “ + a)}

1000000

Page 35: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Off To The Data Races!Some more pseudo code (not Pony) …

global int a = 0

function inc() { for x in range(0, 1000001) { a = a + 1 }}

function main() { thread thread1 = Thread(inc) thread thread2 = Thread(inc) thread1.run() thread2.run() thread1.join() thread2.join() print(“a = “ + a)}

● Run “inc()” simultaneously in two places

● wait for both runs to finish● print the value of “a”

Page 36: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Off To The Data Races!Some more pseudo code (not Pony) …

global int a = 0

function inc() { for x in range(0, 1000001) { a = a + 1 }}

function main() { thread thread1 = Thread(inc) thread thread2 = Thread(inc) thread1.run() thread2.run() thread1.join() thread2.join() print(“a = “ + a)}

Expected (two threads each

incrementing a variable

1000000 times):

2000000

Page 37: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Off To The Data Races!Some more pseudo code (not Pony) …

global int a = 0

function inc() { for x in range(0, 1000001) { a = a + 1 }}

function main() { thread thread1 = Thread(inc) thread thread2 = Thread(inc) thread1.run() thread2.run() thread1.join() thread2.join() print(“a = “ + a)}

Expected:

2000000

Actual Run 1:

1987735

Actual Run 2:

1935010

Actual Run 3:

1941217

Page 38: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Off To The Data Races!Some more pseudo code (not Pony) …

global int a = 0

function inc() { for x in range(0, 1000001) { a = a + 1 }}

function main() { thread thread1 = Thread(inc) thread thread2 = Thread(inc) thread1.run() thread2.run() thread1.join() thread2.join() print(“a = “ + a)}

Expected:

2000000

Actual Run 1:

1987735

Actual Run 2:

1935010

Actual Run 3:

1941217

WHY?

Page 39: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Off To The Data Races!In thread1 …

// get the value of “a”// add 1 to that value// write the new value back to “a”a = a + 1

Meanwhile, in thread2...

// get the value of “a”// add 1 to that value// write the new value back to “a”a = a + 1

a = 26

Page 40: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Off To The Data Races!In thread1 …

// get the value of “a” 26// add 1 to that value// write the new value back to “a”a = a + 1

Meanwhile, in thread2...

// get the value of “a” 26// add 1 to that value// write the new value back to “a”a = a + 1

a = 26

Page 41: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Off To The Data Races!In thread1 …

// get the value of “a” 26// add 1 to that value 26 + 1 = 27// write the new value back to “a”a = a + 1

Meanwhile, in thread2...

// get the value of “a” 26// add 1 to that value 26 + 1 = 27// write the new value back to “a”a = a + 1

a = 26

Page 42: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Off To The Data Races!In thread1 …

// get the value of “a” 26// add 1 to that value 26 + 1 = 27// write the new value back to “a” a = 27a = a + 1

Meanwhile, in thread2...

// get the value of “a” 26// add 1 to that value 26 + 1 = 27// write the new value back to “a” a = 27a = a + 1

a = 27

Page 43: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Off To The Data Races!In thread1 …

// get the value of “a” 26// add 1 to that value 26 + 1 = 27// write the new value back to “a” a = 27a = a + 1

Meanwhile, in thread2...

// get the value of “a” 26// add 1 to that value 26 + 1 = 27// write the new value back to “a” a = 27a = a + 1

a = 27

We wanted a = 28

Page 44: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Off To The Data Races!In thread1 …

// get the value of “a” 26// add 1 to that value 26 + 1 = 27// write the new value back to “a” a = 27a = a + 1

Meanwhile, in thread2...

// get the value of “a” 26// add 1 to that value 26 + 1 = 27// write the new value back to “a” a = 27a = a + 1

a = 27

This may not happen every time, but each time it happens it increases the error of the result.

We wanted a = 28

Page 45: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Off To The Data Races!In thread1 …

// get the value of “a” 26// add 1 to that value 26 + 1 = 27// write the new value back to “a” a = 27a = a + 1

Meanwhile, in thread2...

// get the value of “a” 26// add 1 to that value 26 + 1 = 27// write the new value back to “a” a = 27a = a + 1

a = 27

This may not happen every time, but each time it happens it increases the error of the result.

We wanted a = 28

“Shared mutable state is the root of all evil.” -- several different people, all at the same time

Page 46: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Off To The Data Races!Techniques various and sundry for avoiding data races ...

● Locks! → a unit of execution acquires a lock, no other unit of execution can acquire the lock until it is released

○ C and C++

● Synchronized blocks/functions/methods! → somebody writes the locks for you○ Java

● Everything is read-only! → don’t need to worry about writes anymore○ Erlang

● There’s only one binding to an object at any time! → move, borrow, copy○ Rust

Page 47: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Leaving The Data Races

Page 48: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Leaving The Data RacesPony uses two rules to avoid data races:

● The Read Rule: If an actor can read an object then no other actor can modify that object

● The Write Rule: If an actor can modify an object then no other actor can read or modify it

Page 49: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Actors

Page 50: Software Craftsmanship NYC Meetup:  The Pony Programming Language

ActorsActors store state and act on that state in response to messages

Page 51: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Actor● state● behaviors● functions

Actorsmessage3message4

message1message2 } Queue

Page 52: Software Craftsmanship NYC Meetup:  The Pony Programming Language

THE LIFE OF ACTORTHE LIFE OF ACTORTHE LIFE OF ACTORTHE LIFE OF ACTORTHE LIFE OF ACTOR

THE LIFE OF ACTOR

WHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONE

Actorsmessage3message4

message1message2 } Queue

Page 53: Software Craftsmanship NYC Meetup:  The Pony Programming Language

THE LIFE OF ACTORTHE LIFE OF ACTORTHE LIFE OF ACTORTHE LIFE OF ACTORTHE LIFE OF ACTOR

THE LIFE OF ACTOR

WHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONE

Actors

Get next message

message3message4

message1message2 } Queue

Page 54: Software Craftsmanship NYC Meetup:  The Pony Programming Language

THE LIFE OF ACTORTHE LIFE OF ACTORTHE LIFE OF ACTORTHE LIFE OF ACTORTHE LIFE OF ACTOR

THE LIFE OF ACTOR

WHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONE

Actors

Get next message

Processmessage

message3message4

message1message2 } Queue

Page 55: Software Craftsmanship NYC Meetup:  The Pony Programming Language

THE LIFE OF ACTORTHE LIFE OF ACTORTHE LIFE OF ACTORTHE LIFE OF ACTORTHE LIFE OF ACTOR

THE LIFE OF ACTOR

WHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONE

Actors

Get next message

Processmessage

Collect garbage

message3message4

message1message2 } Queue

Page 56: Software Craftsmanship NYC Meetup:  The Pony Programming Language

THE LIFE OF ACTORTHE LIFE OF ACTORTHE LIFE OF ACTORTHE LIFE OF ACTORTHE LIFE OF ACTOR

THE LIFE OF ACTOR

WHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONEWHICH / ONE WHICH / ONE

Actors

Get next message

Processmessage

Collect garbage

message3message4

message1message2 } Queue

Page 57: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Actorsactor Donald

be foo(x: Something) =>

bar(x)

fun bar(x: Something) =>

// do something …

actor Jessica

let _d: Donald = Donald

let _s: Something = Something

be baz() =>

_d.foo(_s)

Page 58: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Actorsactor Donald

be foo(x: Something) =>

bar(x)

fun bar(x: Something) =>

// do something …

actor Jessica

let _d: Donald = Donald

let _s: Something = Something

be baz() =>

_d.foo(_s)

“be” means “behavior”, this is what processes a message

Page 59: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Actorsactor Donald

be foo(x: Something) =>

bar(x)

fun bar(x: Something) =>

// do something …

actor Jessica

let _d: Donald = Donald

let _s: Something = Something

be baz() =>

_d.foo(_s)

“fun” means “function”, these are run by the actor as part of processing the message

Page 60: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Actorsactor Donald

be foo(x: Something) =>

bar(x)

fun bar(x: Something) =>

// do something …

actor Jessica

let _d: Donald = Donald

let _s: Something = Something

be baz() =>

_d.foo(_s)

An actor sends a message to another actor using the “<a>.<be>(...)” syntax.

Page 61: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Actorsactor Donald

be foo(x: Something) =>

bar(x)

fun bar(x: Something) =>

// do something …

actor Jessica

let _d: Donald = Donald

let _s: Something = Something

be baz() =>

_d.foo(_s)

Calling a behavior places a message on the receiving actor’s queue.

Page 62: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Actorsactor Donald

be foo(x: Something) =>

bar(x)

fun bar(x: Something) =>

// do something …

actor Jessica

let _d: Donald = Donald

let _s: Something = Something

be baz() =>

_d.foo(_s)

Calling a behavior is asynchronous. The call returns immediately and the return value is the actor.

Page 63: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Actorsactor Donald

be foo(x: Something) =>

bar(x)

fun bar(x: Something) =>

// do something …

actor Jessica

let _d: Donald = Donald

let _s: Something = Something

be baz() =>

_d.foo(_s)

When talking about Pony, “method” means either a function or a behavior.

Page 64: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Actors

a1.bar() a2.baz() a1.bar()

a3.dee() a4.doo() a3.doh()

a5.moo()

a8.fee() a7.foo()

a6.mee()

a7.fuz()

time

CPU1: thread1

CPU2: thread2

CPU3: thread3

CPU4: thread4

Actors run on threads (1 thread per CPU by default)

Page 65: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Actors

a1.bar() a2.baz()1 a1.bar()

a3.dee() a4.doo() a3.doh()

a5.moo()

a8.fee() a7.foo()

a6.mee()

a7.fuz()

time

CPU1: thread1

CPU2: thread2

CPU3: thread3

CPU4: thread4

a2.baz()2

Behaviors cannot be preempted

Page 66: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Actors

a1.bar() a2.baz() a1.bar()

a1.buz() a4.doo() a3.doh()

a5.moo()

a8.fee() a7.foo()

a6.mee()

a7.fuz()

time

CPU1: thread1

CPU2: thread2

CPU3: thread3

CPU4: thread4

Actors process one message at a time

Page 67: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Actors: An Example

Page 68: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Actors: An Example

actor Example

var _text: String

new create(text: String) =>

_text = text

be foo(o: Other) =>

o.say(rev())

fun ref rev(): String val =>

_text = recover

_text.reverse()

end

_text

actor Other

let _env: Env

new create(env: Env) =>

_env = env

be say(s: String) =>

_env.out.print(s)

actor Main

new create(env: Env) =>

let e = Example("howdy")

let o = Other(env)

e.foo(o) // prints “ydwoh”

e.foo(o) // prints “howdy”

Page 69: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Actors: An Example

actor Example

var _text: String

new create(text: String) =>

_text = text

be foo(o: Other) =>

o.say(rev())

fun ref rev(): String val =>

_text = recover

_text.reverse()

end

_text

actor Other

let _env: Env

new create(env: Env) =>

_env = env

be say(s: String) =>

_env.out.print(s)

actor Main

new create(env: Env) =>

let e = Example("howdy")

let o = Other(env)

e.foo(o) // prints “ydwoh”

e.foo(o) // prints “howdy”

Main

create(env)

Page 70: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Actors: An Example

actor Example

var _text: String

new create(text: String) =>

_text = text

be foo(o: Other) =>

o.say(rev())

fun ref rev(): String val =>

_text = recover

_text.reverse()

end

_text

actor Other

let _env: Env

new create(env: Env) =>

_env = env

be say(s: String) =>

_env.out.print(s)

actor Main

new create(env: Env) =>

let e = Example("howdy")

let o = Other(env)

e.foo(o) // prints “ydwoh”

e.foo(o) // prints “howdy”

Maincreate(env)

Page 71: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Actors: An Example

actor Example

var _text: String

new create(text: String) =>

_text = text

be foo(o: Other) =>

o.say(rev())

fun ref rev(): String val =>

_text = recover

_text.reverse()

end

_text

actor Other

let _env: Env

new create(env: Env) =>

_env = env

be say(s: String) =>

_env.out.print(s)

actor Main

new create(env: Env) =>

let e = Example("howdy")

let o = Other(env)

e.foo(o) // prints “ydwoh”

e.foo(o) // prints “howdy”

Main e

create(“howdy”)

create(env)

Page 72: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Actors: An Example

actor Example

var _text: String

new create(text: String) =>

_text = text

be foo(o: Other) =>

o.say(rev())

fun ref rev(): String val =>

_text = recover

_text.reverse()

end

_text

actor Other

let _env: Env

new create(env: Env) =>

_env = env

be say(s: String) =>

_env.out.print(s)

actor Main

new create(env: Env) =>

let e = Example("howdy")

let o = Other(env)

e.foo(o) // prints “ydwoh”

e.foo(o) // prints “howdy”

Main e

create(env)

o

create(“howdy)

create(env)

Page 73: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Actors: An Example

actor Example

var _text: String

new create(text: String) =>

_text = text

be foo(o: Other) =>

o.say(rev())

fun ref rev(): String val =>

_text = recover

_text.reverse()

end

_text

actor Other

let _env: Env

new create(env: Env) =>

_env = env

be say(s: String) =>

_env.out.print(s)

actor Main

new create(env: Env) =>

let e = Example("howdy")

let o = Other(env)

e.foo(o) // prints “ydwoh”

e.foo(o) // prints “howdy”

Maincreate(env)create(“howdy)

foo(o)

e_text=”howdy”

o_env=envcreate(env)

Page 74: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Actors: An Example

actor Example

var _text: String

new create(text: String) =>

_text = text

be foo(o: Other) =>

o.say(rev())

fun ref rev(): String val =>

_text = recover

_text.reverse()

end

_text

actor Other

let _env: Env

new create(env: Env) =>

_env = env

be say(s: String) =>

_env.out.print(s)

actor Main

new create(env: Env) =>

let e = Example("howdy")

let o = Other(env)

e.foo(o) // prints “ydwoh”

e.foo(o) // prints “howdy”

Main e_text=”howdy”

o_env=env

foo(o)1

foo(o)2

create(env)

Page 75: Software Craftsmanship NYC Meetup:  The Pony Programming Language

foo(o)1

Actors: An Example

actor Example

var _text: String

new create(text: String) =>

_text = text

be foo(o: Other) =>

o.say(rev())

fun ref rev(): String val =>

_text = recover

_text.reverse()

end

_text

actor Other

let _env: Env

new create(env: Env) =>

_env = env

be say(s: String) =>

_env.out.print(s)

actor Main

new create(env: Env) =>

let e = Example("howdy")

let o = Other(env)

e.foo(o) // prints “ydwoh”

e.foo(o) // prints “howdy”

Main e_text=”ydwoh”

o_env=env

foo(o)2

Page 76: Software Craftsmanship NYC Meetup:  The Pony Programming Language

foo(o)1

Actors: An Example

actor Example

var _text: String

new create(text: String) =>

_text = text

be foo(o: Other) =>

o.say(rev())

fun ref rev(): String val =>

_text = recover

_text.reverse()

end

_text

actor Other

let _env: Env

new create(env: Env) =>

_env = env

be say(s: String) =>

_env.out.print(s)

actor Main

new create(env: Env) =>

let e = Example("howdy")

let o = Other(env)

e.foo(o) // prints “ydwoh”

e.foo(o) // prints “howdy”

Main e_text=”ydwoh”

o_env=env

foo(o)2 say(“ydwoh”)

Page 77: Software Craftsmanship NYC Meetup:  The Pony Programming Language

say(“ydwoh”)foo(o)2

Actors: An Example

actor Example

var _text: String

new create(text: String) =>

_text = text

be foo(o: Other) =>

o.say(rev())

fun ref rev(): String val =>

_text = recover

_text.reverse()

end

_text

actor Other

let _env: Env

new create(env: Env) =>

_env = env

be say(s: String) =>

_env.out.print(s)

actor Main

new create(env: Env) =>

let e = Example("howdy")

let o = Other(env)

e.foo(o) // prints “ydwoh”

e.foo(o) // prints “howdy”

Main e_text=”howdy”

o_env=env

YDWOH

Page 78: Software Craftsmanship NYC Meetup:  The Pony Programming Language

foo(o)2

Actors: An Example

actor Example

var _text: String

new create(text: String) =>

_text = text

be foo(o: Other) =>

o.say(rev())

fun ref rev(): String val =>

_text = recover

_text.reverse()

end

_text

actor Other

let _env: Env

new create(env: Env) =>

_env = env

be say(s: String) =>

_env.out.print(s)

actor Main

new create(env: Env) =>

let e = Example("howdy")

let o = Other(env)

e.foo(o) // prints “ydwoh”

e.foo(o) // prints “howdy”

Main e_text=”howdy”

o_env=env

say(“howdy”)

YDWOH

Page 79: Software Craftsmanship NYC Meetup:  The Pony Programming Language

say(“howdy”)

Actors: An Example

actor Example

var _text: String

new create(text: String) =>

_text = text

be foo(o: Other) =>

o.say(rev())

fun ref rev(): String val =>

_text = recover

_text.reverse()

end

_text

actor Other

let _env: Env

new create(env: Env) =>

_env = env

be say(s: String) =>

_env.out.print(s)

actor Main

new create(env: Env) =>

let e = Example("howdy")

let o = Other(env)

e.foo(o) // prints “ydwoh”

e.foo(o) // prints “howdy”

Main e_text=”howdy”

o_env=env

YDWOHHOWDY

Page 80: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Actors: An Example

actor Example

var _text: String

new create(text: String) =>

_text = text

be foo(o: Other) =>

o.say(rev())

fun ref rev(): String val =>

_text = recover

_text.reverse()

end

_text

actor Other

let _env: Env

new create(env: Env) =>

_env = env

be say(s: String) =>

_env.out.print(s)

actor Main

new create(env: Env) =>

let e = Example("howdy")

let o = Other(env)

e.foo(o) // prints “ydwoh”

e.foo(o) // prints “howdy”

Main e_text=”howdy”

o_env=env

YDWOHHOWDY

Page 81: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities

Page 82: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference CapabilitiesRemember the Read Rule and the Write Rule:

● The Read Rule: If an actor can read an object then no other actor can modify that object

● The Write Rule: If an actor can modify an object then no other actor can read or modify it

Page 83: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference CapabilitiesReference capabilities control whether a given alias can be used to read or modify an object.

The collection of reference capabilities for alias that refer to an object must be consistent with the Read Rule and the Write Rule.

Any alias to an actor, regardless of reference capability, can be used to send messages to that actor.

Page 84: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilitiesclass Foo

class val Bar let v: U32 new val create(vv: U32) => v = vv

actor Main new create(env: Env) => let a: Foo iso = recover Foo end var b = Bar(1) b = Bar(2) baz(b) fun baz(c: Bar): U32 => c.v + 16

Page 85: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference CapabilitiesSPOT THE REFERENCE CAPABILITIES!

class Foo

class val Bar let v: U32 new val create(vv: U32) => v = vv

actor Main new create(env: Env) => let a: Foo iso = recover Foo end var b = Bar(1) b = Bar(2) baz(b) fun baz(c: Bar): U32 => c.v + 16

Page 86: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference CapabilitiesSPOT THE EXPLICIT REFERENCE CAPABILITIES!

SPOT THE IMPLIED REFERENCE CAPABILITIES!

class ref Foo

class val Bar let v: U32 val new val create(vv: U32 val) => v = vv

actor tag Main new create(env: Env val) => let a: Foo iso = recover iso Foo end var b: Bar val = Bar(1) b = Bar(2) baz(b) fun box baz(c: Bar val): U32 val => c.v + 16

Page 87: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference CapabilitiesActors have a default reference capability of tag, objects created from classes have a default reference capability of ref

class ref Foo

class val Bar let v: U32 val new val create(vv: U32 val) => v = vv

actor tag Main new create(env: Env val) => let a: Foo iso = recover iso Foo end var b: Bar val = Bar(1) b = Bar(2) baz(b) fun box baz(c: Bar val): U32 val => c.v + 16

Page 88: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference CapabilitiesYou can change the implicit reference capability of a class (normally it is ref)

You can change the reference capability of the object generated by the constructor (normally it is ref)

class ref Foo

class val Bar let v: U32 val new val create(vv: U32 val) => v = vv

actor tag Main new create(env: Env val) => let a: Foo iso = recover iso Foo end var b: Bar val = Bar(1) b = Bar(2) baz(b) fun box baz(c: Bar val): U32 val => c.v + 16

Page 89: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference CapabilitiesYou can specify the type of reference capability that the receiver must have to call a function

class ref Foo

class val Bar let v: U32 val new val create(vv: U32 val) => v = vv

actor tag Main new create(env: Env val) => let a: Foo iso = recover iso Foo end var b: Bar val = Bar(1) b = Bar(2) baz(b) fun box baz(c: Bar val): U32 val => c.v + 16

Page 90: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference CapabilitiesYou can specify the type of reference capability that the receiver must have to call a function

This can get really tricky!

class ref Foo

class val Bar let v: U32 val new val create(vv: U32 val) => v = vv

actor tag Main new create(env: Env val) => let a: Foo iso = recover iso Foo end var b: Bar val = Bar(1) b = Bar(2) baz(b) fun box baz(c: Bar val): U32 val => c.v + 16

Page 91: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference CapabilitiesAn alias is a name given to a particular object in memory

Aliases are created when

● an object is assigned to a variable● an object is passed as an argument to a

method

class Foo

class val Bar let v: U32 new val create(vv: U32) => v = vv

actor Main new create(env: Env) => let a: Foo iso = recover Foo end var b = Bar(1) b = Bar(2) baz(b) fun baz(c: Bar): U32 => c.v + 16

Page 92: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference CapabilitiesAn alias is a name given to a particular object in memory

Aliases are created when

● an object is assigned to a variable● an object is passed as an argument to a

method

class Foo

class val Bar let v: U32 new val create(vv: U32) => v = vv

actor Main new create(env: Env) => let a: Foo iso = recover Foo end var b = Bar(1) b = Bar(2) baz(b) fun baz(c: Bar): U32 => c.v + 16

Page 93: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference CapabilitiesAn alias is a name given to a particular object in memory

Aliases are created when

● an object is assigned to a variable● an object is passed as an argument to a

method

class Foo

class val Bar let v: U32 new val create(vv: U32) => v = vv

actor Main new create(env: Env) => let a: Foo iso = recover Foo end var b = Bar(1) b = Bar(2) baz(b) fun baz(c: Bar): U32 => c.v + 16

Page 94: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference CapabilitiesAn alias is a name given to a particular object in memory

Aliases are created when

● an object is assigned to a variable● an object is passed as an argument to a

method

class Foo

class val Bar let v: U32 new val create(vv: U32) => v = vv

actor Main new create(env: Env) => let a: Foo iso = recover Foo end var b = Bar(1) b = Bar(2) baz(b) fun baz(c: Bar): U32 => c.v + 16

Page 95: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference CapabilitiesAn alias is a name given to a particular object in memory

Aliases are created when

● an object is assigned to a variable● an object is passed as an argument to a

method

class Foo

class val Bar let v: U32 new val create(vv: U32) => v = vv

actor Main new create(env: Env) => let a: Foo iso = recover Foo end var b = Bar(1) b = Bar(2) baz(b) fun baz(c: Bar): U32 => c.v + 16

Page 96: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference CapabilitiesAn object may have more than one alias, possibly in more than one actor, but the combination of aliases must not violate the read rule and write rule.

● The Read Rule: If an actor can read an object then no other actor can modify that object

● The Write Rule: If an actor can modify an object then no other actor can read or modify it

class Foo

class val Bar let v: U32 new val create(vv: U32) => v = vv

actor Main new create(env: Env) => let a: Foo iso = recover Foo end var b = Bar(1) b = Bar(2) baz(b) fun baz(c: Bar): U32 => c.v + 16

Page 97: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: A Visual Guide

Page 98: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: iso (isolated)● The Read Rule: If an

actor can read an object then no other actor can modify that object

● The Write Rule: If an actor can modify an object then no other actor can read or modify it

iso reference can read and modify an object. No other reference can read or modify the object.

object

alias

alias

alias

Actor A Actor B

iso

Page 99: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: trn (transitional)● The Read Rule: If an

actor can read an object then no other actor can modify that object

● The Write Rule: If an actor can modify an object then no other actor can read or modify it

trn reference can read and modify an object. No other reference can modify the object, but the actor may have other references that can read the object.

object

alias

alias

alias

Actor A Actor B

trn

Page 100: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: ref (reference)● The Read Rule: If an

actor can read an object then no other actor can modify that object

● The Write Rule: If an actor can modify an object then no other actor can read or modify it

ref reference can read and modify an object. Other references in the actor may be able to read or modify the object, but no other actor may have a reference that can read or modify it.

object

alias

alias

alias

Actor A Actor B

ref

Page 101: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: val (value)● The Read Rule: If an

actor can read an object then no other actor can modify that object

● The Write Rule: If an actor can modify an object then no other actor can read or modify it

val reference can read an object. The actor may have other references that can read the object, and other actors may have references that can read the object, but no actor may have a reference that can modify it.

object

alias

alias

alias

Actor A Actor B

val

Page 102: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: box (box)● The Read Rule: If an

actor can read an object then no other actor can modify that object

● The Write Rule: If an actor can modify an object then no other actor can read or modify it

object

alias

alias

alias

Actor A Actor B

box

OR

object

alias

alias

alias

Actor A Actor B

box

Page 103: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: box (box)● The Read Rule: If an

actor can read an object then no other actor can modify that object

● The Write Rule: If an actor can modify an object then no other actor can read or modify it

object

alias

alias

alias

Actor A Actor B

box

OR

object

alias

alias

alias

Actor A Actor B

box

This looks like a val

This looks like a ref

A box capability is used when you want to create a new read-only reference to an object that is either val or ref.

Page 104: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: box (box)class X let v: I32 new create(v': I32) => v = v'

actor Main new create(env: Env) => let a: X ref = X(7) let b: X val = recover X(8) end bar(a) bar(b) fun bar(x: X ???): I32 => x.v() + 1

What should the reference capability be?

Page 105: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: box (box)class X let v: I32 new create(v': I32) => v = v'

actor Main new create(env: Env) => let a: X ref = X(7) let b: X val = recover X(8) end bar(a) bar(b) fun bar(x: X ref): I32 => x.v() + 1

Page 106: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: box (box)class X let v: I32 new create(v': I32) => v = v'

actor Main new create(env: Env) => let a: X ref = X(7) let b: X val = recover X(8) end bar(a) bar(b) fun bar(x: X ref): I32 => x.v() + 1

ref doesn’t work because a ref (x) can’t alias a val (b)

Page 107: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: box (box)class X let v: I32 new create(v': I32) => v = v'

actor Main new create(env: Env) => let a: X ref = X(7) let b: X val = recover X(8) end bar(a) bar(b) fun bar(x: X ref): I32 => x.v() + 1

class X let v: I32 new create(v': I32) => v = v'

actor Main new create(env: Env) => let a: X ref = X(7) let b: X val = recover X(8) end bar(a) bar(b) fun bar(x: X val): I32 => x.v() + 1

ref doesn’t work because a ref (x) can’t alias a val (b)

Page 108: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: box (box)class X let v: I32 new create(v': I32) => v = v'

actor Main new create(env: Env) => let a: X ref = X(7) let b: X val = recover X(8) end bar(a) bar(b) fun bar(x: X ref): I32 => x.v() + 1

class X let v: I32 new create(v': I32) => v = v'

actor Main new create(env: Env) => let a: X ref = X(7) let b: X val = recover X(8) end bar(a) bar(b) fun bar(x: X val): I32 => x.v() + 1

ref doesn’t work because a ref (x) can’t alias a val (b)

val Doesn’t work because a val (x) can’t alias a ref (a)

Page 109: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: box (box)class X let v: I32 new create(v': I32) => v = v'

actor Main new create(env: Env) => let a: X ref = X(7) let b: X val = recover X(8) end bar(a) bar(b) fun bar(x: X ref): I32 => x.v() + 1

class X let v: I32 new create(v': I32) => v = v'

actor Main new create(env: Env) => let a: X ref = X(7) let b: X val = recover X(8) end bar(a) bar(b) fun bar(x: X val): I32 => x.v() + 1

ref doesn’t work because a ref (x) can’t alias a val (b)

val Doesn’t work because a val (x) can’t alias a ref (a)

Page 110: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: box (box)class X let v: I32 new create(v': I32) => v = v'

actor Main new create(env: Env) => let a: X ref = X(7) let b: X val = recover X(8) end bar(a) bar(b) fun bar(x: X ref): I32 => x.v() + 1

class X let v: I32 new create(v': I32) => v = v'

actor Main new create(env: Env) => let a: X ref = X(7) let b: X val = recover X(8) end bar(a) bar(b) fun bar(x: X val): I32 => x.v() + 1

ref doesn’t work because a ref (x) can’t alias a val (b)

val Doesn’t work because a val (x) can’t alias a ref (a)

class X let v: I32 new create(v': I32) => v = v'

actor Main new create(env: Env) => let a: X ref = X(7) let b: X val = recover X(8) end bar(a) bar(b) fun bar(x: X box): I32 => x.v() + 1

Page 111: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: box (box)class X let v: I32 new create(v': I32) => v = v'

actor Main new create(env: Env) => let a: X ref = X(7) let b: X val = recover X(8) end bar(a) bar(b) fun bar(x: X ref): I32 => x.v() + 1

class X let v: I32 new create(v': I32) => v = v'

actor Main new create(env: Env) => let a: X ref = X(7) let b: X val = recover X(8) end bar(a) bar(b) fun bar(x: X val): I32 => x.v() + 1

ref doesn’t work because a ref (x) can’t alias a val (b)

val Doesn’t work because a val (x) can’t alias a ref (a)

class X let v: I32 new create(v': I32) => v = v'

actor Main new create(env: Env) => let a: X ref = X(7) let b: X val = recover X(8) end bar(a) bar(b) fun bar(x: X box): I32 => x.v() + 1

box works because a box (x) can alias a ref (a) or a val (b)

Page 112: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: tag (tag)● The Read Rule: If an

actor can read an object then no other actor can modify that object

● The Write Rule: If an actor can modify an object then no other actor can read or modify it

tag reference cannot read or modify an object, but it can be used to send the object messages if the object is an actor. Other references may read or modify the object as long as they do not violate the Read Rule and the Write Rule.

object

alias

alias

alias

Actor A Actor B

tag

Page 113: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: tag (tag)● The Read Rule: If an

actor can read an object then no other actor can modify that object

● The Write Rule: If an actor can modify an object then no other actor can read or modify it

tag reference cannot read or modify an object, but it can be used to send the object messages if the object is an actor. Other references may read or modify the object as long as they do not violate the Read Rule and the Write Rule.

object

alias

alias

alias

Actor A Actor B

tag

Page 114: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference CapabilitiesReadable → iso, trn, ref, val, box

Page 115: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference CapabilitiesReadable → iso, trn, ref, val, box

Writeable → iso, trn, ref

Page 116: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference CapabilitiesReadable → iso, trn, ref, val, box

Writeable → iso, trn, ref

Sendable → iso, val, tag

Objects with sendable reference capabilities can be sent to other actors in messages

Page 117: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: Sending A valclass Bar

actor Foo be baz(x: Bar val) => // do something with x

actor Main new create(env: Env) => let f = Foo let b = recover val Bar end f.baz(b)

Page 118: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: Sending A valclass Bar

actor Foo be baz(x: Bar val) => // do something with x

actor Main new create(env: Env) => let f = Foo let b = recover val Bar end f.baz(b)

Main

Page 119: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: Sending A valclass Bar

actor Foo be baz(x: Bar val) => // do something with x

actor Main new create(env: Env) => let f = Foo let b = recover val Bar end f.baz(b)

Main Foo

foo

Page 120: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: Sending A valclass Bar

actor Foo be baz(x: Bar val) => // do something with x

actor Main new create(env: Env) => let f = Foo let b = recover val Bar end f.baz(b)

Main Foo

f

Bar

b

Page 121: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: Sending A valclass Bar

actor Foo be baz(x: Bar val) => // do something with x

actor Main new create(env: Env) => let f = Foo let b = recover val Bar end f.baz(b)

Main Foo

f

Bar

b

baz( )

Page 122: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: Sending A valclass Bar

actor Foo be baz(x: Bar val) => // do something with x

actor Main new create(env: Env) => let f = Foo let b = recover val Bar end f.baz(b)

Main Foo

f

Bar

b

baz( )

x

Page 123: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: Sending A valclass Bar

actor Foo be baz(x: Bar val) => // do something with x

actor Main new create(env: Env) => let f = Foo let b = recover val Bar end f.baz(b)

Main Foo

f

Bar

b x

● The Read Rule: If an actor can read an object then no other actor can modify that object

● The Write Rule: If an actor can modify an object then no other actor can read or modify it

Page 124: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: Sending A tagactor Bar

actor Foo be baz(x: Bar tag) => // do something with x

actor Main new create(env: Env) => let f = Foo let b = Bar f.baz(b)

Page 125: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: Sending A tagactor Bar

actor Foo be baz(x: Bar tag) => // do something with x

actor Main new create(env: Env) => let f = Foo let b = Bar f.baz(b)

An actor’s default reference capability is tag

Page 126: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: Sending A tagactor Bar

actor Foo be baz(x: Bar tag) => // do something with x

actor Main new create(env: Env) => let f = Foo let b = Bar f.baz(b)

Main

Page 127: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: Sending A tagactor Bar

actor Foo be baz(x: Bar tag) => // do something with x

actor Main new create(env: Env) => let f = Foo let b = Bar f.baz(b)

Main Foo

foo

Page 128: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: Sending A tagactor Bar

actor Foo be baz(x: Bar tag) => // do something with x

actor Main new create(env: Env) => let f = Foo let b = Bar f.baz(b)

Main Foo

f

Bar

b

Page 129: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: Sending A tagactor Bar

actor Foo be baz(x: Bar tag) => // do something with x

actor Main new create(env: Env) => let f = Foo let b = Bar f.baz(b)

Main Foo

f

Bar

b

baz( )

Page 130: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: Sending A tagactor Bar

actor Foo be baz(x: Bar tag) => // do something with x

actor Main new create(env: Env) => let f = Foo let b = Bar f.baz(b)

Main Foo

f

Bar

b

baz( )

x

Page 131: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: Sending A tagactor Bar

actor Foo be baz(x: Bar tag) => // do something with x

actor Main new create(env: Env) => let f = Foo let b = Bar f.baz(b)

Main Foo

f

Bar

b x

● The Read Rule: If an actor can read an object then no other actor can modify that object

● The Write Rule: If an actor can modify an object then no other actor can read or modify it

Page 132: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: Sending An isoclass Bar

actor Foo be baz(x: Bar iso) => // do something with x

actor Main new create(env: Env) => let f = Foo let b = recover iso Bar end f.baz(consume b)

Page 133: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: Sending An isoclass Bar

actor Foo be baz(x: Bar iso) => // do something with x

actor Main new create(env: Env) => let f = Foo let b = recover iso Bar end f.baz(consume b)

Main

Page 134: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: Sending An isoclass Bar

actor Foo be baz(x: Bar iso) => // do something with x

actor Main new create(env: Env) => let f = Foo let b = recover iso Bar end f.baz(consume b)

Main Foo

foo

Page 135: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: Sending An isoclass Bar

actor Foo be baz(x: Bar iso) => // do something with x

actor Main new create(env: Env) => let f = Foo let b = recover iso Bar end f.baz(consume b)

Main Foo

f

Bar

b

Page 136: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: Sending An isoclass Bar

actor Foo be baz(x: Bar iso) => // do something with x

actor Main new create(env: Env) => let f = Foo let b = recover iso Bar end f.baz(consume b)

Main Foo

f

Bar

b

consume causes b to give up it’s reference

Page 137: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: Sending An isoclass Bar

actor Foo be baz(x: Bar iso) => // do something with x

actor Main new create(env: Env) => let f = Foo let b = recover iso Bar end f.baz(consume b)

Main Foo

f

Bar

b

once we consume b, we can no longer use it

Page 138: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: Sending An isoclass Bar

actor Foo be baz(x: Bar iso) => // do something with x

actor Main new create(env: Env) => let f = Foo let b = recover iso Bar end f.baz(consume b)

Main Foo

f

Bar

b

baz( )

Page 139: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: Sending An isoclass Bar

actor Foo be baz(x: Bar iso) => // do something with x

actor Main new create(env: Env) => let f = Foo let b = recover iso Bar end f.baz(consume b)

Main Foo

f

Bar

b

baz( )

x

Page 140: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Reference Capabilities: Sending An isoclass Bar

actor Foo be baz(x: Bar iso) => // do something with x

actor Main new create(env: Env) => let f = Foo let b = recover iso Bar end f.baz(consume b)

Main Foo

f

Bar

b x

● The Read Rule: If an actor can read an object then no other actor can modify that object

● The Write Rule: If an actor can modify an object then no other actor can read or modify it

Page 141: Software Craftsmanship NYC Meetup:  The Pony Programming Language

No More Data Races

Page 142: Software Craftsmanship NYC Meetup:  The Pony Programming Language

No More Data RacesRemember this?

global int a = 0

function inc() { for x in range(0, 1000001) { a = a + 1 }}

function main() { thread thread1 = Thread(inc) thread thread2 = Thread(inc) thread1.run() thread2.run() thread1.join() thread2.join() print(“a = “ + a)}

Expected:

2000000

Actual Run 1:

1987735

Actual Run 2:

1935010

Actual Run 3:

1941217

Page 143: Software Craftsmanship NYC Meetup:  The Pony Programming Language

No More Data RacesRemember this?

global int a = 0

function inc() { for x in range(0, 1000001) { a = a + 1 }}

function main() { thread thread1 = Thread(inc) thread thread2 = Thread(inc) thread1.run() thread2.run() thread1.join() thread2.join() print(“a = “ + a)}

This program violates the Read Rule and Write Rule, because the variable a can be read and modified from multiple threads.

Page 144: Software Craftsmanship NYC Meetup:  The Pony Programming Language

No More Data RacesA first pass at a Pony equivalent

use “collections”

class Counter var v: U64 = 0 fun ref inc() => v = v + 1

actor Inc be doit(c: Counter) => for x in range(0, 100001) do c.inc() end

actor Main new create(env: Env) => let c = Counter Inc.doit(c) Inc.doit(c) env.out.print(c.v.string())

Page 145: Software Craftsmanship NYC Meetup:  The Pony Programming Language

No More Data RacesA first pass at a Pony equivalent

use “collections”

class Counter var v: U64 = 0 fun ref inc() => v = v + 1

actor Inc be doit(c: Counter) => for x in range(0, 100001) do c.inc() end

actor Main new create(env: Env) => let c = Counter Inc.doit(c) Inc.doit(c) env.out.print(c.v.string())

This doesn’t work because the two Inc actors try to read and write to the Counter

Page 146: Software Craftsmanship NYC Meetup:  The Pony Programming Language

No More Data Racesuse “collections”

actor Counter var _count: U64 = 0 be increment() => _count = _count + 1 be print(env: Env) => env.out.print(_count.string())

actor Incrementer new create(counter: Counter, main: Main) => for x in Range(0, 1_000_001) do counter.increment() end main.finished(this)

actor Main let _finished_count = 0 let _counter: Counter = Counter let _env: Env new create(env: Env) => _env = env let inc1 = Incrementer(_counter, this) let inc2 = Incrementer(_counter, this) be finished() => _finished_count = _finished_count + 1 if _finished_count = 2 then _counter.print(env) end

Page 147: Software Craftsmanship NYC Meetup:  The Pony Programming Language

No More Data Racesuse “collections”

actor Counter var _count: U64 = 0 be increment() => _count = _count + 1 be print(env: Env) => env.out.print(_count.string())

actor Incrementer new create(counter: Counter, main: Main) => for x in Range(0, 1_000_001) do counter.increment() end main.finished(this)

actor Main let _finished_count = 0 let _counter: Counter = Counter let _env: Env new create(env: Env) => _env = env let inc1 = Incrementer(_counter, this) let inc2 = Incrementer(_counter, this) be finished() => _finished_count = _finished_count + 1 if _finished_count = 2 then _counter.print(env) end

Page 148: Software Craftsmanship NYC Meetup:  The Pony Programming Language

No More Data Racesuse “collections”

actor Counter var _count: U64 = 0 be increment() => _count = _count + 1 be print(env: Env) => env.out.print(_count.string())

actor Incrementer new create(counter: Counter, main: Main) => for x in Range(0, 1_000_001) do counter.increment() end main.finished(this)

actor Main let _finished_count = 0 let _counter: Counter = Counter let _env: Env new create(env: Env) => _env = env let inc1 = Incrementer(_counter, this) let inc2 = Incrementer(_counter, this) be finished() => _finished_count = _finished_count + 1 if _finished_count = 2 then _counter.print(env) end

Page 149: Software Craftsmanship NYC Meetup:  The Pony Programming Language

No More Data Racesuse “collections”

actor Counter var _count: U64 = 0 be increment() => _count = _count + 1 be print(env: Env) => env.out.print(_count.string())

actor Incrementer new create(counter: Counter, main: Main) => for x in Range(0, 1_000_001) do counter.increment() end main.finished(this)

actor Main let _finished_count = 0 let _counter: Counter = Counter let _env: Env new create(env: Env) => _env = env let inc1 = Incrementer(_counter, this) let inc2 = Incrementer(_counter, this) be finished() => _finished_count = _finished_count + 1 if _finished_count = 2 then _counter.print(env) end

Page 150: Software Craftsmanship NYC Meetup:  The Pony Programming Language

No More Data Racesuse “collections”

actor Counter var _count: U64 = 0 be increment() => _count = _count + 1 be print(env: Env) => env.out.print(_count.string())

actor Incrementer new create(counter: Counter, main: Main) => for x in Range(0, 1_000_001) do counter.increment() end main.finished(this)

actor Main let _finished_count = 0 let _counter: Counter = Counter let _env: Env new create(env: Env) => _env = env let inc1 = Incrementer(_counter, this) let inc2 = Incrementer(_counter, this) be finished() => _finished_count = _finished_count + 1 if _finished_count = 2 then _counter.print(env) end

Page 151: Software Craftsmanship NYC Meetup:  The Pony Programming Language

No More Data Racesuse “collections”

actor Counter var _count: U64 = 0 be increment() => _count = _count + 1 be print(env: Env) => env.out.print(_count.string())

actor Incrementer new create(counter: Counter, main: Main) => for x in Range(0, 1_000_001) do counter.increment() end main.finished(this)

actor Main let _finished_count = 0 let _counter: Counter = Counter let _env: Env new create(env: Env) => _env = env let inc1 = Incrementer(_counter, this) let inc2 = Incrementer(_counter, this) be finished() => _finished_count = _finished_count + 1 if _finished_count = 2 then _counter.print(env) end

Page 152: Software Craftsmanship NYC Meetup:  The Pony Programming Language

No More Data Racesuse “collections”

actor Counter var _count: U64 = 0 be increment() => _count = _count + 1 be print(env: Env) => env.out.print(_count.string())

actor Incrementer new create(counter: Counter, main: Main) => for x in Range(0, 1_000_001) do counter.increment() end main.finished(this)

actor Main let _finished_count = 0 let _counter: Counter = Counter let _env: Env new create(env: Env) => _env = env let inc1 = Incrementer(_counter, this) let inc2 = Incrementer(_counter, this) be finished() => _finished_count = _finished_count + 1 if _finished_count = 2 then _counter.print(env) end

Page 153: Software Craftsmanship NYC Meetup:  The Pony Programming Language

No More Data Racesuse “collections”

actor Counter var _count: U64 = 0 be increment() => _count = _count + 1 be print(env: Env) => env.out.print(_count.string())

actor Incrementer new create(counter: Counter, main: Main) => for x in Range(0, 1_000_001) do counter.increment() end main.finished(this)

actor Main let _finished_count = 0 let _counter: Counter = Counter let _env: Env new create(env: Env) => _env = env let inc1 = Incrementer(_counter, this) let inc2 = Incrementer(_counter, this) be finished() => _finished_count = _finished_count + 1 if _finished_count = 2 then _counter.print(env) end

Page 154: Software Craftsmanship NYC Meetup:  The Pony Programming Language

No More Data Racesuse “collections”

actor Counter var _count: U64 = 0 be increment() => _count = _count + 1 be print(env: Env) => env.out.print(_count.string())

actor Incrementer new create(counter: Counter, main: Main) => for x in Range(0, 1_000_001) do counter.increment() end main.finished(this)

actor Main let _finished_count = 0 let _counter: Counter = Counter let _env: Env new create(env: Env) => _env = env let inc1 = Incrementer(_counter, this) let inc2 = Incrementer(_counter, this) be finished() => _finished_count = _finished_count + 1 if _finished_count = 2 then _counter.print(env) end

2000000

Page 155: Software Craftsmanship NYC Meetup:  The Pony Programming Language

No More Data Racesuse “collections”

actor Counter var _count: U64 = 0 be increment() => _count = _count + 1 be print(env: Env) => env.out.print(_count.string())

actor Incrementer new create(counter: Counter, main: Main) => for x in Range(0, 1_000_001) do counter.increment() end main.finished(this)

actor Main let _finished_count = 0 let _counter: Counter = Counter let _env: Env new create(env: Env) => _env = env let inc1 = Incrementer(_counter, this) let inc2 = Incrementer(_counter, this) be finished() => _finished_count = _finished_count + 1 if _finished_count = 2 then _counter.print(env) end

The Counter actor “protects” the _count data structure

Page 156: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Sendence’s Experience With Pony

Page 157: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Sendence’s Experience With PonyIt’s nice to catch errors a compile time rather than runtime

Page 158: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Sendence’s Experience With PonyIt’s nice to catch errors a compile time rather than runtime

Pony is a young language (not even 1.0.0 yet)

Page 159: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Sendence’s Experience With PonyIt’s nice to catch errors a compile time rather than runtime

Pony is a young language (not even 1.0.0 yet)

● limited documentation

Page 160: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Sendence’s Experience With PonyIt’s nice to catch errors a compile time rather than runtime

Pony is a young language (not even 1.0.0 yet)

● limited documentation● things change

Page 161: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Sendence’s Experience With PonyIt’s nice to catch errors a compile time rather than runtime

Pony is a young language (not even 1.0.0 yet)

● limited documentation● things change● there are some sharp edges (compiler bugs, runtime bugs)

Page 162: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Sendence’s Experience With PonyIt’s nice to catch errors a compile time rather than runtime

Pony is a young language (not even 1.0.0 yet)

● limited documentation● things change● there are some sharp edges (compiler bugs, runtime bugs)

“America is all about speed. Hot, nasty, badass speed.” -- Eleanor Roosevelt

PONY

Page 163: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Learn More(because I left a lot out)

Page 164: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Learn MoreUser Mailing List

● https://pony.groups.io/g/user

Website

● https://www.ponylang.org

IRC

● freenode #ponylang

Page 165: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Contribute

Page 166: Software Craftsmanship NYC Meetup:  The Pony Programming Language

ContributeDeveloper Mailing List

● https://pony.groups.io/g/dev

Github

● Pony compiler → https://github.com/ponylang/ponyc● RFCs → https://github.com/ponylang/rfcs

Page 167: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Thank You (Really)!

Page 168: Software Craftsmanship NYC Meetup:  The Pony Programming Language

Questions