forked from Mercury-Language/mercury
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mercury_lib.m
72 lines (54 loc) · 2.21 KB
/
mercury_lib.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
% This source file is hereby placed in the public domain. -fjh (the author).
%-----------------------------------------------------------------------------%
:- module mercury_lib.
:- interface.
% A Mercury predicate with multiple modes.
%
:- pred foo(int).
:- mode foo(in) is semidet.
:- mode foo(out) is multi.
% A Mercury function with multiple modes.
%
:- func bar(int) = int.
:- mode bar(in) = out is det.
:- mode bar(out) = in is det.
:- mode bar(in) = in is semidet.
% A semidet (i.e. partial) Mercury function.
:- func baz(int) = int.
:- mode baz(in) = out is semidet.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module int.
:- import_module list.
:- import_module solutions.
% well, this is just a silly example...
foo(42).
foo(53).
foo(197).
bar(X) = X + 1.
baz(1) = 9.
baz(2) = 16.
baz(3) = 27.
%-----------------------------------------------------------------------------%
% The following code provides provides access to the Mercury predicate foo
% from Java code.
:- pragma foreign_export("Java", foo(in), "foo_test").
:- pragma foreign_export("Java", bar(in) = out, "bar").
:- pragma foreign_export("Java", bar(in) = in, "bar_test").
:- pragma foreign_export("Java", bar(out) = in, "bar_inverse").
:- pragma foreign_export("Java", baz(in) = out, "baz").
% The nondet mode of `foo' cannot be exported directly with the
% Mercury/Java interface. To get all solutions, must define a predicate
% which returns all the solutions of foo, and export it to Java. We give
% it the name foo_list() in Java.
:- pred all_foos(list(int)::out) is det.
:- pragma foreign_export("Java", all_foos(out), "foo_list").
all_foos(L) :- solutions((pred(X::out) is multi :- foo(X)), L).
% If we just want one solution, and don't care which one, then we can
% export a `cc_multi' (committed-choice nondeterminism) version of `foo'.
% We give it the name one_foo().
:- pred cc_foo(int::out) is cc_multi.
:- pragma foreign_export("Java", cc_foo(out), "one_foo").
cc_foo(X) :- foo(X).
%-----------------------------------------------------------------------------%