[mercury-users] Prolog DCGs Pattern Matching in Mercury
Ondrej Bojar
oboj7042 at ss1000.ms.mff.cuni.cz
Tue Apr 29 19:41:27 AEST 2003
On Tue, 29 Apr 2003, Goncalo Jorge Coelho e Silva wrote:
> >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.
>
> hmmm, I'm not sure what you meant here.
This means that typing
"foo"
into a Prolog code is by the Prolog internally interpreted as
['f' | ['o' | ['o' | [] ] ] ]
i.e. a list of characters; another shortcut notation for this in Prolog
whould be ['f', 'o', 'o'].
On the other hand, typing
"foo"
in Mercury is interpreted as
"foo"
i.e. the string itself; represented as a null-terminated sequence of
bytes.
There are handy predicates in the library string to convert strings to
lists of chars and vice versa: string__from_char_list,
string__from_rev_char_list and string__to_char_list.
> Do you mean I won't be able to handle DGC
> pattern matching arguments, that might be
> a mix of strings and variables?
I do not exactly understand you what do you mean by a 'mix of strings and
variables'. In Mercury, all the terms (including values of variables or
arguments) must be well typed and this type must be clearly stated in the
:- pred declaration. In order to work with values "of more possible
types", for example strings, chars and integers, you have to create a
"wrapping" type such as:
:- type string__poly_type --->
f(float)
; i(int)
; s(string)
; c(char).
(See the documentation of the library string and for example the predicate
string__format for a sample usage of this type.)
> To give a specific example of what I'd
> like to do, look at this very simplistic
> approach (where the top lines are
> pseudo-code and the bottom ones, would be
> close Mercury syntax):
>
> % set_value Vn
> % ------------
> % HEAP[H] <- Vm ;
> % H <- H+1;
>
> %code_gen([set_value V | Tail],Heap, H) -->
> % {
> % array__array_set(Heap,H,V),
> % H_New = H + 1,
> % }
> % code_gen(Tail,H_New,Heap).
>
> So, in a nutshell, I'd like to iterate
> a list or an array, and be able to generate
> a specific set of instructions for that
> pattern... no necessarily IO instructions
I'm sorry, I do not understand what do you want to do neither from
the pseudo-code, nor from the inteded mercury code nor from the
explanation.
I expect you want something like a predicate that walks a given list and
replaces all the input items with some output items. For simplicity, I
assume you want a one-to-one conversion.
Let's create the conversion predicate first:
:- pred convert(string::in, string::out) is det.
convert(In, Out) :-
if In = "foo"
then Out = "found_foo"
else
if In = "bar"
then Out = "found_bar"
else Out = "something extra found".
And now the predicate that coverts a whole list:
:- pred convert_list(list(string)::in, list(string)::out) is det.
convert_list([], []).
convert_list([InItem | InTail], [OutItem | OutTail]) :-
convert(InItem, OutItem),
convert_list(InTail, OutTail).
You could also use the library predicate list__map to create
convert_list/2 simply:
convert_list(InList, OutList) :-
list__map(convert, InList, OutList).
But I'm probably not answering your question...
O.
> (OK, my last post example was not so fortunate
> with the line:
> "{nl, write(' matched "name(_something_)" '), nl}"
>
> ... in fact, no IO at all.
>
> Thanks a lot.
>
> Cheers,
> Goncalo
>
> --------------------------------------------------------------------------
> 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
> --------------------------------------------------------------------------
>
--------------------------------------------------------------------------
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