PANIC: Lean.MetavarContext:430:17: Unknown Metavariable

by ADMIN 56 views

Introduction

Lean is a powerful proof assistant and programming language that is widely used in the field of formal verification. However, like any other complex system, it is not immune to bugs and errors. In this article, we will discuss a specific issue that has been reported in the Lean community, known as the "PANIC: Lean.MetavarContext:430:17: unknown metavariable" error.

Prerequisites

Before we dive into the details of the issue, it is essential to understand the prerequisites for reproducing the problem. The following steps should be taken:

  • Check that your issue is not already filed on the Lean GitHub repository. This could be a duplicate, but it is not obviously so.
  • Reduce the issue to a minimal, self-contained, reproducible test case. Avoid dependencies to Mathlib or Batteries.
  • Test your test case against the latest nightly release, for example, on the Lean live server.

Description

The following program causes a panic in the language server:

--import Mathlib.Tactic.ComputeDegree

--set_option diagnostics true

def Chain (I : Type) (n : Nat) (T : Fin (n+1) → Type) :=
  (i : Fin (n+1)) → I → (T i) → Prop

def cons_wit_type (S : Type) (n : Nat) (T : Fin (n+1) → Type) : (i : Fin (n+2)) → Type
  | 0 => S
  | {val := i+1, isLt := _} => sorry
  --| {val := i+1, isLt := _} => T i  --depends on Mathlib.Tactic.ComputeDegree

def ConsChain (I S : Type) (n : Nat) (T : Fin (n+1) → Type) :=
  Chain I (n+1) (cons_wit_type S n T)

def mk_cons (I S : Type) (n : Nat) (T : Fin (n+1) → Type) (relS : I → S → Prop) (r : Chain I n T) :
  ConsChain I S n T
  | 0 => relS
  | {val := i+1, isLt := _} => sorry
  --| {val := i+1, isLt := _} => r i  --depends on Mathlib.Tactic.ComputeDegree

def Base (I S : Type) := Chain I 0 (fun | 0 => S)

def mk_base {I S : Type} (relS : I → S → Prop) : Base I S := (fun | 0 => relS)

infixr:50 " >> " => fun l r => mk_cons l r
postfix:60 " ##" => fun r => mk_base r

def eq (x : Nat) (y : Nat) := x = y

