BL Program PROGRAM identifier IS sequence of new instructions BEGIN sequence of statements END identifier Math Model for Program  What pieces of information do we need to keep.

Download Report

Transcript BL Program PROGRAM identifier IS sequence of new instructions BEGIN sequence of statements END identifier Math Model for Program  What pieces of information do we need to keep.

Slide 1

BL Program
PROGRAM identifier IS

sequence of new instructions
BEGIN

sequence of statements
END identifier

Math Model for Program


What pieces of information do we
need to keep track of for a BL
program?





name (IDENTIFIER)
new instructions (CONTEXT)
body (BLOCK STATEMENT)

The Math Model


math subtype PROGRAM is (
name: IDENTIFIER
context: CONTEXT
body: STATEMENT
)
exemplar p
constraint
root (p.body).kind = BLOCK

New Instruction
INSTRUCTION identifier IS

sequence of statements
END identifier

Math Model Continued…


What pieces of information do we
need to keep track of for a new BL
instruction?




name (IDENTIFIER)
body (BLOCK STATEMENT)

Math Model Continued…


math subtype CONTEXT is
finite set of (
name: IDENTIFIER
body: STATEMENT
)
exemplar c
constraint 1. c is a (partial) function
2. for each (name, body) pair in c,
...
name is not one of primitives
nor the empty string, and body
is a BLOCK

Program Component


Type




Program_Kernel is modeled by PROGRAM

Initial Value


IS_INITIAL_PROGRAM (self) 
self.name = empty_string and
self.context = empty_set and
IS_INITIAL_STATEMENT (self.body)

Program SteerClear
Draw a picture of this BL program’s abstract view:
BEGIN
PROGRAM SteerClear IS
WHILE true DO
INSTRUCTION StepAside IS
IF next-is-empty THEN
IF random THEN
skip
turnright
ELSE
ELSE
IF next-is-wall THEN
turnleft
TurnAround
END IF
ELSE
move
StepAside
END StepAside
END IF
INSTRUCTION TurnAround IS
END IF
turnright
END WHILE
turnright
END SteerClear
END TurnAround

Program SteerClear (cont’d)
p=(

,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
,

},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{(

p.name
,

), (

,

) },

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
BLOCK

CALL
“turnright”

), ( “StepAside”,

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK
CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
), ( “StepAside”,

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

BLOCK
WHILE
TRUE

CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

BLOCK

p.context

IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

p.body

IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Program Continued…


Operations










p.Swap_Name (name)
p.Swap_Body (statement)
p.Add_To_Context (name, statement)
p.Remove_From_Context (name, name_copy,
statement)
p.Remove_Any_From_Context (name,
statement)
p.Is_In_Context (name)
p.Size_Of_Context ()

Swap_Name
P=
What effect will
( “Timid”,
{ ( “TurnAround”,
the following
statements have on p,
the program object for
program SteerClear?

BLOCK

CALL
“turnright”

), (“StepAside”,

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

BLOCK

BLOCK

object Text name;
name = “Timid”;
p.Swap_Name (name);
name = “SteerClear”

CALL
“turnright”

WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

CALL
“turnleft”

) },

Swap_Body
What effect will
the following statements have
on object p from the previous
slide?

P=
( “Timid”,
{ ( “TurnAround”,

object Statement b, i, c;
object Text m = “move”;
object Integer test = RANDOM;
c.Compose_Call (m);
b.Add_To_Block (0, c);
i.Compose_If (test, b);
b.Add_to_Block (0, i);
p.Swap_Body (b);

CALL
“turnright”

BLOCK

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

CALL
“turnleft”

BLOCK

b=

WHILE
TRUE

IF
RANDOM

BLOCK

IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK

BLOCK

CALL
“skip”

CALL
“move”

) },

BLOCK

CALL
“turnright”

BLOCK

BLOCK

)

IF_ELSE
NEXT_IS_WALL

BLOCK

CALL
“TurnAround”

BLOCK

CALL
“StepAside”

Context Operations
What result is produced
by these statements if
p has its original value?

P=
( “SteerClear”,
{ ( “TurnAround”,

BLOCK

CALL
“turnright”

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

p.Size_Of_Context () ?

CALL
“turnright”

BLOCK

BLOCK

CALL
“move”

BLOCK

CALL
“turnleft”

WHILE
TRUE

p.Is_In_Context (“StepAside”) ?

BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

) },

Context Operations (cont’d)
What result is produced
by these statements if
p has the value shown on
the previous slide?

P=
( “SteerClear”,
{ (“StepAside”,

object Text n;
object Statement b;
p.Remove_From_Context
(“TurnAround”, n, b) ?
n=“TurnAround”

b=
CALL
“turnright”

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

BLOCK

CALL
“turnright”

) },

CALL
“move”

BLOCK

CALL
“turnleft”

BLOCK
WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Context Operations (cont’d)
What result is produced
by these statements if p
and b have the values
shown on the previous
slide?

P=
( “SteerClear”,
{ ( “TurnBack”,
CALL
“turnright”

b=

), (“StepAside”,

CALL
“turnright”

BLOCK

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

BLOCK

CALL
“turnleft”

BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

) },

CALL
“move”

WHILE
TRUE

n = “TurnBack”;
p.Add_To_Context (n, b) ?
n=“”

BLOCK

)

An Operation on Program
What would it mean to
demobilize a program?

Statements in BL Program
PROGRAM identifier IS

INSTRUCTION identifier IS

BEGIN

END identifier

END identifier

.
.
.

State the Problem
global_procedure Demobilize (
alters Program& p
);
/*!
ensures
p.name = #p.name and
p.context =
CONTEXT_DEMOBILIZE (#p.context) and
p.body = DEMOBILIZE (#p.body)
!*/

State the Problem Continued…
math definition CONTEXT_DEMOBILIZE (
c: CONTEXT
): CONTEXT satisfies
if c = empty_set then
CONTEXT_DEMOBILIZE (c) = c
else
there exists n: IDENTIFIER, s: STATEMENT,
rest_of_context: CONTEXT
(c = {(n, s)} union rest_of_context and
CONTEXT_DEMOBILIZE(c) =
{(n, DEMOBILIZE(s))} union
CONTEXT_DEMOBILIZE (rest_of_context))

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;
// demobilize program body
p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

Implementation Continued…
// demobilize statements in program context
while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}
// restore value of p (to be preserved)
p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;
}

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;

while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}

p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

}

p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;


Slide 2

BL Program
PROGRAM identifier IS

sequence of new instructions
BEGIN

sequence of statements
END identifier

Math Model for Program


What pieces of information do we
need to keep track of for a BL
program?





name (IDENTIFIER)
new instructions (CONTEXT)
body (BLOCK STATEMENT)

The Math Model


math subtype PROGRAM is (
name: IDENTIFIER
context: CONTEXT
body: STATEMENT
)
exemplar p
constraint
root (p.body).kind = BLOCK

New Instruction
INSTRUCTION identifier IS

sequence of statements
END identifier

Math Model Continued…


What pieces of information do we
need to keep track of for a new BL
instruction?




name (IDENTIFIER)
body (BLOCK STATEMENT)

Math Model Continued…


math subtype CONTEXT is
finite set of (
name: IDENTIFIER
body: STATEMENT
)
exemplar c
constraint 1. c is a (partial) function
2. for each (name, body) pair in c,
...
name is not one of primitives
nor the empty string, and body
is a BLOCK

Program Component


Type




Program_Kernel is modeled by PROGRAM

Initial Value


IS_INITIAL_PROGRAM (self) 
self.name = empty_string and
self.context = empty_set and
IS_INITIAL_STATEMENT (self.body)

Program SteerClear
Draw a picture of this BL program’s abstract view:
BEGIN
PROGRAM SteerClear IS
WHILE true DO
INSTRUCTION StepAside IS
IF next-is-empty THEN
IF random THEN
skip
turnright
ELSE
ELSE
IF next-is-wall THEN
turnleft
TurnAround
END IF
ELSE
move
StepAside
END StepAside
END IF
INSTRUCTION TurnAround IS
END IF
turnright
END WHILE
turnright
END SteerClear
END TurnAround

Program SteerClear (cont’d)
p=(

,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
,

},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{(

p.name
,

), (

,

) },

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
BLOCK

CALL
“turnright”

), ( “StepAside”,

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK
CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
), ( “StepAside”,

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

BLOCK
WHILE
TRUE

CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

BLOCK

p.context

IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

p.body

IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Program Continued…


Operations










p.Swap_Name (name)
p.Swap_Body (statement)
p.Add_To_Context (name, statement)
p.Remove_From_Context (name, name_copy,
statement)
p.Remove_Any_From_Context (name,
statement)
p.Is_In_Context (name)
p.Size_Of_Context ()

Swap_Name
P=
What effect will
( “Timid”,
{ ( “TurnAround”,
the following
statements have on p,
the program object for
program SteerClear?

BLOCK

CALL
“turnright”

), (“StepAside”,

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

BLOCK

BLOCK

object Text name;
name = “Timid”;
p.Swap_Name (name);
name = “SteerClear”

CALL
“turnright”

WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

CALL
“turnleft”

) },

Swap_Body
What effect will
the following statements have
on object p from the previous
slide?

P=
( “Timid”,
{ ( “TurnAround”,

object Statement b, i, c;
object Text m = “move”;
object Integer test = RANDOM;
c.Compose_Call (m);
b.Add_To_Block (0, c);
i.Compose_If (test, b);
b.Add_to_Block (0, i);
p.Swap_Body (b);

CALL
“turnright”

BLOCK

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

CALL
“turnleft”

BLOCK

b=

WHILE
TRUE

IF
RANDOM

BLOCK

IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK

BLOCK

CALL
“skip”

CALL
“move”

) },

BLOCK

CALL
“turnright”

BLOCK

BLOCK

)

IF_ELSE
NEXT_IS_WALL

BLOCK

CALL
“TurnAround”

BLOCK

CALL
“StepAside”

Context Operations
What result is produced
by these statements if
p has its original value?

P=
( “SteerClear”,
{ ( “TurnAround”,

BLOCK

CALL
“turnright”

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

p.Size_Of_Context () ?

CALL
“turnright”

BLOCK

BLOCK

CALL
“move”

BLOCK

CALL
“turnleft”

WHILE
TRUE

p.Is_In_Context (“StepAside”) ?

BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

) },

Context Operations (cont’d)
What result is produced
by these statements if
p has the value shown on
the previous slide?

P=
( “SteerClear”,
{ (“StepAside”,

object Text n;
object Statement b;
p.Remove_From_Context
(“TurnAround”, n, b) ?
n=“TurnAround”

b=
CALL
“turnright”

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

BLOCK

CALL
“turnright”

) },

CALL
“move”

BLOCK

CALL
“turnleft”

BLOCK
WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Context Operations (cont’d)
What result is produced
by these statements if p
and b have the values
shown on the previous
slide?

P=
( “SteerClear”,
{ ( “TurnBack”,
CALL
“turnright”

b=

), (“StepAside”,

CALL
“turnright”

BLOCK

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

BLOCK

CALL
“turnleft”

BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

) },

CALL
“move”

WHILE
TRUE

n = “TurnBack”;
p.Add_To_Context (n, b) ?
n=“”

BLOCK

)

An Operation on Program
What would it mean to
demobilize a program?

Statements in BL Program
PROGRAM identifier IS

INSTRUCTION identifier IS

BEGIN

END identifier

END identifier

.
.
.

State the Problem
global_procedure Demobilize (
alters Program& p
);
/*!
ensures
p.name = #p.name and
p.context =
CONTEXT_DEMOBILIZE (#p.context) and
p.body = DEMOBILIZE (#p.body)
!*/

State the Problem Continued…
math definition CONTEXT_DEMOBILIZE (
c: CONTEXT
): CONTEXT satisfies
if c = empty_set then
CONTEXT_DEMOBILIZE (c) = c
else
there exists n: IDENTIFIER, s: STATEMENT,
rest_of_context: CONTEXT
(c = {(n, s)} union rest_of_context and
CONTEXT_DEMOBILIZE(c) =
{(n, DEMOBILIZE(s))} union
CONTEXT_DEMOBILIZE (rest_of_context))

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;
// demobilize program body
p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

Implementation Continued…
// demobilize statements in program context
while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}
// restore value of p (to be preserved)
p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;
}

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;

while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}

p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

}

p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;


Slide 3

BL Program
PROGRAM identifier IS

sequence of new instructions
BEGIN

sequence of statements
END identifier

Math Model for Program


What pieces of information do we
need to keep track of for a BL
program?





name (IDENTIFIER)
new instructions (CONTEXT)
body (BLOCK STATEMENT)

The Math Model


math subtype PROGRAM is (
name: IDENTIFIER
context: CONTEXT
body: STATEMENT
)
exemplar p
constraint
root (p.body).kind = BLOCK

New Instruction
INSTRUCTION identifier IS

sequence of statements
END identifier

Math Model Continued…


What pieces of information do we
need to keep track of for a new BL
instruction?




name (IDENTIFIER)
body (BLOCK STATEMENT)

Math Model Continued…


math subtype CONTEXT is
finite set of (
name: IDENTIFIER
body: STATEMENT
)
exemplar c
constraint 1. c is a (partial) function
2. for each (name, body) pair in c,
...
name is not one of primitives
nor the empty string, and body
is a BLOCK

Program Component


Type




Program_Kernel is modeled by PROGRAM

Initial Value


IS_INITIAL_PROGRAM (self) 
self.name = empty_string and
self.context = empty_set and
IS_INITIAL_STATEMENT (self.body)

Program SteerClear
Draw a picture of this BL program’s abstract view:
BEGIN
PROGRAM SteerClear IS
WHILE true DO
INSTRUCTION StepAside IS
IF next-is-empty THEN
IF random THEN
skip
turnright
ELSE
ELSE
IF next-is-wall THEN
turnleft
TurnAround
END IF
ELSE
move
StepAside
END StepAside
END IF
INSTRUCTION TurnAround IS
END IF
turnright
END WHILE
turnright
END SteerClear
END TurnAround

Program SteerClear (cont’d)
p=(

,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
,

},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{(

p.name
,

), (

,

) },

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
BLOCK

CALL
“turnright”

), ( “StepAside”,

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK
CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
), ( “StepAside”,

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

BLOCK
WHILE
TRUE

CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

BLOCK

p.context

IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

p.body

IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Program Continued…


Operations










p.Swap_Name (name)
p.Swap_Body (statement)
p.Add_To_Context (name, statement)
p.Remove_From_Context (name, name_copy,
statement)
p.Remove_Any_From_Context (name,
statement)
p.Is_In_Context (name)
p.Size_Of_Context ()

Swap_Name
P=
What effect will
( “Timid”,
{ ( “TurnAround”,
the following
statements have on p,
the program object for
program SteerClear?

BLOCK

CALL
“turnright”

), (“StepAside”,

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

BLOCK

BLOCK

object Text name;
name = “Timid”;
p.Swap_Name (name);
name = “SteerClear”

CALL
“turnright”

WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

CALL
“turnleft”

) },

Swap_Body
What effect will
the following statements have
on object p from the previous
slide?

P=
( “Timid”,
{ ( “TurnAround”,

object Statement b, i, c;
object Text m = “move”;
object Integer test = RANDOM;
c.Compose_Call (m);
b.Add_To_Block (0, c);
i.Compose_If (test, b);
b.Add_to_Block (0, i);
p.Swap_Body (b);

CALL
“turnright”

BLOCK

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

CALL
“turnleft”

BLOCK

b=

WHILE
TRUE

IF
RANDOM

BLOCK

IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK

BLOCK

CALL
“skip”

CALL
“move”

) },

BLOCK

CALL
“turnright”

BLOCK

BLOCK

)

IF_ELSE
NEXT_IS_WALL

BLOCK

CALL
“TurnAround”

BLOCK

CALL
“StepAside”

Context Operations
What result is produced
by these statements if
p has its original value?

P=
( “SteerClear”,
{ ( “TurnAround”,

BLOCK

CALL
“turnright”

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

p.Size_Of_Context () ?

CALL
“turnright”

BLOCK

BLOCK

CALL
“move”

BLOCK

CALL
“turnleft”

WHILE
TRUE

p.Is_In_Context (“StepAside”) ?

BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

) },

Context Operations (cont’d)
What result is produced
by these statements if
p has the value shown on
the previous slide?

P=
( “SteerClear”,
{ (“StepAside”,

object Text n;
object Statement b;
p.Remove_From_Context
(“TurnAround”, n, b) ?
n=“TurnAround”

b=
CALL
“turnright”

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

BLOCK

CALL
“turnright”

) },

CALL
“move”

BLOCK

CALL
“turnleft”

BLOCK
WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Context Operations (cont’d)
What result is produced
by these statements if p
and b have the values
shown on the previous
slide?

P=
( “SteerClear”,
{ ( “TurnBack”,
CALL
“turnright”

b=

), (“StepAside”,

CALL
“turnright”

BLOCK

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

BLOCK

CALL
“turnleft”

BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

) },

CALL
“move”

WHILE
TRUE

n = “TurnBack”;
p.Add_To_Context (n, b) ?
n=“”

BLOCK

)

An Operation on Program
What would it mean to
demobilize a program?

Statements in BL Program
PROGRAM identifier IS

INSTRUCTION identifier IS

BEGIN

END identifier

END identifier

.
.
.

State the Problem
global_procedure Demobilize (
alters Program& p
);
/*!
ensures
p.name = #p.name and
p.context =
CONTEXT_DEMOBILIZE (#p.context) and
p.body = DEMOBILIZE (#p.body)
!*/

State the Problem Continued…
math definition CONTEXT_DEMOBILIZE (
c: CONTEXT
): CONTEXT satisfies
if c = empty_set then
CONTEXT_DEMOBILIZE (c) = c
else
there exists n: IDENTIFIER, s: STATEMENT,
rest_of_context: CONTEXT
(c = {(n, s)} union rest_of_context and
CONTEXT_DEMOBILIZE(c) =
{(n, DEMOBILIZE(s))} union
CONTEXT_DEMOBILIZE (rest_of_context))

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;
// demobilize program body
p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

Implementation Continued…
// demobilize statements in program context
while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}
// restore value of p (to be preserved)
p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;
}

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;

while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}

p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

}

p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;


Slide 4

BL Program
PROGRAM identifier IS

sequence of new instructions
BEGIN

sequence of statements
END identifier

Math Model for Program


What pieces of information do we
need to keep track of for a BL
program?





name (IDENTIFIER)
new instructions (CONTEXT)
body (BLOCK STATEMENT)

The Math Model


math subtype PROGRAM is (
name: IDENTIFIER
context: CONTEXT
body: STATEMENT
)
exemplar p
constraint
root (p.body).kind = BLOCK

New Instruction
INSTRUCTION identifier IS

sequence of statements
END identifier

Math Model Continued…


What pieces of information do we
need to keep track of for a new BL
instruction?




name (IDENTIFIER)
body (BLOCK STATEMENT)

Math Model Continued…


math subtype CONTEXT is
finite set of (
name: IDENTIFIER
body: STATEMENT
)
exemplar c
constraint 1. c is a (partial) function
2. for each (name, body) pair in c,
...
name is not one of primitives
nor the empty string, and body
is a BLOCK

Program Component


Type




Program_Kernel is modeled by PROGRAM

Initial Value


IS_INITIAL_PROGRAM (self) 
self.name = empty_string and
self.context = empty_set and
IS_INITIAL_STATEMENT (self.body)

Program SteerClear
Draw a picture of this BL program’s abstract view:
BEGIN
PROGRAM SteerClear IS
WHILE true DO
INSTRUCTION StepAside IS
IF next-is-empty THEN
IF random THEN
skip
turnright
ELSE
ELSE
IF next-is-wall THEN
turnleft
TurnAround
END IF
ELSE
move
StepAside
END StepAside
END IF
INSTRUCTION TurnAround IS
END IF
turnright
END WHILE
turnright
END SteerClear
END TurnAround

Program SteerClear (cont’d)
p=(

,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
,

},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{(

p.name
,

), (

,

) },

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
BLOCK

