Tuesday, July 7, 2009

Prototype based programming in Ioke

Lets look a bit closer on programming in a prototype based language. In class based object oriented programming we have the concept of classes and objects of these classes. The objects behavior is defined in the class, and new objects are created from the class. If we want to reuse behavior in the original class but alter it in some way we can either subclass the original class or delegate calls from a new class to the original one. In contrast, in a prototype based language we create new objects from already existing objects in the system. In the new object we can redefine variables and methods or choose to leave them unaltered. If the new object does not provide a definition for a certain variable the prototype object will be searched, continuing further up the chain until the topmost object in the system is reached, in Ioke called Origin. So if we want to model an elephant in Ioke we can do so by creating a mimic of Origin, and defining the behavior and state we want to use to represent an elephant on that object.

iik> joe = Origin mimic
iik> joe color = "grey"
iik> joe legs = 4

If we then want to create another elephant we can mimic Joe and redefine the cells that differs from Joe's.

iik> charlie = joe mimic
iik> charlie legs = 3
iik> charlie color
+> "grey"

Ioke also has the concept of mixins, similar to traits in Self and Scala, which allows you to share behavior between different classes that don't share a common base object. You create new mixins by mimicing the Mixins kind (strictly speaking you can use any object as a mixin but the convention is to use a mimic of the Mixins kind). The Mixins kind does not have Base in its mimic chain, so you should not create objects (other than new mixins) from the Mixins kind.

iik> runBehavior = Mixins mimic
iik> runBehavior run = method("Run for your life" println)

If we want to use this mixin in our charlie object we add it to charlie's mixin chain with the mixin! method.

iik> charlie mixin!(runBehavior)
iik> charlie run
Run for your life
+> nil

Sokoban example
If you are used to class based object oriented programming it might feel strange to not have any objects to put your behavior in. But if you use it for a while you will notice that it is quite natural. If you want to know more about modeling in a prototype based language I can recommend Organizing Programs Without Classes, a paper about programming in the self language, and Steve Yegge's The Universal Design Pattern.

To get a fell for what a program in a prototype based language can look like I decided to try to translate a Ruby program to Ioke. I chose one of the solutions to the Sokoban Ruby Quiz. You can see the solution by Dave Burt here. The problem in the quiz is to create an implementation of the Sokoban game. It's a good exercise to try to translate the Ruby program yourself. The translation was pretty straightforward. The only problem I encountered was how to read files and user input on the console. But after I found the Ioke System class and its 'in' attribute it was pretty easy. I also decided to alter Dave's solution a bit to make it even more object oriented. I introduced the Direction and Position objects and added some convenience methods.

Considerations
When using the do method after creating a new object the name of objects created inside the do will be somewhat unintuitive (at leas to me). If we create an object A with a cell B inside it these two don't give the same kind for B:

A = Origin mimic
A B = Origin mimic
A B kind
+> "Origin A B"

A = Origin mimic do( B = Origin mimic )
A B kind
+> "Origin B"

So to be able to check if the resident on a certain tile is a crate or a person I reassigned the kind cell on these objects. After that we can refer to them with the name that we want, in this case Sokoban crate and Sokoban person.

After some thought I chose to make wall, crate and person objects in the Sokoban kind and not kinds in them self. Because these objects don't hold any state we do not need separate instances of them. So we can for example reuse the same crate object for all the crates on the level. But since the Floor tiles need to remember if there is anything on them we need separate objects for each of the floor tiles. So floor is a kind that we call mimic on but wall, crate and person are plain objects that we use directly.

Here is the complete Ioke code.

