4 structure Thread
= MLton
.Thread
6 fun generate(f
: ('a
-> unit
) -> unit
): unit
-> 'a option
=
8 val paused
: 'a option Thread
.t option ref
= ref NONE
9 val gen
: unit Thread
.t option ref
= ref NONE
10 fun return(a
: 'a option
): unit
=
11 Thread
.switch(fn t
' =>
12 let val _
= gen
:= SOME t
'
13 val t
= valOf(!paused
)
14 val _
= paused
:= NONE
15 in Thread
.prepare (t
, a
)
18 gen
:= SOME(Thread
.new(fn () => (f (return
o SOME
)
20 in fn () => Thread
.switch(fn t
=> (paused
:= SOME t
21 ; Thread
.prepare (valOf(!gen
), ())))
26 | N
of 'a tree
* 'a tree
28 fun foreach(t
: 'a tree
, f
: 'a
-> unit
): unit
=
32 |
N(l
, r
) => (loop l
; loop r
)
36 fun same(f
: unit
-> 'a option
,
38 eq
: 'a
* 'a
-> bool): bool =
43 |
(SOME x
, SOME y
) => eq(x
, y
) andalso loop()
48 fun fringe(t
: 'a tree
): unit
-> 'a option
=
49 generate(fn f
=> foreach(t
, f
))
51 fun sameFringe(t1
: 'a tree
, t2
: 'a tree
, eq
: 'a
* 'a
-> bool): bool =
52 same(fringe t1
, fringe t2
, eq
)
54 val t1
= N(N(L
1, L
2), N(N(L
3, L
4), L
5))
55 val t2
= N(L
1, N(N(L
2, L
3), N(L
4, L
5)))
58 if sameFringe(t1
, t2
, op =)
59 then print
"success\n"
60 else print
"failure\n"