Skip to content

Partial function crashes when hidden behind a type class #11486

@hgoldstein95

Description

@hgoldstein95

Prerequisites

Please put an X between the brackets as you perform the following steps:

Description

Sorry for the cryptic title, I'm not 100% sure how to explain this. I've been playing around with a DSL embedded using a tagless-final style with typeclasses, and I managed to crash Lean with what seems like it should be a pretty innocuous piece of code. Here's the MWE (or at least as minimal as I could come up with):

class RandomChoice (m : Type → Type) where
  choose : (lo hi : Nat) → (h : lo ≤ hi) → m Nat

def RandomChoice.pick [Monad m] [RandomChoice m] (x y : m α) := do
  if (← choose 0 1 (by simp)) == 0 then x else y

open RandomChoice

def Gen (α : Type) := ∀ {m : Type → Type} [Monad m] [RandomChoice m], m α

instance : RandomChoice IO where
  choose lo hi _ := IO.rand lo hi

partial def Nat.arbitrary : Gen Nat := do
  pick
    (pure 0)
    (do
      let n ← Nat.arbitrary
      pure (n + 1))

def Gen.runIO (g : Gen α) : IO α := g

-- Works fine.
#eval (Nat.arbitrary : IO Nat)

-- This breaks!
#eval Gen.runIO Nat.arbitrary

Context

I encountered this when working with @nomeata on some partial_fixpoint stuff. Originally I thought this must be related to that, but it turns out it works with partial too. The type-class embedding I'm doing is pretty central to the approach, so unfortunately I don't have a good way to work around this problem.

Steps to Reproduce

See the example above. (Tested on nightly.)

Expected behavior: The second #eval behaves the same as the first.

Actual behavior: The second #eval crashes Lean.

Versions

Tested on the Lean online editor on nightly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingcode-generatorThis issue is with the code generator

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions