Use safe_new(x) instead of x.type() in scan grad.

This copies the test_value, making possible to use a test value
for sequences. Add test.