CALL
“turnright”

), ( “StepAside”,

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK
CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
), ( “StepAside”,

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

BLOCK
WHILE
TRUE

CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

BLOCK

p.context

IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

p.body

IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Program Continued…


Operations










p.Swap_Name (name)
p.Swap_Body (statement)
p.Add_To_Context (name, statement)
p.Remove_From_Context (name, name_copy,
statement)
p.Remove_Any_From_Context (name,
statement)
p.Is_In_Context (name)
p.Size_Of_Context ()

Swap_Name
P=
What effect will
( “Timid”,
{ ( “TurnAround”,
the following
statements have on p,
the program object for
program SteerClear?

BLOCK

CALL
“turnright”

), (“StepAside”,

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

BLOCK

BLOCK

object Text name;
name = “Timid”;
p.Swap_Name (name);
name = “SteerClear”

CALL
“turnright”

WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

CALL
“turnleft”

) },

Swap_Body
What effect will
the following statements have
on object p from the previous
slide?

P=
( “Timid”,
{ ( “TurnAround”,

object Statement b, i, c;
object Text m = “move”;
object Integer test = RANDOM;
c.Compose_Call (m);
b.Add_To_Block (0, c);
i.Compose_If (test, b);
b.Add_to_Block (0, i);
p.Swap_Body (b);

CALL
“turnright”

BLOCK

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

CALL
“turnleft”

BLOCK

b=

WHILE
TRUE

IF
RANDOM

BLOCK

IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK

BLOCK

CALL
“skip”

CALL
“move”

) },

BLOCK

CALL
“turnright”

BLOCK

BLOCK

)

IF_ELSE
NEXT_IS_WALL

BLOCK

CALL
“TurnAround”

BLOCK

CALL
“StepAside”

Context Operations
What result is produced
by these statements if
p has its original value?

P=
( “SteerClear”,
{ ( “TurnAround”,

BLOCK

CALL
“turnright”

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

p.Size_Of_Context () ?

CALL
“turnright”

BLOCK

BLOCK

CALL
“move”

BLOCK

CALL
“turnleft”

WHILE
TRUE

p.Is_In_Context (“StepAside”) ?

BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

) },

Context Operations (cont’d)
What result is produced
by these statements if
p has the value shown on
the previous slide?

P=
( “SteerClear”,
{ (“StepAside”,

object Text n;
object Statement b;
p.Remove_From_Context
(“TurnAround”, n, b) ?
n=“TurnAround”

b=
CALL
“turnright”

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

BLOCK

CALL
“turnright”

) },

CALL
“move”

BLOCK

CALL
“turnleft”

BLOCK
WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Context Operations (cont’d)
What result is produced
by these statements if p
and b have the values
shown on the previous
slide?

P=
( “SteerClear”,
{ ( “TurnBack”,
CALL
“turnright”

b=

), (“StepAside”,

CALL
“turnright”

BLOCK

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

BLOCK

CALL
“turnleft”

BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

) },

CALL
“move”

WHILE
TRUE

n = “TurnBack”;
p.Add_To_Context (n, b) ?
n=“”

BLOCK

)

An Operation on Program
What would it mean to
demobilize a program?

Statements in BL Program
PROGRAM identifier IS

INSTRUCTION identifier IS

BEGIN

END identifier

END identifier

.
.
.

State the Problem
global_procedure Demobilize (
alters Program& p
);
/*!
ensures
p.name = #p.name and
p.context =
CONTEXT_DEMOBILIZE (#p.context) and
p.body = DEMOBILIZE (#p.body)
!*/

State the Problem Continued…
math definition CONTEXT_DEMOBILIZE (
c: CONTEXT
): CONTEXT satisfies
if c = empty_set then
CONTEXT_DEMOBILIZE (c) = c
else
there exists n: IDENTIFIER, s: STATEMENT,
rest_of_context: CONTEXT
(c = {(n, s)} union rest_of_context and
CONTEXT_DEMOBILIZE(c) =
{(n, DEMOBILIZE(s))} union
CONTEXT_DEMOBILIZE (rest_of_context))

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;
// demobilize program body
p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

Implementation Continued…
// demobilize statements in program context
while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}
// restore value of p (to be preserved)
p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;
}

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;

while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}

p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

}

p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;


Slide 5

BL Program
PROGRAM identifier IS

sequence of new instructions
BEGIN

sequence of statements
END identifier

Math Model for Program


What pieces of information do we
need to keep track of for a BL
program?





name (IDENTIFIER)
new instructions (CONTEXT)
body (BLOCK STATEMENT)

The Math Model


math subtype PROGRAM is (
name: IDENTIFIER
context: CONTEXT
body: STATEMENT
)
exemplar p
constraint
root (p.body).kind = BLOCK

New Instruction
INSTRUCTION identifier IS

sequence of statements
END identifier

Math Model Continued…


What pieces of information do we
need to keep track of for a new BL
instruction?




name (IDENTIFIER)
body (BLOCK STATEMENT)

Math Model Continued…


math subtype CONTEXT is
finite set of (
name: IDENTIFIER
body: STATEMENT
)
exemplar c
constraint 1. c is a (partial) function
2. for each (name, body) pair in c,
...
name is not one of primitives
nor the empty string, and body
is a BLOCK

Program Component


Type




Program_Kernel is modeled by PROGRAM

Initial Value


IS_INITIAL_PROGRAM (self) 
self.name = empty_string and
self.context = empty_set and
IS_INITIAL_STATEMENT (self.body)

Program SteerClear
Draw a picture of this BL program’s abstract view:
BEGIN
PROGRAM SteerClear IS
WHILE true DO
INSTRUCTION StepAside IS
IF next-is-empty THEN
IF random THEN
skip
turnright
ELSE
ELSE
IF next-is-wall THEN
turnleft
TurnAround
END IF
ELSE
move
StepAside
END StepAside
END IF
INSTRUCTION TurnAround IS
END IF
turnright
END WHILE
turnright
END SteerClear
END TurnAround

Program SteerClear (cont’d)
p=(

,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
,

},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{(

p.name
,

), (

,

) },

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
BLOCK

CALL
“turnright”

), ( “StepAside”,

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK
CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
), ( “StepAside”,

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

BLOCK
WHILE
TRUE

CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

BLOCK

p.context

IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

p.body

IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Program Continued…


Operations










p.Swap_Name (name)
p.Swap_Body (statement)
p.Add_To_Context (name, statement)
p.Remove_From_Context (name, name_copy,
statement)
p.Remove_Any_From_Context (name,
statement)
p.Is_In_Context (name)
p.Size_Of_Context ()

Swap_Name
P=
What effect will
( “Timid”,
{ ( “TurnAround”,
the following
statements have on p,
the program object for
program SteerClear?

BLOCK

CALL
“turnright”

), (“StepAside”,

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

BLOCK

BLOCK

object Text name;
name = “Timid”;
p.Swap_Name (name);
name = “SteerClear”

CALL
“turnright”

WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

CALL
“turnleft”

) },

Swap_Body
What effect will
the following statements have
on object p from the previous
slide?

P=
( “Timid”,
{ ( “TurnAround”,

object Statement b, i, c;
object Text m = “move”;
object Integer test = RANDOM;
c.Compose_Call (m);
b.Add_To_Block (0, c);
i.Compose_If (test, b);
b.Add_to_Block (0, i);
p.Swap_Body (b);

CALL
“turnright”

BLOCK

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

CALL
“turnleft”

BLOCK

b=

WHILE
TRUE

IF
RANDOM

BLOCK

IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK

BLOCK

CALL
“skip”

CALL
“move”

) },

BLOCK

CALL
“turnright”

BLOCK

BLOCK

)

IF_ELSE
NEXT_IS_WALL

BLOCK

CALL
“TurnAround”

BLOCK

CALL
“StepAside”

Context Operations
What result is produced
by these statements if
p has its original value?

P=
( “SteerClear”,
{ ( “TurnAround”,

BLOCK

CALL
“turnright”

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

p.Size_Of_Context () ?

CALL
“turnright”

BLOCK

BLOCK

CALL
“move”

BLOCK

CALL
“turnleft”

WHILE
TRUE

p.Is_In_Context (“StepAside”) ?

BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

) },

Context Operations (cont’d)
What result is produced
by these statements if
p has the value shown on
the previous slide?

P=
( “SteerClear”,
{ (“StepAside”,

object Text n;
object Statement b;
p.Remove_From_Context
(“TurnAround”, n, b) ?
n=“TurnAround”

b=
CALL
“turnright”

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

BLOCK

CALL
“turnright”

) },

CALL
“move”

BLOCK

CALL
“turnleft”

BLOCK
WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Context Operations (cont’d)
What result is produced
by these statements if p
and b have the values
shown on the previous
slide?

P=
( “SteerClear”,
{ ( “TurnBack”,
CALL
“turnright”

b=

), (“StepAside”,

CALL
“turnright”

BLOCK

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

BLOCK

CALL
“turnleft”

BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

) },

CALL
“move”

WHILE
TRUE

n = “TurnBack”;
p.Add_To_Context (n, b) ?
n=“”

BLOCK

)

An Operation on Program
What would it mean to
demobilize a program?

Statements in BL Program
PROGRAM identifier IS

INSTRUCTION identifier IS

BEGIN

END identifier

END identifier

.
.
.

State the Problem
global_procedure Demobilize (
alters Program& p
);
/*!
ensures
p.name = #p.name and
p.context =
CONTEXT_DEMOBILIZE (#p.context) and
p.body = DEMOBILIZE (#p.body)
!*/

State the Problem Continued…
math definition CONTEXT_DEMOBILIZE (
c: CONTEXT
): CONTEXT satisfies
if c = empty_set then
CONTEXT_DEMOBILIZE (c) = c
else
there exists n: IDENTIFIER, s: STATEMENT,
rest_of_context: CONTEXT
(c = {(n, s)} union rest_of_context and
CONTEXT_DEMOBILIZE(c) =
{(n, DEMOBILIZE(s))} union
CONTEXT_DEMOBILIZE (rest_of_context))

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;
// demobilize program body
p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

Implementation Continued…
// demobilize statements in program context
while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}
// restore value of p (to be preserved)
p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;
}

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;

while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}

p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

}

p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;


Slide 6

BL Program
PROGRAM identifier IS

sequence of new instructions
BEGIN

sequence of statements
END identifier

Math Model for Program


What pieces of information do we
need to keep track of for a BL
program?





name (IDENTIFIER)
new instructions (CONTEXT)
body (BLOCK STATEMENT)

The Math Model


math subtype PROGRAM is (
name: IDENTIFIER
context: CONTEXT
body: STATEMENT
)
exemplar p
constraint
root (p.body).kind = BLOCK

New Instruction
INSTRUCTION identifier IS

sequence of statements
END identifier

Math Model Continued…


What pieces of information do we
need to keep track of for a new BL
instruction?




name (IDENTIFIER)
body (BLOCK STATEMENT)

Math Model Continued…


math subtype CONTEXT is
finite set of (
name: IDENTIFIER
body: STATEMENT
)
exemplar c
constraint 1. c is a (partial) function
2. for each (name, body) pair in c,
...
name is not one of primitives
nor the empty string, and body
is a BLOCK

Program Component


Type




Program_Kernel is modeled by PROGRAM

Initial Value


IS_INITIAL_PROGRAM (self) 
self.name = empty_string and
self.context = empty_set and
IS_INITIAL_STATEMENT (self.body)

Program SteerClear
Draw a picture of this BL program’s abstract view:
BEGIN
PROGRAM SteerClear IS
WHILE true DO
INSTRUCTION StepAside IS
IF next-is-empty THEN
IF random THEN
skip
turnright
ELSE
ELSE
IF next-is-wall THEN
turnleft
TurnAround
END IF
ELSE
move
StepAside
END StepAside
END IF
INSTRUCTION TurnAround IS
END IF
turnright
END WHILE
turnright
END SteerClear
END TurnAround

Program SteerClear (cont’d)
p=(

,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
,

},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{(

p.name
,

), (

,

) },

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
BLOCK

CALL
“turnright”

), ( “StepAside”,

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK
CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
), ( “StepAside”,

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

BLOCK
WHILE
TRUE

CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

BLOCK

p.context

IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

p.body

IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Program Continued…


Operations










p.Swap_Name (name)
p.Swap_Body (statement)
p.Add_To_Context (name, statement)
p.Remove_From_Context (name, name_copy,
statement)
p.Remove_Any_From_Context (name,
statement)
p.Is_In_Context (name)
p.Size_Of_Context ()

Swap_Name
P=
What effect will
( “Timid”,
{ ( “TurnAround”,
the following
statements have on p,
the program object for
program SteerClear?

BLOCK

CALL
“turnright”

), (“StepAside”,

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

BLOCK

BLOCK

object Text name;
name = “Timid”;
p.Swap_Name (name);
name = “SteerClear”

CALL
“turnright”

WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

CALL
“turnleft”

) },

Swap_Body
What effect will
the following statements have
on object p from the previous
slide?

P=
( “Timid”,
{ ( “TurnAround”,

object Statement b, i, c;
object Text m = “move”;
object Integer test = RANDOM;
c.Compose_Call (m);
b.Add_To_Block (0, c);
i.Compose_If (test, b);
b.Add_to_Block (0, i);
p.Swap_Body (b);

CALL
“turnright”

BLOCK

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

CALL
“turnleft”

BLOCK

b=

WHILE
TRUE

IF
RANDOM

BLOCK

IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK

BLOCK

CALL
“skip”

CALL
“move”

) },

BLOCK

CALL
“turnright”

BLOCK

BLOCK

)

IF_ELSE
NEXT_IS_WALL

BLOCK

CALL
“TurnAround”

BLOCK

CALL
“StepAside”

Context Operations
What result is produced
by these statements if
p has its original value?

P=
( “SteerClear”,
{ ( “TurnAround”,

BLOCK

CALL
“turnright”

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

p.Size_Of_Context () ?

CALL
“turnright”

BLOCK

BLOCK

CALL
“move”

BLOCK

CALL
“turnleft”

WHILE
TRUE

p.Is_In_Context (“StepAside”) ?

BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

) },

Context Operations (cont’d)
What result is produced
by these statements if
p has the value shown on
the previous slide?

P=
( “SteerClear”,
{ (“StepAside”,

object Text n;
object Statement b;
p.Remove_From_Context
(“TurnAround”, n, b) ?
n=“TurnAround”

b=
CALL
“turnright”

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

BLOCK

CALL
“turnright”

) },

CALL
“move”

BLOCK

CALL
“turnleft”

BLOCK
WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Context Operations (cont’d)
What result is produced
by these statements if p
and b have the values
shown on the previous
slide?

P=
( “SteerClear”,
{ ( “TurnBack”,
CALL
“turnright”

b=

), (“StepAside”,

CALL
“turnright”

BLOCK

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

BLOCK

CALL
“turnleft”

BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

) },

CALL
“move”

WHILE
TRUE

n = “TurnBack”;
p.Add_To_Context (n, b) ?
n=“”

BLOCK

)

An Operation on Program
What would it mean to
demobilize a program?

Statements in BL Program
PROGRAM identifier IS

INSTRUCTION identifier IS

BEGIN

END identifier

END identifier

.
.
.

State the Problem
global_procedure Demobilize (
alters Program& p
);
/*!
ensures
p.name = #p.name and
p.context =
CONTEXT_DEMOBILIZE (#p.context) and
p.body = DEMOBILIZE (#p.body)
!*/

State the Problem Continued…
math definition CONTEXT_DEMOBILIZE (
c: CONTEXT
): CONTEXT satisfies
if c = empty_set then
CONTEXT_DEMOBILIZE (c) = c
else
there exists n: IDENTIFIER, s: STATEMENT,
rest_of_context: CONTEXT
(c = {(n, s)} union rest_of_context and
CONTEXT_DEMOBILIZE(c) =
{(n, DEMOBILIZE(s))} union
CONTEXT_DEMOBILIZE (rest_of_context))

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;
// demobilize program body
p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

Implementation Continued…
// demobilize statements in program context
while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}
// restore value of p (to be preserved)
p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;
}

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;

while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}

p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

}

p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;


Slide 7

BL Program
PROGRAM identifier IS

sequence of new instructions
BEGIN

sequence of statements
END identifier

Math Model for Program


What pieces of information do we
need to keep track of for a BL
program?





name (IDENTIFIER)
new instructions (CONTEXT)
body (BLOCK STATEMENT)

The Math Model


math subtype PROGRAM is (
name: IDENTIFIER
context: CONTEXT
body: STATEMENT
)
exemplar p
constraint
root (p.body).kind = BLOCK

New Instruction
INSTRUCTION identifier IS

sequence of statements
END identifier

Math Model Continued…


What pieces of information do we
need to keep track of for a new BL
instruction?




name (IDENTIFIER)
body (BLOCK STATEMENT)

Math Model Continued…


math subtype CONTEXT is
finite set of (
name: IDENTIFIER
body: STATEMENT
)
exemplar c
constraint 1. c is a (partial) function
2. for each (name, body) pair in c,
...
name is not one of primitives
nor the empty string, and body
is a BLOCK

Program Component


Type




Program_Kernel is modeled by PROGRAM

Initial Value


IS_INITIAL_PROGRAM (self) 
self.name = empty_string and
self.context = empty_set and
IS_INITIAL_STATEMENT (self.body)

Program SteerClear
Draw a picture of this BL program’s abstract view:
BEGIN
PROGRAM SteerClear IS
WHILE true DO
INSTRUCTION StepAside IS
IF next-is-empty THEN
IF random THEN
skip
turnright
ELSE
ELSE
IF next-is-wall THEN
turnleft
TurnAround
END IF
ELSE
move
StepAside
END StepAside
END IF
INSTRUCTION TurnAround IS
END IF
turnright
END WHILE
turnright
END SteerClear
END TurnAround

Program SteerClear (cont’d)
p=(

,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
,

},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{(

p.name
,

), (

,

) },

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
BLOCK

CALL
“turnright”

), ( “StepAside”,

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK
CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
), ( “StepAside”,

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

BLOCK
WHILE
TRUE

CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

BLOCK

p.context

IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

p.body

IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Program Continued…


Operations










p.Swap_Name (name)
p.Swap_Body (statement)
p.Add_To_Context (name, statement)
p.Remove_From_Context (name, name_copy,
statement)
p.Remove_Any_From_Context (name,
statement)
p.Is_In_Context (name)
p.Size_Of_Context ()

Swap_Name
P=
What effect will
( “Timid”,
{ ( “TurnAround”,
the following
statements have on p,
the program object for
program SteerClear?

BLOCK

CALL
“turnright”

), (“StepAside”,

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

BLOCK

BLOCK

object Text name;
name = “Timid”;
p.Swap_Name (name);
name = “SteerClear”

CALL
“turnright”

WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

CALL
“turnleft”

) },

Swap_Body
What effect will
the following statements have
on object p from the previous
slide?

P=
( “Timid”,
{ ( “TurnAround”,

object Statement b, i, c;
object Text m = “move”;
object Integer test = RANDOM;
c.Compose_Call (m);
b.Add_To_Block (0, c);
i.Compose_If (test, b);
b.Add_to_Block (0, i);
p.Swap_Body (b);

CALL
“turnright”

BLOCK

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

CALL
“turnleft”

BLOCK

b=

WHILE
TRUE

IF
RANDOM

BLOCK

IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK

BLOCK

CALL
“skip”

CALL
“move”

) },

BLOCK

CALL
“turnright”

BLOCK

BLOCK

)

IF_ELSE
NEXT_IS_WALL

BLOCK

CALL
“TurnAround”

BLOCK

CALL
“StepAside”

Context Operations
What result is produced
by these statements if
p has its original value?

P=
( “SteerClear”,
{ ( “TurnAround”,

BLOCK

CALL
“turnright”

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

p.Size_Of_Context () ?

CALL
“turnright”

BLOCK

BLOCK

CALL
“move”

BLOCK

CALL
“turnleft”

WHILE
TRUE

p.Is_In_Context (“StepAside”) ?

BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

) },

Context Operations (cont’d)
What result is produced
by these statements if
p has the value shown on
the previous slide?

P=
( “SteerClear”,
{ (“StepAside”,

object Text n;
object Statement b;
p.Remove_From_Context
(“TurnAround”, n, b) ?
n=“TurnAround”

b=
CALL
“turnright”

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

BLOCK

CALL
“turnright”

) },

CALL
“move”

BLOCK

CALL
“turnleft”

BLOCK
WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Context Operations (cont’d)
What result is produced
by these statements if p
and b have the values
shown on the previous
slide?

P=
( “SteerClear”,
{ ( “TurnBack”,
CALL
“turnright”

b=

), (“StepAside”,

CALL
“turnright”

BLOCK

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

BLOCK

CALL
“turnleft”

BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

) },

CALL
“move”

WHILE
TRUE

n = “TurnBack”;
p.Add_To_Context (n, b) ?
n=“”

BLOCK

)

An Operation on Program
What would it mean to
demobilize a program?

Statements in BL Program
PROGRAM identifier IS

INSTRUCTION identifier IS

BEGIN

END identifier

END identifier

.
.
.

State the Problem
global_procedure Demobilize (
alters Program& p
);
/*!
ensures
p.name = #p.name and
p.context =
CONTEXT_DEMOBILIZE (#p.context) and
p.body = DEMOBILIZE (#p.body)
!*/

State the Problem Continued…
math definition CONTEXT_DEMOBILIZE (
c: CONTEXT
): CONTEXT satisfies
if c = empty_set then
CONTEXT_DEMOBILIZE (c) = c
else
there exists n: IDENTIFIER, s: STATEMENT,
rest_of_context: CONTEXT
(c = {(n, s)} union rest_of_context and
CONTEXT_DEMOBILIZE(c) =
{(n, DEMOBILIZE(s))} union
CONTEXT_DEMOBILIZE (rest_of_context))

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;
// demobilize program body
p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

Implementation Continued…
// demobilize statements in program context
while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}
// restore value of p (to be preserved)
p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;
}

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;

while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}

p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

}

p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;


Slide 8

BL Program
PROGRAM identifier IS

sequence of new instructions
BEGIN

