lean.synthInstanceFailed
Type classes are the mechanism that Lean and many other programming languages use to handle overloaded operations. The code that handles a particular overloaded operation is an instance of a typeclass; deciding which instance to use for a given overloaded operation is called synthesizing an instance.
As an example, when Lean encounters an expression x + y where x and y both have type Int,
it is necessary to look up how it should add two integers and also look up what the resulting type
will be. This is described as synthesizing an instance of the type class HAdd Int Int t for some
type t.
Many failures to synthesize an instance of a type class are the result of using the wrong binary operation. Both success and failure are not always straightforward, because some instances are defined in terms of other instances, and Lean must recursively search to find appropriate instances. It's possible to inspect Lean's instance synthesis](https://lean-lang.org/doc/reference/latest/find/?domain=Verso.Genre.Manual.section&name=instance-search), and this can be helpful for diagnosing tricky failures of type class instance synthesis.
Examples
Using the Wrong Binary Operation
Arguments Have the Wrong Type
Lean does not allow integers and strings to be added directly. The function ToString.toString uses
type class overloading to convert values to strings; by successfully searching for an instance of
ToString Int, the second example will succeed.
Missing Type Class Instance
inductive MyColor where
| chartreuse | sienna | thistle
def forceColor (oc : Option MyColor) :=
oc.get!
inductive MyColor where
| chartreuse | sienna | thistle
deriving Inhabited
def forceColor (oc : Option MyColor) :=
oc.get!
inductive MyColor where
| chartreuse | sienna | thistle
deriving instance Inhabited for MyColor
def forceColor (oc : Option MyColor) :=
oc.get!
inductive MyColor where
| chartreuse | sienna | thistle
instance : Inhabited MyColor where
default := .sienna
def forceColor (oc : Option MyColor) :=
oc.get!
Type class synthesis can fail because an instance of the type class simply needs to be provided.
This commonly happens for type classes like Repr, BEq, ToJson and Inhabited. Lean can often
automatically generate instances of the type class with the deriving keyword,
either when the type is defined or with the stand-alone deriving command.