def chain := eq >> (eq ##)

Steps to Reproduce

  1. Paste the given program into a new project using Lean v4.19.0-rc2.
  2. Mouse over (eq ##) on the last line.
  3. Observe the panic shown below under Actual behavior:.

Expected Behavior

Just the following type error without the panic:

Repro.lean:31:13

overloaded, errors 
  application type mismatch
    (fun l r ↦ mk_cons l r) eq
  argument
    eq
  has type
    Nat → Nat → Prop : Type
  but is expected to have type
    Type : Type 1
  
  failed to synthesize
    HAndThen (Nat → Nat → Prop) (Base Nat Nat) ?m.3797
  
  Additional diagnostic information may be available using the `set_option diagnostics true` command.

Actual Behavior

The expected type error, but also this panic (e.g., in the Output pane of VS Code):

PANIC at Lean.MetavarContext.getDecl Lean.MetavarContext:430:17: unknown metavariable
backtrace:
/home/daira/.elan/toolchains/leanprover--lean4---v4.19.0-rc2/lib/lean/libleanshared.so(+0x7c8c362) [0x7f13a148c362]
/home/daira/.elan/toolchains/leanprover--lean4---v4.19.0-rc2/lib/lean/libleanshared.so(lean_panic+0x4e) [0x7f13a148c27e]
/home/daira/.elan/toolchains/leanprover--lean4---v4.19.0-rc2/lib/lean/libleanshared.so(lean_panic_fn+0x15) [0x7f13a148c3f5]
/home/daira/.elan/toolchains/leanprover--lean4---v4.19.0-rc2/lib/lean/libleanshared.so(l___private_Lean_MetavarContext_0__Lean_MetavarContext_MkBinding_elimMVar+0x46) [0x7f139b114606]
/home/daira/.elan/toolchains/leanprover--lean4---v4.19.0-rc2/lib/lean/libleanshared.so(l___private_Lean_MetavarContext_0__Lean_MetavarContext_MkBinding_elimApp+0xf19) [0x7f139b10e9e9]
/home/daira/.elan/toolchains/leanprover--lean4---v4.19.0-rc2/lib/lean/libleanshared.so(l___private_Lean_MetavarContext_0__Lean_MetavarContext_MkBinding_visit+0x36f) [0x7f139b10f64f]
/home/daira/.elan/toolchains/leanprover--lean4---v4.19.0-rc2/lib/lean/libleanshared.so(l___private_Lean_MetavarContext_0__Lean_MetavarContext_MkBinding_elim+0x325) [0x7f139b10af55]
/home/daira/.elan/toolchains/leanprover--lean4---v4.19.0-rc2/lib/lean/libleanshared.so(l___private_Lean_MetavarContext_0__Lean_MetavarContext_MkBinding_visit+0x36f) [0x7f139b10f64f]
/home/daira/.elan/toolchains/leanprover--lean4---v4.19.0-rc2/lib/lean/libleanshared.so(l___private_Lean_MetavarContext_0__Lean_MetavarContext_MkBinding_elim+0x79c) [0x7f139b10b3cc]
/home/daira/.elan/toolchains/leanprover--lean4---v4.19.0-rc2/lib/lean/libleanshared.so(l_Lean_MetavarContext_MkBinding_elimMVarDeps+0x53) [0x7f139b11e103]
/home/daira/.elan/toolchains/leanprover--lean4---v4.19.0-rc2/lib/lean/libleanshared.so(l_Nat_foldRevM_loop___at_Lean_MetavarContext_MkBinding_mkBinding___spec__1+0x7b9) [0x7f139b120759]
/home/daira/.elan/toolchains/leanprover--lean4---v4.19.0-rc2/lib/lean/libleanshared.so(l_Lean_MetavarContext_MkBinding_mkBinding+0x1f2) [0x7f139b121842]
/home/daira/.elan/toolchains/leanprover--lean4---v4.19.0-rc2/lib/lean/libleanshared.so(l_Lean_MetavarContext_mkBinding+0x1dc) [0x7f139b1223bc]
/home/daira/.elan/toolchains/leanprover--lean4---v4.19.0-rc2/lib/lean/libleanshared.so(l_Lean_Meta_mkForallFVars+0x36c) [0x7f139fd0fc6c]
/home/daira/.elan/toolchains/leanprover--lean4---v4.19.0-rc2/lib/lean/libleanshared.so(l___private_Lean_Meta_InferType_0__Lean_Meta_inferLambdaType___lambda__1+0x20d) [0x7f139f8a1dcd]
/home/daira/.elan/toolchains/leanprover--lean4---v4.19.0-rc2/lib/lean/libleanshared.so(l___private_Lean_Meta_InferType_0__Lean_Meta_inferLambdaType___lambda__1___boxed+0x18) [0x7f139f8a2018]
/home/daira/.elan/toolchains/leanprover--lean4---v4.19.0-rc2/lib/lean/libleanshared.so(lean_apply_5+0x3bd) [0x7f13a149f8cd]
/home/daira/.elan/toolchains/leanprover--lean4---v4.19.0-rc2/lib/lean/libleanshared.so(l___private_Lean_Meta_Basic_0__Lean_Meta_lambdaTelescopeImp_process___rarg+0xf6f) [<br/>
# PANIC: Lean.MetavarContext:430:17: unknown metavariable

## Q&A

### Q: What is the PANIC: Lean.MetavarContext:430:17: unknown metavariable error?

A: The PANIC: Lean.MetavarContext:430:17: unknown metavariable error is a bug in the Lean proof assistant that causes the language server to panic when trying to infer the type of a metavariable.

### Q: What is a metavariable?

A: A metavariable is a placeholder in a Lean expression that represents a type or a value that is not yet known. Metavariables are used to represent unknown values or types that will be determined later in the proof.

### Q: Why does the PANIC error occur?

A: The PANIC error occurs when the Lean language server is unable to infer the type of a metavariable. This can happen when the metavariable is used in a way that is not compatible with its type, or when the type of the metavariable is not clear.

### Q: How can I reproduce the PANIC error?

A: To reproduce the PANIC error, you can use the following code:

```lean
def Chain (I : Type) (n : Nat) (T : Fin (n+1) → Type) :=
  (i : Fin (n+1)) → I → (T i) → Prop

def cons_wit_type (S : Type) (n : Nat) (T : Fin (n+1) → Type) : (i : Fin (n+2)) → Type
  | 0 => S
  | {val := i+1, isLt := _} => sorry
  --| {val := i+1, isLt := _} => T i  --depends on Mathlib.Tactic.ComputeDegree

def ConsChain (I S : Type) (n : Nat) (T : Fin (n+1) → Type) :=
  Chain I (n+1) (cons_wit_type S n T)

def mk_cons (I S : Type) (n : Nat) (T : Fin (n+1) → Type) (relS : I → S → Prop) (r : Chain I n T) :
  ConsChain I S n T
  | 0 => relS
  | {val := i+1, isLt := _} => sorry
  --| {val := i+1, isLt := _} => r i  --depends on Mathlib.Tactic.ComputeDegree

def Base (I S : Type) := Chain I 0 (fun | 0 => S)

def mk_base {I S : Type} (relS : I → S → Prop) : Base I S := (fun | 0 => relS)

infixr:50 " >> " => fun l r => mk_cons l r
postfix:60 " ##" => fun r => mk_base r

def eq (x : Nat) (y : Nat) := x = y

def chain := eq >> (eq ##)

Q: How can I fix the PANIC error?

A: To fix the PANIC error, you can try the following:

  • Check that your issue is not already filed on the Lean GitHub repository.
  • Reduce the issue to a minimal, self-contained, reproducible test case. Avoid dependencies to Mathlib or Batteries.
  • Test your test case against the latest nightly release, for example, on the Lean live server.
  • If the issue is still not resolved, you can try to debug the code by using the set_option diagnostics true command to get more detailed error messages.

Q: What are the possible causes of the PANIC error?

A: The possible causes of the PANIC error include:

  • Using a metavariable in a way that is not compatible with its type.
  • The type of the metavariable is not clear.
  • The Lean language server is unable to infer the type of the metavariable.

Q: How can I prevent the PANIC error from occurring?

A: To prevent the PANIC error from occurring, you can try the following:

  • Use explicit type annotations to make it clear what type a metavariable should have.
  • Avoid using metavariables in a way that is not compatible with their type.
  • Use the set_option diagnostics true command to get more detailed error messages when debugging your code.

Q: What are the implications of the PANIC error?

A: The implications of the PANIC error are that the Lean language server is unable to infer the type of a metavariable, which can cause the proof to fail. This can be a significant issue in formal verification, as it can make it difficult to prove the correctness of a system.

Q: How can I report the PANIC error?

A: To report the PANIC error, you can file an issue on the Lean GitHub repository. Be sure to include a minimal, self-contained, reproducible test case that demonstrates the issue.