Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Agda panic: Pattern match failure #5506

Closed
alexarice opened this issue Aug 10, 2021 · 5 comments · Fixed by #5518
Closed

Agda panic: Pattern match failure #5506

alexarice opened this issue Aug 10, 2021 · 5 comments · Fixed by #5518
Assignees
Labels
forcing Forcing analysis and forcing translation of clauses inlining Inlining optimisation internal-error Concerning internal errors of Agda regression in 2.6.1 Regression that first appeared in Agda 2.6.1 regression in 2.6.2 Regression that first appeared in Agda 2.6.2 type: bug Issues and pull requests about actual bugs unification Unification on the left-hand-side (not conversion checking)
Milestone

Comments

@alexarice
Copy link
Contributor

alexarice commented Aug 10, 2021

I seem to have managed to trigger a panic in the Agda codebase. I've reduced it down to this code:

{-# OPTIONS --safe --without-K --exact-split #-}

module Catt.Broken where


open import Data.Nat
open import Data.Fin
open import Data.Fin.Patterns

variable
  n m d d′ submax :data Ctx : Set
data Ty : Ctx n d  Set
data Tm : Ctx n d  Set
data Sub : Ctx n d  Ctx m d′  Set

variable
  Γ Δ : Ctx n d
  A : Ty Γ n
  s t  : Tm Γ n
  σ  : Sub Γ Δ

infixl 25 _,_
data Ctx where
  : Ctx 0 0
  _,_ :: Ctx m d)  (A : Ty Γ n)  Ctx (suc m) (d ⊔ n)

ctxLength :: Ctx n d)  ℕ
ctxLength {n} Γ = n

ty-dim : Ty Γ n  ℕ
ty-dim {n = n} A = n

ctx-dim : Ctx n d  ℕ
ctx-dim {d = d} Γ = d

tm-dim : Tm Γ n  ℕ
tm-dim {n = n} t = n

lookupDim :: Ctx n d)  Fin n  ℕ
lookupDim (Γ , A) zero = ty-dim A
lookupDim (Γ , A) (suc i) = lookupDim Γ i

infix 30 _─⟨_⟩⟶_
data Ty where
  : Ty Γ 1
  _─⟨_⟩⟶_ : (s : Tm Γ (suc n))  (A : Ty Γ n)  (t : Tm Γ (suc n))  Ty Γ (suc n)

data Sub where
  ⟨⟩ : Sub ∅ Δ
  ⟨_,_⟩ :: Sub Γ Δ)  {A : Ty Γ m}  (t : Tm Δ (suc m))  Sub (Γ , A) Δ

data Tm where
  Var : (i : (Fin (ctxLength Γ)))  Tm Γ (suc (lookupDim Γ i))
  Coh :: Ctx n d)  (A : Ty Δ m) : Sub Δ Γ)  Tm Γ (suc (ctx-dim Δ) ⊔ suc m)

liftTerm : Tm Γ n  Tm (Γ , A) n
liftSub : Sub Δ Γ  Sub Δ (Γ , A)
liftType : Ty Γ n  Ty (Γ , A) n

liftTerm (Var i) = Var (suc i)
liftTerm (Coh Δ A σ) = Coh Δ A (liftSub σ)

liftSub ⟨⟩ = ⟨⟩
liftSub ⟨ σ , t ⟩ = ⟨ liftSub σ , liftTerm t ⟩

liftType ⋆ = ⋆
liftType (s ─⟨ A ⟩⟶ t) = liftTerm s ─⟨ liftType A ⟩⟶ liftTerm t

data _⊢pd[_][_]_∶_ :: Ctx n d)  (d : ℕ)  Tm Γ (suc (suc d))  Ty Γ (suc d)  Set


getFocusTerm : Γ ⊢pd[ submax ][ d ] t ∶ A  Tm Γ (suc (suc d))
getFocusTerm {t = t} pdb = t

getFocusType : Γ ⊢pd[ submax ][ d ] t ∶ A  Ty Γ (suc d)
getFocusType {A = A} pdb = A

-- Uniquely extend a pasting context
extend :: Ctx n m} {t : Tm Γ (suc (suc d))} {A : Ty Γ (suc d)}  Γ ⊢pd[ submax ][ d ] t ∶ A  Ctx (suc (suc n)) (m ⊔ suc d ⊔ suc (suc d))
extend {Γ = Γ} pdb = Γ , getFocusType pdb , liftTerm (getFocusTerm pdb) ─⟨ liftType (getFocusType pdb) ⟩⟶ Var 0F

