[mercury-users] Prolog DCGs Pattern Matching in Mercury

Goncalo Jorge Coelho e Silva l10454 at alunos.uevora.pt
Wed Apr 30 09:23:02 AEST 2003


Hi,

 I'll try to explain it a bit
better...

 Recognizing terms which are a mix
of atomic things (like strings) and 
variables, without using 'if the else'
or switches.

ie.:
 recognizing patterns in a list such as
		push_structure FunctorArity, Xi
 where Xi is a variable
		set_variable Xi
 where Xi is a variable, or
		set_value Vn
 where Vn is a variable

like [push_structure f/2, x2, set_variable x2, ...]


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

Right. For now, I'm not sure whether I can use
that for this issue. That would imply analising
char by char (or actually, lst of chars by list
of chars). DCGs in Prolog would enable me to
identify the whole pattern.


>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 

So, on the cases I mentioned, I'd be handling pattern
matching for a list of strings as input.
 Thing is, in the DCG clause, there's part of the
string in the input, I'd like to unify with a 
variable.


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

Recovering the code, and the explanation:
 
 % set_value Vn 
 % ------------ 
 % HEAP[H] <- Vm ;    sets a Vm on the Hth array position (HEAP)
 % H <- H+1;          adds the value of H by 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). 
 
This lookalike Mercury code
 would identify that the term of the head of the list
 is of the form 'set_value SOME_VALUE' (ie. set_value 3),
 put SOME_VALUE in the Hth position (ie. 3) of the array
 (Heap) and add H with 1. Done that, it would 
 continue it's pattern matching with the tail.

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

 No quite...
 Simply to produce code with side-effects on the 
main program, without building fancy auxiliary predicates,
just like PROLOG would enable.

So, to sum it all up, is this possible in Mercury?

Many thanks.
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
--------------------------------------------------------------------------



More information about the users mailing list