[mercury-users] Help: generic tree predicates; modules

Ralph Becket rafe at cs.mu.OZ.AU
Thu May 9 16:02:56 AEST 2002


Bob McKay, Thursday,  9 May 2002:
> Hi folks; I'm just getting my feet wet(ter) with mercury, and I have
> a couple of questions which I can solve, but maybe there are easier
> ways (this is in the context of a generic evolutionary computation
> system that Raymee Chau and Daryl Essam have previously posted about).
> 
> .I'd like to be able to write some generic tree manipulation
> 	predicates, such as count_nodes(T,N), which would be passed
> 	an instantiated tree T, and would return the number of nodes.
> 	And I need it to be determinate. This is straightforward if I
> 	know the set of functors, but I want this to be part of a library
> 	for a GP system, so the set of functors won't be known until the
> 	time that a particular problem is being defined. Is there a
> 	straightforward way to do this? The way I can half-see, defining
> 	a higher-order predicate and passing it a list of allowed
> 	functors, looks a bit scary...

If you don't want to pass higher order terms around explicitly (it's
really very natural) you could always define a type class

:- typeclass countable(T) where [ func count_nodes(T) = int ].

and then ensure that you provide instance implementations for each of the
types you're interested in.

Another solution is to use Mercury's run-time type information
facilities to deconstruct terms and count the number of nodes in the
representation.  This will work for very simple trees (e.g. where the
arguments of a node are either other tree constructors or simple scalar
values which may be counted as leaf nodes), but is a bit of a hack and
may lead to pain in the end.

- 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