42
Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF END IF How can we use tree to model a BL statement ?

Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Embed Size (px)

Citation preview

Page 1: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Statement Component

IF next-is-empty THEN move

turnrightELSE IF next-is-enemy THEN infect END IFEND IF

How can we use tree to model a BL statement ?

Page 2: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Statement Component

What’s wrong with

IF_ELSENEXT_IS_EMPTY

CALLmov

e

IFNEXT_IS_ENEMY

CALLinfect

CALLturnright

Page 3: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Statement Component

An Abstract Syntax TreeIF_ELSE

NEXT_IS_EMPTY

CALLturnright

IFNEXT_IS_ENEMY

CALLinfect

BLOCK

BLOCK BLOCK

CALLmove

Page 4: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

BL Statements

IF test THEN

END IF

WHILE test DO

END WHILE

IF test THEN

ELSE

END IF instruction

Page 5: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

BL Statements: IF

IF test THEN

END IF

IFtest

BLOCK

. . .

Page 6: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

BL Statements: IF_ELSE

BLOCK

. . .

IF test THEN

ELSE

END IF

IF_ELSEtest

BLOCK

. . .

Page 7: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

BL Statements: WHILE

WHILEtest

BLOCK

. . .

WHILE test DO

END WHILE

Page 8: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

BL Statements: CALL

CALLinstructioninstruction

Page 9: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

An Example

WHILE true DO IF next-is-enemy THEN infect ELSE IF next-is-empty THEN move ELSE turnleft END IF END IFEND WHILE

CALLinfect

CALLmove

IF_ELSENEXT_IS_EMPTY

BLOCK BLOCK

CALLturnleft

WHILETRUE

BLOCK

IF_ELSENEXT_IS_ENEMY

BLOCK BLOCK

Page 10: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Statement Continued…

Type Statement_Kernel is modeled by

STATEMENT Initial Value

IS_INITIAL_STATEMENT (self)

Page 11: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Statement Continued… math subtype STATEMENT_LABEL is (

kind: Statement_Kind test: Condition instruction: IDENTIFIER ) constraint …

math subtype STATEMENT is tree of STATEMENT_LABEL exemplar s constraint IS_LEGAL_STATEMENT (s)

Page 12: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Statement Continued… Statement_Kind

BLOCK IF IF_ELSE WHILE CALL

Condition NEXT_IS_EMPTY NEXT_IS_NOT_EMPTY NEXT_IS_WALL NEXT_IS_NOT_WALL NEXT_IS_FRIEND NEXT_IS_NOT_FRIEND NEXT_IS_ENEMY NEXT_IS_NOT_ENEMY RANDOM TRUE

These are all integer values, but we use the names becausethey are more meaningful than arbitrary integer values.

Page 13: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Statement Continued…

IS_LEGAL_STATEMENT?

IFcondition

BLOCK

WHILEcondition

BLOCK

CALLinstruction

IF_ELSEcondition

BLOCK BLOCK

BLOCK

Page 14: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Statement Operations Operations

s.Add_To_Block (pos, statement) s.Remove_From_Block (pos, statement) s.Length_Of_Block () s.Compose_If (cond, block) s.Decompose_If (cond, block) s.Compose_If_Else (cond, if_block, else_block) s.Decompose_If_Else (cond, if_block, else_block) s.Compose_While (cond, block) s.Decompose_While (cond, block) s.Compose_Call (inst) s.Decompose_Call (inst) s.Kind ()

Page 15: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Statement Block Operations s.Add_To_Block (pos, statement) s.Remove_From_Block (pos, statement) s.Length_Of_Block ()

Page 16: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Statement If Operations s.Compose_If (cond, block) s.Decompose_If (cond, block)

Page 17: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Statement If_Else Operations s.Compose_If_Else (

cond, if_block, else_block) s.Decompose_If_Else (

cond, if_block, else_block)

Page 18: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Statement While Operations s.Compose_While (cond, block) s.Decompose_While (cond, block)

Page 19: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Statement Call Operations s.Compose_Call (inst) s.Decompose_Call (inst)

Page 20: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Statement Other Operations s.Kind ()

Page 21: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Using Statement Operations

What operations are needed to produce Statement if_stmt =

object Statement call, block, if_stmt;

object Text inst = “infect”;

IFNEXT_IS_ENEMY

CALLinfect

BLOCK

call.Compose_Call (inst);

block.Add_To_Block (0, call);

object Integer cond = NEXT_IS_ENEMY;

if_stmt.Compose_If (cond, block);

Page 22: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Using Statement OperationsConsider Statement objectIf_Else_stmt with this value: IF_ELSE

NEXT_IS_EMPTY

CALLturnright

IFNEXT_IS_ENEMY

CALLinfect

BLOCK

BLOCK BLOCK

CALLmove

Page 23: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Using Statement Operations

Show the operations to produce If_Else_stmt on the previous slide:

object Text inst = “move”;

object Statement If_Else_stmt, then, else, call, if_stmt, block;

call.Compose_Call (inst);

then.Add_To_Block (0, call);inst = “turnright”;

call.Compose_Call (inst);

then.Add_To_Block (1, call);

object Integer cond = NEXT_IS_ENEMY;

call.Compose_Call (inst);

block.Add_To_Block (0, call);if_stmt.Compose_If (cond, block);

inst = “infect”;

else.Add_To_Block (0, if_stmt);

If_Else_stmt.Compose_If_Else (cond, then, else);

cond = NEXT_IS_EMPTY;

Page 24: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Practice Operation Most operations on Statement have to

be recursive Use 5 step process to recursion:

0. State the problem1. Visualize recursive structure2. Verify that visualized recursive structure

can be leveraged into an implementation3. Visualize a recursive implementation4. Write a skeleton for the operation body5. Refine the skeleton into an operation body

