leanprover / leanprover/fp-lean

Fragmented Explanation for Coercion of `Nat to Option (Option (Option Nat))` Example

Open
#263 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Lean
Stars
192
Forks
73
PR merge metrics
No merged PRs in 30d

Description

The following at the end of 3.6.3 introduces the example:

Coercions are only activated automatically when Lean encounters a mismatch between 
an inferred type and a type that is imposed from the rest of the program. 
In cases with other errors, coercions are not activated. 
For example, if the error is that an instance is missing, coercions will not be used:
def perhapsPerhapsPerhapsNat : Option (Option (Option Nat)) :=
  392

failed to synthesize
  OfNat (Option (Option (Option Nat))) 392
numerals are polymorphic in Lean, but the numeral `392` cannot be used in a context 
where the expected type is
  Option (Option (Option Nat))
due to the absence of the instance above

Hint: Additional diagnostic information may be available using the `set_option diagnostics true` command.

This can be worked around by manually indicating the desired type to be used for OfNat:
def perhapsPerhapsPerhapsNat : Option (Option (Option Nat)) :=
  (392 : Nat)

Additionally, coercions can be manually inserted using an up arrow:
def perhapsPerhapsPerhapsNat : Option (Option (Option Nat)) :=
  ↑(392 : Nat)

In some cases, this can be used to ensure that Lean finds the right instances.
 It can also make the programmer's intentions more clear.

and then 3.6.7 provides more explanation


Natural number literals are overloaded with the OfNat type class. Because coercions fire 
in cases where types don't match, rather than in cases of missing instances, 
a missing OfNat instance for a type does not cause a coercion from Nat to be applied:
def perhapsPerhapsPerhapsNat : Option (Option (Option Nat)) :=
  392

failed to synthesize
  OfNat (Option (Option (Option Nat))) 392
numerals are polymorphic in Lean, but the numeral `392` cannot be used in a context where the expected type is
  Option (Option (Option Nat))
due to the absence of the instance above

Hint: Additional diagnostic information may be available using the `set_option diagnostics true` command.

3.6.3 left me confused and 3.6.7 clarified this confusion, which is why I'm suggesting merging both.


This section from 3.6.3 has 'the spirit' of 3.6.7 but wasn't fully understandable until I read 3.6.7 and went back to 3.6.3

Coercions are only activated automatically when Lean encounters a mismatch between 
an inferred type and a type that is imposed from the rest of the program. 

In cases with other errors, coercions are not activated. 
For example, if the error is that an instance is missing, coercions will not be used:

Moreover , this section from 3.6.3 made me correctly guess what was explicitly stated in 3.6.7 which reinforces the case of merging

This can be worked around by manually indicating the desired type to be used for OfNat:
def perhapsPerhapsPerhapsNat : Option (Option (Option Nat)) :=
  (392 : Nat)

I am willing to propose changes and prepare a pull request that would merge 3.6.7 into 3.6.3 and remove section 3.6.7 if such changes would be accepted.

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 by comparing sections 3.6.3 and 3.6.7, focusing on the repeated perhapsPerhapsPerhapsNat example and their explanations of coercions and missing instances. Check the surrounding chapter structure for consistency. Done means the explanation is consolidated without losing the clarification from 3.6.7 and the redundant section is removed if appropriate.

Written by the indexing model from the issue text.

Assessment

Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.