[mercury-users] Prolog DCGs Pattern Matching in Mercury

Ralph Becket rafe at cs.mu.OZ.AU
Mon Apr 28 12:58:31 AEST 2003

Goncalo Jorge Coelho e Silva, Sunday, 27 April 2003:
>  I've been trying to explore how to use DCGs
> in Mercury, for the same purpose as they are
> used in Prolog's pattern matching. 

DCGs are the same in Mercury and Prolog.  Strings in Mercury, however,
are primitive whereas in Prolog they are lists of character codes, which
is one of the reasons why your program won't work.

>  Prolog example:
>  --------------
> gce(name(N)) --> {nl, write(' matched "name(_something_)" '), nl},

- Mercury only allows you to perform IO if you have an IO state;
- IO states are unique;
- therefore they can only be used in deterministic code;
- therefore they cannot be used in any code that may fail or backtrack,
  such as the rule above.

> :- pred main(io__state, io__state).
> :- mode main(di, uo) is det.
> %:- mode main(di, uo) is semidet.

main/2 must be det (or cc_multi - don't worry about that one for now.)

> :- pred seee(string, io__state, io__state).
> :- mode seee(in, di, uo)is semidet.

seee/3 is semidet and therefore it is an error for it to take a
unique input.

>  In this test example, I was trying to catch 
> an occurence of the string "faraway" and 
> print the sentence "Never ending!".

You need an if-then-else goal:

main(IO0, IO) :-
	( if seee("foo")
	  then io__write_string("bar", IO0, IO)
	  else IO = IO0

and get rid of the two IO state arguments to seee/3.

- Ralph
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