Page 25: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Step 0: State the Problem Procedure Demobilize replaces every

occurrence of the “move” instruction in statement s with a “skip” instruction.

global_procedure Demobilize ( alters Statement& s ); /*! ensures

s = DEMOBILIZE (#s) !*/

Page 26: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Demobilize this Statement

BLOCK

IF_ELSENEXT_IS_EMPTY

IFNEXT_IS_ENEMY

CALLinfect

BLOCK BLOCK

CALLmove

CALLmove

CALLskip

CALLskip

Page 27: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Step 0: State the Problem Continued…math definition DEMOBILIZE ( s: STATEMENT ): STATEMENT satisfies if root (s).kind = CALL then if root (s).instruction = “move” then DEMOBILIZE (s) = compose ((CALL, TRUE, “skip”), empty_string) else DEMOBILIZE (s) = s else there exists label: STATEMENT_LABEL, nested_stmts: string of STATEMENT (s = compose (label, nested_stmts) and DEMOBILIZE (s) = compose (label, STRING_DEMOBILIZE

(nested_stmts))

Page 28: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Step 0: State the Problem Continued…math definition STRING_DEMOBILIZE ( str: string of STATEMENT ): string of STATEMENT satisfies if str = empty_string then STRING_DEMOBILIZE (str) = empty_string else there exists s: STATEMENT, rest: string of STATEMENT (str = <s> * rest and STRING_DEMOBILIZE (str) = DEMOBILIZE (s) * STRING_DEMOBILIZE (rest))

Page 29: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Step 1: Visualize Recursive Structure

s =

BLOCK

...

root (s).kind = BLOCK

IFtest

root (s).kind = IF

IF_ELSEtest

root (s).kind = IF_ELSE

WHILEtest

root (s).kind = WHILECALL

instruction

root (s).kind = CALL

Page 30: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Step 2: Verify That Leveraging Works Ask yourself: If Demobilize could get a

helper to demobilize the nested statements in each of the five (four?) cases, could it take advantage of this generous offer?

Yes! Once you know how to demobilize the nested statements, you can demobilize the entire statement.

Page 31: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Step 2½: Let’s Jump Ahead

procedure_body Demobilize ( alters Statement& s ){ case_select (s.Kind ()) { case BLOCK: {Demobilize_Block (s);} break; case IF: {Demobilize_If (s);} break; case IF_ELSE: {Demobilize_If_Else (s);} break; case WHILE: {Demobilize_While (s);} break; case CALL: { object Text inst, skipinst = “skip”; s.Decompose_Call (inst); if ( inst == “move”) { inst &= skipinst; } s.Compose_Call (inst); } break; }}

Page 32: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Step 2¾ (or back to Step 0 ): State the Problem

global_procedure Demobilize_Block ( alters Statement& s ); /*! requires root (s).kind = BLOCK ensures s = DEMOBILIZE (#s) !*/

Page 33: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Steps 1-5 Condensed (BLOCK)

object Statement nested;object Integer pos;while (pos < s.Length_Of_Block ()){ s.Remove_From_Block (pos, nested); Demobilize (nested);

s.Add_To_Block (pos, nested); pos ++; }

s =BLOCK

...

procedure_body Demobilize_Block ( alters Statement& s ){

}

Page 34: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Step ? (or back to Step 2¾ ): State the Problem Continued…

global_procedure Demobilize_If ( alters Statement& s ); /*! requires root (s).kind = IF ensures s = DEMOBILIZE (#s)

!*/

Page 35: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

procedure_body Demobilize_If ( alters Statement& s ){

}

Steps 1-5 Condensed (IF)

object Statement if_blk;object Integer cond;s.Decompose_If (cond, if_blk);Demobilize (if_blk);s.Compose_If (cond, if_blk);

IFtest

s =

Page 36: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Steps 1-5 Condensed (IF_ELSE)

procedure_body Demobilize_If_Else ( alters Statement& s ){ // You try it!

}

s =IF_ELSE

test

Page 37: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Statement: Representation

The representation for Statement_Kernel_1 has one field: tree_rep: a Tree_Of_Tree_Node where

a Tree_Node is a Record with three fields:

kind: Integer test: Integer instruction: Text

Page 38: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Statement: Correspondence

self, the Statement, is equal to self.tree_rep, the Tree used to represent it.

Page 39: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Statement: Convention

All the labels in the tree, tree_rep, are legal statement labels (i.e., they satisfy the constraint for the definition of STATEMENT_LABEL);

The tree itself is a legal statement (i.e., it satisfies the constraint for the definition of STATEMENT).

Page 40: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Statement: CCD

S_Pretty_Printext

iS_Pretty_Print_1

iext

S_Kernel

S_Kernel_1

i

Representation Record Tree_Kernel

u

u uenc

i i i

i

Page 41: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Statement: Compose_Ifobject Tree_Of_Tree_Node new_tree_rep, t;

// make t a valid representation for an initial value of Statementt[current][kind] = BLOCK;t[current][test] = TRUE;

// extract representation tree from block and consume blockblock[tree_rep] &= t;

// construct new representation treenew_tree_rep[current][kind] = IF;new_tree_rep[current][test] &= cond; // consume condnew_tree_rep.Add (0, t);

// produce selfself[tree_rep] &= new_tree_rep;

Page 42: Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Statement: Decompose_Ifobject Tree_Of_Tree_Node new_tree_rep, t;

// make new_tree_rep a valid representation for// an initial value of Statementnew_tree_rep[current][kind] = BLOCK;new_tree_rep[current][test] = TRUE;

// consume selfself[tree_rep] &= new_tree_rep;

// extract condition and body from representation of ifnew_tree_rep[current][test] &= cond;new_tree_rep.Remove (0, block[tree_rep]);