Skip to content

Commit

Permalink
CHANGED: Rewrite of the counter module API.
Browse files Browse the repository at this point in the history
Changes in module `counter`:

- Renamed `create_counter/1` to `counter_create/1`.
- Renamed `increment_counter/1` to `counter_increment/1`.
- Added `counter_increment/2`.
- Reimplemented by using module `nb_ext`.

Additions to module `nb_ext`:

- Added `nb_increment/3`.
- Added `nb_plus/4`.
  • Loading branch information
wouterbeek committed Apr 3, 2021
1 parent 30d393c commit 87dd2df
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
30 changes: 17 additions & 13 deletions prolog/counter.pl
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
:- module(
counter,
[
counter_value/2, % +Counter, ?N
create_counter/1, % -Counter
increment_counter/1 % +Counter
counter_create/1, % -Counter
counter_increment/1, % +Counter
counter_increment/2, % +Counter, -Value
counter_value/2 % +Counter, ?Value
]
).

/** <module> A simple counter
*/

:- use_module(library(nb_ext)).



%! counter_create(-Counter:compound) is det.

%! counter_value(+Counter:compound, +N:nonneg) is semidet.
%! counter_value(+Counter:compound, -N:nonneg) is det.
counter_create(counter(0)).

counter_value(counter(N), N).


%! counter_increment(+Counter:compound) is det.
%! counter_increment(+Counter:compound, -Value:nonneg) is det.

%! create_counter(-Counter:compound) is det.
counter_increment(Counter) :-
counter_increment(Counter, _).

create_counter(counter(0)).

counter_increment(Counter, Value) :-
nb_increment(Counter, 1, Value).


%! increment_counter(+Counter:compound) is det.

increment_counter(Counter) :-
arg(1, Counter, N1),
N2 is N1 + 1,
nb_setarg(1, Counter, N2).
%! counter_value(+Counter:compound, +N:nonneg) is semidet.
%! counter_value(+Counter:compound, -N:nonneg) is det.

counter_value(counter(N), N).
29 changes: 23 additions & 6 deletions prolog/nb_ext.pl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
nb_ext,
[
nb_increment/2, % +State, +Index
nb_plus/3 % +State, +Index, +Value
nb_increment/3, % +State, +Index, -Value
nb_plus/3, % +State, +Index, +Increment
nb_plus/4 % +State, +Index, +Increment, -Value
]
).

Expand All @@ -12,20 +14,35 @@
*/


:- use_module(library(clpfd)).



%! nb_increment(+State:compound, +Index:positive_integer) is det.

nb_increment(State, Index) :-
nb_plus(State, Index, 1).
nb_increment(State, Index, _).


%! nb_increment(+State:compound, +Index:positive_integer, -Value:nonneg) is det.

nb_increment(State, Index, Value) :-
nb_plus(State, Index, 1, Value).



%! nb_plus(+State:compound, +Index:positive_integer, +Increment:number) is det.

nb_plus(State, Index, Increment) :-
nb_plus(State, Index, Increment, _).


%! nb_plus(+State:compound, +Index:positive_integer, +Value:number) is det.
%! nb_plus(+State:compound,
%! +Index:positive_integer,
%! +Increment:number,
%! -Value:nonneg) is det.

nb_plus(State, Index, Value) :-
nb_plus(State, Index, Increment, Value1) :-
arg(Index, State, Value1),
Value2 is Value1 + Value,
Value2 #= Value1 + Increment,
nb_setarg(Index, State, Value2).

0 comments on commit 87dd2df

Please sign in to comment.