Zulip Chat Archive

Stream: Is there code for X?

Topic: specific implementation of Applicative/Functor from Monad


Asei Inoue (Jun 02 2024 at 05:32):

inductive Many (α : Type u) where
  | none : Many α
  | more : α  (Unit  Many α)  Many α

def Many.one (x : α) : Many α := Many.more x (fun () => Many.none)

def Many.union {α : Type u} : Many α  Many α  Many α
  | Many.none, ys => ys
  | Many.more x xs, ys => Many.more x (fun () => union (xs ()) ys)

def Many.bind : Many α  (α  Many β)  Many β
  | Many.none, _ =>
    Many.none
  | Many.more x xs, f =>
    (f x).union (bind (xs ()) f)

instance instMonadMany : Monad Many where
  pure := Many.one
  bind := Many.bind

-- how to get the implementation of `Functor` instance, or `Applicative` insatance?

-- The specific implementation cannot be known from this output message.
/-
Applicative.toFunctor
-/
#synth Functor Many

/-
unknown constant 'Many.map'
-/
#check Many.map

Yaël Dillies (Jun 02 2024 at 05:35):

Have you tried to #print the instance?

Asei Inoue (Jun 02 2024 at 05:36):

I don't know the instance name of Functor for Many

Yaël Dillies (Jun 02 2024 at 05:37):

That's why we have typeclass inference! Try #print show Functor Many by infer_instance

Miyahara Kō (Jun 02 2024 at 05:38):

No, try #synth Functor Many.

Yaël Dillies (Jun 02 2024 at 05:39):

That's what Asei tried above. It doesn't show you the body of the definition

Asei Inoue (Jun 02 2024 at 05:39):

Thank you! but I get an error

/-
unexpected token 'show'; expected 'axioms', 'eqns', 'equations', identifier or string literal
-/
#print show Functor Many by infer_instance

Yaël Dillies (Jun 02 2024 at 05:39):

Put brackets around? or do #print (inferInstance : Functor Many)

Asei Inoue (Jun 02 2024 at 05:40):

still erorr (I tried this in Lean 4 playground)

/-
unexpected token '('; expected 'axioms', 'eqns', 'equations', identifier or string literal
-/
#print (inferInstance : Functor Many)

Asei Inoue (Jun 02 2024 at 05:41):

oh no... This does not produce an error, but only useless output.

def instFunc : Functor Many := by infer_instance

/-
def instFunc.{u_1} : Functor Many :=
inferInstance
-/
#print instFunc

Yaël Dillies (Jun 02 2024 at 05:42):

Doesn't seem fantastic...

Andrew Yang (Jun 02 2024 at 07:26):

What info do you want to get? Can you click on Applicative.toFunctor to see the implicit arguments?

Asei Inoue (Jun 02 2024 at 07:33):

I want to get this information (which is an implementation of Functor class form Monad) automatically from Lean.

example (f : α  β) (x : Many α) : f <$> x = x.bind (Many.one  f) := by rfl

Andrew Yang (Jun 02 2024 at 07:56):

Clicking on Applicative.toFunctor should point you to Monad.toApplicative, and going there you should see that the implementation is map f x := bind x (Function.comp pure f).

Miyahara Kō (Jun 02 2024 at 08:00):

Yaël Dillies said:

That's what Asei tried above. It doesn't show you the body of the definition

Sorry, try #whnf (inferInstance : Functor Many)!

Miyahara Kō (Jun 02 2024 at 08:04):

#whnf (inferInstance : Functor Many)
/-
{ map := fun {α β} f x ↦ x.bind (Many.one ∘ f), mapConst := fun {α β} ↦ (fun f x ↦ x.bind (Many.one ∘ f)) ∘ const β }
-/

Asei Inoue (Jun 02 2024 at 08:15):

@Pol'tta / Miyahara Kō Thank you!!

Notification Bot (Jun 02 2024 at 08:15):

Asei Inoue has marked this topic as resolved.

Notification Bot (Jun 02 2024 at 10:58):

Asei Inoue has marked this topic as unresolved.

Asei Inoue (Jun 02 2024 at 10:58):

why this does not work?

-- missing implementation info
/-
Applicative.mk
-/
#whnf (inferInstance : Applicative Many)

Miyahara Kō (Jun 02 2024 at 11:03):

This is because Applicative is an extended class.
To see the implementation of <*>, Try #whnf (inferInstance : Seq Many).

Yaël Dillies (Jun 02 2024 at 16:45):

I would really expect #print to be the tool to reach for when you want to know how something is defined.

Eric Wieser (Jun 02 2024 at 21:15):

pp.explicit true will make print check behave here

Eric Wieser (Jun 02 2024 at 21:16):

Or just click on inferInstance in the info view

nesken7777 (Jun 03 2024 at 09:26):

I usually use #reduce instMonadMany.toSeq in this case, but this will show you the unfolded result up to Many.rec.

Asei Inoue (Jun 08 2024 at 10:10):

@Eric Wieser why #print (inferInstance : Functor Many) raise an error? (it says "unexpected token")

Eric Wieser (Jun 08 2024 at 10:11):

You meant #check

Asei Inoue (Jun 08 2024 at 12:02):

why this #print fails?

inductive Many (α : Type u) where
  | none : Many α
  | more : α  (Unit  Many α)  Many α

def Many.one (x : α) : Many α := Many.more x (fun () => Many.none)

def Many.union {α : Type u} : Many α  Many α  Many α
  | Many.none, ys => ys
  | Many.more x xs, ys => Many.more x (fun () => union (xs ()) ys)

def Many.bind : Many α  (α  Many β)  Many β
  | Many.none, _ =>
    Many.none
  | Many.more x xs, f =>
    (f x).union (bind (xs ()) f)

instance instMonadMany : Monad Many where
  pure := Many.one
  bind := Many.bind

-- pass!!
#check (inferInstance : Functor Many)

/-
unexpected token '('; expected 'axioms', 'eqns', 'equations', identifier or string literal
-/
#print (inferInstance : Functor Many)

Eric Wieser (Jun 08 2024 at 12:03):

Print expects a single name, not an expression

Asei Inoue (Jun 08 2024 at 12:05):

@Eric Wieser thanks!


Last updated: May 02 2025 at 03:31 UTC