chapel-lang / chapel

a Productive Parallel Programming Language
https://chapel-lang.org
Other
1.78k stars 419 forks source link

Internal error when assigning lambdas #18413

Open twesterhout opened 3 years ago

twesterhout commented 3 years ago

Summary of Problem

The compiler crashes with internal error:

error.chpl:5: internal error: AST-EXP-0409 chpl version 1.24.1

Steps to Reproduce

Source Code:

var isHammingWeightFixed: bool = true;

var projFn = if isHammingWeightFixed
  then lambda(x) { return x; }
  else lambda(x) { return x; };

Compile command:

chpl -o error error.chpl

Configuration Information

mppf commented 3 years ago

hi @twesterhout thanks for the bug report. First class functions/lambdas are more of a prototype in Chapel today and need some more attention to arrive at a stable language design point. Related to that, the implementation is not complete. (Issue #8351 tracks the shortcomings of the current implementation).

The documentation we do have is available here -- https://chapel-lang.org/docs/main/technotes/firstClassFns.html -- and this documentation points out that we don't currently support capturing a generic function/lambda.

So here is an example that is inspired by your bug report but that compiles and runs today. It makes two changes:

  1. Uses a conditional statement instead of a conditional expression (to avoid the internal error reported here)
  2. Uses a concrete argument type rather than a generic one (since generic lambdas are currently unsupported)
config var isHammingWeightFixed: bool = true;

var projFn: func(int, int);

if isHammingWeightFixed {
  projFn = lambda(x:int) { return x; };
} else {
  projFn = lambda(x:int) { return x+1; };
}

writeln(projFn(1));

Please let us know if this workaround is sufficient for what you are doing.

twesterhout commented 3 years ago

Hello @mppf, sorry for the delay. I've tried out your workaround and it seems to work for my use case, thanks! Somehow I missed this part and thought that variables of function type can never be declared, only deduced.

mppf commented 3 years ago

@twesterhout - no worries, thanks for getting back to me. Since you have a workaround & there are other issues to fix with first-class functions, I'm expecting that we'll put off fixing this issue (probably until we have a more concentrated effort on fixing up the first class function support).

bradcray commented 3 years ago

until we have a more concentrated effort on fixing up the first class function support

Which may get attention during this upcoming release cycle. We're currently starting to kick around priorities and resourcing.