sequence of statements
END identifier

Math Model for Program


What pieces of information do we
need to keep track of for a BL
program?





name (IDENTIFIER)
new instructions (CONTEXT)
body (BLOCK STATEMENT)

The Math Model


math subtype PROGRAM is (
name: IDENTIFIER
context: CONTEXT
body: STATEMENT
)
exemplar p
constraint
root (p.body).kind = BLOCK

New Instruction
INSTRUCTION identifier IS

sequence of statements
END identifier

Math Model Continued…


What pieces of information do we
need to keep track of for a new BL
instruction?




name (IDENTIFIER)
body (BLOCK STATEMENT)

Math Model Continued…


math subtype CONTEXT is
finite set of (
name: IDENTIFIER
body: STATEMENT
)
exemplar c
constraint 1. c is a (partial) function
2. for each (name, body) pair in c,
...
name is not one of primitives
nor the empty string, and body
is a BLOCK

Program Component


Type




Program_Kernel is modeled by PROGRAM

Initial Value


IS_INITIAL_PROGRAM (self) 
self.name = empty_string and
self.context = empty_set and
IS_INITIAL_STATEMENT (self.body)

Program SteerClear
Draw a picture of this BL program’s abstract view:
BEGIN
PROGRAM SteerClear IS
WHILE true DO
INSTRUCTION StepAside IS
IF next-is-empty THEN
IF random THEN
skip
turnright
ELSE
ELSE
IF next-is-wall THEN
turnleft
TurnAround
END IF
ELSE
move
StepAside
END StepAside
END IF
INSTRUCTION TurnAround IS
END IF
turnright
END WHILE
turnright
END SteerClear
END TurnAround

Program SteerClear (cont’d)
p=(

,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
,

},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{(

p.name
,

), (

,

) },

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
BLOCK

CALL
“turnright”

), ( “StepAside”,

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK
CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
), ( “StepAside”,

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

BLOCK
WHILE
TRUE

CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

BLOCK

p.context

IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

p.body

IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Program Continued…


Operations










p.Swap_Name (name)
p.Swap_Body (statement)
p.Add_To_Context (name, statement)
p.Remove_From_Context (name, name_copy,
statement)
p.Remove_Any_From_Context (name,
statement)
p.Is_In_Context (name)
p.Size_Of_Context ()

Swap_Name
P=
What effect will
( “Timid”,
{ ( “TurnAround”,
the following
statements have on p,
the program object for
program SteerClear?

BLOCK

CALL
“turnright”

), (“StepAside”,

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

BLOCK

BLOCK

object Text name;
name = “Timid”;
p.Swap_Name (name);
name = “SteerClear”

CALL
“turnright”

WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

CALL
“turnleft”

) },

Swap_Body
What effect will
the following statements have
on object p from the previous
slide?

P=
( “Timid”,
{ ( “TurnAround”,

object Statement b, i, c;
object Text m = “move”;
object Integer test = RANDOM;
c.Compose_Call (m);
b.Add_To_Block (0, c);
i.Compose_If (test, b);
b.Add_to_Block (0, i);
p.Swap_Body (b);

CALL
“turnright”

BLOCK

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

CALL
“turnleft”

BLOCK

b=

WHILE
TRUE

IF
RANDOM

BLOCK

IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK

BLOCK

CALL
“skip”

CALL
“move”

) },

BLOCK

CALL
“turnright”

BLOCK

BLOCK

)

IF_ELSE
NEXT_IS_WALL

BLOCK

CALL
“TurnAround”

BLOCK

CALL
“StepAside”

Context Operations
What result is produced
by these statements if
p has its original value?

P=
( “SteerClear”,
{ ( “TurnAround”,

BLOCK

CALL
“turnright”

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

p.Size_Of_Context () ?

CALL
“turnright”

BLOCK

BLOCK

CALL
“move”

BLOCK

CALL
“turnleft”

WHILE
TRUE

p.Is_In_Context (“StepAside”) ?

BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

) },

Context Operations (cont’d)
What result is produced
by these statements if
p has the value shown on
the previous slide?

P=
( “SteerClear”,
{ (“StepAside”,

object Text n;
object Statement b;
p.Remove_From_Context
(“TurnAround”, n, b) ?
n=“TurnAround”

b=
CALL
“turnright”

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

BLOCK

CALL
“turnright”

) },

CALL
“move”

BLOCK

CALL
“turnleft”

BLOCK
WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Context Operations (cont’d)
What result is produced
by these statements if p
and b have the values
shown on the previous
slide?

P=
( “SteerClear”,
{ ( “TurnBack”,
CALL
“turnright”

b=

), (“StepAside”,

CALL
“turnright”

BLOCK

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

BLOCK

CALL
“turnleft”

BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

) },

CALL
“move”

WHILE
TRUE

n = “TurnBack”;
p.Add_To_Context (n, b) ?
n=“”

BLOCK

)

An Operation on Program
What would it mean to
demobilize a program?

Statements in BL Program
PROGRAM identifier IS

INSTRUCTION identifier IS

BEGIN

END identifier

END identifier

.
.
.

State the Problem
global_procedure Demobilize (
alters Program& p
);
/*!
ensures
p.name = #p.name and
p.context =
CONTEXT_DEMOBILIZE (#p.context) and
p.body = DEMOBILIZE (#p.body)
!*/

State the Problem Continued…
math definition CONTEXT_DEMOBILIZE (
c: CONTEXT
): CONTEXT satisfies
if c = empty_set then
CONTEXT_DEMOBILIZE (c) = c
else
there exists n: IDENTIFIER, s: STATEMENT,
rest_of_context: CONTEXT
(c = {(n, s)} union rest_of_context and
CONTEXT_DEMOBILIZE(c) =
{(n, DEMOBILIZE(s))} union
CONTEXT_DEMOBILIZE (rest_of_context))

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;
// demobilize program body
p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

Implementation Continued…
// demobilize statements in program context
while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}
// restore value of p (to be preserved)
p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;
}

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;

while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}

p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

}

p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;


Slide 9

BL Program
PROGRAM identifier IS

sequence of new instructions
BEGIN

sequence of statements
END identifier

Math Model for Program


What pieces of information do we
need to keep track of for a BL
program?





name (IDENTIFIER)
new instructions (CONTEXT)
body (BLOCK STATEMENT)

The Math Model


math subtype PROGRAM is (
name: IDENTIFIER
context: CONTEXT
body: STATEMENT
)
exemplar p
constraint
root (p.body).kind = BLOCK

New Instruction
INSTRUCTION identifier IS

sequence of statements
END identifier

Math Model Continued…


What pieces of information do we
need to keep track of for a new BL
instruction?




name (IDENTIFIER)
body (BLOCK STATEMENT)

Math Model Continued…


math subtype CONTEXT is
finite set of (
name: IDENTIFIER
body: STATEMENT
)
exemplar c
constraint 1. c is a (partial) function
2. for each (name, body) pair in c,
...
name is not one of primitives
nor the empty string, and body
is a BLOCK

Program Component


Type




Program_Kernel is modeled by PROGRAM

Initial Value


IS_INITIAL_PROGRAM (self) 
self.name = empty_string and
self.context = empty_set and
IS_INITIAL_STATEMENT (self.body)

Program SteerClear
Draw a picture of this BL program’s abstract view:
BEGIN
PROGRAM SteerClear IS
WHILE true DO
INSTRUCTION StepAside IS
IF next-is-empty THEN
IF random THEN
skip
turnright
ELSE
ELSE
IF next-is-wall THEN
turnleft
TurnAround
END IF
ELSE
move
StepAside
END StepAside
END IF
INSTRUCTION TurnAround IS
END IF
turnright
END WHILE
turnright
END SteerClear
END TurnAround

Program SteerClear (cont’d)
p=(

,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
,

},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{(

p.name
,

), (

,

) },

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
BLOCK

CALL
“turnright”

), ( “StepAside”,

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK
CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
), ( “StepAside”,

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

BLOCK
WHILE
TRUE

CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

BLOCK

p.context

IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

p.body

IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Program Continued…


Operations










p.Swap_Name (name)
p.Swap_Body (statement)
p.Add_To_Context (name, statement)
p.Remove_From_Context (name, name_copy,
statement)
p.Remove_Any_From_Context (name,
statement)
p.Is_In_Context (name)
p.Size_Of_Context ()

Swap_Name
P=
What effect will
( “Timid”,
{ ( “TurnAround”,
the following
statements have on p,
the program object for
program SteerClear?

BLOCK

CALL
“turnright”

), (“StepAside”,

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

BLOCK

BLOCK

object Text name;
name = “Timid”;
p.Swap_Name (name);
name = “SteerClear”

CALL
“turnright”

WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

CALL
“turnleft”

) },

Swap_Body
What effect will
the following statements have
on object p from the previous
slide?

P=
( “Timid”,
{ ( “TurnAround”,

object Statement b, i, c;
object Text m = “move”;
object Integer test = RANDOM;
c.Compose_Call (m);
b.Add_To_Block (0, c);
i.Compose_If (test, b);
b.Add_to_Block (0, i);
p.Swap_Body (b);

CALL
“turnright”

BLOCK

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

CALL
“turnleft”

BLOCK

b=

WHILE
TRUE

IF
RANDOM

BLOCK

IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK

BLOCK

CALL
“skip”

CALL
“move”

) },

BLOCK

CALL
“turnright”

BLOCK

BLOCK

)

IF_ELSE
NEXT_IS_WALL

BLOCK

CALL
“TurnAround”

BLOCK

CALL
“StepAside”

Context Operations
What result is produced
by these statements if
p has its original value?

P=
( “SteerClear”,
{ ( “TurnAround”,

BLOCK

CALL
“turnright”

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

p.Size_Of_Context () ?

CALL
“turnright”

BLOCK

BLOCK

CALL
“move”

BLOCK

CALL
“turnleft”

WHILE
TRUE

p.Is_In_Context (“StepAside”) ?

BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

) },

Context Operations (cont’d)
What result is produced
by these statements if
p has the value shown on
the previous slide?

P=
( “SteerClear”,
{ (“StepAside”,

object Text n;
object Statement b;
p.Remove_From_Context
(“TurnAround”, n, b) ?
n=“TurnAround”

b=
CALL
“turnright”

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

BLOCK

CALL
“turnright”

) },

CALL
“move”

BLOCK

CALL
“turnleft”

BLOCK
WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Context Operations (cont’d)
What result is produced
by these statements if p
and b have the values
shown on the previous
slide?

P=
( “SteerClear”,
{ ( “TurnBack”,
CALL
“turnright”

b=

), (“StepAside”,

CALL
“turnright”

BLOCK

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

BLOCK

CALL
“turnleft”

BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

) },

CALL
“move”

WHILE
TRUE

n = “TurnBack”;
p.Add_To_Context (n, b) ?
n=“”

BLOCK

)

An Operation on Program
What would it mean to
demobilize a program?

Statements in BL Program
PROGRAM identifier IS

INSTRUCTION identifier IS

BEGIN

END identifier

END identifier

.
.
.

State the Problem
global_procedure Demobilize (
alters Program& p
);
/*!
ensures
p.name = #p.name and
p.context =
CONTEXT_DEMOBILIZE (#p.context) and
p.body = DEMOBILIZE (#p.body)
!*/

State the Problem Continued…
math definition CONTEXT_DEMOBILIZE (
c: CONTEXT
): CONTEXT satisfies
if c = empty_set then
CONTEXT_DEMOBILIZE (c) = c
else
there exists n: IDENTIFIER, s: STATEMENT,
rest_of_context: CONTEXT
(c = {(n, s)} union rest_of_context and
CONTEXT_DEMOBILIZE(c) =
{(n, DEMOBILIZE(s))} union
CONTEXT_DEMOBILIZE (rest_of_context))

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;
// demobilize program body
p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

Implementation Continued…
// demobilize statements in program context
while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}
// restore value of p (to be preserved)
p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;
}

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;

while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}

p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

}

p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;


Slide 10

BL Program
PROGRAM identifier IS

sequence of new instructions
BEGIN

sequence of statements
END identifier

Math Model for Program


What pieces of information do we
need to keep track of for a BL
program?





name (IDENTIFIER)
new instructions (CONTEXT)
body (BLOCK STATEMENT)

The Math Model


math subtype PROGRAM is (
name: IDENTIFIER
context: CONTEXT
body: STATEMENT
)
exemplar p
constraint
root (p.body).kind = BLOCK

New Instruction
INSTRUCTION identifier IS

sequence of statements
END identifier

Math Model Continued…


What pieces of information do we
need to keep track of for a new BL
instruction?




name (IDENTIFIER)
body (BLOCK STATEMENT)

Math Model Continued…


math subtype CONTEXT is
finite set of (
name: IDENTIFIER
body: STATEMENT
)
exemplar c
constraint 1. c is a (partial) function
2. for each (name, body) pair in c,
...
name is not one of primitives
nor the empty string, and body
is a BLOCK

Program Component


Type




Program_Kernel is modeled by PROGRAM

Initial Value


IS_INITIAL_PROGRAM (self) 
self.name = empty_string and
self.context = empty_set and
IS_INITIAL_STATEMENT (self.body)

Program SteerClear
Draw a picture of this BL program’s abstract view:
BEGIN
PROGRAM SteerClear IS
WHILE true DO
INSTRUCTION StepAside IS
IF next-is-empty THEN
IF random THEN
skip
turnright
ELSE
ELSE
IF next-is-wall THEN
turnleft
TurnAround
END IF
ELSE
move
StepAside
END StepAside
END IF
INSTRUCTION TurnAround IS
END IF
turnright
END WHILE
turnright
END SteerClear
END TurnAround

Program SteerClear (cont’d)
p=(

,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
,

},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{(

p.name
,

), (

,

) },

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
BLOCK

CALL
“turnright”

), ( “StepAside”,

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK
CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
), ( “StepAside”,

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

BLOCK
WHILE
TRUE

CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

BLOCK

p.context

IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

p.body

IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Program Continued…


Operations










p.Swap_Name (name)
p.Swap_Body (statement)
p.Add_To_Context (name, statement)
p.Remove_From_Context (name, name_copy,
statement)
p.Remove_Any_From_Context (name,
statement)
p.Is_In_Context (name)
p.Size_Of_Context ()

Swap_Name
P=
What effect will
( “Timid”,
{ ( “TurnAround”,
the following
statements have on p,
the program object for
program SteerClear?

BLOCK

CALL
“turnright”

), (“StepAside”,

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

BLOCK

BLOCK

object Text name;
name = “Timid”;
p.Swap_Name (name);
name = “SteerClear”

CALL
“turnright”

WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

CALL
“turnleft”

) },

Swap_Body
What effect will
the following statements have
on object p from the previous
slide?

P=
( “Timid”,
{ ( “TurnAround”,

object Statement b, i, c;
object Text m = “move”;
object Integer test = RANDOM;
c.Compose_Call (m);
b.Add_To_Block (0, c);
i.Compose_If (test, b);
b.Add_to_Block (0, i);
p.Swap_Body (b);

CALL
“turnright”

BLOCK

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

CALL
“turnleft”

BLOCK

b=

WHILE
TRUE

IF
RANDOM

BLOCK

IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK

BLOCK

CALL
“skip”

CALL
“move”

) },

BLOCK

CALL
“turnright”

BLOCK

BLOCK

)

IF_ELSE
NEXT_IS_WALL

BLOCK

CALL
“TurnAround”

BLOCK

CALL
“StepAside”

Context Operations
What result is produced
by these statements if
p has its original value?

P=
( “SteerClear”,
{ ( “TurnAround”,

BLOCK

CALL
“turnright”

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

p.Size_Of_Context () ?

CALL
“turnright”

BLOCK

BLOCK

CALL
“move”

BLOCK

CALL
“turnleft”

WHILE
TRUE

p.Is_In_Context (“StepAside”) ?

BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

) },

Context Operations (cont’d)
What result is produced
by these statements if
p has the value shown on
the previous slide?

P=
( “SteerClear”,
{ (“StepAside”,

object Text n;
object Statement b;
p.Remove_From_Context
(“TurnAround”, n, b) ?
n=“TurnAround”

b=
CALL
“turnright”

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

BLOCK

CALL
“turnright”

) },

CALL
“move”

BLOCK

CALL
“turnleft”

BLOCK
WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Context Operations (cont’d)
What result is produced
by these statements if p
and b have the values
shown on the previous
slide?

P=
( “SteerClear”,
{ ( “TurnBack”,
CALL
“turnright”

b=

), (“StepAside”,

CALL
“turnright”

BLOCK

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

BLOCK

CALL
“turnleft”

BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

) },

CALL
“move”

WHILE
TRUE

n = “TurnBack”;
p.Add_To_Context (n, b) ?
n=“”

BLOCK

)

An Operation on Program
What would it mean to
demobilize a program?

Statements in BL Program
PROGRAM identifier IS

INSTRUCTION identifier IS

BEGIN

END identifier

END identifier

.
.
.

State the Problem
global_procedure Demobilize (
alters Program& p
);
/*!
ensures
p.name = #p.name and
p.context =
CONTEXT_DEMOBILIZE (#p.context) and
p.body = DEMOBILIZE (#p.body)
!*/

State the Problem Continued…
math definition CONTEXT_DEMOBILIZE (
c: CONTEXT
): CONTEXT satisfies
if c = empty_set then
CONTEXT_DEMOBILIZE (c) = c
else
there exists n: IDENTIFIER, s: STATEMENT,
rest_of_context: CONTEXT
(c = {(n, s)} union rest_of_context and
CONTEXT_DEMOBILIZE(c) =
{(n, DEMOBILIZE(s))} union
CONTEXT_DEMOBILIZE (rest_of_context))

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;
// demobilize program body
p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

Implementation Continued…
// demobilize statements in program context
while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}
// restore value of p (to be preserved)
p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;
}

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;

while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}

p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

}

p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;


Slide 11

BL Program
PROGRAM identifier IS

sequence of new instructions
BEGIN

sequence of statements
END identifier

Math Model for Program


What pieces of information do we
need to keep track of for a BL
program?





name (IDENTIFIER)
new instructions (CONTEXT)
body (BLOCK STATEMENT)

The Math Model


math subtype PROGRAM is (
name: IDENTIFIER
context: CONTEXT
body: STATEMENT
)
exemplar p
constraint
root (p.body).kind = BLOCK

New Instruction
INSTRUCTION identifier IS

sequence of statements
END identifier

Math Model Continued…


What pieces of information do we
need to keep track of for a new BL
instruction?




name (IDENTIFIER)
body (BLOCK STATEMENT)

Math Model Continued…


math subtype CONTEXT is
finite set of (
name: IDENTIFIER
body: STATEMENT
)
exemplar c
constraint 1. c is a (partial) function
2. for each (name, body) pair in c,
...
name is not one of primitives
nor the empty string, and body
is a BLOCK

Program Component


Type




Program_Kernel is modeled by PROGRAM

Initial Value


IS_INITIAL_PROGRAM (self) 
self.name = empty_string and
self.context = empty_set and
IS_INITIAL_STATEMENT (self.body)

Program SteerClear
Draw a picture of this BL program’s abstract view:
BEGIN
PROGRAM SteerClear IS
WHILE true DO
INSTRUCTION StepAside IS
IF next-is-empty THEN
IF random THEN
skip
turnright
ELSE
ELSE
IF next-is-wall THEN
turnleft
TurnAround
END IF
ELSE
move
StepAside
END StepAside
END IF
INSTRUCTION TurnAround IS
END IF
turnright
END WHILE
turnright
END SteerClear
END TurnAround

Program SteerClear (cont’d)
p=(

,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
,

},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{(

p.name
,

), (

,

) },

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
BLOCK