Sokoban = Origin mimic do (

Tile = Origin mimic do (
asText = method("~")
storage? = method(self kind == Sokoban Storage kind)
hasPerson? = method(false)
hasCrate? = method(false)
free? = method(false)
walkable? = method(false)
solved? = method(true) ; treat all tiles as solved unless they overried this method
kind = "Sokoban Tile"
)

CharTile = Tile mimic do (
initialize = method(chr,
@ chr = chr
)
asText = method(@ chr)
kind = "Sokoban CharTile"
)

wall = Tile mimic do (
asText = method("#")
kind = "Sokoban wall"
)

Floor = Tile mimic do (
initialize = method(resident nil,
@ resident = resident
)
asText = method(
if(@ resident,
@ resident asText,
" ")
)

clear = method(
r = @ resident
@ resident = nil
r
)

hasCrate? = method(!free? && @ resident kind?("Sokoban crate"))
hasPerson? = method(!free? && @ resident kind?("Sokoban person"))
free? = method(!(@ resident))
walkable? = method(true)

cell("<<") = method(resident,
if(@ resident, error!("Can't go there - this tile is full"))
@ resident = resident
)

kind = "Sokoban Floor"
)

Storage = Floor mimic do (
initialize = method(resident nil,
super(resident)
)

asText = method(
case(@ resident kind,
Sokoban crate kind, "*",
Sokoban person kind, "+",
"."
)
)
solved? = method(hasCrate?)
kind = "Sokoban Storage"
)

crate = Origin mimic do (
asText = method("o")
kind = "Sokoban crate"
)

person = Origin mimic do (
asText = method("@")
kind = "Sokoban person"
)

Tile createTile = method(chr nil,
case(chr,
"#", Sokoban wall,
" ", Sokoban Floor mimic,
"@", Sokoban Floor mimic(Sokoban person),
"o", Sokoban Floor mimic(Sokoban crate),
".", Sokoban Storage mimic,
"+", Sokoban Storage mimic(Sokoban person),
"*", Sokoban Storage mimic(Sokoban crate),
Sokoban CharTile mimic(chr)
)
)

Point = Origin mimic do(
initialize = method(x, y,
@ x = x
@ y = y
)
)

Direction = Point mimic(0,0) do(
kind = "Sokoban Direction"

cell("*") = method(mult,
Sokoban Position mimic(@ x * 2, @ y * 2)
)
)

Position = Point mimic(0,0) do(
cell("+") = method(dir,
Sokoban Position mimic(@ x + dir x, @ y + dir y)
)

kind = "Sokoban Position"
)

NORTH = Direction mimic(-1,0)
SOUTH = Direction mimic(1,0)
EAST = Direction mimic(0,1)
WEST = Direction mimic(0,-1)

Level = Origin mimic do(
initialize = method(levelsString,
@ grid = createLevels(levelsString)
if(!playerIndex, error!("No player found on level"))
if(solved?, error!("No challenge!"))
@ moves = 0
)

kind = "Sokoban Level"

createLevels = method(levelsString,
levelsString split("\n") map(ln,
ln split("") map(c, Sokoban Tile createTile(c) )
)
)

cell("[]") = method(position,
@ grid[ position x ][ position y ]
)

asText = method(
@ grid map(row, row join) join("\n")
)

playerIndex = method(
"Returns the position of the player"
@ grid each(rowIndex, row,
row each(colIndex, tile,
if(tile hasPerson?,
return Sokoban Position mimic(rowIndex, colIndex)
)
)
)
nil
)

solved? = method(
; a level is solved when every tile is solved
@ grid flatten all?(solved?)
)

moveCrate = method(
"Move the crate in front of the player in the given direction",
cratePos, newCratePos,
self[newCratePos] << self[cratePos] clear
)

movePlayer = method(
"Move the player to the given position",
newPlayerPos,
self[newPlayerPos] << self[playerIndex] clear
)

move = method(
"Move the player in the given direction and update the level to reflect it",
dir,
pos = playerIndex
playerTarget = pos + dir
if(self[playerTarget] walkable?,
if(self[playerTarget] hasCrate?,
crateTarget = pos + dir * 2
if(self[crateTarget] free?,
moveCrate(playerTarget, crateTarget)
movePlayer(playerTarget)
),
movePlayer(playerTarget)
)
return @ moves += 1
)
nil
)
)

; command-line interface
cli = method(levelsFile "sokoban_levels.txt",
cliHelp = "

Dave's Cheap Ruby Sokoban
Ported to Ioke by Mikael Amborn
© Dave Burt 2004

@ is you
+ is you standing on storage
# is a wall
. is empty storage
o is a crate
* is a crate on storage

Move all the crates onto storage.

to move: k
|
h -+- l
|
j
to restart the level: r
to quit: q
to show this message: ?

You can queue commands like this: hhjjlll...

"
cliHelp replaceAll(#/\t+/, " : ")
cliHelp println
"Press [enter] to begin." println
System in read

FileSystem readFully(levelsFile) split("\n\n") each(levelIndex, levelString,
level = Sokoban Level mimic(levelString)
while(!level solved?,
level println
"L:#{levelIndex+1} M:#{level moves} > " print
System in read asText split("") each(c,
case(c,
"h", level move(Sokoban WEST),
"j", level move(Sokoban SOUTH),
"k", level move(Sokoban NORTH),
"l", level move(Sokoban EAST),
"r", level = Sokoban Level mimic(levelString),
"q", "Bye!" println . System exit,
"d",
"ioke> " print
bind(
handle(fn(c, c println)),
Message doText(System in read code) println
),
"?", cliHelp println,
or("\n", "\r", "\t", " ", "."),
; System in read gives ".\n" if you just press enter so ignore . as well
nil,
"Invalid command: #{c}" println
)
)
)
"\nCongratulations - you beat level #{levelIndex + 1}!\n\n" println
)
)
)

;Sokoban cli



To be able to do the refactorings I did without breaking the implementation I also added some ISpec tests.

use("ispec")
use("sokoban_refactor")

SIMPLE_LEVEL = "#####\n#@o.#\n#####"
SOLVED_LEVEL = "#####\n# @*#\n#####"

position = method(x, y,
Sokoban Position mimic(x,y)
)
direction = method(x, y,
Sokoban Direction mimic(x,y)
)

describe(Sokoban,

describe(Sokoban wall,
it("should have the correct kind",
Sokoban wall should have kind("Sokoban wall")
)

it("should not be possible to walk on",
Sokoban wall should not be walkable
)
)

describe(Sokoban Crate,
it("should have the correct kind",
Sokoban Crate should have kind("Sokoban Crate")
)
)

describe(Sokoban Floor,
it("should have the correct kind",
Sokoban Floor should have kind("Sokoban Floor")
)
)

describe(Sokoban Floor,

it("should have the correct kind",
Sokoban Floor should have kind("Sokoban Floor")
)

describe("walkable?",
it("should be possible to walk on",
Sokoban Floor mimic should be walkable
)
)

describe("<<",
before(floor = Sokoban Floor mimic)
it("should be able to add a person to the floor tile",
floor clear
floor << Sokoban person
floor hasPerson? should == true
)

it("should be able to add a crate to the floor tile",
floor clear
floor << Sokoban crate mimic
floor hasCrate? should == true
)
)

describe("clear",
before(floor = Sokoban Floor mimic)
it("should return and remove resident",
floor clear
floor << Sokoban Crate mimic
floor clear should have kind("Sokoban Crate")
)
)

describe("free?",
before(floor = Sokoban Floor mimic)
it("should return true when there is no one on the tile",
floor clear
floor should be free
)
)

)

describe(Sokoban Storage,

it("should have the correct kind",
Sokoban Storage should have kind("Sokoban Storage")
)

describe("walkable?",
it("should be possible to walk on",
Sokoban Storage mimic should be walkable
)
)

describe("solved?",
before(storage = Sokoban Storage mimic)

it("should return false when it has no resident",
storage should not be solved
)

it("should return false when it has a person on it",
storage << Sokoban person
storage should not be solved
)

it("should return true when it has a crate on it",
storage clear
storage << Sokoban crate
storage should be solved
)
)
)

describe(Sokoban Tile,
describe("createTile",
it("should be able to create Floor tiles",
Sokoban Tile createTile(" ") should have kind("Sokoban Floor")
)
it("should be able to create Wall tiles",
Sokoban Tile createTile("#") should have kind("Sokoban wall")
)
it("should be able to create Storage tiles",
Sokoban Tile createTile(".") should have kind("Sokoban Storage")
)
it("should be able to create Floor tiles with a Person on them",
floorWithPerson = Sokoban Tile createTile("@")
floorWithPerson should have kind("Sokoban Floor")
floorWithPerson hasPerson? should be true
)
it("should be able to create Floor tiles with a Crate on them",
floorWithCrate = Sokoban Tile createTile("o")
floorWithCrate should have kind("Sokoban Floor")
floorWithCrate hasCrate? should be true
)
it("should be able to create Storage tiles with a Person on them",
floorWithPerson = Sokoban Tile createTile("+")
floorWithPerson should have kind("Sokoban Storage")
floorWithPerson hasPerson? should be true
)
it("should be able to create Storage tiles with a Crate on them",
floorWithCrate = Sokoban Tile createTile("*")
floorWithCrate should have kind("Sokoban Storage")
floorWithCrate hasCrate? should be true
)

)
)

describe(Sokoban Direction,
it("should have the correct kind",
Sokoban Direction should have kind("Sokoban Direction")
)

describe("*",
it("should multiply x and y with the given multiplyer",
dir = direction(1,2) * 2
dir x should == 2
dir y should == 4
)
)
)

describe(Sokoban Position,
it("should have the correct kind",
Sokoban Position should have kind("Sokoban Position")
)

it("should have an x and a y coordinate",
pos = position(1,2)
pos x should == 1
pos y should == 2
)

describe("+",
it("should add a direction to a position",
pos = position(1,2) + direction(1,2)
pos x should == 2
pos y should == 4
)
)
)

describe(Sokoban Level,

it("should have the correct kind",
Sokoban Level should have kind("Sokoban Level")
)

it("should be able to construct a level from a string",
Sokoban Level mimic(SIMPLE_LEVEL)
; just check that we do not get any errors
)

describe("asText",
before(level = Sokoban Level mimic(SIMPLE_LEVEL))
it("should return the level as a string",
level asText should == SIMPLE_LEVEL
)
)

describe("[]",
before(level = Sokoban Level mimic(SIMPLE_LEVEL))
it("should return the tile at the given index",
level[position(1,3)] should have kind("Sokoban Storage")
)
)

describe("playerIndex",
before(level = Sokoban Level mimic(SIMPLE_LEVEL))
it("should return the index of the player",
level playerIndex x should == 1
level playerIndex y should == 1
)
)

describe("movePlayer",
before(level = Sokoban Level mimic(SIMPLE_LEVEL))
it("should update the players position",
level movePlayer(position(1,3))
level playerIndex x should == 1
level playerIndex y should == 3
)
)

describe("moveCrate",
before(level = Sokoban Level mimic(SIMPLE_LEVEL))
it("should update the crates position",
level moveCrate(position(1,2), position(1,3))
level[position(1,3)] hasCrate? should be true
level[position(1,2)] hasCrate? should be false
)
)

describe("move",
before(level = Sokoban Level mimic(SIMPLE_LEVEL))
it("should update both the players and the crates position",
level move(Sokoban EAST)
level asText should == SOLVED_LEVEL
)
)

describe("solved?",
before(level = Sokoban Level mimic(SIMPLE_LEVEL))
it("should return false when all crates not on storage tiles",
level should not be solved
)

it("should return true when all crates are on storage tiles",
level moveCrate(position(1,2), Sokoban position(1,3))
level should be solved
)
)
)

)