60 likes | 340 Views
Boehm - Jacopini Theorem. The essence of the theorem is that every program that uses goto instructions can be expressed via if and while. In other words, a language that includes if and while is functionally equivalent to the same language with go. Boehm - Jacopini Theorem.
E N D
Boehm - Jacopini Theorem The essence of the theorem is that every program that uses goto instructions can be expressed via if and while. In other words, a language that includes if and while is functionally equivalent to the same language with go.
Boehm - Jacopini Theorem Each instruction could be if, goto, etc. Begin with a segment of code, including goto instructions: S1; S2; ... SN Step 1 Relabel every instruction (Sj) with Lj and update gotos to match. L1: S1; L2: S2; ... LN: SN Step 2 Select an integer variable, call it loc, not used in the program. Wrap the following around the entire program: loc = 1; while (loc != N) { translatedCodeGoesHere }
Step 3 Replace each instruction (Sj) according to these rules: goto Rule Replace Lk: goto Lm; with if (loc==k) loc = m; if-goto Rule Replace Lk: if (cond) goto Lm; with if (loc==k) { if (cond) loc=m; else loc++; } other Rule Replace Lk: instructionk; with if (loc==k) {instructionk; loc++;}
Example loc = 1; while (loc!=7) { if (loc==1) { j=1; loc++; } if (loc==2) { if (j>N) loc = 6; else loc++; } if (loc==3) { a[j]=0; loc++; } if (loc==4) { j = j + 1; loc++; } if (loc==5) loc=2; if (loc==6) { write(“the end”); loc++; } } Original j = 1; Top: if (j>N) goto Done; a[j] = 0; j = j + 1; goto Top; Done: write( “the end”); The End!