CALL
“turnright”

), ( “StepAside”,

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK
CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
), ( “StepAside”,

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

BLOCK
WHILE
TRUE

CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

BLOCK

p.context

IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

p.body

IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Program Continued…


Operations










p.Swap_Name (name)
p.Swap_Body (statement)
p.Add_To_Context (name, statement)
p.Remove_From_Context (name, name_copy,
statement)
p.Remove_Any_From_Context (name,
statement)
p.Is_In_Context (name)
p.Size_Of_Context ()

Swap_Name
P=
What effect will
( “Timid”,
{ ( “TurnAround”,
the following
statements have on p,
the program object for
program SteerClear?

BLOCK

CALL
“turnright”

), (“StepAside”,

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

BLOCK

BLOCK

object Text name;
name = “Timid”;
p.Swap_Name (name);
name = “SteerClear”

CALL
“turnright”

WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

CALL
“turnleft”

) },

Swap_Body
What effect will
the following statements have
on object p from the previous
slide?

P=
( “Timid”,
{ ( “TurnAround”,

object Statement b, i, c;
object Text m = “move”;
object Integer test = RANDOM;
c.Compose_Call (m);
b.Add_To_Block (0, c);
i.Compose_If (test, b);
b.Add_to_Block (0, i);
p.Swap_Body (b);

CALL
“turnright”

BLOCK

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

CALL
“turnleft”

BLOCK

b=

WHILE
TRUE

IF
RANDOM

BLOCK

IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK

BLOCK

CALL
“skip”

CALL
“move”

) },

BLOCK

CALL
“turnright”

BLOCK

BLOCK

)

IF_ELSE
NEXT_IS_WALL

BLOCK

CALL
“TurnAround”

BLOCK

CALL
“StepAside”

Context Operations
What result is produced
by these statements if
p has its original value?

P=
( “SteerClear”,
{ ( “TurnAround”,

BLOCK

CALL
“turnright”

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

p.Size_Of_Context () ?

CALL
“turnright”

BLOCK

BLOCK

CALL
“move”

BLOCK

CALL
“turnleft”

WHILE
TRUE

p.Is_In_Context (“StepAside”) ?

BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

) },

Context Operations (cont’d)
What result is produced
by these statements if
p has the value shown on
the previous slide?

P=
( “SteerClear”,
{ (“StepAside”,

object Text n;
object Statement b;
p.Remove_From_Context
(“TurnAround”, n, b) ?
n=“TurnAround”

b=
CALL
“turnright”

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

BLOCK

CALL
“turnright”

) },

CALL
“move”

BLOCK

CALL
“turnleft”

BLOCK
WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Context Operations (cont’d)
What result is produced
by these statements if p
and b have the values
shown on the previous
slide?

P=
( “SteerClear”,
{ ( “TurnBack”,
CALL
“turnright”

b=

), (“StepAside”,

CALL
“turnright”

BLOCK

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

BLOCK

CALL
“turnleft”

BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

) },

CALL
“move”

WHILE
TRUE

n = “TurnBack”;
p.Add_To_Context (n, b) ?
n=“”

BLOCK

)

An Operation on Program
What would it mean to
demobilize a program?

Statements in BL Program
PROGRAM identifier IS

INSTRUCTION identifier IS

BEGIN

END identifier

END identifier

.
.
.

State the Problem
global_procedure Demobilize (
alters Program& p
);
/*!
ensures
p.name = #p.name and
p.context =
CONTEXT_DEMOBILIZE (#p.context) and
p.body = DEMOBILIZE (#p.body)
!*/

State the Problem Continued…
math definition CONTEXT_DEMOBILIZE (
c: CONTEXT
): CONTEXT satisfies
if c = empty_set then
CONTEXT_DEMOBILIZE (c) = c
else
there exists n: IDENTIFIER, s: STATEMENT,
rest_of_context: CONTEXT
(c = {(n, s)} union rest_of_context and
CONTEXT_DEMOBILIZE(c) =
{(n, DEMOBILIZE(s))} union
CONTEXT_DEMOBILIZE (rest_of_context))

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;
// demobilize program body
p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

Implementation Continued…
// demobilize statements in program context
while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}
// restore value of p (to be preserved)
p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;
}

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;

while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}

p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

}

p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;


Slide 12

BL Program
PROGRAM identifier IS

sequence of new instructions
BEGIN

sequence of statements
END identifier

Math Model for Program


What pieces of information do we
need to keep track of for a BL
program?





name (IDENTIFIER)
new instructions (CONTEXT)
body (BLOCK STATEMENT)

The Math Model


math subtype PROGRAM is (
name: IDENTIFIER
context: CONTEXT
body: STATEMENT
)
exemplar p
constraint
root (p.body).kind = BLOCK

New Instruction
INSTRUCTION identifier IS

sequence of statements
END identifier

Math Model Continued…


What pieces of information do we
need to keep track of for a new BL
instruction?




name (IDENTIFIER)
body (BLOCK STATEMENT)

Math Model Continued…


math subtype CONTEXT is
finite set of (
name: IDENTIFIER
body: STATEMENT
)
exemplar c
constraint 1. c is a (partial) function
2. for each (name, body) pair in c,
...
name is not one of primitives
nor the empty string, and body
is a BLOCK

Program Component


Type




Program_Kernel is modeled by PROGRAM

Initial Value


IS_INITIAL_PROGRAM (self) 
self.name = empty_string and
self.context = empty_set and
IS_INITIAL_STATEMENT (self.body)

Program SteerClear
Draw a picture of this BL program’s abstract view:
BEGIN
PROGRAM SteerClear IS
WHILE true DO
INSTRUCTION StepAside IS
IF next-is-empty THEN
IF random THEN
skip
turnright
ELSE
ELSE
IF next-is-wall THEN
turnleft
TurnAround
END IF
ELSE
move
StepAside
END StepAside
END IF
INSTRUCTION TurnAround IS
END IF
turnright
END WHILE
turnright
END SteerClear
END TurnAround

Program SteerClear (cont’d)
p=(

,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
,

},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{(

p.name
,

), (

,

) },

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
BLOCK

CALL
“turnright”

), ( “StepAside”,

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK
CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
), ( “StepAside”,

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

BLOCK
WHILE
TRUE

CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

BLOCK

p.context

IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

p.body

IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Program Continued…


Operations










p.Swap_Name (name)
p.Swap_Body (statement)
p.Add_To_Context (name, statement)
p.Remove_From_Context (name, name_copy,
statement)
p.Remove_Any_From_Context (name,
statement)
p.Is_In_Context (name)
p.Size_Of_Context ()

Swap_Name
P=
What effect will
( “Timid”,
{ ( “TurnAround”,
the following
statements have on p,
the program object for
program SteerClear?

BLOCK

CALL
“turnright”

), (“StepAside”,

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

BLOCK

BLOCK

object Text name;
name = “Timid”;
p.Swap_Name (name);
name = “SteerClear”

CALL
“turnright”

WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

CALL
“turnleft”

) },

Swap_Body
What effect will
the following statements have
on object p from the previous
slide?

P=
( “Timid”,
{ ( “TurnAround”,

object Statement b, i, c;
object Text m = “move”;
object Integer test = RANDOM;
c.Compose_Call (m);
b.Add_To_Block (0, c);
i.Compose_If (test, b);
b.Add_to_Block (0, i);
p.Swap_Body (b);

CALL
“turnright”

BLOCK

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

CALL
“turnleft”

BLOCK

b=

WHILE
TRUE

IF
RANDOM

BLOCK

IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK

BLOCK

CALL
“skip”

CALL
“move”

) },

BLOCK

CALL
“turnright”

BLOCK

BLOCK

)

IF_ELSE
NEXT_IS_WALL

BLOCK

CALL
“TurnAround”

BLOCK

CALL
“StepAside”

Context Operations
What result is produced
by these statements if
p has its original value?

P=
( “SteerClear”,
{ ( “TurnAround”,

BLOCK

CALL
“turnright”

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

p.Size_Of_Context () ?

CALL
“turnright”

BLOCK

BLOCK

CALL
“move”

BLOCK

CALL
“turnleft”

WHILE
TRUE

p.Is_In_Context (“StepAside”) ?

BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

) },

Context Operations (cont’d)
What result is produced
by these statements if
p has the value shown on
the previous slide?

P=
( “SteerClear”,
{ (“StepAside”,

object Text n;
object Statement b;
p.Remove_From_Context
(“TurnAround”, n, b) ?
n=“TurnAround”

b=
CALL
“turnright”

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

BLOCK

CALL
“turnright”

) },

CALL
“move”

BLOCK

CALL
“turnleft”

BLOCK
WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Context Operations (cont’d)
What result is produced
by these statements if p
and b have the values
shown on the previous
slide?

P=
( “SteerClear”,
{ ( “TurnBack”,
CALL
“turnright”

b=

), (“StepAside”,

CALL
“turnright”

BLOCK

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

BLOCK

CALL
“turnleft”

BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

) },

CALL
“move”

WHILE
TRUE

n = “TurnBack”;
p.Add_To_Context (n, b) ?
n=“”

BLOCK

)

An Operation on Program
What would it mean to
demobilize a program?

Statements in BL Program
PROGRAM identifier IS

INSTRUCTION identifier IS

BEGIN

END identifier

END identifier

.
.
.

State the Problem
global_procedure Demobilize (
alters Program& p
);
/*!
ensures
p.name = #p.name and
p.context =
CONTEXT_DEMOBILIZE (#p.context) and
p.body = DEMOBILIZE (#p.body)
!*/

State the Problem Continued…
math definition CONTEXT_DEMOBILIZE (
c: CONTEXT
): CONTEXT satisfies
if c = empty_set then
CONTEXT_DEMOBILIZE (c) = c
else
there exists n: IDENTIFIER, s: STATEMENT,
rest_of_context: CONTEXT
(c = {(n, s)} union rest_of_context and
CONTEXT_DEMOBILIZE(c) =
{(n, DEMOBILIZE(s))} union
CONTEXT_DEMOBILIZE (rest_of_context))

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;
// demobilize program body
p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

Implementation Continued…
// demobilize statements in program context
while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}
// restore value of p (to be preserved)
p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;
}

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;

while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}

p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

}

p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;


Slide 13

BL Program
PROGRAM identifier IS

sequence of new instructions
BEGIN

sequence of statements
END identifier

Math Model for Program


What pieces of information do we
need to keep track of for a BL
program?





name (IDENTIFIER)
new instructions (CONTEXT)
body (BLOCK STATEMENT)

The Math Model


math subtype PROGRAM is (
name: IDENTIFIER
context: CONTEXT
body: STATEMENT
)
exemplar p
constraint
root (p.body).kind = BLOCK

New Instruction
INSTRUCTION identifier IS

sequence of statements
END identifier

Math Model Continued…


What pieces of information do we
need to keep track of for a new BL
instruction?




name (IDENTIFIER)
body (BLOCK STATEMENT)

Math Model Continued…


math subtype CONTEXT is
finite set of (
name: IDENTIFIER
body: STATEMENT
)
exemplar c
constraint 1. c is a (partial) function
2. for each (name, body) pair in c,
...
name is not one of primitives
nor the empty string, and body
is a BLOCK

Program Component


Type




Program_Kernel is modeled by PROGRAM

Initial Value


IS_INITIAL_PROGRAM (self) 
self.name = empty_string and
self.context = empty_set and
IS_INITIAL_STATEMENT (self.body)

Program SteerClear
Draw a picture of this BL program’s abstract view:
BEGIN
PROGRAM SteerClear IS
WHILE true DO
INSTRUCTION StepAside IS
IF next-is-empty THEN
IF random THEN
skip
turnright
ELSE
ELSE
IF next-is-wall THEN
turnleft
TurnAround
END IF
ELSE
move
StepAside
END StepAside
END IF
INSTRUCTION TurnAround IS
END IF
turnright
END WHILE
turnright
END SteerClear
END TurnAround

Program SteerClear (cont’d)
p=(

,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
,

},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{(

p.name
,

), (

,

) },

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
BLOCK

CALL
“turnright”

), ( “StepAside”,

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK
CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
), ( “StepAside”,

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

BLOCK
WHILE
TRUE

CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

BLOCK

p.context

IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

p.body

IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Program Continued…


Operations










p.Swap_Name (name)
p.Swap_Body (statement)
p.Add_To_Context (name, statement)
p.Remove_From_Context (name, name_copy,
statement)
p.Remove_Any_From_Context (name,
statement)
p.Is_In_Context (name)
p.Size_Of_Context ()

Swap_Name
P=
What effect will
( “Timid”,
{ ( “TurnAround”,
the following
statements have on p,
the program object for
program SteerClear?

BLOCK

CALL
“turnright”

), (“StepAside”,

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

BLOCK

BLOCK

object Text name;
name = “Timid”;
p.Swap_Name (name);
name = “SteerClear”

CALL
“turnright”

WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

CALL
“turnleft”

) },

Swap_Body
What effect will
the following statements have
on object p from the previous
slide?

P=
( “Timid”,
{ ( “TurnAround”,

object Statement b, i, c;
object Text m = “move”;
object Integer test = RANDOM;
c.Compose_Call (m);
b.Add_To_Block (0, c);
i.Compose_If (test, b);
b.Add_to_Block (0, i);
p.Swap_Body (b);

CALL
“turnright”

BLOCK

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

CALL
“turnleft”

BLOCK

b=

WHILE
TRUE

IF
RANDOM

BLOCK

IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK

BLOCK

CALL
“skip”

CALL
“move”

) },

BLOCK

CALL
“turnright”

BLOCK

BLOCK

)

IF_ELSE
NEXT_IS_WALL

BLOCK

CALL
“TurnAround”

BLOCK

CALL
“StepAside”

Context Operations
What result is produced
by these statements if
p has its original value?

P=
( “SteerClear”,
{ ( “TurnAround”,

BLOCK

CALL
“turnright”

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

p.Size_Of_Context () ?

CALL
“turnright”

BLOCK

BLOCK

CALL
“move”

BLOCK

CALL
“turnleft”

WHILE
TRUE

p.Is_In_Context (“StepAside”) ?

BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

) },

Context Operations (cont’d)
What result is produced
by these statements if
p has the value shown on
the previous slide?

P=
( “SteerClear”,
{ (“StepAside”,

object Text n;
object Statement b;
p.Remove_From_Context
(“TurnAround”, n, b) ?
n=“TurnAround”

b=
CALL
“turnright”

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

BLOCK

CALL
“turnright”

) },

CALL
“move”

BLOCK

CALL
“turnleft”

BLOCK
WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Context Operations (cont’d)
What result is produced
by these statements if p
and b have the values
shown on the previous
slide?

P=
( “SteerClear”,
{ ( “TurnBack”,
CALL
“turnright”

b=

), (“StepAside”,

CALL
“turnright”

BLOCK

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

BLOCK

CALL
“turnleft”

BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

) },

CALL
“move”

WHILE
TRUE

n = “TurnBack”;
p.Add_To_Context (n, b) ?
n=“”

BLOCK

)

An Operation on Program
What would it mean to
demobilize a program?

Statements in BL Program
PROGRAM identifier IS

INSTRUCTION identifier IS

BEGIN

END identifier

END identifier

.
.
.

State the Problem
global_procedure Demobilize (
alters Program& p
);
/*!
ensures
p.name = #p.name and
p.context =
CONTEXT_DEMOBILIZE (#p.context) and
p.body = DEMOBILIZE (#p.body)
!*/

State the Problem Continued…
math definition CONTEXT_DEMOBILIZE (
c: CONTEXT
): CONTEXT satisfies
if c = empty_set then
CONTEXT_DEMOBILIZE (c) = c
else
there exists n: IDENTIFIER, s: STATEMENT,
rest_of_context: CONTEXT
(c = {(n, s)} union rest_of_context and
CONTEXT_DEMOBILIZE(c) =
{(n, DEMOBILIZE(s))} union
CONTEXT_DEMOBILIZE (rest_of_context))

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;
// demobilize program body
p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

Implementation Continued…
// demobilize statements in program context
while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}
// restore value of p (to be preserved)
p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;
}

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;

while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}

p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

}

p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;


Slide 14

BL Program
PROGRAM identifier IS

sequence of new instructions
BEGIN

sequence of statements
END identifier

Math Model for Program


What pieces of information do we
need to keep track of for a BL
program?





name (IDENTIFIER)
new instructions (CONTEXT)
body (BLOCK STATEMENT)

The Math Model


math subtype PROGRAM is (
name: IDENTIFIER
context: CONTEXT
body: STATEMENT
)
exemplar p
constraint
root (p.body).kind = BLOCK

New Instruction
INSTRUCTION identifier IS

sequence of statements
END identifier

Math Model Continued…


What pieces of information do we
need to keep track of for a new BL
instruction?




name (IDENTIFIER)
body (BLOCK STATEMENT)

Math Model Continued…


math subtype CONTEXT is
finite set of (
name: IDENTIFIER
body: STATEMENT
)
exemplar c
constraint 1. c is a (partial) function
2. for each (name, body) pair in c,
...
name is not one of primitives
nor the empty string, and body
is a BLOCK

Program Component


Type




Program_Kernel is modeled by PROGRAM

Initial Value


IS_INITIAL_PROGRAM (self) 
self.name = empty_string and
self.context = empty_set and
IS_INITIAL_STATEMENT (self.body)

Program SteerClear
Draw a picture of this BL program’s abstract view:
BEGIN
PROGRAM SteerClear IS
WHILE true DO
INSTRUCTION StepAside IS
IF next-is-empty THEN
IF random THEN
skip
turnright
ELSE
ELSE
IF next-is-wall THEN
turnleft
TurnAround
END IF
ELSE
move
StepAside
END StepAside
END IF
INSTRUCTION TurnAround IS
END IF
turnright
END WHILE
turnright
END SteerClear
END TurnAround

Program SteerClear (cont’d)
p=(

,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
,

},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{(

p.name
,

), (

,

) },

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
BLOCK

CALL
“turnright”

), ( “StepAside”,

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK
CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
), ( “StepAside”,

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

BLOCK
WHILE
TRUE

CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

BLOCK

p.context

IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

p.body

IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Program Continued…


Operations










p.Swap_Name (name)
p.Swap_Body (statement)
p.Add_To_Context (name, statement)
p.Remove_From_Context (name, name_copy,
statement)
p.Remove_Any_From_Context (name,
statement)
p.Is_In_Context (name)
p.Size_Of_Context ()

Swap_Name
P=
What effect will
( “Timid”,
{ ( “TurnAround”,
the following
statements have on p,
the program object for
program SteerClear?

BLOCK

CALL
“turnright”

), (“StepAside”,

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

BLOCK

BLOCK

object Text name;
name = “Timid”;
p.Swap_Name (name);
name = “SteerClear”

CALL
“turnright”

WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

CALL
“turnleft”

) },

Swap_Body
What effect will
the following statements have
on object p from the previous
slide?

P=
( “Timid”,
{ ( “TurnAround”,

object Statement b, i, c;
object Text m = “move”;
object Integer test = RANDOM;
c.Compose_Call (m);
b.Add_To_Block (0, c);
i.Compose_If (test, b);
b.Add_to_Block (0, i);
p.Swap_Body (b);

CALL
“turnright”

BLOCK

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

CALL
“turnleft”

BLOCK

b=

WHILE
TRUE

IF
RANDOM

BLOCK

IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK

BLOCK

CALL
“skip”

CALL
“move”

) },

BLOCK

CALL
“turnright”

BLOCK

BLOCK

)

IF_ELSE
NEXT_IS_WALL

BLOCK

CALL
“TurnAround”

BLOCK

CALL
“StepAside”

Context Operations
What result is produced
by these statements if
p has its original value?

P=
( “SteerClear”,
{ ( “TurnAround”,

BLOCK

CALL
“turnright”

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

p.Size_Of_Context () ?

CALL
“turnright”

BLOCK

BLOCK

CALL
“move”

BLOCK

CALL
“turnleft”

WHILE
TRUE

p.Is_In_Context (“StepAside”) ?

BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

) },

Context Operations (cont’d)
What result is produced
by these statements if
p has the value shown on
the previous slide?

P=
( “SteerClear”,
{ (“StepAside”,

object Text n;
object Statement b;
p.Remove_From_Context
(“TurnAround”, n, b) ?
n=“TurnAround”

b=
CALL
“turnright”

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

BLOCK

CALL
“turnright”

) },

CALL
“move”

BLOCK

CALL
“turnleft”

BLOCK
WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Context Operations (cont’d)
What result is produced
by these statements if p
and b have the values
shown on the previous
slide?

P=
( “SteerClear”,
{ ( “TurnBack”,
CALL
“turnright”

b=

), (“StepAside”,

CALL
“turnright”

BLOCK

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

BLOCK

CALL
“turnleft”

BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

) },

CALL
“move”

WHILE
TRUE

n = “TurnBack”;
p.Add_To_Context (n, b) ?
n=“”

BLOCK

)

An Operation on Program
What would it mean to
demobilize a program?

Statements in BL Program
PROGRAM identifier IS

INSTRUCTION identifier IS

BEGIN

END identifier

END identifier

.
.
.

State the Problem
global_procedure Demobilize (
alters Program& p
);
/*!
ensures
p.name = #p.name and
p.context =
CONTEXT_DEMOBILIZE (#p.context) and
p.body = DEMOBILIZE (#p.body)
!*/

State the Problem Continued…
math definition CONTEXT_DEMOBILIZE (
c: CONTEXT
): CONTEXT satisfies
if c = empty_set then
CONTEXT_DEMOBILIZE (c) = c
else
there exists n: IDENTIFIER, s: STATEMENT,
rest_of_context: CONTEXT
(c = {(n, s)} union rest_of_context and
CONTEXT_DEMOBILIZE(c) =
{(n, DEMOBILIZE(s))} union
CONTEXT_DEMOBILIZE (rest_of_context))

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;
// demobilize program body
p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

Implementation Continued…
// demobilize statements in program context
while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}
// restore value of p (to be preserved)
p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;
}

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;

while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}

p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

}

p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;


Slide 15

BL Program
PROGRAM identifier IS

sequence of new instructions
BEGIN

sequence of statements
END identifier

Math Model for Program


What pieces of information do we
need to keep track of for a BL
program?





name (IDENTIFIER)
new instructions (CONTEXT)
body (BLOCK STATEMENT)

The Math Model


math subtype PROGRAM is (
name: IDENTIFIER
context: CONTEXT
body: STATEMENT
)
exemplar p
constraint
root (p.body).kind = BLOCK

