[mercury-users] Handy Programming tool for Mercury.
Ralph Becket
rwab1 at cam.sri.com
Tue May 11 21:53:06 AEST 1999
I've written an Awk script, state2m, to simplify the coding up of
`state containers' in Mercury (and Prolog, if you're that way
inclined). By state container I mean a type that encapsulates some
bit of state and which comes with all the handy boiler-plate to access
and update the various members.
For example, piping the following through state2m
* type foo(T)
* a int
* b char readonly
* c string mutable
* d T private
* typespec foo
* predspec foo
* implementation foo
you get
% * type foo(T)
% * a int
% * b char readonly
% * c string mutable
% * d T private
% * typespec foo
:- type foo(T) --->
foo(
int,
char,
string,
T
).
% * predspec foo
:- pred a(foo, int).
:- mode a(in, out) is det.
:- pred set_a(foo, int, foo).
:- mode set_a(in, in, out) is det.
:- pred b(foo, char).
:- mode b(in, out) is det.
:- pred c(foo, string).
:- mode c(in, out) is det.
:- pred set_c(foo, string, foo).
:- mode set_c(in, in, out) is det.
:- pred chg_c(foo, pred(string, string), foo).
:- mode chg_c(in, pred(in, out) is det, out) is det.
% * implementation foo
a(foo(X, _, _, _), X).
set_a(foo(_, X1, X2, X3), X, foo(X, X1, X2, X3)).
:- pred set_b(foo, char, foo).
:- mode set_b(in, in, out) is det.
b(foo(_, X, _, _), X).
set_b(foo(X0, _, X2, X3), X, foo(X0, X, X2, X3)).
c(foo(_, _, X, _), X).
set_c(foo(X0, X1, _, X3), X, foo(X0, X1, X, X3)).
chg_c(foo(X0, X1, X, X3), F, foo(X0, X1, Y, X3)) :-
F(X, Y).
:- pred d(foo, T).
:- mode d(in, out) is det.
:- pred set_d(foo, T, foo).
:- mode set_d(in, in, out) is det.
d(foo(_, _, _, X), X).
set_d(foo(X0, X1, X2, _), X, foo(X0, X1, X2, X)).
You can also get it to supply func versions instead. Processing
* type bar
* x int mutable func
* typespec bar
* predspec bar
* implementation bar
we get
% * type bar
% * x int mutable func
% * typespec bar
:- type bar --->
bar(
int
).
% * predspec bar
:- func x(bar) = int.
:- func set_x(bar, int) = bar.
:- func chg_x(bar, func(int) = int) = bar.
% * implementation bar
x(bar(X)) = X.
set_x(bar(_), X) = bar(X).
chg_x(bar(X), F) = bar(F(X)).
I hope people will find this useful. The development team is welcome
to add it to the distribution if they wish. There is plenty of
documentation at the top of the file.
Cheers,
Ralph
--
Ralph Becket | rwab1 at cam.sri.com | http://www.cam.sri.com/people/becket.html
--------------------------------------------------------------------------
mercury-users mailing list
post: mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe: Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------
More information about the users
mailing list