[m-rev.] for review: relation__write

Peter Ross peter.ross at miscrit.be
Thu Feb 14 20:50:31 AEDT 2002


Hi,

For anyone to review.

===================================================================


Estimated hours taken: 1
Branches: main

NEWS:
    Mention that relation__write has been added.

library/relation.m:
    Add the predicate relation__write which uses higher order predicates
    to output a relation.


Index: NEWS
===================================================================
RCS file: /home/mercury1/repository/mercury/NEWS,v
retrieving revision 1.241
diff -u -r1.241 NEWS
--- NEWS	13 Feb 2002 01:04:41 -0000	1.241
+++ NEWS	14 Feb 2002 09:49:05 -0000
@@ -344,7 +344,8 @@
 
 Changes to the standard library:
 
-* We've added new predicates map__foldl2, tree234__foldl2 and
+* We've added some new library predicates: assoc_list__keys_and_values,
+  list__map2, list__map3, map__foldl2, tree234__foldl2, relation__write,
   std_util__aggregate2, and builtin__promise_only_solution_io.
 
 * We've added function versions of std_util__solutions,
@@ -428,9 +429,6 @@
   to get the unwrapped value's functor, arity and arguments, then you can
   call "univ_value(Univ)" to extract the value before calling deconstruct.
   (Doing that also works in Mercury 0.9 and Mercury 0.10.)
-
-* We've addes some new library predicates: assoc_list__keys_and_values,
-  list__map2 and list__map3.
 
 * We've added func versions of the remaining preds in int.m that
   did not already have them.
Index: library/relation.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/relation.m,v
retrieving revision 1.25
diff -u -r1.25 relation.m
--- library/relation.m	12 Nov 2000 08:51:36 -0000	1.25
+++ library/relation.m	14 Feb 2002 09:49:06 -0000
@@ -18,7 +18,7 @@
 :- module relation.
 
 :- interface.
-:- import_module list, set, set_bbbtree, assoc_list.
+:- import_module list, set, set_bbbtree, assoc_list, io.
 
 :- type relation(T).
 
@@ -297,6 +297,17 @@
 
 :- func relation__rtc(relation(T)) = relation(T).
 
+	% relation__write(R, WriteNode, WriteEdge) will write a graph to
+	% the current output stream using the higher order predicate
+	% WriteNode to write out each node in the relation and WriteEdge
+	% to write each edge in the relation.  Each node is output
+	% followed by all the edges originating at that node, until all
+	% the nodes have been output.
+:- pred relation__write(relation(T)::in,
+		pred(T, io__state, io__state)::pred(in, di, uo) is det,
+		pred(T, T, io__state, io__state)::pred(in, in, di, uo) is det,
+		io__state::di, io__state::uo) is det.
+
 %------------------------------------------------------------------------------%
 
 :- implementation.
@@ -1111,6 +1122,36 @@
 relation__add_cartesian_product_2(K1, [K2 | Ks2], RTC0, RTC) :-
 	relation__add(RTC0, K1, K2, RTC1),
 	relation__add_cartesian_product_2(K1, Ks2, RTC1, RTC).
+
+%------------------------------------------------------------------------------%
+
+relation__write(Relation, WriteNode, WriteEdge) -->
+	{ Domain = to_sorted_list(relation__domain(Relation)) },
+	relation__write_nodes(Domain, Relation, WriteNode, WriteEdge).
+
+:- pred relation__write_nodes(list(T)::in, relation(T)::in,
+		pred(T, io__state, io__state)::pred(in, di, uo) is det,
+		pred(T, T, io__state, io__state)::pred(in, in, di, uo) is det,
+		io__state::di, io__state::uo) is det.
+
+relation__write_nodes([], _, _, _) --> [].
+relation__write_nodes([Node | Nodes], Relation, WriteNode, WriteEdge) -->
+	{ Children = to_sorted_list(lookup_from(Relation,
+			lookup_element(Relation, Node))) },
+	WriteNode(Node),
+	relation__write_children(Children, Node, Relation, WriteEdge),
+	relation__write_nodes(Nodes, Relation, WriteNode, WriteEdge).
+
+:- pred relation__write_children(list(relation_key)::in, T::in,
+		relation(T)::in,
+		pred(T, T, io__state, io__state)::pred(in, in, di, uo) is det,
+		io__state::di, io__state::uo) is det.
+
+relation__write_children([], _, _, _) --> [].
+relation__write_children([ChildKey | Children], Parent, Relation, WriteEdge) -->
+	{ Child = lookup_key(Relation, ChildKey) },
+	WriteEdge(Parent, Child),
+	relation__write_children(Children, Parent, Relation, WriteEdge).
 
 %------------------------------------------------------------------------------%
 %------------------------------------------------------------------------------%

--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list