[m-users.] Mercury Crash Course

Julian Fondren jfondren at minimaltype.com
Tue Aug 6 04:45:51 AEST 2019


On 2019-08-05 10:57, emacstheviking wrote:
>>>> - DCG syntax
>>> 
>>> You can probably skip those, as they have been all but superseded
>> by
>>> state variables.
>> 
>> What do you think about marking DCG syntax as deprecated and remove
>> it from the language after a while?
> 
> Can somebody explain how state variables supercede DCG sytax
> please...I vaguaely get the destructive in / unique out concept but I
> am confused now!

Neither requires di/uo modes.

Consider:

   :- module nonunique.
   :- interface.
   :- import_module io.
   :- pred main(io::di, io::uo) is det.
   :- implementation.
   :- import_module int.

   main(!IO) :-
       statevars(1, N),
       dcg(1, M),
       io.print_line({N, M}, !IO).

   :- pred statevars(int::in, int::out) is det.
   statevars(!N) :-
       !:N = !.N + 1,
       ( if !.N > 10 then
           true
       else
           statevars(!N)
       ).

   :- pred dcg(int::in, int::out) is det.
   dcg -->
       statevars,
       ( if { true } then
           halve
       else
           { true }
       ).

   :- pred halve(int::in, int::out) is det.
   halve(N, M) :- N / 2 = M.

1. DCG syntax gives you a single pair of threaded values,
    but you can have as many state variables as you want.

2. DCG syntax requires that you {escape} goals that shouldn't
    be transformed. State variables you just don't place as
    parameters to such goals.

3. State variables make it easy to refer to state itself
    within the predicates using them.

4. it's useful to have unpaired !:X or !.X parameters, which
    you can't do with dcg syntax.


More information about the users mailing list