Files
mercury/tests/hard_coded/impure_foreign.m
Peter Wang 88047bbb45 Delete Erlang from tests.
tests/general/float_test.exp3:
tests/general/float_test.m:
tests/general/read_dir_regression.exp4:
tests/general/read_dir_regression.m:
tests/hard_coded/remove_file.exp2:
tests/hard_coded/remove_file.m:
    Delete Erlang backend specific expected outputs.

tests/hard_coded/Mmakefile:
tests/hard_coded/erlang_deconstruct.exp:
tests/hard_coded/erlang_deconstruct.m:
tests/hard_coded/existential_list.exp:
tests/hard_coded/existential_list.m:
tests/valid/Mmakefile:
tests/valid/erl_ite_vars.m:
tests/valid/zf_erlang_bug.m:
    Delete erlang target specific tests.

tests/*:
    Delete Erlang foreign procs and foreign types.
2020-10-27 11:10:11 +11:00

103 lines
2.1 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ts=4 sw=4 et ft=mercury
%---------------------------------------------------------------------------%
:- module impure_foreign.
:- interface.
:- import_module io.
:- impure pred main(io::di, io::uo) is det.
:- implementation.
:- import_module require.
main(!IO) :-
% Inlining this call forces recomputation of purity for main/2
% (because of the `:- pragma promise_pure').
% In some versions of this compiler, this recomputation would
% erroneously infer that the inlined calls to incr/1 below
% were `pure'. Duplicate call elimination would then remove
% all but one of them.
unsafe_get(Val0),
io.write_int(Val0, !IO),
io.nl(!IO),
impure incr(_),
impure incr(_),
impure incr(_),
semipure get(Val),
io.write_int(Val, !IO),
io.nl(!IO).
:- pragma foreign_decl("C",
"
int counter;
").
:- pragma foreign_code("C",
"
int counter = 1;
").
:- pragma foreign_code("C#", "static int counter = 1;").
:- pragma foreign_code("Java", "static int counter = 1;").
:- impure pred incr(int::out) is det.
incr(_::out) :-
error("incr/1 called for language other than C").
:- pragma foreign_proc("C",
incr(Val::out),
[will_not_call_mercury],
"
counter++; Val = counter;
").
:- pragma foreign_proc("C#",
incr(Val::out),
[will_not_call_mercury],
"
counter++; Val = counter;
").
:- pragma foreign_proc("Java",
incr(Val::out),
[will_not_call_mercury],
"
counter++; Val = counter;
").
:- semipure pred get(int::out) is det.
get(_::out) :-
error("get/1 called for language other than C").
:- pragma foreign_proc("C",
get(Val::out),
[will_not_call_mercury, promise_semipure],
"
Val = counter
").
:- pragma foreign_proc("C#",
get(Val::out),
[will_not_call_mercury, promise_semipure],
"
Val = counter;
").
:- pragma foreign_proc("Java",
get(Val::out),
[will_not_call_mercury, promise_semipure],
"
Val = counter;
").
:- pred unsafe_get(int::out) is det.
:- pragma promise_pure(unsafe_get/1).
unsafe_get(X) :-
semipure get(X).