GrammaticalFramework/gf-core

Assertion errors in PGF2 not catchable exceptions

johnjcamilleri opened this issue · 5 comments

Take this minimum working example:

import PGF2
import Control.Exception
import qualified Data.Map as Map

main :: IO ()
main = do
  pgf <- readPGF "Feedback.pgf"
  let Just eng = Map.lookup "FeedbackEng" (languages pgf)
  onException (do
    let expr = mkApp (mkCId "BaseFeature") []
    let s = linearize eng expr
    putStrLn s
    ) (putStrLn "Caught exception")

where:

BaseFeature : Feature -> Feature -> ListFeature

So in my code I am building an invalid tree and expect to get an error. What currently happens is that my program crashes with the following:

pgf_cnc_resolve_app (pgf/linearizer.c:176): assertion failed
	n_args == gu_seq_length(papply->args)

Is there any way this can be thrown as a Haskell exception so that it can be handled appropriately?

Ah I see, fair enough. I guess this can be better documented, I'll add something to the Haskell docs for PGF2.

Perhaps we should make a safer API that enforces that dynamically built expressions are type-checked before they can be used with linearize and similar functions?
E.g by adding a newtype that can only be built by type-checking or by unsafeAssumeTypeChecked.

I guess the major question is how large the performance penalty of type-checking would be. If it is minimal, we could just always do type checking before linearizing and add an unsafe variant that doesn't type check first.