Index: src/GenPoly/Box.cc
===================================================================
--- src/GenPoly/Box.cc	(revision d2ded3e7b80d4d46102610712560152e44975503)
+++ src/GenPoly/Box.cc	(revision e56cfdb005c6b37a022fa1d268ad747523f7d2c4)
@@ -9,7 +9,7 @@
 // Author           : Richard C. Bilson
 // Created On       : Mon May 18 07:44:20 2015
-// Last Modified By : Rob Schluntz
-// Last Modified On : Tue Aug 11 16:22:35 2015
-// Update Count     : 89
+// Last Modified By : Peter A. Buhr
+// Last Modified On : Thu Nov 19 17:40:51 2015
+// Update Count     : 133
 //
 
@@ -47,4 +47,6 @@
 	namespace {
 		const std::list<Label> noLabels;
+
+		FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars );
 
 		class Pass1 : public PolyMutator {
@@ -78,5 +80,5 @@
 			ObjectDecl *makeTemporary( Type *type );
 
-			typedef std::map< std::string, FunctionDecl *> AdapterMap;
+			typedef std::map< std::string, DeclarationWithType *> AdapterMap;
 			std::map< std::string, DeclarationWithType *> assignOps;
 			std::stack< AdapterMap > adapters;
@@ -144,37 +146,7 @@
 		}
 
-		bool isPolyRet( FunctionType *function, std::string &name, const TyVarMap &otherTyVars ) {
-			bool doTransform = false;
-			if ( ! function->get_returnVals().empty() ) {
-				if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( function->get_returnVals().front()->get_type() ) ) {
-	
-					// figure out if the return type is specified by a type parameter
-					for ( std::list< TypeDecl *>::const_iterator tyVar = function->get_forall().begin(); tyVar != function->get_forall().end(); ++tyVar ) {
-						if ( (*tyVar)->get_name() == typeInst->get_name() ) {
-							doTransform = true;
-							name = typeInst->get_name();
-							break;
-						} // if
-					} // for
-					if ( ! doTransform && otherTyVars.find( typeInst->get_name() ) != otherTyVars.end() ) {
-						doTransform = true;
-					} // if
-				} // if
-			} // if
-			return doTransform;
-		}
-
-		bool isPolyRet( FunctionType *function, std::string &name ) {
-			TyVarMap dummyTyVars;
-			return isPolyRet( function, name, dummyTyVars );
-		}
-
-		bool isPolyRet( FunctionType *function, const TyVarMap &otherTyVars ) {
-			std::string dummyString;
-			return isPolyRet( function, dummyString, otherTyVars );
-		}
-
 		Pass1::Pass1()
 			: useRetval( false ), tempNamer( "_temp" ) {
+			adapters.push(AdapterMap());
 		}
 
@@ -210,9 +182,11 @@
 
 		DeclarationWithType *Pass1::mutate( FunctionDecl *functionDecl ) {
-			if ( functionDecl->get_statements() ) {
+			if ( functionDecl->get_statements() ) {		// empty routine body ?
+				doBeginScope();
 				TyVarMap oldtyVars = scopeTyVars;
 				DeclarationWithType *oldRetval = retval;
 				bool oldUseRetval = useRetval;
-	
+
+				// process polymorphic return value
 				retval = 0;
 				std::string typeName;
@@ -227,19 +201,44 @@
 				} // if
 	
-				scopeTyVars.clear();
-///     std::cerr << "clear\n";
+				FunctionType *functionType = functionDecl->get_functionType();
 				makeTyVarMap( functionDecl->get_functionType(), scopeTyVars );
 				findAssignOps( functionDecl->get_functionType()->get_forall() );
+
+				std::list< DeclarationWithType *> &paramList = functionType->get_parameters();
+				std::list< FunctionType *> functions;
+				for ( std::list< TypeDecl *>::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) {
+					for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->get_assertions().begin(); assert != (*tyVar)->get_assertions().end(); ++assert ) {
+						findFunction( (*assert)->get_type(), functions, scopeTyVars, needsAdapter );
+					} // for
+				} // for
+				for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) {
+					findFunction( (*arg)->get_type(), functions, scopeTyVars, needsAdapter );
+				} // for
+				AdapterMap & adapters = Pass1::adapters.top();
+				for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) {
+					std::string mangleName = SymTab::Mangler::mangle( *funType );
+					if ( isPolyRet( *funType, scopeTyVars ) ) {
+						// if the return type involved polymorphic types, then the adapter will need to take those
+						// polymorphic types as pointers. Therefore, there can be two different functions with the same
+						// mangled name, so we need the mangled names to be different.
+						mangleName += "polyret_";
+					} // if
+					if ( adapters.find( mangleName ) == adapters.end() ) {
+						std::string adapterName = makeAdapterName( mangleName );
+						adapters.insert( std::pair< std::string, DeclarationWithType *>( mangleName, new ObjectDecl( adapterName, DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), makeAdapterType( *funType, scopeTyVars ) ), 0 ) ) );
+					} // if
+				} // for
+
 				functionDecl->set_statements( functionDecl->get_statements()->acceptMutator( *this ) );
   
 				scopeTyVars = oldtyVars;
-///     std::cerr << "end FunctionDecl: ";
-///     for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {
-///       std::cerr << i->first << " ";
-///     }
-///     std::cerr << "\n";
+				// std::cerr << "end FunctionDecl: ";
+				// for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {
+				// 	std::cerr << i->first << " ";
+				// }
+				// std::cerr << "\n";
 				retval = oldRetval;
 				useRetval = oldUseRetval;
-				// doEndScope();
+				doEndScope();
 			} // if
 			return functionDecl;
@@ -349,4 +348,7 @@
 			} // if
 			std::string mangleName = SymTab::Mangler::mangle( function );
+			if ( isPolyRet( function, tyVars ) ) {
+				mangleName += "polyret_";
+			} // if
 			std::string adapterName = makeAdapterName( mangleName );
 
@@ -457,8 +459,10 @@
 ///       return new CastExpr( new VariableExpr( param ), arg->get_type()->clone() );
 ///     } else {
-				UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
-				deref->get_args().push_back( new CastExpr( new VariableExpr( param ), new PointerType( Type::Qualifiers(), arg->get_type()->clone() ) ) );
-				deref->get_results().push_back( arg->get_type()->clone() );
-				return deref;
+				if ( dynamic_cast<TypeInstType *>(arg->get_type()) == NULL ) {
+					UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
+					deref->get_args().push_back( new CastExpr( new VariableExpr( param ), new PointerType( Type::Qualifiers(), arg->get_type()->clone() ) ) );
+					deref->get_results().push_back( arg->get_type()->clone() );
+					return deref;
+				} // if
 ///     }
 			} // if
@@ -544,7 +548,6 @@
 			} // for
 
-			// parameter function types for which an appropriate adapter has been generated.
-			// we cannot use the types after applying substitutions, since two different 
-			// parameter types may be unified to the same type
+			// parameter function types for which an appropriate adapter has been generated.  we cannot use the types
+			// after applying substitutions, since two different parameter types may be unified to the same type
 			std::set< std::string > adaptersDone;
 
@@ -554,47 +557,33 @@
 				std::string mangleName = SymTab::Mangler::mangle( realFunction );
 
-				// only attempt to create an adapter or pass one as a parameter if we haven't 
-				// already done so for this pre-substitution parameter function type.
+				// only attempt to create an adapter or pass one as a parameter if we haven't already done so for this
+				// pre-substitution parameter function type.
 				if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) {
-					std::string mangleName = SymTab::Mangler::mangle( realFunction );
 					adaptersDone.insert( adaptersDone.begin(), mangleName );
 					
-					// apply substitution to type variables to figure out what the 
-					// adapter's type should look like
+					// apply substitution to type variables to figure out what the adapter's type should look like
 					assert( env );
 					env->apply( realFunction );
 					mangleName = SymTab::Mangler::mangle( realFunction );
 
-					if ( needsAdapter( realFunction, exprTyVars, true ) ) {
-						// the function still contains type variables, which means we are in a polymorphic
-						// context and the adapter function is a parameter - call the parameter and don't 
-						// create a new adapter.
-						appExpr->get_args().push_front( new NameExpr( makeAdapterName ( mangleName ) ) );
-					} else {
-						if ( isPolyRet( originalFunction, exprTyVars ) ) {
-							// if the return type involved polymorphic types, then
-							// the adapter will need to take those polymorphic types
-							// as pointers. Therefore, there can be two different
-							// functions with the same mangled name, so we need two adapter map
-							// stacks and also we need the mangled names to be different.
-							mangleName += "polyret_";
-						}
-
-						AdapterMap & adapters = Pass1::adapters.top();
-						AdapterMap::iterator adapter = adapters.find( mangleName );
-						if ( adapter == adapters.end() ) {
-							// adapter has not been created yet in the current scope, so define it
-							FunctionDecl *newAdapter = makeAdapter( *funType, realFunction, mangleName, exprTyVars );
-							adapter = adapters.insert( adapters.begin(), std::pair< std::string, FunctionDecl *>( mangleName, newAdapter ) );
-							stmtsToAdd.push_back( new DeclStmt( noLabels, newAdapter ) );
-						} // if
-						assert( adapter != adapters.end() );
-
-						// add the appropriate adapter as a parameter
-						appExpr->get_args().push_front( new VariableExpr( adapter->second ) );
-					} // if
+					if ( isPolyRet( originalFunction, exprTyVars ) ) {
+						mangleName += "polyret_";
+					} // if
+
+					AdapterMap & adapters = Pass1::adapters.top();
+					AdapterMap::iterator adapter = adapters.find( mangleName );
+					if ( adapter == adapters.end() ) {
+						// adapter has not been created yet in the current scope, so define it
+						FunctionDecl *newAdapter = makeAdapter( *funType, realFunction, mangleName, exprTyVars );
+						adapter = adapters.insert( adapters.begin(), std::pair< std::string, DeclarationWithType *>( mangleName, newAdapter ) );
+						stmtsToAdd.push_back( new DeclStmt( noLabels, newAdapter ) );
+					} // if
+					assert( adapter != adapters.end() );
+
+					// add the appropriate adapter as a parameter
+					appExpr->get_args().push_front( new VariableExpr( adapter->second ) );
 				} // if
 			} // for
-		}
+		} // passAdapters
 
 		TypeInstType *isPolyPtr( Type *type, const TypeSubstitution *env, const TyVarMap &tyVars ) {
@@ -769,9 +758,9 @@
 
 		Expression *Pass1::mutate( ApplicationExpr *appExpr ) {
-///     std::cerr << "mutate appExpr: ";
-///     for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {
-///       std::cerr << i->first << " ";
-///     }
-///     std::cerr << "\n";
+			// std::cerr << "mutate appExpr: ";
+			// for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {
+			// 	std::cerr << i->first << " ";
+			// }
+			// std::cerr << "\n";
 			bool oldUseRetval = useRetval;
 			useRetval = false;
@@ -799,9 +788,9 @@
 				ret = addPolyRetParam( appExpr, function, typeName, arg );
 			} else if ( needsAdapter( function, scopeTyVars ) ) {
-///     std::cerr << "needs adapter: ";
-///     for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {
-///       std::cerr << i->first << " ";
-///     }
-///     std::cerr << "\n";
+				// std::cerr << "needs adapter: ";
+				// for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {
+				// 	std::cerr << i->first << " ";
+				// }
+				// std::cerr << "\n";
 				// change the application so it calls the adapter rather than the passed function
 				ret = applyAdapter( appExpr, function, arg, scopeTyVars );
@@ -913,5 +902,5 @@
 			// actually, maybe this could (should?) push
 			// a copy of the current map
-			adapters.push(AdapterMap());
+			adapters.push(adapters.top());
 		}
 
@@ -935,4 +924,7 @@
 			for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) {
 				std::string mangleName = SymTab::Mangler::mangle( *funType );
+				if ( isPolyRet( *funType, scopeTyVars ) ) {
+					mangleName += "polyret_";
+				} // if
 				if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) {
 					std::string adapterName = makeAdapterName( mangleName );
@@ -1000,4 +992,5 @@
 			for ( std::list< TypeDecl *>::const_iterator tyParm = funcType->get_forall().begin(); tyParm != funcType->get_forall().end(); ++tyParm ) {
 				ObjectDecl *thisParm;
+				// add all size parameters to parameter list
 				if ( (*tyParm)->get_kind() == TypeDecl::Any ) {
 					thisParm = newObj->clone();
@@ -1006,4 +999,5 @@
 					++last;
 				}
+				// move all assertions into parameter list
 				for ( std::list< DeclarationWithType *>::iterator assert = (*tyParm)->get_assertions().begin(); assert != (*tyParm)->get_assertions().end(); ++assert ) {
 ///      *assert = (*assert)->acceptMutator( *this );
Index: src/GenPoly/Box.h
===================================================================
--- src/GenPoly/Box.h	(revision d2ded3e7b80d4d46102610712560152e44975503)
+++ src/GenPoly/Box.h	(revision e56cfdb005c6b37a022fa1d268ad747523f7d2c4)
@@ -10,6 +10,6 @@
 // Created On       : Mon May 18 07:44:20 2015
 // Last Modified By : Peter A. Buhr
-// Last Modified On : Tue May 19 07:32:33 2015
-// Update Count     : 2
+// Last Modified On : Thu Nov 19 17:24:01 2015
+// Update Count     : 5
 //
 
Index: src/GenPoly/GenPoly.cc
===================================================================
--- src/GenPoly/GenPoly.cc	(revision d2ded3e7b80d4d46102610712560152e44975503)
+++ src/GenPoly/GenPoly.cc	(revision e56cfdb005c6b37a022fa1d268ad747523f7d2c4)
@@ -10,6 +10,6 @@
 // Created On       : Mon May 18 07:44:20 2015
 // Last Modified By : Peter A. Buhr
-// Last Modified On : Tue May 19 07:37:46 2015
-// Update Count     : 1
+// Last Modified On : Thu Nov 19 17:23:44 2015
+// Update Count     : 10
 //
 
@@ -21,36 +21,56 @@
 
 namespace GenPoly {
-	// interface functions
-	bool isPolyVal( Type *type, const TyVarMap &tyVars ) {
-		return isPolyVal( type, tyVars, false );
+	// A function needs an adapter if it returns a polymorphic value or if any of its
+	// parameters have polymorphic type
+	bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) {
+		if ( ! adaptee->get_returnVals().empty() && isPolyVal( adaptee->get_returnVals().front()->get_type(), tyVars ) ) {
+			return true;
+		} // if
+		for ( std::list< DeclarationWithType* >::const_iterator innerArg = adaptee->get_parameters().begin(); innerArg != adaptee->get_parameters().end(); ++innerArg ) {
+			if ( isPolyVal( (*innerArg)->get_type(), tyVars ) ) {
+				return true;
+			} // if
+		} // for
+		return false;
 	}
 
-	bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) {  
-		return needsAdapter( adaptee, tyVars, false );
+	bool isPolyRet( FunctionType *function, std::string &name, const TyVarMap &otherTyVars ) {
+		bool doTransform = false;
+		if ( ! function->get_returnVals().empty() ) {
+			if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( function->get_returnVals().front()->get_type() ) ) {
+	
+				// figure out if the return type is specified by a type parameter
+				for ( std::list< TypeDecl *>::const_iterator tyVar = function->get_forall().begin(); tyVar != function->get_forall().end(); ++tyVar ) {
+					if ( (*tyVar)->get_name() == typeInst->get_name() ) {
+						doTransform = true;
+						name = typeInst->get_name();
+						break;
+					} // if
+				} // for
+				if ( ! doTransform && otherTyVars.find( typeInst->get_name() ) != otherTyVars.end() ) {
+					doTransform = true;
+				} // if
+			} // if
+		} // if
+		return doTransform;
 	}
 
-	bool isPolyVal( Type *type, const TyVarMap &tyVars, bool considerAllTyVars ) {
-		if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) {
+	bool isPolyRet( FunctionType *function, std::string &name ) {
+		TyVarMap dummyTyVars;
+		return isPolyRet( function, name, dummyTyVars );
+	}
+
+	bool isPolyRet( FunctionType *function, const TyVarMap &otherTyVars ) {
+		std::string dummyString;
+		return isPolyRet( function, dummyString, otherTyVars );
+	}
+
+	bool isPolyVal( Type *type, const TyVarMap &tyVars ) {
+		if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
 			if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) {
 				return true;
 			} // if
-			return considerAllTyVars;
 		} // if
 		return false;
-	}
-
-	// A function needs an adapter if it returns a polymorphic value or if any of its
-	// parameters have polymorphic type
-	bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars, bool considerAllTyVars ) {
-		bool needsAdapter = false;
-		if ( ! adaptee->get_returnVals().empty() && isPolyVal( adaptee->get_returnVals().front()->get_type(), tyVars, considerAllTyVars ) ) {
-			needsAdapter = true;
-		} // if
-		for ( std::list< DeclarationWithType* >::const_iterator innerArg = adaptee->get_parameters().begin(); ! needsAdapter && innerArg != adaptee->get_parameters().end(); ++innerArg ) {
-			if ( isPolyVal( (*innerArg)->get_type(), tyVars, considerAllTyVars ) ) {
-				needsAdapter = true;
-			} // if
-		} // for
-		return needsAdapter;
 	}
 
Index: src/GenPoly/GenPoly.h
===================================================================
--- src/GenPoly/GenPoly.h	(revision d2ded3e7b80d4d46102610712560152e44975503)
+++ src/GenPoly/GenPoly.h	(revision e56cfdb005c6b37a022fa1d268ad747523f7d2c4)
@@ -10,6 +10,6 @@
 // Created On       : Mon May 18 07:44:20 2015
 // Last Modified By : Peter A. Buhr
-// Last Modified On : Tue May 19 07:38:34 2015
-// Update Count     : 1
+// Last Modified On : Thu Nov 19 17:24:03 2015
+// Update Count     : 4
 //
 
@@ -25,12 +25,10 @@
 	typedef std::map< std::string, TypeDecl::Kind > TyVarMap;
 
-	// considerAllTyVars allows ignoring the contents of the TyVarMap parameter, for the situations where
-	// it is important only that a TypeInstType node exists.
-
 	bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVarr );
-	bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars, bool considerAllTyVars );
-	bool isPolyFun( FunctionType *fun, const TyVarMap &tyVars );
+	bool isPolyRet( FunctionType *function, std::string &name, const TyVarMap &otherTyVars );
+	bool isPolyRet( FunctionType *function, std::string &name );
+	bool isPolyRet( FunctionType *function, const TyVarMap &otherTyVars );
+//	bool isPolyFun( FunctionType *fun, const TyVarMap &tyVars );
 	bool isPolyVal( Type *type, const TyVarMap &tyVars );
-	bool isPolyVal( Type *type, const TyVarMap &tyVars, bool considerAllTyVars );
 	void printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap );
 } // namespace GenPoly
Index: src/examples/Makefile.am
===================================================================
--- src/examples/Makefile.am	(revision d2ded3e7b80d4d46102610712560152e44975503)
+++ src/examples/Makefile.am	(revision e56cfdb005c6b37a022fa1d268ad747523f7d2c4)
@@ -11,6 +11,6 @@
 ## Created On       : Sun May 31 09:08:15 2015
 ## Last Modified By : Peter A. Buhr
-## Last Modified On : Thu Jun  4 23:13:10 2015
-## Update Count     : 22
+## Last Modified On : Thu Nov 19 18:01:56 2015
+## Update Count     : 23
 ###############################################################################
 
