[m-rev.] diff: add a basic test of tabling with uints

Julien Fischer jfischer at opturion.com
Wed Apr 26 16:50:09 AEST 2017


Add a basic test of tabling with uints.

tests/tabling/Mmakefile:
tests/tabling/fib_uint.m:
tests/tabling/fib_uint.exp:
     As above.

Julien.

diff --git a/tests/tabling/Mmakefile b/tests/tabling/Mmakefile
index f408507..bfa37e6 100644
--- a/tests/tabling/Mmakefile
+++ b/tests/tabling/Mmakefile
@@ -23,6 +23,7 @@ SIMPLE_NONLOOP_PROGS = \
  	fib_list \
  	fib_stats \
  	fib_string \
+	fib_uint \
  	loopcheck_no_loop \
  	loopcheck_nondet_no_loop \
  	mercury_java_parser_dead_proc_elim_bug \
diff --git a/tests/tabling/fib_uint.exp b/tests/tabling/fib_uint.exp
new file mode 100644
index 0000000..6302bcd
--- /dev/null
+++ b/tests/tabling/fib_uint.exp
@@ -0,0 +1 @@
+tabling works
diff --git a/tests/tabling/fib_uint.m b/tests/tabling/fib_uint.m
new file mode 100644
index 0000000..bea0277
--- /dev/null
+++ b/tests/tabling/fib_uint.m
@@ -0,0 +1,83 @@
+% vim: ts=4 sw=4 et ft=mercury
+
+:- module fib_uint.
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is cc_multi.
+
+:- implementation.
+
+:- import_module benchmarking.
+:- import_module int.
+:- import_module uint.
+:- import_module require.
+
+:- pragma require_feature_set([memo]).
+
+main(!IO) :-
+    perform_trials(20u, !IO).
+
+:- pred perform_trials(uint::in, io::di, io::uo) is cc_multi.
+
+perform_trials(N, !IO) :-
+    trial(N, Time, MTime),
+    trace [compiletime(flag("progress")), io(!S)] (
+        io.write_string("trial ", !S),
+        io.write_uint(N, !S),
+        io.write_string(": ", !S),
+        io.write_int(Time, !S),
+        io.write_string("ms nonmemoed vs ", !S),
+        io.write_int(MTime, !S),
+        io.write_string("ms memoed\n", !S)
+    ),
+    (
+        (
+            Time > 10 * MTime,
+            MTime > 0   % untabled takes ten times as long
+        ;
+            Time > 100, % untabled takes at least 100 ms
+            MTime < 1   % while tabled takes at most 1 ms
+        )
+    ->
+        io.write_string("tabling works\n", !IO)
+    ;
+        Time > 10000    % Untabled takes at least 10 seconds
+    ->
+        io.write_string("tabling does not appear to work\n", !IO)
+    ;
+        % We couldn't get a measurable result with N,
+        % and it looks like we can afford a bigger trial
+        perform_trials(N + 3u, !IO)
+    ).
+
+:- pred trial(uint::in, int::out, int::out) is cc_multi.
+
+trial(N, Time, MTime) :-
+    benchmark_det(fib, N, Res, 1, Time),
+    benchmark_det(mfib, N, MRes, 1, MTime),
+    require(unify(Res, MRes), "tabling produces wrong answer").
+
+:- pred fib(uint::in, uint::out) is det.
+
+fib(N, F) :-
+    ( N < 2u ->
+        F = 1u
+    ;
+        fib(N - 1u, F1),
+        fib(N - 2u, F2),
+        F = F1 + F2
+    ).
+
+:- pred mfib(uint::in, uint::out) is det.
+:- pragma memo(mfib/2).
+
+mfib(N, F) :-
+    ( N < 2u ->
+        F = 1u
+    ;
+        mfib(N - 1u, F1),
+        mfib(N - 2u, F2),
+        F = F1 + F2
+    ).


More information about the reviews mailing list