agda / agda2hs

Compiling Agda code to readable Haskell

Home Page:https://agda.github.io/agda2hs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

__IMPOSSIBLE__

HeinrichApfelmus opened this issue · comments

Hello! I'm using Agda2hs because it's awesome for writing Haskell programs that I can also prove correct. Unfortunately, it's also a bit rough on the edges: agda2hs instructs me to report a bug.

Specifically, in the fine-types-core package of the repository/branch
https://github.com/cardano-foundation/fine-types/tree/HeinrichApfelmus/ADP-3153/agda2hs-bug/lib/fine-types-core , I get

fine-types-core % ./generate-haskell.sh
Writing ./src/haskell/Language/FineTypes/Typ.hs
Writing ./src/haskell/Data/Embedding.hs
Writing ./src/haskell/Language/FineTypes/Value/Def.hs
An internal error has occurred. Please report this as a bug.
Location of the error: __IMPOSSIBLE__, called at src/Agda2Hs/Compile/Function.hs:133:39 in main:Agda2Hs.Compile.Function

The error seemingly occurs when compiling the file ./src/agda/Language/FineTypes/Embedding.agda. The interactive mode of Agda version 2.6.3 accepts the file just fine.

I have not attempted to make the test case smaller, as the codebase is still quite small.

Thank you for the report! The bug itself is triggered by the following line:

body <- compileTerm $ fromMaybe __IMPOSSIBLE__ clauseBody

It seems that somehow agda2hs is trying to compile a clause without a RHS, i.e. an absurd clause. Could you run agda2hs again with the option -v agda2hs.compile:7 and copy the (last part of) the output here?

Could you run agda2hs again with the option -v agda2hs.compile:7 and copy the (last part of) the output here?

Sure, here is the full log: compile_impossible.stdout.txt.

From the log, it appears that agda2hs chokes when compiling the to field of the source definition

distributeVal
  : ∀ {@0 A B C : Typ}
  → Embedding
      (Value ((A + B) × C))
      (Value ((A × C) + (B × C)))
distributeVal =
  record
    { to = λ
      { (Product2 (Sum2L a) c) → Sum2L (Product2 a c)
      ; (Product2 (Sum2R b) c) → Sum2R (Product2 b c)
      }
    ; …

because Agda inserts two impossible cases

to =
   λ { (Product2 (Sum2L a) c) → Sum2L (Product2 a c)
     ; (Product2 (Sum2R b) c) → Sum2R (Product2 b c)
     ; (Two ())
     ; (Product2 (Two ()) x)
     }

The source definition of Value contains the constructor

data Value : @0 Typ → Set where
  …
  Two     : ∀ {@0 A B : Typ} {@0 op : OpTwo}
          → TwoF (Value A) (Value B) op
          → Value (Typ.Two op A B)

but the type TwoF is equivalent to , mostly because I had commented out a constructor that I wanted to implement later:

data TwoF (a b : Set) : @0 Typ.OpTwo → Set where
--  FiniteMap : Map a b → TwoF a b

Judging from the above, the following is a minimal test case:

data Void : Set where
data Maybe (a : Set) : Set where
    Nothing : Maybe a
    Just : a → Maybe a

test : Maybe Void → Maybe Void
test = λ
    { Nothing → Nothing
    }

{-# COMPILE AGDA2HS test #-}

Agda2hs will choke with __IMPOSSIBLE__ when it's trying to compile the case expression, because Agda has added an impossible case that was not present in the source file:

compiling term: λ { Nothing → Nothing ; (Just ()) }