40 lines
904 B
Plaintext
40 lines
904 B
Plaintext
:- module evenodd.
|
|
:- interface.
|
|
:- import_module io.
|
|
:- pred main(io::di, io::uo) is det.
|
|
:- implementation.
|
|
:- import_module list, string.
|
|
|
|
:- type coin
|
|
---> heads
|
|
; tails.
|
|
|
|
:- inst heads for coin/0
|
|
---> heads.
|
|
:- inst tails for coin/0
|
|
---> tails.
|
|
|
|
main(!IO) :-
|
|
(if heads(heads, N) then
|
|
print_heads(N, !IO)
|
|
else true).
|
|
|
|
:- pred print_heads(coin::in(heads), io::di, io::uo) is det.
|
|
print_heads(_, !IO) :-
|
|
io.write_string("heads\n", !IO).
|
|
|
|
:- pred print_tails(coin::in(tails), io::di, io::uo) is det.
|
|
print_tails(_, !IO) :-
|
|
io.write_string("tails\n", !IO).
|
|
|
|
:- pragma foreign_export_enum("C", coin/0, [prefix("COIN_"), uppercase]).
|
|
|
|
:- pred heads(coin::in, coin::out(heads)) is semidet.
|
|
:- pragma foreign_proc("C",
|
|
heads(N::in, M::out(heads)),
|
|
[promise_pure, will_not_call_mercury],
|
|
"
|
|
M = N;
|
|
SUCCESS_INDICATOR = N == COIN_HEADS;
|
|
").
|