Classic Algorithm W for type inference.