google-research / dex-lang

Research language for array processing in the Haskell/ML family
BSD 3-Clause "New" or "Revised" License
1.58k stars 106 forks source link

Compiler bug when usings parser combinators #1193

Closed duvenaud closed 1 year ago

duvenaud commented 1 year ago

I'm trying to write a tokenizer in Dex. I could use the parser combinator library to write a simple word-splitter that runs fine at the top level, but when I wrapped it into a function, I got a compiler bug. Here is a MWE:

import parser
def split_parser = MkParser \h. parse_digit
run_parser "" split_parser

gives

Compiler bug!
Please report this at github.com/google-research/dex-lang/issues

Pattern match failure in do expression at src/lib/Simplify.hs:887:5-39
CallStack (from HasCallStack):
  error, called at src/lib/Err.hs:222:12 in dex-0.1.0.0-Foy45KZj4WmEDjD4LlSjBZ:Err

I realize the above code doesn't do anything useful, it's been stripped down to make a MWE.

axch commented 1 year ago

So I traced this a little further, and it's due to a current limitation on catch, that it can't deal with a block that has a functional value. For example:

catch do f64_to_f
> Compiler bug!
> Please report this at github.com/google-research/dex-lang/issues
>
> Pattern match failure in do expression at src/lib/Simplify.hs:891:5-39
> CallStack (from HasCallStack):
>   error, called at src/lib/Err.hs:222:12 in dex-0.1.0.0-Gi9B8TxRbicLQnuTnmjlXJ:Err

@dougalm Is that intentional? Should we improve the error message (and put a Data constraint on catch once we have that), or should we fix it? Internally, the issue is assuming the block being caught from simplifies with an identity reconstruction.

It occurs in your program @duvenaud because you wrote (unintentionally?) a parser that produces a parser (which internally contains a function). To wit,

:t split_parser
> (Parser (Parser Int32))

which seems like not what you want? Trying to invoke this puts it through the catch inside run_parser, producing the crash.

duvenaud commented 1 year ago

Yes, I don't think I want that behavior, and once I got my parser to work, I didn't have this error anymore. So it's not a blocking issue, I just reported as I went along in case it uncovered something more important.