This set of examples are a re-write of Figures 25, 26 in Chapter 2.
The names of parameters are clearer, and routines' types are explicit.
declare writeList: [L cont] -> List1 Cont1 ;
List1 Cont1 -> First Rest;
write First;
writeList Rest;
Cont1
.
where L = [cont [int [L cont]]]
declare EmptyList:[cont 'a] -> Empty1 Full1;
Empty1
.
declare Cons:[int L ['b [int L]]] -> x List2 ListClient;
ListClient → Empty2 Full2;
Full2 x List2
.
Cons 1 EmptyList -> List3;
Cons 2 List3 -> List4;
writeList List4;
terminate
Abbreviations:
A = -> List3;
Cons 2 List3 -> List4;
writeList List4;
terminate
B = -> Empty2 Full2;
Full2 x+1 List2+1
C = -> List4;
writeList List4;
terminate
D = -> First Rest;
write First;
writeList Rest;
Cont1+1
E = writeList Rest+1;
Cont1+2
Notation:
means fragment A, with non-local referencing environment n.
"=" means "substitute the body of a routine"
"==" means "after looking up bindings"
foo:bar means formal parameter foo, actual bar
"=>" means "invokes"
numbers refer to referencing environments; 0 is main.
{n} means "outputs n"
+n means "found n levels away in non-local referencing environment"
0 =>
1 Cons(x:1, List2:EmptyList, ListClient:) =
ListClient → Empty2 Full2;
Full2 x List2
==
=>
2 (List3:) =
Cons 2 List3 -> List4;
writeList List4;
terminate
==
Cons 2 =>
3 Cons(x:2, List2:, ListClient:) =
ListClient → Empty2 Full2;
Full2 x List2
==
=>
4 (List4:) =
writeList List4;
terminate
==
writeList terminate =>
5 writeList(List1:,Cont1:terminate) =
List1 Cont1 -> First Rest;
write First;
writeList Rest;
Cont1
==
terminate =>
6 (Empty2:terminate,Full2:) =
Full2 x+1 List2+1
==
2 =>
7 (First:2,Rest:) =
write First;
writeList Rest;
Cont1+1
==
write 2 =>
8 {2} =>
9
==
writeList terminate =>
10 writeList(List1:, Cont1:terminate) =
List1 Cont1 -> First Rest;
write First;
writeList Rest;
Cont1
==
terminate =>
11 (Empty2:terminate, Full2:) =
Full2 x+1 List2+1
==
1 EmptyList =>
12 (First:1, Rest:EmptyList) =
write First;
writeList Rest;
Cont1+1
==
write 1 =>
13 {1} =>
14 =
writeList Rest+1 Cont1+2
==
writeList EmptyList terminate =>
15 writeList(List1:EmptyList, Cont1:terminate) =
List1 Cont1
writeList Rest;
Cont1
==
EmptyList terminate =>
16 EmptyList(Empty1:terminate, Full1:) =
Empty1
==
terminate