Skip to content

Instantly share code, notes, and snippets.

@bartosz-witkowski
Last active July 12, 2020 03:09
Show Gist options
  • Save bartosz-witkowski/8511389 to your computer and use it in GitHub Desktop.
Save bartosz-witkowski/8511389 to your computer and use it in GitHub Desktop.
Example of using the mercury store.
:- module store_example.
:- interface.
:- import_module store, io.
:- pred main(io::di, io::uo) is det.
:- some [S] pred new_counter(int_var(S)::out, S::uo) is det => store(S).
:- implementation.
:- import_module store, string, int.
:- type int_var(S) == generic_mutvar(int, S).
:- pred loop(int_var(S)::in, S::di, S::uo, io::di, io::uo) is det <= store(S).
loop(Var, !Store, !IO) :-
store.get_mutvar(Var, X, !Store),
print("X: " ++ string.from_int(X) ++ "\n", !IO),
(if X < 10 then
NewX = X + 1,
store.set_mutvar(Var, NewX, !Store),
loop(Var, !Store, !IO)
else
true).
new_counter(Var, Store) :-
store.init(Store0),
store.new_mutvar(0, Var, Store0, Store).
main(!IO) :-
new_counter(Var, Store0),
loop(Var, Store0, _, !IO).
/*
# compiling using the default grade causes an error:
# """
# Making Mercury/int3s/store_example.int3
# Making Mercury/ints/store_example.int
# Making Mercury/cs/store_example.c
# Uncaught Mercury exception:
# Software Error: ll_backend.llds_out.llds_out_data: predicate `ll_backend.llds_out.llds_out_data.output_lval'/4: Unexpected: stack var out of range
# Stack dump not available in this grade.
# ** Error making `Mercury/cs/store_example.c'.
# """
#
# so we use the asm_fast.gc.profdeep grade.
#
# I have no idea why this works, and the compilation fails for the default
# grade.
$ mmc --make --grade asm_fast.gc.profdeep store_exampl
$ ./store_example
X: 0
X: 1
X: 2
X: 3
X: 4
X: 5
X: 6
X: 7
X: 8
X: 9
X: 10
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment