implement folding to goal