New Instruction
INSTRUCTION identifier IS

sequence of statements
END identifier

Math Model Continued…


What pieces of information do we
need to keep track of for a new BL
instruction?




name (IDENTIFIER)
body (BLOCK STATEMENT)

Math Model Continued…


math subtype CONTEXT is
finite set of (
name: IDENTIFIER
body: STATEMENT
)
exemplar c
constraint 1. c is a (partial) function
2. for each (name, body) pair in c,
...
name is not one of primitives
nor the empty string, and body
is a BLOCK

Program Component


Type




Program_Kernel is modeled by PROGRAM

Initial Value


IS_INITIAL_PROGRAM (self) 
self.name = empty_string and
self.context = empty_set and
IS_INITIAL_STATEMENT (self.body)

Program SteerClear
Draw a picture of this BL program’s abstract view:
BEGIN
PROGRAM SteerClear IS
WHILE true DO
INSTRUCTION StepAside IS
IF next-is-empty THEN
IF random THEN
skip
turnright
ELSE
ELSE
IF next-is-wall THEN
turnleft
TurnAround
END IF
ELSE
move
StepAside
END StepAside
END IF
INSTRUCTION TurnAround IS
END IF
turnright
END WHILE
turnright
END SteerClear
END TurnAround

Program SteerClear (cont’d)
p=(

,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
,

},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{(

p.name
,

), (

,

) },

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
BLOCK

CALL
“turnright”

), ( “StepAside”,

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK
CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
), ( “StepAside”,

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

BLOCK
WHILE
TRUE

CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

BLOCK

p.context

IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

p.body

IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Program Continued…


Operations










p.Swap_Name (name)
p.Swap_Body (statement)
p.Add_To_Context (name, statement)
p.Remove_From_Context (name, name_copy,
statement)
p.Remove_Any_From_Context (name,
statement)
p.Is_In_Context (name)
p.Size_Of_Context ()

Swap_Name
P=
What effect will
( “Timid”,
{ ( “TurnAround”,
the following
statements have on p,
the program object for
program SteerClear?

BLOCK

CALL
“turnright”

), (“StepAside”,

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

BLOCK

BLOCK

object Text name;
name = “Timid”;
p.Swap_Name (name);
name = “SteerClear”

CALL
“turnright”

WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

CALL
“turnleft”

) },

Swap_Body
What effect will
the following statements have
on object p from the previous
slide?

P=
( “Timid”,
{ ( “TurnAround”,

object Statement b, i, c;
object Text m = “move”;
object Integer test = RANDOM;
c.Compose_Call (m);
b.Add_To_Block (0, c);
i.Compose_If (test, b);
b.Add_to_Block (0, i);
p.Swap_Body (b);

CALL
“turnright”

BLOCK

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

CALL
“turnleft”

BLOCK

b=

WHILE
TRUE

IF
RANDOM

BLOCK

IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK

BLOCK

CALL
“skip”

CALL
“move”

) },

BLOCK

CALL
“turnright”

BLOCK

BLOCK

)

IF_ELSE
NEXT_IS_WALL

BLOCK

CALL
“TurnAround”

BLOCK

CALL
“StepAside”

Context Operations
What result is produced
by these statements if
p has its original value?

P=
( “SteerClear”,
{ ( “TurnAround”,

BLOCK

CALL
“turnright”

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

p.Size_Of_Context () ?

CALL
“turnright”

BLOCK

BLOCK

CALL
“move”

BLOCK

CALL
“turnleft”

WHILE
TRUE

p.Is_In_Context (“StepAside”) ?

BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

) },

Context Operations (cont’d)
What result is produced
by these statements if
p has the value shown on
the previous slide?

P=
( “SteerClear”,
{ (“StepAside”,

object Text n;
object Statement b;
p.Remove_From_Context
(“TurnAround”, n, b) ?
n=“TurnAround”

b=
CALL
“turnright”

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

BLOCK

CALL
“turnright”

) },

CALL
“move”

BLOCK

CALL
“turnleft”

BLOCK
WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Context Operations (cont’d)
What result is produced
by these statements if p
and b have the values
shown on the previous
slide?

P=
( “SteerClear”,
{ ( “TurnBack”,
CALL
“turnright”

b=

), (“StepAside”,

CALL
“turnright”

BLOCK

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

BLOCK

CALL
“turnleft”

BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

) },

CALL
“move”

WHILE
TRUE

n = “TurnBack”;
p.Add_To_Context (n, b) ?
n=“”

BLOCK

)

An Operation on Program
What would it mean to
demobilize a program?

Statements in BL Program
PROGRAM identifier IS

INSTRUCTION identifier IS

BEGIN

END identifier

END identifier

.
.
.

State the Problem
global_procedure Demobilize (
alters Program& p
);
/*!
ensures
p.name = #p.name and
p.context =
CONTEXT_DEMOBILIZE (#p.context) and
p.body = DEMOBILIZE (#p.body)
!*/

State the Problem Continued…
math definition CONTEXT_DEMOBILIZE (
c: CONTEXT
): CONTEXT satisfies
if c = empty_set then
CONTEXT_DEMOBILIZE (c) = c
else
there exists n: IDENTIFIER, s: STATEMENT,
rest_of_context: CONTEXT
(c = {(n, s)} union rest_of_context and
CONTEXT_DEMOBILIZE(c) =
{(n, DEMOBILIZE(s))} union
CONTEXT_DEMOBILIZE (rest_of_context))

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;
// demobilize program body
p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

Implementation Continued…
// demobilize statements in program context
while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}
// restore value of p (to be preserved)
p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;
}

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;

while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}

p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

}

p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;


Slide 16

BL Program
PROGRAM identifier IS

sequence of new instructions
BEGIN

sequence of statements
END identifier

Math Model for Program


What pieces of information do we
need to keep track of for a BL
program?





name (IDENTIFIER)
new instructions (CONTEXT)
body (BLOCK STATEMENT)

The Math Model


math subtype PROGRAM is (
name: IDENTIFIER
context: CONTEXT
body: STATEMENT
)
exemplar p
constraint
root (p.body).kind = BLOCK

New Instruction
INSTRUCTION identifier IS

sequence of statements
END identifier

Math Model Continued…


What pieces of information do we
need to keep track of for a new BL
instruction?




name (IDENTIFIER)
body (BLOCK STATEMENT)

Math Model Continued…


math subtype CONTEXT is
finite set of (
name: IDENTIFIER
body: STATEMENT
)
exemplar c
constraint 1. c is a (partial) function
2. for each (name, body) pair in c,
...
name is not one of primitives
nor the empty string, and body
is a BLOCK

Program Component


Type




Program_Kernel is modeled by PROGRAM

Initial Value


IS_INITIAL_PROGRAM (self) 
self.name = empty_string and
self.context = empty_set and
IS_INITIAL_STATEMENT (self.body)

Program SteerClear
Draw a picture of this BL program’s abstract view:
BEGIN
PROGRAM SteerClear IS
WHILE true DO
INSTRUCTION StepAside IS
IF next-is-empty THEN
IF random THEN
skip
turnright
ELSE
ELSE
IF next-is-wall THEN
turnleft
TurnAround
END IF
ELSE
move
StepAside
END StepAside
END IF
INSTRUCTION TurnAround IS
END IF
turnright
END WHILE
turnright
END SteerClear
END TurnAround

Program SteerClear (cont’d)
p=(

,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
,

},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{(

p.name
,

), (

,

) },

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
BLOCK

CALL
“turnright”

), ( “StepAside”,

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK
CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
), ( “StepAside”,

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

BLOCK
WHILE
TRUE

CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

BLOCK

p.context

IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

p.body

IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Program Continued…


Operations










p.Swap_Name (name)
p.Swap_Body (statement)
p.Add_To_Context (name, statement)
p.Remove_From_Context (name, name_copy,
statement)
p.Remove_Any_From_Context (name,
statement)
p.Is_In_Context (name)
p.Size_Of_Context ()

Swap_Name
P=
What effect will
( “Timid”,
{ ( “TurnAround”,
the following
statements have on p,
the program object for
program SteerClear?

BLOCK

CALL
“turnright”

), (“StepAside”,

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

BLOCK

BLOCK

object Text name;
name = “Timid”;
p.Swap_Name (name);
name = “SteerClear”

CALL
“turnright”

WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

CALL
“turnleft”

) },

Swap_Body
What effect will
the following statements have
on object p from the previous
slide?

P=
( “Timid”,
{ ( “TurnAround”,

object Statement b, i, c;
object Text m = “move”;
object Integer test = RANDOM;
c.Compose_Call (m);
b.Add_To_Block (0, c);
i.Compose_If (test, b);
b.Add_to_Block (0, i);
p.Swap_Body (b);

CALL
“turnright”

BLOCK

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

CALL
“turnleft”

BLOCK

b=

WHILE
TRUE

IF
RANDOM

BLOCK

IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK

BLOCK

CALL
“skip”

CALL
“move”

) },

BLOCK

CALL
“turnright”

BLOCK

BLOCK

)

IF_ELSE
NEXT_IS_WALL

BLOCK

CALL
“TurnAround”

BLOCK

CALL
“StepAside”

Context Operations
What result is produced
by these statements if
p has its original value?

P=
( “SteerClear”,
{ ( “TurnAround”,

BLOCK

CALL
“turnright”

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

p.Size_Of_Context () ?

CALL
“turnright”

BLOCK

BLOCK

CALL
“move”

BLOCK

CALL
“turnleft”

WHILE
TRUE

p.Is_In_Context (“StepAside”) ?

BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

) },

Context Operations (cont’d)
What result is produced
by these statements if
p has the value shown on
the previous slide?

P=
( “SteerClear”,
{ (“StepAside”,

object Text n;
object Statement b;
p.Remove_From_Context
(“TurnAround”, n, b) ?
n=“TurnAround”

b=
CALL
“turnright”

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

BLOCK

CALL
“turnright”

) },

CALL
“move”

BLOCK

CALL
“turnleft”

BLOCK
WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Context Operations (cont’d)
What result is produced
by these statements if p
and b have the values
shown on the previous
slide?

P=
( “SteerClear”,
{ ( “TurnBack”,
CALL
“turnright”

b=

), (“StepAside”,

CALL
“turnright”

BLOCK

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

BLOCK

CALL
“turnleft”

BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

) },

CALL
“move”

WHILE
TRUE

n = “TurnBack”;
p.Add_To_Context (n, b) ?
n=“”

BLOCK

)

An Operation on Program
What would it mean to
demobilize a program?

Statements in BL Program
PROGRAM identifier IS

INSTRUCTION identifier IS

BEGIN

END identifier

END identifier

.
.
.

State the Problem
global_procedure Demobilize (
alters Program& p
);
/*!
ensures
p.name = #p.name and
p.context =
CONTEXT_DEMOBILIZE (#p.context) and
p.body = DEMOBILIZE (#p.body)
!*/

State the Problem Continued…
math definition CONTEXT_DEMOBILIZE (
c: CONTEXT
): CONTEXT satisfies
if c = empty_set then
CONTEXT_DEMOBILIZE (c) = c
else
there exists n: IDENTIFIER, s: STATEMENT,
rest_of_context: CONTEXT
(c = {(n, s)} union rest_of_context and
CONTEXT_DEMOBILIZE(c) =
{(n, DEMOBILIZE(s))} union
CONTEXT_DEMOBILIZE (rest_of_context))

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;
// demobilize program body
p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

Implementation Continued…
// demobilize statements in program context
while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}
// restore value of p (to be preserved)
p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;
}

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;

while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}

p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

}

p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;


Slide 17

BL Program
PROGRAM identifier IS

sequence of new instructions
BEGIN

sequence of statements
END identifier

Math Model for Program


What pieces of information do we
need to keep track of for a BL
program?





name (IDENTIFIER)
new instructions (CONTEXT)
body (BLOCK STATEMENT)

The Math Model


math subtype PROGRAM is (
name: IDENTIFIER
context: CONTEXT
body: STATEMENT
)
exemplar p
constraint
root (p.body).kind = BLOCK

New Instruction
INSTRUCTION identifier IS

sequence of statements
END identifier

Math Model Continued…


What pieces of information do we
need to keep track of for a new BL
instruction?




name (IDENTIFIER)
body (BLOCK STATEMENT)

Math Model Continued…


math subtype CONTEXT is
finite set of (
name: IDENTIFIER
body: STATEMENT
)
exemplar c
constraint 1. c is a (partial) function
2. for each (name, body) pair in c,
...
name is not one of primitives
nor the empty string, and body
is a BLOCK

Program Component


Type




Program_Kernel is modeled by PROGRAM

Initial Value


IS_INITIAL_PROGRAM (self) 
self.name = empty_string and
self.context = empty_set and
IS_INITIAL_STATEMENT (self.body)

Program SteerClear
Draw a picture of this BL program’s abstract view:
BEGIN
PROGRAM SteerClear IS
WHILE true DO
INSTRUCTION StepAside IS
IF next-is-empty THEN
IF random THEN
skip
turnright
ELSE
ELSE
IF next-is-wall THEN
turnleft
TurnAround
END IF
ELSE
move
StepAside
END StepAside
END IF
INSTRUCTION TurnAround IS
END IF
turnright
END WHILE
turnright
END SteerClear
END TurnAround

Program SteerClear (cont’d)
p=(

,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
,

},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{(

p.name
,

), (

,

) },

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
BLOCK

CALL
“turnright”

), ( “StepAside”,

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK
CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
), ( “StepAside”,

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

BLOCK
WHILE
TRUE

CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

BLOCK

p.context

IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

p.body

IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Program Continued…


Operations










p.Swap_Name (name)
p.Swap_Body (statement)
p.Add_To_Context (name, statement)
p.Remove_From_Context (name, name_copy,
statement)
p.Remove_Any_From_Context (name,
statement)
p.Is_In_Context (name)
p.Size_Of_Context ()

Swap_Name
P=
What effect will
( “Timid”,
{ ( “TurnAround”,
the following
statements have on p,
the program object for
program SteerClear?

BLOCK

CALL
“turnright”

), (“StepAside”,

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

BLOCK

BLOCK

object Text name;
name = “Timid”;
p.Swap_Name (name);
name = “SteerClear”

CALL
“turnright”

WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

CALL
“turnleft”

) },

Swap_Body
What effect will
the following statements have
on object p from the previous
slide?

P=
( “Timid”,
{ ( “TurnAround”,

object Statement b, i, c;
object Text m = “move”;
object Integer test = RANDOM;
c.Compose_Call (m);
b.Add_To_Block (0, c);
i.Compose_If (test, b);
b.Add_to_Block (0, i);
p.Swap_Body (b);

CALL
“turnright”

BLOCK

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

CALL
“turnleft”

BLOCK

b=

WHILE
TRUE

IF
RANDOM

BLOCK

IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK

BLOCK

CALL
“skip”

CALL
“move”

) },

BLOCK

CALL
“turnright”

BLOCK

BLOCK

)

IF_ELSE
NEXT_IS_WALL

BLOCK

CALL
“TurnAround”

BLOCK

CALL
“StepAside”

Context Operations
What result is produced
by these statements if
p has its original value?

P=
( “SteerClear”,
{ ( “TurnAround”,

BLOCK

CALL
“turnright”

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

p.Size_Of_Context () ?

CALL
“turnright”

BLOCK

BLOCK

CALL
“move”

BLOCK

CALL
“turnleft”

WHILE
TRUE

p.Is_In_Context (“StepAside”) ?

BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

) },

Context Operations (cont’d)
What result is produced
by these statements if
p has the value shown on
the previous slide?

P=
( “SteerClear”,
{ (“StepAside”,

object Text n;
object Statement b;
p.Remove_From_Context
(“TurnAround”, n, b) ?
n=“TurnAround”

b=
CALL
“turnright”

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

BLOCK

CALL
“turnright”

) },

CALL
“move”

BLOCK

CALL
“turnleft”

BLOCK
WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Context Operations (cont’d)
What result is produced
by these statements if p
and b have the values
shown on the previous
slide?

P=
( “SteerClear”,
{ ( “TurnBack”,
CALL
“turnright”

b=

), (“StepAside”,

CALL
“turnright”

BLOCK

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

BLOCK

CALL
“turnleft”

BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

) },

CALL
“move”

WHILE
TRUE

n = “TurnBack”;
p.Add_To_Context (n, b) ?
n=“”

BLOCK

)

An Operation on Program
What would it mean to
demobilize a program?

Statements in BL Program
PROGRAM identifier IS

INSTRUCTION identifier IS

BEGIN

END identifier

END identifier

.
.
.

State the Problem
global_procedure Demobilize (
alters Program& p
);
/*!
ensures
p.name = #p.name and
p.context =
CONTEXT_DEMOBILIZE (#p.context) and
p.body = DEMOBILIZE (#p.body)
!*/

State the Problem Continued…
math definition CONTEXT_DEMOBILIZE (
c: CONTEXT
): CONTEXT satisfies
if c = empty_set then
CONTEXT_DEMOBILIZE (c) = c
else
there exists n: IDENTIFIER, s: STATEMENT,
rest_of_context: CONTEXT
(c = {(n, s)} union rest_of_context and
CONTEXT_DEMOBILIZE(c) =
{(n, DEMOBILIZE(s))} union
CONTEXT_DEMOBILIZE (rest_of_context))

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;
// demobilize program body
p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

Implementation Continued…
// demobilize statements in program context
while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}
// restore value of p (to be preserved)
p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;
}

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;

while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}

p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

}

p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;


Slide 18

BL Program
PROGRAM identifier IS

sequence of new instructions
BEGIN

sequence of statements
END identifier

Math Model for Program


What pieces of information do we
need to keep track of for a BL
program?





name (IDENTIFIER)
new instructions (CONTEXT)
body (BLOCK STATEMENT)

The Math Model


math subtype PROGRAM is (
name: IDENTIFIER
context: CONTEXT
body: STATEMENT
)
exemplar p
constraint
root (p.body).kind = BLOCK

New Instruction
INSTRUCTION identifier IS

sequence of statements
END identifier

Math Model Continued…


What pieces of information do we
need to keep track of for a new BL
instruction?




name (IDENTIFIER)
body (BLOCK STATEMENT)

Math Model Continued…


math subtype CONTEXT is
finite set of (
name: IDENTIFIER
body: STATEMENT
)
exemplar c
constraint 1. c is a (partial) function
2. for each (name, body) pair in c,
...
name is not one of primitives
nor the empty string, and body
is a BLOCK

Program Component


Type




Program_Kernel is modeled by PROGRAM

Initial Value


IS_INITIAL_PROGRAM (self) 
self.name = empty_string and
self.context = empty_set and
IS_INITIAL_STATEMENT (self.body)

Program SteerClear
Draw a picture of this BL program’s abstract view:
BEGIN
PROGRAM SteerClear IS
WHILE true DO
INSTRUCTION StepAside IS
IF next-is-empty THEN
IF random THEN
skip
turnright
ELSE
ELSE
IF next-is-wall THEN
turnleft
TurnAround
END IF
ELSE
move
StepAside
END StepAside
END IF
INSTRUCTION TurnAround IS
END IF
turnright
END WHILE
turnright
END SteerClear
END TurnAround

Program SteerClear (cont’d)
p=(

,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
,

},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{(

p.name
,

), (

,

) },

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
BLOCK

