I do not understand the error I get here, any insight welcome

I have the main:

fun main(args:Array<String>) {

  val n = 1000000000

  val delta = 1.0 / n

  val startTimeNanos = System.nanoTime()

  val pi = 4.0 * delta * (1..n).reduce({t, i ->

  val x = (i - 0.5) * delta

  t + 1.0 / (1.0 + x * x)

  })

  val elapseTime = (System.nanoTime() - startTimeNanos) / 1e9

  out("pi_sequential_reduce", pi, n, elapseTime)

}

Sadly this gives me the following error which gives me no useful information. It probably contains information but nothing useful to me!

Information:Kotlin: Kotlin JPS plugin version 0.7.193
Error:(5, 30) Kotlin: [Internal Error] org.jetbrains.jet.codegen.CompilationException: Back-end (JVM) Internal error: Error types are not allowed when classBuilderMode = FULL. Type: [ERROR : Type for 4.0 * delta * (1…n).iterator().reduce({t, i ->
  val x = (i - 0.5) * delta
  t + 1.0 / (1.0 + x * x)
  })] (ErrorTypeImpl). Descriptor: <ERROR CLASS: >. For declaration null:null in null:null
Cause: Error types are not allowed when classBuilderMode = FULL. Type: [ERROR : Type for 4.0 * delta * (1…n).iterator().reduce({t, i ->
  val x = (i - 0.5) * delta
  t + 1.0 / (1.0 + x * x)
  })] (ErrorTypeImpl). Descriptor: <ERROR CLASS: >. For declaration null:null in null:null
