lean4-htt/src/Lean/Meta/NatInstTesters.lean
Kyle Miller 1c20b53419
feat: shorten auto-generated instance names (#3089)
Implements a new method to generate instance names for anonymous
instances that uses a heuristic that tends to produce shorter names. A
design goal is to make them relatively unique within projects and
definitely unique across projects, while also using accessible names so
that they can be referred to as needed, both in Lean code and in
discussions.

The new method also takes into account binders provided to the instance,
and it adds project-based suffixes. Despite this, a median new name is
73% its original auto-generated length. (Compare: [old generated
names](https://gist.github.com/kmill/b72bb43f5b01dafef41eb1d2e57a8237)
and [new generated
names](https://gist.github.com/kmill/393acc82e7a8d67fc7387829f4ed547e).)

Some notes:
* The naming is sensitive to what is explicitly provided as a binder vs
what is provided via a `variable`. It does not make use of `variable`s
since, when names are generated, it is not yet known which variables are
used in the body of the instance.
* If the instance name refers to declarations in the current "project"
(given by the root module), then it does not add a suffix. Otherwise, it
adds the project name as a suffix to protect against cross-project
collisions.
* `set_option trace.Elab.instance.mkInstanceName true` can be used to
see what name the auto-generator would give, even if the instance
already has an explicit name.

There were a number of instances that were referred to explicitly in
meta code, and these have been given explicit names.

Removes the unused `Lean.Elab.mkFreshInstanceName` along with the
Command state's `nextInstIdx`.

Fixes #2343
2024-04-13 18:08:50 +00:00

63 lines
2 KiB
Text

/-
Copyright (c) 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import Lean.Meta.Basic
namespace Lean.Meta
/-!
Functions for testing whether expressions are canonical `Nat` instances.
-/
def isInstOfNatNat (e : Expr) : MetaM Bool := do
let_expr instOfNatNat _ ← e | return false
return true
def isInstAddNat (e : Expr) : MetaM Bool := do
let_expr instAddNat ← e | return false
return true
def isInstSubNat (e : Expr) : MetaM Bool := do
let_expr instSubNat ← e | return false
return true
def isInstMulNat (e : Expr) : MetaM Bool := do
let_expr instMulNat ← e | return false
return true
def isInstDivNat (e : Expr) : MetaM Bool := do
let_expr Nat.instDiv ← e | return false
return true
def isInstModNat (e : Expr) : MetaM Bool := do
let_expr Nat.instMod ← e | return false
return true
def isInstNatPowNat (e : Expr) : MetaM Bool := do
let_expr instNatPowNat ← e | return false
return true
def isInstPowNat (e : Expr) : MetaM Bool := do
let_expr instPowNat _ i ← e | return false
isInstNatPowNat i
def isInstHAddNat (e : Expr) : MetaM Bool := do
let_expr instHAdd _ i ← e | return false
isInstAddNat i
def isInstHSubNat (e : Expr) : MetaM Bool := do
let_expr instHSub _ i ← e | return false
isInstSubNat i
def isInstHMulNat (e : Expr) : MetaM Bool := do
let_expr instHMul _ i ← e | return false
isInstMulNat i
def isInstHDivNat (e : Expr) : MetaM Bool := do
let_expr instHDiv _ i ← e | return false
isInstDivNat i
def isInstHModNat (e : Expr) : MetaM Bool := do
let_expr instHMod _ i ← e | return false
isInstModNat i
def isInstHPowNat (e : Expr) : MetaM Bool := do
let_expr instHPow _ _ i ← e | return false
isInstPowNat i
def isInstLTNat (e : Expr) : MetaM Bool := do
let_expr instLTNat ← e | return false
return true
def isInstLENat (e : Expr) : MetaM Bool := do
let_expr instLENat ← e | return false
return true
end Lean.Meta