CALL
“turnright”

), ( “StepAside”,

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK
CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
), ( “StepAside”,

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

BLOCK
WHILE
TRUE

CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

BLOCK

p.context

IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

p.body

IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Program Continued…


Operations










p.Swap_Name (name)
p.Swap_Body (statement)
p.Add_To_Context (name, statement)
p.Remove_From_Context (name, name_copy,
statement)
p.Remove_Any_From_Context (name,
statement)
p.Is_In_Context (name)
p.Size_Of_Context ()

Swap_Name
P=
What effect will
( “Timid”,
{ ( “TurnAround”,
the following
statements have on p,
the program object for
program SteerClear?

BLOCK

CALL
“turnright”

), (“StepAside”,

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

BLOCK

BLOCK

object Text name;
name = “Timid”;
p.Swap_Name (name);
name = “SteerClear”

CALL
“turnright”

WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

CALL
“turnleft”

) },

Swap_Body
What effect will
the following statements have
on object p from the previous
slide?

P=
( “Timid”,
{ ( “TurnAround”,

object Statement b, i, c;
object Text m = “move”;
object Integer test = RANDOM;
c.Compose_Call (m);
b.Add_To_Block (0, c);
i.Compose_If (test, b);
b.Add_to_Block (0, i);
p.Swap_Body (b);

CALL
“turnright”

BLOCK

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

CALL
“turnleft”

BLOCK

b=

WHILE
TRUE

IF
RANDOM

BLOCK

IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK

BLOCK

CALL
“skip”

CALL
“move”

) },

BLOCK

CALL
“turnright”

BLOCK

BLOCK

)

IF_ELSE
NEXT_IS_WALL

BLOCK

CALL
“TurnAround”

BLOCK

CALL
“StepAside”

Context Operations
What result is produced
by these statements if
p has its original value?

P=
( “SteerClear”,
{ ( “TurnAround”,

BLOCK

CALL
“turnright”

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

p.Size_Of_Context () ?

CALL
“turnright”

BLOCK

BLOCK

CALL
“move”

BLOCK

CALL
“turnleft”

WHILE
TRUE

p.Is_In_Context (“StepAside”) ?

BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

) },

Context Operations (cont’d)
What result is produced
by these statements if
p has the value shown on
the previous slide?

P=
( “SteerClear”,
{ (“StepAside”,

object Text n;
object Statement b;
p.Remove_From_Context
(“TurnAround”, n, b) ?
n=“TurnAround”

b=
CALL
“turnright”

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

BLOCK

CALL
“turnright”

) },

CALL
“move”

BLOCK

CALL
“turnleft”

BLOCK
WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Context Operations (cont’d)
What result is produced
by these statements if p
and b have the values
shown on the previous
slide?

P=
( “SteerClear”,
{ ( “TurnBack”,
CALL
“turnright”

b=

), (“StepAside”,

CALL
“turnright”

BLOCK

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

BLOCK

CALL
“turnleft”

BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

) },

CALL
“move”

WHILE
TRUE

n = “TurnBack”;
p.Add_To_Context (n, b) ?
n=“”

BLOCK

)

An Operation on Program
What would it mean to
demobilize a program?

Statements in BL Program
PROGRAM identifier IS

INSTRUCTION identifier IS

BEGIN

END identifier

END identifier

.
.
.

State the Problem
global_procedure Demobilize (
alters Program& p
);
/*!
ensures
p.name = #p.name and
p.context =
CONTEXT_DEMOBILIZE (#p.context) and
p.body = DEMOBILIZE (#p.body)
!*/

State the Problem Continued…
math definition CONTEXT_DEMOBILIZE (
c: CONTEXT
): CONTEXT satisfies
if c = empty_set then
CONTEXT_DEMOBILIZE (c) = c
else
there exists n: IDENTIFIER, s: STATEMENT,
rest_of_context: CONTEXT
(c = {(n, s)} union rest_of_context and
CONTEXT_DEMOBILIZE(c) =
{(n, DEMOBILIZE(s))} union
CONTEXT_DEMOBILIZE (rest_of_context))

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;
// demobilize program body
p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

Implementation Continued…
// demobilize statements in program context
while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}
// restore value of p (to be preserved)
p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;
}

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;

while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}

p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

}

p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;


Slide 19

BL Program
PROGRAM identifier IS

sequence of new instructions
BEGIN

sequence of statements
END identifier

Math Model for Program


What pieces of information do we
need to keep track of for a BL
program?





name (IDENTIFIER)
new instructions (CONTEXT)
body (BLOCK STATEMENT)

The Math Model


math subtype PROGRAM is (
name: IDENTIFIER
context: CONTEXT
body: STATEMENT
)
exemplar p
constraint
root (p.body).kind = BLOCK

New Instruction
INSTRUCTION identifier IS

sequence of statements
END identifier

Math Model Continued…


What pieces of information do we
need to keep track of for a new BL
instruction?




name (IDENTIFIER)
body (BLOCK STATEMENT)

Math Model Continued…


math subtype CONTEXT is
finite set of (
name: IDENTIFIER
body: STATEMENT
)
exemplar c
constraint 1. c is a (partial) function
2. for each (name, body) pair in c,
...
name is not one of primitives
nor the empty string, and body
is a BLOCK

Program Component


Type




Program_Kernel is modeled by PROGRAM

Initial Value


IS_INITIAL_PROGRAM (self) 
self.name = empty_string and
self.context = empty_set and
IS_INITIAL_STATEMENT (self.body)

Program SteerClear
Draw a picture of this BL program’s abstract view:
BEGIN
PROGRAM SteerClear IS
WHILE true DO
INSTRUCTION StepAside IS
IF next-is-empty THEN
IF random THEN
skip
turnright
ELSE
ELSE
IF next-is-wall THEN
turnleft
TurnAround
END IF
ELSE
move
StepAside
END StepAside
END IF
INSTRUCTION TurnAround IS
END IF
turnright
END WHILE
turnright
END SteerClear
END TurnAround

Program SteerClear (cont’d)
p=(

,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
,

},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{(

p.name
,

), (

,

) },

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
BLOCK

CALL
“turnright”

), ( “StepAside”,

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK
CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
), ( “StepAside”,

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

BLOCK
WHILE
TRUE

CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

BLOCK

p.context

IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

p.body

IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Program Continued…


Operations










p.Swap_Name (name)
p.Swap_Body (statement)
p.Add_To_Context (name, statement)
p.Remove_From_Context (name, name_copy,
statement)
p.Remove_Any_From_Context (name,
statement)
p.Is_In_Context (name)
p.Size_Of_Context ()

Swap_Name
P=
What effect will
( “Timid”,
{ ( “TurnAround”,
the following
statements have on p,
the program object for
program SteerClear?

BLOCK

CALL
“turnright”

), (“StepAside”,

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

BLOCK

BLOCK

object Text name;
name = “Timid”;
p.Swap_Name (name);
name = “SteerClear”

CALL
“turnright”

WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

CALL
“turnleft”

) },

Swap_Body
What effect will
the following statements have
on object p from the previous
slide?

P=
( “Timid”,
{ ( “TurnAround”,

object Statement b, i, c;
object Text m = “move”;
object Integer test = RANDOM;
c.Compose_Call (m);
b.Add_To_Block (0, c);
i.Compose_If (test, b);
b.Add_to_Block (0, i);
p.Swap_Body (b);

CALL
“turnright”

BLOCK

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

CALL
“turnleft”

BLOCK

b=

WHILE
TRUE

IF
RANDOM

BLOCK

IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK

BLOCK

CALL
“skip”

CALL
“move”

) },

BLOCK

CALL
“turnright”

BLOCK

BLOCK

)

IF_ELSE
NEXT_IS_WALL

BLOCK

CALL
“TurnAround”

BLOCK

CALL
“StepAside”

Context Operations
What result is produced
by these statements if
p has its original value?

P=
( “SteerClear”,
{ ( “TurnAround”,

BLOCK

CALL
“turnright”

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

p.Size_Of_Context () ?

CALL
“turnright”

BLOCK

BLOCK

CALL
“move”

BLOCK

CALL
“turnleft”

WHILE
TRUE

p.Is_In_Context (“StepAside”) ?

BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

) },

Context Operations (cont’d)
What result is produced
by these statements if
p has the value shown on
the previous slide?

P=
( “SteerClear”,
{ (“StepAside”,

object Text n;
object Statement b;
p.Remove_From_Context
(“TurnAround”, n, b) ?
n=“TurnAround”

b=
CALL
“turnright”

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

BLOCK

CALL
“turnright”

) },

CALL
“move”

BLOCK

CALL
“turnleft”

BLOCK
WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Context Operations (cont’d)
What result is produced
by these statements if p
and b have the values
shown on the previous
slide?

P=
( “SteerClear”,
{ ( “TurnBack”,
CALL
“turnright”

b=

), (“StepAside”,

CALL
“turnright”

BLOCK

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

BLOCK

CALL
“turnleft”

BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

) },

CALL
“move”

WHILE
TRUE

n = “TurnBack”;
p.Add_To_Context (n, b) ?
n=“”

BLOCK

)

An Operation on Program
What would it mean to
demobilize a program?

Statements in BL Program
PROGRAM identifier IS

INSTRUCTION identifier IS

BEGIN

END identifier

END identifier

.
.
.

State the Problem
global_procedure Demobilize (
alters Program& p
);
/*!
ensures
p.name = #p.name and
p.context =
CONTEXT_DEMOBILIZE (#p.context) and
p.body = DEMOBILIZE (#p.body)
!*/

State the Problem Continued…
math definition CONTEXT_DEMOBILIZE (
c: CONTEXT
): CONTEXT satisfies
if c = empty_set then
CONTEXT_DEMOBILIZE (c) = c
else
there exists n: IDENTIFIER, s: STATEMENT,
rest_of_context: CONTEXT
(c = {(n, s)} union rest_of_context and
CONTEXT_DEMOBILIZE(c) =
{(n, DEMOBILIZE(s))} union
CONTEXT_DEMOBILIZE (rest_of_context))

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;
// demobilize program body
p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

Implementation Continued…
// demobilize statements in program context
while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}
// restore value of p (to be preserved)
p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;
}

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;

while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}

p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

}

p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;


Slide 20

BL Program
PROGRAM identifier IS

sequence of new instructions
BEGIN

sequence of statements
END identifier

Math Model for Program


What pieces of information do we
need to keep track of for a BL
program?





name (IDENTIFIER)
new instructions (CONTEXT)
body (BLOCK STATEMENT)

The Math Model


math subtype PROGRAM is (
name: IDENTIFIER
context: CONTEXT
body: STATEMENT
)
exemplar p
constraint
root (p.body).kind = BLOCK

New Instruction
INSTRUCTION identifier IS

sequence of statements
END identifier

Math Model Continued…


What pieces of information do we
need to keep track of for a new BL
instruction?




name (IDENTIFIER)
body (BLOCK STATEMENT)

Math Model Continued…


math subtype CONTEXT is
finite set of (
name: IDENTIFIER
body: STATEMENT
)
exemplar c
constraint 1. c is a (partial) function
2. for each (name, body) pair in c,
...
name is not one of primitives
nor the empty string, and body
is a BLOCK

Program Component


Type




Program_Kernel is modeled by PROGRAM

Initial Value


IS_INITIAL_PROGRAM (self) 
self.name = empty_string and
self.context = empty_set and
IS_INITIAL_STATEMENT (self.body)

Program SteerClear
Draw a picture of this BL program’s abstract view:
BEGIN
PROGRAM SteerClear IS
WHILE true DO
INSTRUCTION StepAside IS
IF next-is-empty THEN
IF random THEN
skip
turnright
ELSE
ELSE
IF next-is-wall THEN
turnleft
TurnAround
END IF
ELSE
move
StepAside
END StepAside
END IF
INSTRUCTION TurnAround IS
END IF
turnright
END WHILE
turnright
END SteerClear
END TurnAround

Program SteerClear (cont’d)
p=(

,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
,

},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{(

p.name
,

), (

,

) },

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
BLOCK

CALL
“turnright”

), ( “StepAside”,

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK
CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
), ( “StepAside”,

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

BLOCK
WHILE
TRUE

CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

BLOCK

p.context

IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

p.body

IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Program Continued…


Operations










p.Swap_Name (name)
p.Swap_Body (statement)
p.Add_To_Context (name, statement)
p.Remove_From_Context (name, name_copy,
statement)
p.Remove_Any_From_Context (name,
statement)
p.Is_In_Context (name)
p.Size_Of_Context ()

Swap_Name
P=
What effect will
( “Timid”,
{ ( “TurnAround”,
the following
statements have on p,
the program object for
program SteerClear?

BLOCK

CALL
“turnright”

), (“StepAside”,

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

BLOCK

BLOCK

object Text name;
name = “Timid”;
p.Swap_Name (name);
name = “SteerClear”

CALL
“turnright”

WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

CALL
“turnleft”

) },

Swap_Body
What effect will
the following statements have
on object p from the previous
slide?

P=
( “Timid”,
{ ( “TurnAround”,

object Statement b, i, c;
object Text m = “move”;
object Integer test = RANDOM;
c.Compose_Call (m);
b.Add_To_Block (0, c);
i.Compose_If (test, b);
b.Add_to_Block (0, i);
p.Swap_Body (b);

CALL
“turnright”

BLOCK

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

CALL
“turnleft”

BLOCK

b=

WHILE
TRUE

IF
RANDOM

BLOCK

IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK

BLOCK

CALL
“skip”

CALL
“move”

) },

BLOCK

CALL
“turnright”

BLOCK

BLOCK

)

IF_ELSE
NEXT_IS_WALL

BLOCK

CALL
“TurnAround”

BLOCK

CALL
“StepAside”

Context Operations
What result is produced
by these statements if
p has its original value?

P=
( “SteerClear”,
{ ( “TurnAround”,

BLOCK

CALL
“turnright”

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

p.Size_Of_Context () ?

CALL
“turnright”

BLOCK

BLOCK

CALL
“move”

BLOCK

CALL
“turnleft”

WHILE
TRUE

p.Is_In_Context (“StepAside”) ?

BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

) },

Context Operations (cont’d)
What result is produced
by these statements if
p has the value shown on
the previous slide?

P=
( “SteerClear”,
{ (“StepAside”,

object Text n;
object Statement b;
p.Remove_From_Context
(“TurnAround”, n, b) ?
n=“TurnAround”

b=
CALL
“turnright”

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

BLOCK

CALL
“turnright”

) },

CALL
“move”

BLOCK

CALL
“turnleft”

BLOCK
WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Context Operations (cont’d)
What result is produced
by these statements if p
and b have the values
shown on the previous
slide?

P=
( “SteerClear”,
{ ( “TurnBack”,
CALL
“turnright”

b=

), (“StepAside”,

CALL
“turnright”

BLOCK

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

BLOCK

CALL
“turnleft”

BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

) },

CALL
“move”

WHILE
TRUE

n = “TurnBack”;
p.Add_To_Context (n, b) ?
n=“”

BLOCK

)

An Operation on Program
What would it mean to
demobilize a program?

Statements in BL Program
PROGRAM identifier IS

INSTRUCTION identifier IS

BEGIN

END identifier

END identifier

.
.
.

State the Problem
global_procedure Demobilize (
alters Program& p
);
/*!
ensures
p.name = #p.name and
p.context =
CONTEXT_DEMOBILIZE (#p.context) and
p.body = DEMOBILIZE (#p.body)
!*/

State the Problem Continued…
math definition CONTEXT_DEMOBILIZE (
c: CONTEXT
): CONTEXT satisfies
if c = empty_set then
CONTEXT_DEMOBILIZE (c) = c
else
there exists n: IDENTIFIER, s: STATEMENT,
rest_of_context: CONTEXT
(c = {(n, s)} union rest_of_context and
CONTEXT_DEMOBILIZE(c) =
{(n, DEMOBILIZE(s))} union
CONTEXT_DEMOBILIZE (rest_of_context))

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;
// demobilize program body
p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

Implementation Continued…
// demobilize statements in program context
while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}
// restore value of p (to be preserved)
p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;
}

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;

while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}

p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

}

p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;


Slide 21

BL Program
PROGRAM identifier IS

sequence of new instructions
BEGIN

sequence of statements
END identifier

Math Model for Program


What pieces of information do we
need to keep track of for a BL
program?





name (IDENTIFIER)
new instructions (CONTEXT)
body (BLOCK STATEMENT)

The Math Model


math subtype PROGRAM is (
name: IDENTIFIER
context: CONTEXT
body: STATEMENT
)
exemplar p
constraint
root (p.body).kind = BLOCK

New Instruction
INSTRUCTION identifier IS

sequence of statements
END identifier

Math Model Continued…


What pieces of information do we
need to keep track of for a new BL
instruction?




name (IDENTIFIER)
body (BLOCK STATEMENT)

Math Model Continued…


math subtype CONTEXT is
finite set of (
name: IDENTIFIER
body: STATEMENT
)
exemplar c
constraint 1. c is a (partial) function
2. for each (name, body) pair in c,
...
name is not one of primitives
nor the empty string, and body
is a BLOCK

Program Component


Type




Program_Kernel is modeled by PROGRAM

Initial Value


IS_INITIAL_PROGRAM (self) 
self.name = empty_string and
self.context = empty_set and
IS_INITIAL_STATEMENT (self.body)

Program SteerClear
Draw a picture of this BL program’s abstract view:
BEGIN
PROGRAM SteerClear IS
WHILE true DO
INSTRUCTION StepAside IS
IF next-is-empty THEN
IF random THEN
skip
turnright
ELSE
ELSE
IF next-is-wall THEN
turnleft
TurnAround
END IF
ELSE
move
StepAside
END StepAside
END IF
INSTRUCTION TurnAround IS
END IF
turnright
END WHILE
turnright
END SteerClear
END TurnAround

Program SteerClear (cont’d)
p=(

,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
,

},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{(

p.name
,

), (

,

) },

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
BLOCK

CALL
“turnright”

), ( “StepAside”,

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK
CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
), ( “StepAside”,

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

BLOCK
WHILE
TRUE

CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

BLOCK

p.context

IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

p.body

IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Program Continued…


Operations










p.Swap_Name (name)
p.Swap_Body (statement)
p.Add_To_Context (name, statement)
p.Remove_From_Context (name, name_copy,
statement)
p.Remove_Any_From_Context (name,
statement)
p.Is_In_Context (name)
p.Size_Of_Context ()

Swap_Name
P=
What effect will
( “Timid”,
{ ( “TurnAround”,
the following
statements have on p,
the program object for
program SteerClear?

BLOCK

CALL
“turnright”

), (“StepAside”,

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

BLOCK

BLOCK

object Text name;
name = “Timid”;
p.Swap_Name (name);
name = “SteerClear”

CALL
“turnright”

WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

CALL
“turnleft”

) },

Swap_Body
What effect will
the following statements have
on object p from the previous
slide?

P=
( “Timid”,
{ ( “TurnAround”,

object Statement b, i, c;
object Text m = “move”;
object Integer test = RANDOM;
c.Compose_Call (m);
b.Add_To_Block (0, c);
i.Compose_If (test, b);
b.Add_to_Block (0, i);
p.Swap_Body (b);

CALL
“turnright”

BLOCK

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

CALL
“turnleft”

BLOCK

b=

WHILE
TRUE

IF
RANDOM

BLOCK

IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK

BLOCK

CALL
“skip”

CALL
“move”

) },

BLOCK

CALL
“turnright”

BLOCK

BLOCK

)

IF_ELSE
NEXT_IS_WALL

BLOCK

CALL
“TurnAround”

BLOCK

CALL
“StepAside”

Context Operations
What result is produced
by these statements if
p has its original value?

P=
( “SteerClear”,
{ ( “TurnAround”,

BLOCK

CALL
“turnright”

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

p.Size_Of_Context () ?

CALL
“turnright”

BLOCK

BLOCK

CALL
“move”

BLOCK

CALL
“turnleft”

WHILE
TRUE

p.Is_In_Context (“StepAside”) ?

BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

) },

Context Operations (cont’d)
What result is produced
by these statements if
p has the value shown on
the previous slide?

P=
( “SteerClear”,
{ (“StepAside”,

object Text n;
object Statement b;
p.Remove_From_Context
(“TurnAround”, n, b) ?
n=“TurnAround”

b=
CALL
“turnright”

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

BLOCK

CALL
“turnright”

) },

CALL
“move”

BLOCK

CALL
“turnleft”

BLOCK
WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Context Operations (cont’d)
What result is produced
by these statements if p
and b have the values
shown on the previous
slide?

P=
( “SteerClear”,
{ ( “TurnBack”,
CALL
“turnright”

b=

), (“StepAside”,

CALL
“turnright”

BLOCK

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

BLOCK

CALL
“turnleft”

BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

) },

CALL
“move”

WHILE
TRUE

n = “TurnBack”;
p.Add_To_Context (n, b) ?
n=“”

BLOCK

)

An Operation on Program
What would it mean to
demobilize a program?

Statements in BL Program
PROGRAM identifier IS

INSTRUCTION identifier IS

BEGIN

END identifier

END identifier

.
.
.

State the Problem
global_procedure Demobilize (
alters Program& p
);
/*!
ensures
p.name = #p.name and
p.context =
CONTEXT_DEMOBILIZE (#p.context) and
p.body = DEMOBILIZE (#p.body)
!*/

State the Problem Continued…
math definition CONTEXT_DEMOBILIZE (
c: CONTEXT
): CONTEXT satisfies
if c = empty_set then
CONTEXT_DEMOBILIZE (c) = c
else
there exists n: IDENTIFIER, s: STATEMENT,
rest_of_context: CONTEXT
(c = {(n, s)} union rest_of_context and
CONTEXT_DEMOBILIZE(c) =
{(n, DEMOBILIZE(s))} union
CONTEXT_DEMOBILIZE (rest_of_context))

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;
// demobilize program body
p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

Implementation Continued…
// demobilize statements in program context
while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}
// restore value of p (to be preserved)
p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;
}

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;

while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}

p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

}

p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;


Slide 22

BL Program
PROGRAM identifier IS

sequence of new instructions
BEGIN

sequence of statements
END identifier

Math Model for Program


What pieces of information do we
need to keep track of for a BL
program?





name (IDENTIFIER)
new instructions (CONTEXT)
body (BLOCK STATEMENT)

The Math Model


math subtype PROGRAM is (
name: IDENTIFIER
context: CONTEXT
body: STATEMENT
)
exemplar p
constraint
root (p.body).kind = BLOCK

New Instruction
INSTRUCTION identifier IS

sequence of statements
END identifier

Math Model Continued…


What pieces of information do we
need to keep track of for a new BL
instruction?




name (IDENTIFIER)
body (BLOCK STATEMENT)

Math Model Continued…


math subtype CONTEXT is
finite set of (
name: IDENTIFIER
body: STATEMENT
)
exemplar c
constraint 1. c is a (partial) function
2. for each (name, body) pair in c,
...
name is not one of primitives
nor the empty string, and body
is a BLOCK