@@ -20,4 +20,4 @@
 
 noinst_PROGRAMS = fstream_test vector_test
-fstream_test_SOURCES = iostream.c fstream.c fstream_test.c
+fstream_test_SOURCES = iostream.c fstream.c fstream_test.c iterator.c
 vector_test_SOURCES = vector_int.c fstream.c iostream.c array.c iterator.c vector_test.c
Index: src/examples/Makefile.in
===================================================================
--- src/examples/Makefile.in	(revision d2ded3e7b80d4d46102610712560152e44975503)
+++ src/examples/Makefile.in	(revision e56cfdb005c6b37a022fa1d268ad747523f7d2c4)
@@ -49,5 +49,5 @@
 PROGRAMS = $(noinst_PROGRAMS)
 am_fstream_test_OBJECTS = iostream.$(OBJEXT) fstream.$(OBJEXT) \
-	fstream_test.$(OBJEXT)
+	fstream_test.$(OBJEXT) iterator.$(OBJEXT)
 fstream_test_OBJECTS = $(am_fstream_test_OBJECTS)
 fstream_test_LDADD = $(LDADD)
@@ -176,5 +176,5 @@
 top_builddir = @top_builddir@
 top_srcdir = @top_srcdir@
-fstream_test_SOURCES = iostream.c fstream.c fstream_test.c
+fstream_test_SOURCES = iostream.c fstream.c fstream_test.c iterator.c
 vector_test_SOURCES = vector_int.c fstream.c iostream.c array.c iterator.c vector_test.c
 all: all-am
Index: src/examples/iostream.c
===================================================================
--- src/examples/iostream.c	(revision d2ded3e7b80d4d46102610712560152e44975503)
+++ src/examples/iostream.c	(revision e56cfdb005c6b37a022fa1d268ad747523f7d2c4)
@@ -10,6 +10,6 @@
 // Created On       : Wed May 27 17:56:53 2015
 // Last Modified By : Peter A. Buhr
-// Last Modified On : Wed May 27 18:18:13 2015
-// Update Count     : 2
+// Last Modified On : Thu Nov 19 17:54:38 2015
+// Update Count     : 4
 //
 
@@ -30,5 +30,5 @@
 forall( dtype ostype | ostream( ostype ) )
 ostype * ?<<?( ostype *os, int i ) {
-	char buffer[20];      // larger than the largest integer
+	char buffer[32];									// larger than the largest integer
 	sprintf( buffer, "%d", i );
 	return write( os, buffer, strlen( buffer ) );
@@ -37,5 +37,5 @@
 forall( dtype ostype | ostream( ostype ) )
 ostype * ?<<?( ostype *os, double d ) {
-	char buffer[32];      // larger than the largest double
+	char buffer[32];									// larger than the largest double
 	sprintf( buffer, "%g", d );
 	return write( os, buffer, strlen( buffer ) );
@@ -46,4 +46,32 @@
 	return write( os, cp, strlen( cp ) );
 }
+
+forall( dtype ostype | ostream( ostype ) )
+ostype * ?<<?( ostype *os, const void *p ) {
+	char buffer[32];									// larger than the largest pointer
+	sprintf( buffer, "%p", p );
+	return write( os, buffer, strlen( buffer ) );
+}
+
+forall( type elt_type | writeable( elt_type ),
+		type iterator_type | iterator( iterator_type, elt_type ),
+		dtype os_type | ostream( os_type ) )
+void write( iterator_type begin, iterator_type end, os_type *os ) {
+	void print( elt_type i ) {
+		os << i << ' ';
+	}
+	for_each( begin, end, print );
+}
+
+forall( type elt_type | writeable( elt_type ),
+		type iterator_type | iterator( iterator_type, elt_type ),
+		dtype os_type | ostream( os_type ) )
+void write_reverse( iterator_type begin, iterator_type end, os_type *os ) {
+	void print( elt_type i ) {
+		os << i << ' ';
+	}
+	for_each_reverse( begin, end, print );
+}
+
 
 forall( dtype istype | istream( istype ) )
Index: src/examples/iostream.h
===================================================================
--- src/examples/iostream.h	(revision d2ded3e7b80d4d46102610712560152e44975503)
+++ src/examples/iostream.h	(revision e56cfdb005c6b37a022fa1d268ad747523f7d2c4)
@@ -10,10 +10,12 @@
 // Created On       : Wed May 27 17:56:53 2015
 // Last Modified By : Peter A. Buhr
-// Last Modified On : Wed May 27 18:18:46 2015
-// Update Count     : 1
+// Last Modified On : Thu Nov 19 17:56:51 2015
+// Update Count     : 5
 //
 
 #ifndef IOSTREAM_H
 #define IOSTREAM_H
+
+#include "iterator.h"
 
 typedef unsigned long streamsize_type;
@@ -34,4 +36,16 @@
 forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, double );
 forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, const char * );
+forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, void * );
+
+// writes the range [begin, end) to the given stream
+forall( type elt_type | writeable( elt_type ),
+		type iterator_type | iterator( iterator_type, elt_type ),
+		dtype os_type | ostream( os_type ) )
+void write( iterator_type begin, iterator_type end, os_type *os );
+
+forall( type elt_type | writeable( elt_type ),
+		type iterator_type | iterator( iterator_type, elt_type ),
+		dtype os_type | ostream( os_type ) )
+void write_reverse( iterator_type begin, iterator_type end, os_type *os );
 
 
Index: src/examples/iterator.c
===================================================================
--- src/examples/iterator.c	(revision d2ded3e7b80d4d46102610712560152e44975503)
+++ src/examples/iterator.c	(revision e56cfdb005c6b37a022fa1d268ad747523f7d2c4)
@@ -10,40 +10,23 @@
 // Created On       : Wed May 27 17:56:53 2015
 // Last Modified By : Peter A. Buhr
-// Last Modified On : Wed May 27 18:41:41 2015
-// Update Count     : 3
+// Last Modified On : Thu Nov 19 17:54:37 2015
+// Update Count     : 24
 //
 
 #include "iterator.h"
 
-/// forall( type iterator_type, type elt_type | iterator( iterator_type, elt_type ) )
-/// void
-/// for_each( iterator_type begin, iterator_type end, void (*func)( elt_type ) )
-/// {
-///   iterator_type i;
-///   for ( i = begin; i != end; ++i ) {
-///	 func( *i );
-///   }
-/// }
-
-forall( type elt_type | writeable( elt_type ),
-		type iterator_type | iterator( iterator_type, elt_type ),
-		dtype os_type | ostream( os_type ) )
-void write_all( iterator_type begin, iterator_type end, os_type *os ) {
-	iterator_type i;
-	for ( i = begin; i != end; ++i ) {
-		os << *i << ' ';
+forall( type iterator_type, type elt_type | iterator( iterator_type, elt_type ) )
+void for_each( iterator_type begin, iterator_type end, void (*func)( elt_type ) ) {
+	for ( iterator_type i = begin; i != end; ++i ) {
+		func( *i );
 	}
 }
 
-forall( type elt_type | writeable( elt_type ),
-		type iterator_type | iterator( iterator_type, elt_type ),
-		dtype os_type | ostream( os_type ) )
-void write_reverse( iterator_type begin, iterator_type end, os_type *os ) {
-	iterator_type i; // "= end;" does not work
-	i = end;
-	do {
+forall( type iterator_type, type elt_type | iterator( iterator_type, elt_type ) )
+void for_each_reverse( iterator_type begin, iterator_type end, void (*func)( elt_type ) ) {
+	for ( iterator_type i = end; i != begin; ) {
 		--i;
-		os << *i << ' ';
-	} while ( i != begin );
+		func( *i );
+	}
 }
 
Index: src/examples/iterator.h
===================================================================
--- src/examples/iterator.h	(revision d2ded3e7b80d4d46102610712560152e44975503)
+++ src/examples/iterator.h	(revision e56cfdb005c6b37a022fa1d268ad747523f7d2c4)
@@ -10,12 +10,10 @@
 // Created On       : Wed May 27 17:56:53 2015
 // Last Modified By : Peter A. Buhr
-// Last Modified On : Wed May 27 18:41:57 2015
-// Update Count     : 3
+// Last Modified On : Thu Nov 19 17:58:28 2015
+// Update Count     : 6
 //
 
 #ifndef ITERATOR_H
 #define ITERATOR_H
-
-#include "iostream.h"
 
 // An iterator can be used to traverse a data structure.
@@ -34,5 +32,5 @@
 };
 
-context iterator_for ( type iterator_type, type collection_type, type elt_type | iterator( iterator_type, elt_type ) ) {
+context iterator_for( type iterator_type, type collection_type, type elt_type | iterator( iterator_type, elt_type ) ) {
 //	[ iterator_type begin, iterator_type end ] get_iterators( collection_type );
 	iterator_type begin( collection_type );
@@ -43,14 +41,6 @@
 void for_each( iterator_type begin, iterator_type end, void (*func)( elt_type ) );
 
-// writes the range [begin, end) to the given stream
-forall( type elt_type | writeable( elt_type ),
-		type iterator_type | iterator( iterator_type, elt_type ),
-		dtype os_type | ostream( os_type ) )
-void write_all( iterator_type begin, iterator_type end, os_type *os );
-
-forall( type elt_type | writeable( elt_type ),
-		type iterator_type | iterator( iterator_type, elt_type ),
-		dtype os_type | ostream( os_type ) )
-void write_reverse( iterator_type begin, iterator_type end, os_type *os );
+forall( type iterator_type, type elt_type | iterator( iterator_type, elt_type ) )
+void for_each_reverse( iterator_type begin, iterator_type end, void (*func)( elt_type ) );
 
 #endif // ITERATOR_H
Index: src/examples/vector_test.c
===================================================================
--- src/examples/vector_test.c	(revision d2ded3e7b80d4d46102610712560152e44975503)
+++ src/examples/vector_test.c	(revision e56cfdb005c6b37a022fa1d268ad747523f7d2c4)
@@ -10,6 +10,6 @@
 // Created On       : Wed May 27 17:56:53 2015
 // Last Modified By : Peter A. Buhr
-// Last Modified On : Wed May 27 18:42:55 2015
-// Update Count     : 2
+// Last Modified On : Thu Nov 19 17:54:34 2015
+// Update Count     : 9
 //
 
@@ -29,25 +29,18 @@
 	sout << "enter N elements and C-d on a separate line:\n";
 	for ( ;; ) {
-	sin >> &num;
+		sin >> &num;
 	  if ( fail( sin ) || eof( sin ) ) break;
-	append( &vec, num );
+		append( &vec, num );
 	}
 	// write out the numbers
 
 	sout << "Array elements:\n";
-//	write_all( begin( vec ), end( vec ), sout );
-//	sout << "\n";
-	for ( int index = 0; index <= last( vec ); index += 1 ) {
-	sout << vec[ index ] << " ";
-	}
+	write( begin( vec ), end( vec ), sout );
 	sout << "\n";
-#if 1
+
 	sout << "Array elements reversed:\n";
 	write_reverse( begin( vec ), end( vec ), sout );
 	sout << "\n";
-#endif
 }
-
-// ../bin/cfa vector_test.c fstream.o iostream.o vector_int.o iterator.o array.o
 
 // Local Variables: //