data _⊢pd[_][_]_∶_ where
  ExtendM :: Ctx n m}
           {t : Tm Γ (suc (suc d))}
           {A : Ty Γ (suc d)}
           (pdb : Γ ⊢pd[ 0 ][ d ] t ∶ A) 
            extend pdb ⊢pd[ 0 ][ suc d ] Var 0F ∶ liftTerm (liftTerm t) ─⟨ liftType (liftType A) ⟩⟶ Var 1F


test : Γ ⊢pd[ submax ][ d ] t ∶ A  Set
test (ExtendM pdb) = ?

Which gives the following error with agda version 2.6.2:

Panic: Pattern match failure in do expression at
src/full/Agda/TypeChecking/Rules/LHS/Unify.hs:1313:7-14
when checking that the pattern ExtendM pdb has type
Γ ⊢pd[ submax ][ d ] t ∶ A

Inlining the call to extend pdb in the constructor type seems to fix the issue, though I'm guessing it is not ideal that agda is doing this.

@andreasabel andreasabel added forcing Forcing analysis and forcing translation of clauses internal-error Concerning internal errors of Agda type: bug Issues and pull requests about actual bugs unification Unification on the left-hand-side (not conversion checking) labels Aug 13, 2021
@andreasabel andreasabel added this to the 2.6.3 milestone Aug 13, 2021
@andreasabel
Copy link
Member

andreasabel commented Aug 13, 2021

This is a bug in the forcing optimization, you can work around it with {-# OPTIONS --no-forcing #-}.

Error location is here:

Just md' <- gets $ IntMap.lookup i

@andreasabel andreasabel added the regression in 2.6.2 Regression that first appeared in Agda 2.6.2 label Aug 13, 2021
@andreasabel
Copy link
Member

Here is a version without the standard library (probably not minimal yet):

{-# OPTIONS --without-K --exact-split #-}
-- {-# OPTIONS --no-forcing #-}

open import Agda.Builtin.Nat renaming (Nat to ℕ)

infixl 8  _⊔_
postulate _⊔_ : (n m : ℕ) variable
  n m d d′ submax :data Fin : Set where
  zero : Fin (suc n)
  suc  : (i : Fin n)  Fin (suc n)

data Ctx : Set
data Ty : Ctx n d  Set
data Tm : Ctx n d  Set
data Sub : Ctx n d  Ctx m d′  Set

variable
  Γ Δ : Ctx n d
  A : Ty Γ n
  s t  : Tm Γ n
  σ  : Sub Γ Δ

infixl 25 _,_
data Ctx where
  : Ctx 0 0
  _,_ :: Ctx m d)  (A : Ty Γ n)  Ctx (suc m) (d ⊔ n)

ctxLength :: Ctx n d)  ℕ
ctxLength {n} Γ = n

ty-dim : Ty Γ n  ℕ
ty-dim {n = n} A = n

ctx-dim : Ctx n d  ℕ
ctx-dim {d = d} Γ = d

tm-dim : Tm Γ n  ℕ
tm-dim {n = n} t = n

lookupDim :: Ctx n d)  Fin n  ℕ
lookupDim (Γ , A) zero = ty-dim A
lookupDim (Γ , A) (suc i) = lookupDim Γ i

infix 30 _─⟨_⟩⟶_
data Ty where
  : Ty Γ 1
  _─⟨_⟩⟶_ : (s : Tm Γ (suc n))  (A : Ty Γ n)  (t : Tm Γ (suc n))  Ty Γ (suc n)

data Sub where
  ⟨⟩ : Sub ∅ Δ
  ⟨_,_⟩ :: Sub Γ Δ)  {A : Ty Γ m}  (t : Tm Δ (suc m))  Sub (Γ , A) Δ

data Tm where
  Var : (i : (Fin (ctxLength Γ)))  Tm Γ (suc (lookupDim Γ i))
  Coh :: Ctx n d)  (A : Ty Δ m) : Sub Δ Γ)  Tm Γ (suc (ctx-dim Δ) ⊔ suc m)

liftTerm : Tm Γ n  Tm (Γ , A) n
liftSub : Sub Δ Γ  Sub Δ (Γ , A)
liftType : Ty Γ n  Ty (Γ , A) n

liftTerm (Var i) = Var (suc i)
liftTerm (Coh Δ A σ) = Coh Δ A (liftSub σ)

liftSub ⟨⟩ = ⟨⟩
liftSub ⟨ σ , t ⟩ = ⟨ liftSub σ , liftTerm t ⟩

liftType ⋆ = ⋆
liftType (s ─⟨ A ⟩⟶ t) = liftTerm s ─⟨ liftType A ⟩⟶ liftTerm t

data _⊢pd[_][_]_∶_ :: Ctx n d)  (d : ℕ)  Tm Γ (suc (suc d))  Ty Γ (suc d)  Set


getFocusTerm : Γ ⊢pd[ submax ][ d ] t ∶ A  Tm Γ (suc (suc d))
getFocusTerm {t = t} pdb = t