Program Component


Type




Program_Kernel is modeled by PROGRAM

Initial Value


IS_INITIAL_PROGRAM (self) 
self.name = empty_string and
self.context = empty_set and
IS_INITIAL_STATEMENT (self.body)

Program SteerClear
Draw a picture of this BL program’s abstract view:
BEGIN
PROGRAM SteerClear IS
WHILE true DO
INSTRUCTION StepAside IS
IF next-is-empty THEN
IF random THEN
skip
turnright
ELSE
ELSE
IF next-is-wall THEN
turnleft
TurnAround
END IF
ELSE
move
StepAside
END StepAside
END IF
INSTRUCTION TurnAround IS
END IF
turnright
END WHILE
turnright
END SteerClear
END TurnAround

Program SteerClear (cont’d)
p=(

,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
,

},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{(

p.name
,

), (

,

) },

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
BLOCK

CALL
“turnright”

), ( “StepAside”,

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK
CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
), ( “StepAside”,

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

BLOCK
WHILE
TRUE

CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

BLOCK

p.context

IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

p.body

IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Program Continued…


Operations










p.Swap_Name (name)
p.Swap_Body (statement)
p.Add_To_Context (name, statement)
p.Remove_From_Context (name, name_copy,
statement)
p.Remove_Any_From_Context (name,
statement)
p.Is_In_Context (name)
p.Size_Of_Context ()

Swap_Name
P=
What effect will
( “Timid”,
{ ( “TurnAround”,
the following
statements have on p,
the program object for
program SteerClear?

BLOCK

CALL
“turnright”

), (“StepAside”,

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

BLOCK

BLOCK

object Text name;
name = “Timid”;
p.Swap_Name (name);
name = “SteerClear”

CALL
“turnright”

WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

CALL
“turnleft”

) },

Swap_Body
What effect will
the following statements have
on object p from the previous
slide?

P=
( “Timid”,
{ ( “TurnAround”,

object Statement b, i, c;
object Text m = “move”;
object Integer test = RANDOM;
c.Compose_Call (m);
b.Add_To_Block (0, c);
i.Compose_If (test, b);
b.Add_to_Block (0, i);
p.Swap_Body (b);

CALL
“turnright”

BLOCK

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

CALL
“turnleft”

BLOCK

b=

WHILE
TRUE

IF
RANDOM

BLOCK

IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK

BLOCK

CALL
“skip”

CALL
“move”

) },

BLOCK

CALL
“turnright”

BLOCK

BLOCK

)

IF_ELSE
NEXT_IS_WALL

BLOCK

CALL
“TurnAround”

BLOCK

CALL
“StepAside”

Context Operations
What result is produced
by these statements if
p has its original value?

P=
( “SteerClear”,
{ ( “TurnAround”,

BLOCK

CALL
“turnright”

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

p.Size_Of_Context () ?

CALL
“turnright”

BLOCK

BLOCK

CALL
“move”

BLOCK

CALL
“turnleft”

WHILE
TRUE

p.Is_In_Context (“StepAside”) ?

BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

) },

Context Operations (cont’d)
What result is produced
by these statements if
p has the value shown on
the previous slide?

P=
( “SteerClear”,
{ (“StepAside”,

object Text n;
object Statement b;
p.Remove_From_Context
(“TurnAround”, n, b) ?
n=“TurnAround”

b=
CALL
“turnright”

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

BLOCK

CALL
“turnright”

) },

CALL
“move”

BLOCK

CALL
“turnleft”

BLOCK
WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Context Operations (cont’d)
What result is produced
by these statements if p
and b have the values
shown on the previous
slide?

P=
( “SteerClear”,
{ ( “TurnBack”,
CALL
“turnright”

b=

), (“StepAside”,

CALL
“turnright”

BLOCK

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

BLOCK

CALL
“turnleft”

BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

) },

CALL
“move”

WHILE
TRUE

n = “TurnBack”;
p.Add_To_Context (n, b) ?
n=“”

BLOCK

)

An Operation on Program
What would it mean to
demobilize a program?

Statements in BL Program
PROGRAM identifier IS

INSTRUCTION identifier IS

BEGIN

END identifier

END identifier

.
.
.

State the Problem
global_procedure Demobilize (
alters Program& p
);
/*!
ensures
p.name = #p.name and
p.context =
CONTEXT_DEMOBILIZE (#p.context) and
p.body = DEMOBILIZE (#p.body)
!*/

State the Problem Continued…
math definition CONTEXT_DEMOBILIZE (
c: CONTEXT
): CONTEXT satisfies
if c = empty_set then
CONTEXT_DEMOBILIZE (c) = c
else
there exists n: IDENTIFIER, s: STATEMENT,
rest_of_context: CONTEXT
(c = {(n, s)} union rest_of_context and
CONTEXT_DEMOBILIZE(c) =
{(n, DEMOBILIZE(s))} union
CONTEXT_DEMOBILIZE (rest_of_context))

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;
// demobilize program body
p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

Implementation Continued…
// demobilize statements in program context
while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}
// restore value of p (to be preserved)
p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;
}

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;

while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}

p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

}

p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;


Slide 23

BL Program
PROGRAM identifier IS

sequence of new instructions
BEGIN

sequence of statements
END identifier

Math Model for Program


What pieces of information do we
need to keep track of for a BL
program?





name (IDENTIFIER)
new instructions (CONTEXT)
body (BLOCK STATEMENT)

The Math Model


math subtype PROGRAM is (
name: IDENTIFIER
context: CONTEXT
body: STATEMENT
)
exemplar p
constraint
root (p.body).kind = BLOCK

New Instruction
INSTRUCTION identifier IS

sequence of statements
END identifier

Math Model Continued…


What pieces of information do we
need to keep track of for a new BL
instruction?




name (IDENTIFIER)
body (BLOCK STATEMENT)

Math Model Continued…


math subtype CONTEXT is
finite set of (
name: IDENTIFIER
body: STATEMENT
)
exemplar c
constraint 1. c is a (partial) function
2. for each (name, body) pair in c,
...
name is not one of primitives
nor the empty string, and body
is a BLOCK

Program Component


Type




Program_Kernel is modeled by PROGRAM

Initial Value


IS_INITIAL_PROGRAM (self) 
self.name = empty_string and
self.context = empty_set and
IS_INITIAL_STATEMENT (self.body)

Program SteerClear
Draw a picture of this BL program’s abstract view:
BEGIN
PROGRAM SteerClear IS
WHILE true DO
INSTRUCTION StepAside IS
IF next-is-empty THEN
IF random THEN
skip
turnright
ELSE
ELSE
IF next-is-wall THEN
turnleft
TurnAround
END IF
ELSE
move
StepAside
END StepAside
END IF
INSTRUCTION TurnAround IS
END IF
turnright
END WHILE
turnright
END SteerClear
END TurnAround

Program SteerClear (cont’d)
p=(

,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
,

},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{(

p.name
,

), (

,

) },

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
BLOCK

CALL
“turnright”

), ( “StepAside”,

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK
CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
), ( “StepAside”,

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

BLOCK
WHILE
TRUE

CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

BLOCK

p.context

IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

p.body

IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Program Continued…


Operations










p.Swap_Name (name)
p.Swap_Body (statement)
p.Add_To_Context (name, statement)
p.Remove_From_Context (name, name_copy,
statement)
p.Remove_Any_From_Context (name,
statement)
p.Is_In_Context (name)
p.Size_Of_Context ()

Swap_Name
P=
What effect will
( “Timid”,
{ ( “TurnAround”,
the following
statements have on p,
the program object for
program SteerClear?

BLOCK

CALL
“turnright”

), (“StepAside”,

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

BLOCK

BLOCK

object Text name;
name = “Timid”;
p.Swap_Name (name);
name = “SteerClear”

CALL
“turnright”

WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

CALL
“turnleft”

) },

Swap_Body
What effect will
the following statements have
on object p from the previous
slide?

P=
( “Timid”,
{ ( “TurnAround”,

object Statement b, i, c;
object Text m = “move”;
object Integer test = RANDOM;
c.Compose_Call (m);
b.Add_To_Block (0, c);
i.Compose_If (test, b);
b.Add_to_Block (0, i);
p.Swap_Body (b);

CALL
“turnright”

BLOCK

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

CALL
“turnleft”

BLOCK

b=

WHILE
TRUE

IF
RANDOM

BLOCK

IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK

BLOCK

CALL
“skip”

CALL
“move”

) },

BLOCK

CALL
“turnright”

BLOCK

BLOCK

)

IF_ELSE
NEXT_IS_WALL

BLOCK

CALL
“TurnAround”

BLOCK

CALL
“StepAside”

Context Operations
What result is produced
by these statements if
p has its original value?

P=
( “SteerClear”,
{ ( “TurnAround”,

BLOCK

CALL
“turnright”

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

p.Size_Of_Context () ?

CALL
“turnright”

BLOCK

BLOCK

CALL
“move”

BLOCK

CALL
“turnleft”

WHILE
TRUE

p.Is_In_Context (“StepAside”) ?

BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

) },

Context Operations (cont’d)
What result is produced
by these statements if
p has the value shown on
the previous slide?

P=
( “SteerClear”,
{ (“StepAside”,

object Text n;
object Statement b;
p.Remove_From_Context
(“TurnAround”, n, b) ?
n=“TurnAround”

b=
CALL
“turnright”

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

BLOCK

CALL
“turnright”

) },

CALL
“move”

BLOCK

CALL
“turnleft”

BLOCK
WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Context Operations (cont’d)
What result is produced
by these statements if p
and b have the values
shown on the previous
slide?

P=
( “SteerClear”,
{ ( “TurnBack”,
CALL
“turnright”

b=

), (“StepAside”,

CALL
“turnright”

BLOCK

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

BLOCK

CALL
“turnleft”

BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

) },

CALL
“move”

WHILE
TRUE

n = “TurnBack”;
p.Add_To_Context (n, b) ?
n=“”

BLOCK

)

An Operation on Program
What would it mean to
demobilize a program?

Statements in BL Program
PROGRAM identifier IS

INSTRUCTION identifier IS

BEGIN

END identifier

END identifier

.
.
.

State the Problem
global_procedure Demobilize (
alters Program& p
);
/*!
ensures
p.name = #p.name and
p.context =
CONTEXT_DEMOBILIZE (#p.context) and
p.body = DEMOBILIZE (#p.body)
!*/

State the Problem Continued…
math definition CONTEXT_DEMOBILIZE (
c: CONTEXT
): CONTEXT satisfies
if c = empty_set then
CONTEXT_DEMOBILIZE (c) = c
else
there exists n: IDENTIFIER, s: STATEMENT,
rest_of_context: CONTEXT
(c = {(n, s)} union rest_of_context and
CONTEXT_DEMOBILIZE(c) =
{(n, DEMOBILIZE(s))} union
CONTEXT_DEMOBILIZE (rest_of_context))

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;
// demobilize program body
p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

Implementation Continued…
// demobilize statements in program context
while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}
// restore value of p (to be preserved)
p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;
}

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;

while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}

p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

}

p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;


Slide 24

BL Program
PROGRAM identifier IS

sequence of new instructions
BEGIN

sequence of statements
END identifier

Math Model for Program


What pieces of information do we
need to keep track of for a BL
program?





name (IDENTIFIER)
new instructions (CONTEXT)
body (BLOCK STATEMENT)

The Math Model


math subtype PROGRAM is (
name: IDENTIFIER
context: CONTEXT
body: STATEMENT
)
exemplar p
constraint
root (p.body).kind = BLOCK

New Instruction
INSTRUCTION identifier IS

sequence of statements
END identifier

Math Model Continued…


What pieces of information do we
need to keep track of for a new BL
instruction?




name (IDENTIFIER)
body (BLOCK STATEMENT)

Math Model Continued…


math subtype CONTEXT is
finite set of (
name: IDENTIFIER
body: STATEMENT
)
exemplar c
constraint 1. c is a (partial) function
2. for each (name, body) pair in c,
...
name is not one of primitives
nor the empty string, and body
is a BLOCK

Program Component


Type




Program_Kernel is modeled by PROGRAM

Initial Value


IS_INITIAL_PROGRAM (self) 
self.name = empty_string and
self.context = empty_set and
IS_INITIAL_STATEMENT (self.body)

Program SteerClear
Draw a picture of this BL program’s abstract view:
BEGIN
PROGRAM SteerClear IS
WHILE true DO
INSTRUCTION StepAside IS
IF next-is-empty THEN
IF random THEN
skip
turnright
ELSE
ELSE
IF next-is-wall THEN
turnleft
TurnAround
END IF
ELSE
move
StepAside
END StepAside
END IF
INSTRUCTION TurnAround IS
END IF
turnright
END WHILE
turnright
END SteerClear
END TurnAround

Program SteerClear (cont’d)
p=(

,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
,

},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{(

p.name
,

), (

,

) },

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
BLOCK

CALL
“turnright”

), ( “StepAside”,

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK
CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
), ( “StepAside”,

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

BLOCK
WHILE
TRUE

CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

BLOCK

p.context

IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

p.body

IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Program Continued…


Operations










p.Swap_Name (name)
p.Swap_Body (statement)
p.Add_To_Context (name, statement)
p.Remove_From_Context (name, name_copy,
statement)
p.Remove_Any_From_Context (name,
statement)
p.Is_In_Context (name)
p.Size_Of_Context ()

Swap_Name
P=
What effect will
( “Timid”,
{ ( “TurnAround”,
the following
statements have on p,
the program object for
program SteerClear?

BLOCK

CALL
“turnright”

), (“StepAside”,

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

BLOCK

BLOCK

object Text name;
name = “Timid”;
p.Swap_Name (name);
name = “SteerClear”

CALL
“turnright”

WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

CALL
“turnleft”

) },

Swap_Body
What effect will
the following statements have
on object p from the previous
slide?

P=
( “Timid”,
{ ( “TurnAround”,

object Statement b, i, c;
object Text m = “move”;
object Integer test = RANDOM;
c.Compose_Call (m);
b.Add_To_Block (0, c);
i.Compose_If (test, b);
b.Add_to_Block (0, i);
p.Swap_Body (b);

CALL
“turnright”

BLOCK

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

CALL
“turnleft”

BLOCK

b=

WHILE
TRUE

IF
RANDOM

BLOCK

IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK

BLOCK

CALL
“skip”

CALL
“move”

) },

BLOCK

CALL
“turnright”

BLOCK

BLOCK

)

IF_ELSE
NEXT_IS_WALL

BLOCK

CALL
“TurnAround”

BLOCK

CALL
“StepAside”

Context Operations
What result is produced
by these statements if
p has its original value?

P=
( “SteerClear”,
{ ( “TurnAround”,

BLOCK

CALL
“turnright”

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

p.Size_Of_Context () ?

CALL
“turnright”

BLOCK

BLOCK

CALL
“move”

BLOCK

CALL
“turnleft”

WHILE
TRUE

p.Is_In_Context (“StepAside”) ?

BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

) },

Context Operations (cont’d)
What result is produced
by these statements if
p has the value shown on
the previous slide?

P=
( “SteerClear”,
{ (“StepAside”,

object Text n;
object Statement b;
p.Remove_From_Context
(“TurnAround”, n, b) ?
n=“TurnAround”

b=
CALL
“turnright”

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

BLOCK

CALL
“turnright”

) },

CALL
“move”

BLOCK

CALL
“turnleft”

BLOCK
WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Context Operations (cont’d)
What result is produced
by these statements if p
and b have the values
shown on the previous
slide?

P=
( “SteerClear”,
{ ( “TurnBack”,
CALL
“turnright”

b=

), (“StepAside”,

CALL
“turnright”

BLOCK

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

BLOCK

CALL
“turnleft”

BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

) },

CALL
“move”

WHILE
TRUE

n = “TurnBack”;
p.Add_To_Context (n, b) ?
n=“”

BLOCK

)

An Operation on Program
What would it mean to
demobilize a program?

Statements in BL Program
PROGRAM identifier IS

INSTRUCTION identifier IS

BEGIN

END identifier

END identifier

.
.
.

State the Problem
global_procedure Demobilize (
alters Program& p
);
/*!
ensures
p.name = #p.name and
p.context =
CONTEXT_DEMOBILIZE (#p.context) and
p.body = DEMOBILIZE (#p.body)
!*/

State the Problem Continued…
math definition CONTEXT_DEMOBILIZE (
c: CONTEXT
): CONTEXT satisfies
if c = empty_set then
CONTEXT_DEMOBILIZE (c) = c
else
there exists n: IDENTIFIER, s: STATEMENT,
rest_of_context: CONTEXT
(c = {(n, s)} union rest_of_context and
CONTEXT_DEMOBILIZE(c) =
{(n, DEMOBILIZE(s))} union
CONTEXT_DEMOBILIZE (rest_of_context))

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;
// demobilize program body
p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

Implementation Continued…
// demobilize statements in program context
while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}
// restore value of p (to be preserved)
p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;
}

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;

while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}

p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

}

p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;


Slide 25

BL Program
PROGRAM identifier IS

sequence of new instructions
BEGIN

sequence of statements
END identifier

Math Model for Program


What pieces of information do we
need to keep track of for a BL
program?





name (IDENTIFIER)
new instructions (CONTEXT)
body (BLOCK STATEMENT)

The Math Model


math subtype PROGRAM is (
name: IDENTIFIER
context: CONTEXT
body: STATEMENT
)
exemplar p
constraint
root (p.body).kind = BLOCK

New Instruction
INSTRUCTION identifier IS

sequence of statements
END identifier

Math Model Continued…


What pieces of information do we
need to keep track of for a new BL
instruction?




name (IDENTIFIER)
body (BLOCK STATEMENT)

Math Model Continued…


math subtype CONTEXT is
finite set of (
name: IDENTIFIER
body: STATEMENT
)
exemplar c
constraint 1. c is a (partial) function
2. for each (name, body) pair in c,
...
name is not one of primitives
nor the empty string, and body
is a BLOCK

Program Component


Type




Program_Kernel is modeled by PROGRAM

Initial Value


IS_INITIAL_PROGRAM (self) 
self.name = empty_string and
self.context = empty_set and
IS_INITIAL_STATEMENT (self.body)

Program SteerClear
Draw a picture of this BL program’s abstract view:
BEGIN
PROGRAM SteerClear IS
WHILE true DO
INSTRUCTION StepAside IS
IF next-is-empty THEN
IF random THEN
skip
turnright
ELSE
ELSE
IF next-is-wall THEN
turnleft
TurnAround
END IF
ELSE
move
StepAside
END StepAside
END IF
INSTRUCTION TurnAround IS
END IF
turnright
END WHILE
turnright
END SteerClear
END TurnAround

Program SteerClear (cont’d)
p=(

,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
,

},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{(

p.name
,

), (

,

) },

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
BLOCK

CALL
“turnright”

), ( “StepAside”,

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK
CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
), ( “StepAside”,

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

BLOCK
WHILE
TRUE

CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

BLOCK

p.context

IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

p.body

IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Program Continued…


Operations










p.Swap_Name (name)
p.Swap_Body (statement)
p.Add_To_Context (name, statement)
p.Remove_From_Context (name, name_copy,
statement)
p.Remove_Any_From_Context (name,
statement)
p.Is_In_Context (name)
p.Size_Of_Context ()

Swap_Name
P=
What effect will
( “Timid”,
{ ( “TurnAround”,
the following
statements have on p,
the program object for
program SteerClear?

BLOCK

CALL
“turnright”

), (“StepAside”,

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

BLOCK

BLOCK

object Text name;
name = “Timid”;
p.Swap_Name (name);
name = “SteerClear”

CALL
“turnright”

WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

CALL
“turnleft”

) },

Swap_Body
What effect will
the following statements have
on object p from the previous
slide?

P=
( “Timid”,
{ ( “TurnAround”,

object Statement b, i, c;
object Text m = “move”;
object Integer test = RANDOM;
c.Compose_Call (m);
b.Add_To_Block (0, c);
i.Compose_If (test, b);
b.Add_to_Block (0, i);
p.Swap_Body (b);

CALL
“turnright”

BLOCK

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

CALL
“turnleft”

BLOCK

b=

WHILE
TRUE

IF
RANDOM

BLOCK

IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK

BLOCK

CALL
“skip”

CALL
“move”

) },

BLOCK

CALL
“turnright”

BLOCK

BLOCK

)

IF_ELSE
NEXT_IS_WALL

BLOCK

CALL
“TurnAround”

BLOCK

CALL
“StepAside”

Context Operations
What result is produced
by these statements if
p has its original value?

P=
( “SteerClear”,
{ ( “TurnAround”,

BLOCK

CALL
“turnright”

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

p.Size_Of_Context () ?

CALL
“turnright”

BLOCK

BLOCK

CALL
“move”

BLOCK

CALL
“turnleft”

WHILE
TRUE

p.Is_In_Context (“StepAside”) ?

BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

) },

Context Operations (cont’d)
What result is produced
by these statements if
p has the value shown on
the previous slide?

P=
( “SteerClear”,
{ (“StepAside”,

object Text n;
object Statement b;
p.Remove_From_Context
(“TurnAround”, n, b) ?
n=“TurnAround”

b=
CALL
“turnright”

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

BLOCK

CALL
“turnright”

) },

CALL
“move”

BLOCK

CALL
“turnleft”

BLOCK
WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Context Operations (cont’d)
What result is produced
by these statements if p
and b have the values
shown on the previous
slide?

P=
( “SteerClear”,
{ ( “TurnBack”,
CALL
“turnright”

b=

), (“StepAside”,

CALL
“turnright”

BLOCK

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

BLOCK

CALL
“turnleft”

BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

) },

CALL
“move”

WHILE
TRUE

n = “TurnBack”;
p.Add_To_Context (n, b) ?
n=“”

BLOCK

)

An Operation on Program
What would it mean to
demobilize a program?

Statements in BL Program
PROGRAM identifier IS

INSTRUCTION identifier IS

BEGIN

END identifier

END identifier

.
.
.

State the Problem
global_procedure Demobilize (
alters Program& p
);
/*!
ensures
p.name = #p.name and
p.context =
CONTEXT_DEMOBILIZE (#p.context) and
p.body = DEMOBILIZE (#p.body)
!*/

State the Problem Continued…
math definition CONTEXT_DEMOBILIZE (
c: CONTEXT
): CONTEXT satisfies
if c = empty_set then
CONTEXT_DEMOBILIZE (c) = c
else
there exists n: IDENTIFIER, s: STATEMENT,
rest_of_context: CONTEXT
(c = {(n, s)} union rest_of_context and
CONTEXT_DEMOBILIZE(c) =
{(n, DEMOBILIZE(s))} union
CONTEXT_DEMOBILIZE (rest_of_context))

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;
// demobilize program body
p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

Implementation Continued…
// demobilize statements in program context
while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}
// restore value of p (to be preserved)
p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;
}

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;

while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}

p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

}

