[mercury-users] bintree_set iterators? read_term to type conversions?

Ralph Becket rafe at cs.mu.OZ.AU
Wed Feb 15 13:14:21 AEDT 2006


Hi Doug,

doug.auclair at logicaltypes.com, Tuesday, 14 February 2006:
> Hi!  It appears that bintree_set has only one form of iteration (using
> member/2 and delete/3): left-to-right traversal.

You could also use bintree_set.to_sorted_list/1.  Or you could use
set_tree234 which has a fold function.

> Unfortunately for
> me, my application requires top-to-bottom traversal.  Is there a way
> to achieve this? 

What exactly do you mean by top-to-bottom traversal?  Do you mean
a "root, left branch, right branch" order traversal?

If so, why do you want that particular order?  The order in which items
in the set would be visited would depend upon a combination of their
relative order and the order in which they were added to the set.

> When I tried to create an iterator on the type (which appears to
> devolve to the functors tree/4 and empty/0), the compiler rejected my
> program -- these type discriminators are not accessable, it appears.

That's right: bintree_set/1 is exported as an abstract type, which was a
deliberate design decision.

> So next I wrote the representation out and then piped it into a
> program that read_term/3 off of stdin.

You could use plain io.read/3, but either way, this sounds like the hard
way of achieving what you want...

> BUT, when I called try_term_to_type/2, that predicate resulted in an
> error(type_error/1).

Can you show us the program?  It's hard to work out where the bug is
otherwise.

> So, then I declared a type:
> 
> :- type binary_tree ---> tree(string, unit, binary_tree, binary_tree);
> empty.

Why the unit type there?

> Still the same error/1 from the try_term_to_type/2 predicate, but I
> know the type is okay, because I can write/1 out a hardcoded instance:
> 
> write(tree("ar", unit, tree("ak", unit, empty, tree("al", unit, empty,
> empty)), tree("au", unit, empty, tree("az", unit, empty, empty)))).
> 
> Neither the term coming from stdin nor the example term above have any
> variables: both are entirely ground.
> 
> So: user controlled iteration over binary trees? Converting terms to
> declared types?  How can I do these things?

I think we'd have to see your code to answer your questions.

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