Skip to content

Commit d8c4409

Browse files
committed
Add another guard demo.
1 parent b3dca9d commit d8c4409

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

example/guards.ml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,71 @@ let guard5 () =
9292
42);
9393
]
9494

95+
let f b n =
96+
Run.first_or_terminate
97+
[
98+
(fun () ->
99+
Control.terminate_unless
100+
(let i = ref 0 in
101+
while !i < n do
102+
i := !i + 1;
103+
Control.yield ()
104+
done;
105+
true);
106+
b);
107+
(fun () ->
108+
Control.terminate_unless
109+
(let j = ref 0 in
110+
while !j < n do
111+
j := !j + 1;
112+
Control.yield ()
113+
done;
114+
false);
115+
false);
116+
]
117+
118+
let demo () =
119+
Run.first_or_terminate
120+
[
121+
(fun () ->
122+
Control.terminate_unless (f false 1000);
123+
17);
124+
(fun () ->
125+
Control.terminate_unless (f false 80000);
126+
11);
127+
(fun () ->
128+
Control.terminate_unless
129+
(while true do
130+
Control.yield ()
131+
done;
132+
true);
133+
27);
134+
(fun () ->
135+
Control.terminate_unless
136+
(Run.first_or_terminate
137+
[
138+
(fun () ->
139+
Control.terminate_unless (f false 100000);
140+
false);
141+
(fun () ->
142+
Control.terminate_unless (f true 200000);
143+
true);
144+
]);
145+
42);
146+
(fun () ->
147+
Control.terminate_unless
148+
(Run.first_or_terminate
149+
[
150+
(fun () ->
151+
Control.terminate_unless (f true 100);
152+
false);
153+
(fun () ->
154+
Control.terminate_unless (f false 10);
155+
false);
156+
]);
157+
13);
158+
]
159+
95160
let () =
96161
(* We use the test scheduler here, but in more practical use cases one should
97162
likely use e.g. the multififo or the random scheduler to compute with
@@ -107,5 +172,6 @@ let () =
107172
if 42 <> guard4 () then
108173
print_endline "Guard 4 wasn't 42, but that is a known issue.");
109174
(fun () -> assert (42 = guard5 ()));
175+
(fun () -> assert (42 = demo ()));
110176
];
111177
print_endline "Ran guarded case statement examples."

0 commit comments

Comments
 (0)