File being compiled and position: (5,30) in /home/users/russel/Progs/Applications/Pi_Quadrature/Kotlin/src/pi_sequential_reduce.kt
PsiElement: {
  val n = 1000000000
  val delta = 1.0 / n
  val startTimeNanos = System.nanoTime()
  val pi = 4.0 * delta * (1…n).iterator().reduce({t, i ->
  val x = (i - 0.5) * delta
  t + 1.0 / (1.0 + x * x)
  })
  val elapseTime = (System.nanoTime() - startTimeNanos) / 1e9
  out(“pi_sequential_reduce”, pi, n, elapseTime)
}
The root cause was thrown at: JetTypeMapper.java:242
at org.jetbrains.jet.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:247)
at org.jetbrains.jet.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:218)
at org.jetbrains.jet.codegen.ExpressionCodegen.gen(ExpressionCodegen.java:253)
at org.jetbrains.jet.codegen.ExpressionCodegen.returnExpression(ExpressionCodegen.java:1609)
at org.jetbrains.jet.codegen.FunctionGenerationStrategy$FunctionDefault.doGenerateBody(FunctionGenerationStrategy.java:71)
at org.jetbrains.jet.codegen.FunctionGenerationStrategy$CodegenBased.generateBody(FunctionGenerationStrategy.java:93)
at org.jetbrains.jet.codegen.FunctionCodegen.generateMethodBody(FunctionCodegen.java:290)
at org.jetbrains.jet.codegen.FunctionCodegen.generateMethod(FunctionCodegen.java:141)
at org.jetbrains.jet.codegen.FunctionCodegen.generateMethod(FunctionCodegen.java:94)
at org.jetbrains.jet.codegen.FunctionCodegen.gen(FunctionCodegen.java:80)
at org.jetbrains.jet.codegen.MemberCodegen.genFunctionOrProperty(MemberCodegen.java:62)
at org.jetbrains.jet.codegen.PackagePartCodegen.generate(PackagePartCodegen.java:82)
at org.jetbrains.jet.codegen.PackageCodegen.generate(PackageCodegen.java:195)
at org.jetbrains.jet.codegen.PackageCodegen.generate(PackageCodegen.java:116)
at org.jetbrains.jet.codegen.KotlinCodegenFacade.generatePackage(KotlinCodegenFacade.java:70)
at org.jetbrains.jet.codegen.KotlinCodegenFacade.compileCorrectFiles(KotlinCodegenFacade.java:57)
at org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.generate(KotlinToJVMBytecodeCompiler.java:313)
at org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyzeAndGenerate(KotlinToJVMBytecodeCompiler.java:263)
at org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModule(KotlinToJVMBytecodeCompiler.java:104)
at org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules(KotlinToJVMBytecodeCompiler.java:140)
at org.jetbrains.jet.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.java:133)
at org.jetbrains.jet.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.java:49)
at org.jetbrains.jet.cli.common.CLICompiler.exec(CLICompiler.java:152)
at org.jetbrains.jet.cli.common.CLICompiler.exec(CLICompiler.java:138)
at org.jetbrains.jet.cli.common.CLICompiler.exec(CLICompiler.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.jetbrains.jet.compiler.runner.CompilerRunnerUtil.invokeExecMethod(CompilerRunnerUtil.java:121)
at org.jetbrains.jet.compiler.runner.KotlinCompilerRunner.execCompiler(KotlinCompilerRunner.java:111)
at org.jetbrains.jet.compiler.runner.KotlinCompilerRunner.access$000(KotlinCompilerRunner.java:39)
at org.jetbrains.jet.compiler.runner.KotlinCompilerRunner$1.fun(KotlinCompilerRunner.java:94)
at org.jetbrains.jet.compiler.runner.KotlinCompilerRunner$1.fun(KotlinCompilerRunner.java:91)
at org.jetbrains.jet.compiler.runner.CompilerRunnerUtil.outputCompilerMessagesAndHandleExitCode(CompilerRunnerUtil.java:130)
at org.jetbrains.jet.compiler.runner.KotlinCompilerRunner.runCompiler(KotlinCompilerRunner.java:91)
at org.jetbrains.jet.compiler.runner.KotlinCompilerRunner.runK2JvmCompiler(KotlinCompilerRunner.java:57)
at org.jetbrains.jet.jps.build.KotlinBuilder.build(KotlinBuilder.java:168)
at org.jetbrains.jps.incremental.IncProjectBuilder.runModuleLevelBuilders(IncProjectBuilder.java:1095)
at org.jetbrains.jps.incremental.IncProjectBuilder.runBuildersForChunk(IncProjectBuilder.java:809)
at org.jetbrains.jps.incremental.IncProjectBuilder.buildTargetsChunk(IncProjectBuilder.java:857)
at org.jetbrains.jps.incremental.IncProjectBuilder.buildChunkIfAffected(IncProjectBuilder.java:772)
at org.jetbrains.jps.incremental.IncProjectBuilder.buildChunks(IncProjectBuilder.java:595)
at org.jetbrains.jps.incremental.IncProjectBuilder.runBuild(IncProjectBuilder.java:352)
at org.jetbrains.jps.incremental.IncProjectBuilder.build(IncProjectBuilder.java:184)
at org.jetbrains.jps.cmdline.BuildRunner.runBuild(BuildRunner.java:129)
at org.jetbrains.jps.cmdline.BuildSession.runBuild(BuildSession.java:224)
at org.jetbrains.jps.cmdline.BuildSession.run(BuildSession.java:113)
at org.jetbrains.jps.cmdline.BuildMain$MyMessageHandler$1.run(BuildMain.java:133)
at org.jetbrains.jps.service.impl.SharedThreadPoolImpl$1.run(SharedThreadPoolImpl.java:41)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.IllegalStateException: Error types are not allowed when classBuilderMode = FULL. Type: [ERROR : Type for 4.0 * delta * (1…n).iterator().reduce({t, i ->
  val x = (i - 0.5) * delta
  t + 1.0 / (1.0 + x * x)
  })] (ErrorTypeImpl). Descriptor: <ERROR CLASS: >. For declaration null:null in null:null
at org.jetbrains.jet.codegen.state.JetTypeMapper.mapType(JetTypeMapper.java:242)
at org.jetbrains.jet.codegen.state.JetTypeMapper.mapType(JetTypeMapper.java:192)
at org.jetbrains.jet.codegen.state.JetTypeMapper.mapType(JetTypeMapper.java:176)
at org.jetbrains.jet.codegen.ExpressionCodegen.asmType(ExpressionCodegen.java:322)
at org.jetbrains.jet.codegen.ExpressionCodegen.generateLocalVariableDeclaration(ExpressionCodegen.java:1463)
at org.jetbrains.jet.codegen.ExpressionCodegen.generateBlock(ExpressionCodegen.java:1423)
at org.jetbrains.jet.codegen.ExpressionCodegen.visitBlockExpression(ExpressionCodegen.java:1282)
at org.jetbrains.jet.codegen.ExpressionCodegen.visitBlockExpression(ExpressionCodegen.java:83)
at org.jetbrains.jet.lang.psi.JetBlockExpression.accept(JetBlockExpression.java:42)
at org.jetbrains.jet.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:237)
… 54 more

This is a bug in type inference. Workaround is to replace 1..n with 1.0..n

Thanks for reporting

I reported this to the tracker: http://youtrack.jetbrains.com/issue/KT-4711

Prompted by Hadi's tweet I tried that, it's good to know it was the right thing to do :-).  Sadly, although things compile, and indeed run, the resultant code takes 233s instead of the 8s of a explicit loop. :-(

We'll investigate this case. Thanks

This difference in performance is due to the use of iterators in reduce() as opposed to direct number addition in the optimized for-loop. If you assign the range to a variable of type Iterable<Int>, it will be much closer to the inlined case, but still about two times slower. This slowdown is due to one extra box/unbox operation that arises after inilining generic code, and we'll probably fix it.

Iterable<Double> rather than Iterable<Int> is required since the reduction is over floating point numbers. I'll give it a go. Thanks.