p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;


Slide 26

BL Program
PROGRAM identifier IS

sequence of new instructions
BEGIN

sequence of statements
END identifier

Math Model for Program


What pieces of information do we
need to keep track of for a BL
program?





name (IDENTIFIER)
new instructions (CONTEXT)
body (BLOCK STATEMENT)

The Math Model


math subtype PROGRAM is (
name: IDENTIFIER
context: CONTEXT
body: STATEMENT
)
exemplar p
constraint
root (p.body).kind = BLOCK

New Instruction
INSTRUCTION identifier IS

sequence of statements
END identifier

Math Model Continued…


What pieces of information do we
need to keep track of for a new BL
instruction?




name (IDENTIFIER)
body (BLOCK STATEMENT)

Math Model Continued…


math subtype CONTEXT is
finite set of (
name: IDENTIFIER
body: STATEMENT
)
exemplar c
constraint 1. c is a (partial) function
2. for each (name, body) pair in c,
...
name is not one of primitives
nor the empty string, and body
is a BLOCK

Program Component


Type




Program_Kernel is modeled by PROGRAM

Initial Value


IS_INITIAL_PROGRAM (self) 
self.name = empty_string and
self.context = empty_set and
IS_INITIAL_STATEMENT (self.body)

Program SteerClear
Draw a picture of this BL program’s abstract view:
BEGIN
PROGRAM SteerClear IS
WHILE true DO
INSTRUCTION StepAside IS
IF next-is-empty THEN
IF random THEN
skip
turnright
ELSE
ELSE
IF next-is-wall THEN
turnleft
TurnAround
END IF
ELSE
move
StepAside
END StepAside
END IF
INSTRUCTION TurnAround IS
END IF
turnright
END WHILE
turnright
END SteerClear
END TurnAround

Program SteerClear (cont’d)
p=(

,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
,

},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{(

p.name
,

), (

,

) },

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
BLOCK

CALL
“turnright”

), ( “StepAside”,

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK
CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
), ( “StepAside”,

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

BLOCK
WHILE
TRUE

CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

BLOCK

p.context

IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

p.body

IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Program Continued…


Operations










p.Swap_Name (name)
p.Swap_Body (statement)
p.Add_To_Context (name, statement)
p.Remove_From_Context (name, name_copy,
statement)
p.Remove_Any_From_Context (name,
statement)
p.Is_In_Context (name)
p.Size_Of_Context ()

Swap_Name
P=
What effect will
( “Timid”,
{ ( “TurnAround”,
the following
statements have on p,
the program object for
program SteerClear?

BLOCK

CALL
“turnright”

), (“StepAside”,

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

BLOCK

BLOCK

object Text name;
name = “Timid”;
p.Swap_Name (name);
name = “SteerClear”

CALL
“turnright”

WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

CALL
“turnleft”

) },

Swap_Body
What effect will
the following statements have
on object p from the previous
slide?

P=
( “Timid”,
{ ( “TurnAround”,

object Statement b, i, c;
object Text m = “move”;
object Integer test = RANDOM;
c.Compose_Call (m);
b.Add_To_Block (0, c);
i.Compose_If (test, b);
b.Add_to_Block (0, i);
p.Swap_Body (b);

CALL
“turnright”

BLOCK

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

CALL
“turnleft”

BLOCK

b=

WHILE
TRUE

IF
RANDOM

BLOCK

IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK

BLOCK

CALL
“skip”

CALL
“move”

) },

BLOCK

CALL
“turnright”

BLOCK

BLOCK

)

IF_ELSE
NEXT_IS_WALL

BLOCK

CALL
“TurnAround”

BLOCK

CALL
“StepAside”

Context Operations
What result is produced
by these statements if
p has its original value?

P=
( “SteerClear”,
{ ( “TurnAround”,

BLOCK

CALL
“turnright”

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

p.Size_Of_Context () ?

CALL
“turnright”

BLOCK

BLOCK

CALL
“move”

BLOCK

CALL
“turnleft”

WHILE
TRUE

p.Is_In_Context (“StepAside”) ?

BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

) },

Context Operations (cont’d)
What result is produced
by these statements if
p has the value shown on
the previous slide?

P=
( “SteerClear”,
{ (“StepAside”,

object Text n;
object Statement b;
p.Remove_From_Context
(“TurnAround”, n, b) ?
n=“TurnAround”

b=
CALL
“turnright”

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

BLOCK

CALL
“turnright”

) },

CALL
“move”

BLOCK

CALL
“turnleft”

BLOCK
WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Context Operations (cont’d)
What result is produced
by these statements if p
and b have the values
shown on the previous
slide?

P=
( “SteerClear”,
{ ( “TurnBack”,
CALL
“turnright”

b=

), (“StepAside”,

CALL
“turnright”

BLOCK

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

BLOCK

CALL
“turnleft”

BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

) },

CALL
“move”

WHILE
TRUE

n = “TurnBack”;
p.Add_To_Context (n, b) ?
n=“”

BLOCK

)

An Operation on Program
What would it mean to
demobilize a program?

Statements in BL Program
PROGRAM identifier IS

INSTRUCTION identifier IS

BEGIN

END identifier

END identifier

.
.
.

State the Problem
global_procedure Demobilize (
alters Program& p
);
/*!
ensures
p.name = #p.name and
p.context =
CONTEXT_DEMOBILIZE (#p.context) and
p.body = DEMOBILIZE (#p.body)
!*/

State the Problem Continued…
math definition CONTEXT_DEMOBILIZE (
c: CONTEXT
): CONTEXT satisfies
if c = empty_set then
CONTEXT_DEMOBILIZE (c) = c
else
there exists n: IDENTIFIER, s: STATEMENT,
rest_of_context: CONTEXT
(c = {(n, s)} union rest_of_context and
CONTEXT_DEMOBILIZE(c) =
{(n, DEMOBILIZE(s))} union
CONTEXT_DEMOBILIZE (rest_of_context))

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;
// demobilize program body
p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

Implementation Continued…
// demobilize statements in program context
while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}
// restore value of p (to be preserved)
p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;
}

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;

while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}

p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

}

p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;


Slide 27

BL Program
PROGRAM identifier IS

sequence of new instructions
BEGIN

sequence of statements
END identifier

Math Model for Program


What pieces of information do we
need to keep track of for a BL
program?





name (IDENTIFIER)
new instructions (CONTEXT)
body (BLOCK STATEMENT)

The Math Model


math subtype PROGRAM is (
name: IDENTIFIER
context: CONTEXT
body: STATEMENT
)
exemplar p
constraint
root (p.body).kind = BLOCK

New Instruction
INSTRUCTION identifier IS

sequence of statements
END identifier

Math Model Continued…


What pieces of information do we
need to keep track of for a new BL
instruction?




name (IDENTIFIER)
body (BLOCK STATEMENT)

Math Model Continued…


math subtype CONTEXT is
finite set of (
name: IDENTIFIER
body: STATEMENT
)
exemplar c
constraint 1. c is a (partial) function
2. for each (name, body) pair in c,
...
name is not one of primitives
nor the empty string, and body
is a BLOCK

Program Component


Type




Program_Kernel is modeled by PROGRAM

Initial Value


IS_INITIAL_PROGRAM (self) 
self.name = empty_string and
self.context = empty_set and
IS_INITIAL_STATEMENT (self.body)

Program SteerClear
Draw a picture of this BL program’s abstract view:
BEGIN
PROGRAM SteerClear IS
WHILE true DO
INSTRUCTION StepAside IS
IF next-is-empty THEN
IF random THEN
skip
turnright
ELSE
ELSE
IF next-is-wall THEN
turnleft
TurnAround
END IF
ELSE
move
StepAside
END StepAside
END IF
INSTRUCTION TurnAround IS
END IF
turnright
END WHILE
turnright
END SteerClear
END TurnAround

Program SteerClear (cont’d)
p=(

,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
,

},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{(

p.name
,

), (

,

) },

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
BLOCK

CALL
“turnright”

), ( “StepAside”,

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK
CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
), ( “StepAside”,

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

BLOCK
WHILE
TRUE

CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

BLOCK

p.context

IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

p.body

IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Program Continued…


Operations










p.Swap_Name (name)
p.Swap_Body (statement)
p.Add_To_Context (name, statement)
p.Remove_From_Context (name, name_copy,
statement)
p.Remove_Any_From_Context (name,
statement)
p.Is_In_Context (name)
p.Size_Of_Context ()

Swap_Name
P=
What effect will
( “Timid”,
{ ( “TurnAround”,
the following
statements have on p,
the program object for
program SteerClear?

BLOCK

CALL
“turnright”

), (“StepAside”,

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

BLOCK

BLOCK

object Text name;
name = “Timid”;
p.Swap_Name (name);
name = “SteerClear”

CALL
“turnright”

WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

CALL
“turnleft”

) },

Swap_Body
What effect will
the following statements have
on object p from the previous
slide?

P=
( “Timid”,
{ ( “TurnAround”,

object Statement b, i, c;
object Text m = “move”;
object Integer test = RANDOM;
c.Compose_Call (m);
b.Add_To_Block (0, c);
i.Compose_If (test, b);
b.Add_to_Block (0, i);
p.Swap_Body (b);

CALL
“turnright”

BLOCK

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

CALL
“turnleft”

BLOCK

b=

WHILE
TRUE

IF
RANDOM

BLOCK

IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK

BLOCK

CALL
“skip”

CALL
“move”

) },

BLOCK

CALL
“turnright”

BLOCK

BLOCK

)

IF_ELSE
NEXT_IS_WALL

BLOCK

CALL
“TurnAround”

BLOCK

CALL
“StepAside”

Context Operations
What result is produced
by these statements if
p has its original value?

P=
( “SteerClear”,
{ ( “TurnAround”,

BLOCK

CALL
“turnright”

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

p.Size_Of_Context () ?

CALL
“turnright”

BLOCK

BLOCK

CALL
“move”

BLOCK

CALL
“turnleft”

WHILE
TRUE

p.Is_In_Context (“StepAside”) ?

BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

) },

Context Operations (cont’d)
What result is produced
by these statements if
p has the value shown on
the previous slide?

P=
( “SteerClear”,
{ (“StepAside”,

object Text n;
object Statement b;
p.Remove_From_Context
(“TurnAround”, n, b) ?
n=“TurnAround”

b=
CALL
“turnright”

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

BLOCK

CALL
“turnright”

) },

CALL
“move”

BLOCK

CALL
“turnleft”

BLOCK
WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Context Operations (cont’d)
What result is produced
by these statements if p
and b have the values
shown on the previous
slide?

P=
( “SteerClear”,
{ ( “TurnBack”,
CALL
“turnright”

b=

), (“StepAside”,

CALL
“turnright”

BLOCK

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

BLOCK

CALL
“turnleft”

BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

) },

CALL
“move”

WHILE
TRUE

n = “TurnBack”;
p.Add_To_Context (n, b) ?
n=“”

BLOCK

)

An Operation on Program
What would it mean to
demobilize a program?

Statements in BL Program
PROGRAM identifier IS

INSTRUCTION identifier IS

BEGIN

END identifier

END identifier

.
.
.

State the Problem
global_procedure Demobilize (
alters Program& p
);
/*!
ensures
p.name = #p.name and
p.context =
CONTEXT_DEMOBILIZE (#p.context) and
p.body = DEMOBILIZE (#p.body)
!*/

State the Problem Continued…
math definition CONTEXT_DEMOBILIZE (
c: CONTEXT
): CONTEXT satisfies
if c = empty_set then
CONTEXT_DEMOBILIZE (c) = c
else
there exists n: IDENTIFIER, s: STATEMENT,
rest_of_context: CONTEXT
(c = {(n, s)} union rest_of_context and
CONTEXT_DEMOBILIZE(c) =
{(n, DEMOBILIZE(s))} union
CONTEXT_DEMOBILIZE (rest_of_context))

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;
// demobilize program body
p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

Implementation Continued…
// demobilize statements in program context
while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}
// restore value of p (to be preserved)
p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;
}

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;

while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}

p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

}

p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;


Slide 28

BL Program
PROGRAM identifier IS

sequence of new instructions
BEGIN

sequence of statements
END identifier

Math Model for Program


What pieces of information do we
need to keep track of for a BL
program?





name (IDENTIFIER)
new instructions (CONTEXT)
body (BLOCK STATEMENT)

The Math Model


math subtype PROGRAM is (
name: IDENTIFIER
context: CONTEXT
body: STATEMENT
)
exemplar p
constraint
root (p.body).kind = BLOCK

New Instruction
INSTRUCTION identifier IS

sequence of statements
END identifier

Math Model Continued…


What pieces of information do we
need to keep track of for a new BL
instruction?




name (IDENTIFIER)
body (BLOCK STATEMENT)

Math Model Continued…


math subtype CONTEXT is
finite set of (
name: IDENTIFIER
body: STATEMENT
)
exemplar c
constraint 1. c is a (partial) function
2. for each (name, body) pair in c,
...
name is not one of primitives
nor the empty string, and body
is a BLOCK

Program Component


Type




Program_Kernel is modeled by PROGRAM

Initial Value


IS_INITIAL_PROGRAM (self) 
self.name = empty_string and
self.context = empty_set and
IS_INITIAL_STATEMENT (self.body)

Program SteerClear
Draw a picture of this BL program’s abstract view:
BEGIN
PROGRAM SteerClear IS
WHILE true DO
INSTRUCTION StepAside IS
IF next-is-empty THEN
IF random THEN
skip
turnright
ELSE
ELSE
IF next-is-wall THEN
turnleft
TurnAround
END IF
ELSE
move
StepAside
END StepAside
END IF
INSTRUCTION TurnAround IS
END IF
turnright
END WHILE
turnright
END SteerClear
END TurnAround

Program SteerClear (cont’d)
p=(

,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,

p.name
,

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{

p.name
,

},

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{(

p.name
,

), (

,

) },

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
BLOCK

CALL
“turnright”

), ( “StepAside”,

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK
CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

p.context
p.body
)

Program SteerClear (cont’d)
p = ( “SteerClear”,
{ ( “TurnAround”,

p.name
), ( “StepAside”,

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

BLOCK
WHILE
TRUE

CALL
“turnright”

) },

CALL
“move”

BLOCK
CALL
“turnleft”

BLOCK

p.context

IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

p.body

IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Program Continued…


Operations










p.Swap_Name (name)
p.Swap_Body (statement)
p.Add_To_Context (name, statement)
p.Remove_From_Context (name, name_copy,
statement)
p.Remove_Any_From_Context (name,
statement)
p.Is_In_Context (name)
p.Size_Of_Context ()

Swap_Name
P=
What effect will
( “Timid”,
{ ( “TurnAround”,
the following
statements have on p,
the program object for
program SteerClear?

BLOCK

CALL
“turnright”

), (“StepAside”,

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

BLOCK

BLOCK

object Text name;
name = “Timid”;
p.Swap_Name (name);
name = “SteerClear”

CALL
“turnright”

WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

CALL
“turnleft”

) },

Swap_Body
What effect will
the following statements have
on object p from the previous
slide?

P=
( “Timid”,
{ ( “TurnAround”,

object Statement b, i, c;
object Text m = “move”;
object Integer test = RANDOM;
c.Compose_Call (m);
b.Add_To_Block (0, c);
i.Compose_If (test, b);
b.Add_to_Block (0, i);
p.Swap_Body (b);

CALL
“turnright”

BLOCK

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

CALL
“move”

CALL
“turnleft”

BLOCK

b=

WHILE
TRUE

IF
RANDOM

BLOCK

IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK

BLOCK

CALL
“skip”

CALL
“move”

) },

BLOCK

CALL
“turnright”

BLOCK

BLOCK

)

IF_ELSE
NEXT_IS_WALL

BLOCK

CALL
“TurnAround”

BLOCK

CALL
“StepAside”

Context Operations
What result is produced
by these statements if
p has its original value?

P=
( “SteerClear”,
{ ( “TurnAround”,

BLOCK

CALL
“turnright”

), (“StepAside”,
IF_ELSE
RANDOM

CALL
“turnright”

BLOCK

p.Size_Of_Context () ?

CALL
“turnright”

BLOCK

BLOCK

CALL
“move”

BLOCK

CALL
“turnleft”

WHILE
TRUE

p.Is_In_Context (“StepAside”) ?

BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

) },

Context Operations (cont’d)
What result is produced
by these statements if
p has the value shown on
the previous slide?

P=
( “SteerClear”,
{ (“StepAside”,

object Text n;
object Statement b;
p.Remove_From_Context
(“TurnAround”, n, b) ?
n=“TurnAround”

b=
CALL
“turnright”

BLOCK

CALL
“turnright”

BLOCK

IF_ELSE
RANDOM

BLOCK

CALL
“turnright”

) },

CALL
“move”

BLOCK

CALL
“turnleft”

BLOCK
WHILE
TRUE
BLOCK
IF_ELSE
NEXT_IS_EMPTY

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

)

Context Operations (cont’d)
What result is produced
by these statements if p
and b have the values
shown on the previous
slide?

P=
( “SteerClear”,
{ ( “TurnBack”,
CALL
“turnright”

b=

), (“StepAside”,

CALL
“turnright”

BLOCK

BLOCK

IF
RANDOM

CALL
“turnright”

BLOCK

BLOCK

CALL
“turnleft”

BLOCK
IF_ELSE
NEXT_IS_EMPTY
BLOCK

BLOCK

BLOCK
IF_ELSE
NEXT_IS_WALL

CALL
“skip”
BLOCK

CALL
“TurnAround”

BLOCK
CALL
“StepAside”

) },

CALL
“move”

WHILE
TRUE

n = “TurnBack”;
p.Add_To_Context (n, b) ?
n=“”

BLOCK

)

An Operation on Program
What would it mean to
demobilize a program?

Statements in BL Program
PROGRAM identifier IS

INSTRUCTION identifier IS

BEGIN

END identifier

END identifier

.
.
.

State the Problem
global_procedure Demobilize (
alters Program& p
);
/*!
ensures
p.name = #p.name and
p.context =
CONTEXT_DEMOBILIZE (#p.context) and
p.body = DEMOBILIZE (#p.body)
!*/

State the Problem Continued…
math definition CONTEXT_DEMOBILIZE (
c: CONTEXT
): CONTEXT satisfies
if c = empty_set then
CONTEXT_DEMOBILIZE (c) = c
else
there exists n: IDENTIFIER, s: STATEMENT,
rest_of_context: CONTEXT
(c = {(n, s)} union rest_of_context and
CONTEXT_DEMOBILIZE(c) =
{(n, DEMOBILIZE(s))} union
CONTEXT_DEMOBILIZE (rest_of_context))

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;
// demobilize program body
p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

Implementation Continued…
// demobilize statements in program context
while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}
// restore value of p (to be preserved)
p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;
}

Implementation
procedure_body Demobilize (
alters Program& p
)
{
object Program tmp;
object Text name;
object Statement body;

while (p.Size_Of_Context () > 0)
{
p.Remove_Any_From_Context (name, body);
Demobilize (body);
tmp.Add_To_Context (name, body);
}

p.Swap_Body (body);
Demobilize (body);
tmp.Swap_Body (body);

}

p.Swap_Name (name);
tmp.Swap_Name (name);
p &= tmp;