getFocusType : Γ ⊢pd[ submax ][ d ] t ∶ A  Ty Γ (suc d)
getFocusType {A = A} pdb = A

-- Uniquely extend a pasting context
extend :: Ctx n m} {t : Tm Γ (suc (suc d))} {A : Ty Γ (suc d)}  Γ ⊢pd[ submax ][ d ] t ∶ A  Ctx (suc (suc n)) (m ⊔ suc d ⊔ suc (suc d))
extend {Γ = Γ} pdb = Γ , getFocusType pdb , liftTerm (getFocusTerm pdb) ─⟨ liftType (getFocusType pdb) ⟩⟶ Var zero

data _⊢pd[_][_]_∶_ where
  ExtendM :: Ctx n m}
           {t : Tm Γ (suc (suc d))}
           {A : Ty Γ (suc d)}
           (pdb : Γ ⊢pd[ 0 ][ d ] t ∶ A) 
            extend pdb ⊢pd[ 0 ][ suc d ] Var zero ∶ liftTerm (liftTerm t) ─⟨ liftType (liftType A) ⟩⟶ Var (suc zero)


test : Γ ⊢pd[ submax ][ d ] t ∶ A  Set
test (ExtendM pdb) = {!!}

It works in 2.6.1, so it is a regression in 2.6.2.

@andreasabel
Copy link
Member

Bisection points to commit 2f212bf by @UlfNorell

Date:   Thu May 28 09:54:29 2020 +0200

Add --auto-inline and make it off by default

#4681

Inlining the call to extend pdb in the constructor type seems to fix the issue

Likely, Agda did that before the above commit. But this commit only brings the bug to surface, I suppose.

@andreasabel andreasabel added the inlining Inlining optimisation label Aug 13, 2021
@andreasabel
Copy link
Member

andreasabel commented Aug 18, 2021

I shrank this to:

open import Agda.Builtin.Nat

data Unit : Set where
  unit : Unit

data Ctx : Nat  Set where  -- index needed
  cons : (m : Nat) (A : Unit)  Ctx (suc m)

mutual

  data P : (n : Nat) (Γ : Ctx n)  Set

  -- Needs to be mutual
  {-# NOINLINE getFocus #-}
  getFocus : (n : Nat) (A : Unit)  Unit
  getFocus n A = A  -- needs to be A, not unit

  data P where
    c : (n : Nat) (A : Unit)
       P (suc n) (cons n (getFocus n A))

test : (n : Nat) (Γ : Ctx n)  P n Γ  Set1
test n Γ (c m A) = Set
  -- ^ n := suc m fixes the issue

This version already crashes with 2.6.1 (and passes in 2.6.0), so we can restart bisection!

@andreasabel andreasabel added the regression in 2.6.1 Regression that first appeared in Agda 2.6.1 label Aug 18, 2021
@andreasabel
Copy link
Member

There is a new blamed commit: 8760d15 (ping @UlfNorell)

Date:   Fri Oct 18 12:08:40 2019 +0200

    Handle modalities when moving binding site of forced variables

 src/full/Agda/TypeChecking/Rules/LHS/Problem.hs |  5 ++-
 src/full/Agda/TypeChecking/Rules/LHS/Unify.hs   | 54 ++++++++++++++++---------
 test/Succeed/ForcingRelevance.agda              | 23 +++++++++++

andreasabel added a commit that referenced this issue Aug 18, 2021
Note: apparently, the author of the blamed commit
8760d15 was convinced of some
invariant that was refuted by #5506.

It makes sense to go back and investigate whether the invariant was
assumed in error or whether something else breaks this invariant.
@andreasabel andreasabel self-assigned this Aug 18, 2021
@andreasabel andreasabel modified the milestones: 2.6.3, 2.6.2.1 Aug 18, 2021
andreasabel added a commit that referenced this issue Aug 24, 2021
Note: apparently, the author of the blamed commit
8760d15 was convinced of some
invariant that was refuted by #5506.

It makes sense to go back and investigate whether the invariant was
assumed in error or whether something else breaks this invariant.
andreasabel added a commit that referenced this issue Nov 17, 2021
Note: apparently, the author of the blamed commit
8760d15 was convinced of some
invariant that was refuted by #5506.

It makes sense to go back and investigate whether the invariant was
assumed in error or whether something else breaks this invariant.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
forcing Forcing analysis and forcing translation of clauses inlining Inlining optimisation internal-error Concerning internal errors of Agda regression in 2.6.1 Regression that first appeared in Agda 2.6.1 regression in 2.6.2 Regression that first appeared in Agda 2.6.2 type: bug Issues and pull requests about actual bugs unification Unification on the left-hand-side (not conversion checking)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants