google-research / dex-lang

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

Can't throw an exception in a context of non-data type #1220

Closed axch closed 1 year ago

axch commented 1 year ago
catch do
  f = if True
    then throw ()
    else \x. x + x
  f 1.0
> Compiler bug!
> Please report this at github.com/google-research/dex-lang/issues
>
> Not a type of runtime-representable data: (Float32 -> Float32)
> CallStack (from HasCallStack):
>   error, called at src/lib/Simplify.hs:164:23 in dex-0.1.0.0-FpQQZUZ8GVjAdZmhEYdUJk:Simplify

Expected Nothing (of type Maybe Float).

axch commented 1 year ago

I think the blocking problem is that simplify tries to turn a ThrowException (Atom CoreIR) into a ThrowException (Atom SimpIR) which is detected by exceptToMaybeExpr later. The Atom represents the type that the throw doesn't return (not the type of the exception itself, which is Unit); and if that type is functional, it can't be represented as a Atom SimpIR.

axch commented 1 year ago

Note: The same example breaks with error as well as throw.

dougalm commented 1 year ago

We discussed this offline. Ideally error and throw should work with non-data return types but the current compiler doesn't handle them. Fixing that is one todo among many, and in the meantime it's better to have a type error than a compiler crash. So let's add the Data constraint for now, with the intention of eventually removing it. We can keep this issue open to track it.

dougalm commented 1 year ago

I added the Data constraints in #1235 . I'll open a separate feature request to relax this constraint.