leanprover / leanprover/lean4

Issue finding `Inhabited` instances in partial definition

Open
#2,204 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

P-medium
Dominant language
Lean
Stars
9.2k
Forks
990
Avg merge
1d 17h
Merged PRs (30d)
175

Description

Prerequisites
  • Put an X between the brackets on this line if you have done all of the following:
    • Checked that your issue isn't already filed.
    • Reduced the issue to a self-contained, reproducible test case.
Description

The following definition fails to compile because Lean can't infer that m (Unit × β) is inhabited.

variable {α β ε : Type _} (m : Type _ → Type _) [Monad m] [MonadExcept ε m]

partial def test (q : m β) : m (Unit × β) :=
  let rec loop (p : Unit) : m (Unit × β) :=
    try
      return (p, ← q)
    catch _ => do
      loop p
  loop ()

The following workaround does compile.

variable {α β ε : Type _} (m : Type _ → Type _) [Monad m] [MonadExcept ε m]

partial def test' (q : m β) : m (Unit × β) :=
  let inst := Inhabited.mk do return ((), (← q))
  let rec loop (p : Unit) : m (Unit × β) :=
    try
      return (p, ← q)
    catch _ => do
      let _ := inst
      loop p
  loop ()

Both lets are necessary to compile. The second let is especially confusing, I think just fixing that would already be a significant improvement.

Versions

On MacOs with Lean (version 4.0.0-nightly-2023-04-20, commit f9da1d8b55ca, Release)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the self-contained partial def test reproducer and compare it with the compiling test' workaround. Investigate how partial definitions infer Inhabited for m (Unit × β), especially why the inner let _ := inst changes compilation. Done means the original definition, or an equivalent minimal case, compiles without the confusing second let.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.