Skip to content

Commit 8338fc4

Browse files
eric-wieserReemMelamed
authored andcommitted
chore: add a computable shortcut for AddCommMonoid ℂ and NonUnitalCommRing ℂ (leanprover-community#40163)
We already do this for the `Module` instance as of leanprover-community#29609. Also adds some `delta%`s to very slightly reduce term size.
1 parent 6e9f1ca commit 8338fc4

3 files changed

Lines changed: 60 additions & 4 deletions

File tree

Mathlib/Analysis/Complex/Basic.lean

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,13 @@ We also register the fact that `ℂ` is an `RCLike` field.
4545

4646
assert_not_exists Absorbs
4747

48+
namespace Complex
49+
4850
/-- A shortcut instance to ensure computability; otherwise we get the noncomputable instance
4951
`Complex.instNormedField.toNormedModule.toModule`. -/
50-
instance Complex.instModuleSelf : Module ℂ ℂ := delta% inferInstance
52+
instance instModuleSelf : Module ℂ ℂ := delta% inferInstance
53+
54+
end Complex
5155

5256
noncomputable section
5357

Mathlib/Data/Complex/Basic.lean

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,19 +389,34 @@ instance commRing : CommRing ℂ :=
389389
mul_one := by intros; ext <;> simp
390390
mul_comm := by intros; ext <;> simp <;> ring }
391391

392+
section computable_shortcuts
393+
392394
/-- This shortcut instance ensures we do not find `Ring` via the noncomputable `Complex.field`
393395
instance. -/
394-
instance : Ring ℂ := by infer_instance
396+
instance : Ring ℂ :=
397+
delta% inferInstance
398+
399+
/-- This shortcut instance ensures we do not find `NonUnitalCommRing` via the noncomputable
400+
`instCommCStarAlgebraComplex` instance. -/
401+
instance : NonUnitalCommRing ℂ :=
402+
delta% inferInstance
395403

396404
/-- This shortcut instance ensures we do not find `CommSemiring` via the noncomputable
397405
`Complex.field` instance. -/
398406
instance : CommSemiring ℂ :=
399-
inferInstance
407+
delta% inferInstance
400408

401409
/-- This shortcut instance ensures we do not find `Semiring` via the noncomputable
402410
`Complex.field` instance. -/
403411
instance : Semiring ℂ :=
404-
inferInstance
412+
delta% inferInstance
413+
414+
/-- This shortcut instance ensures we do not find `AddCommMonoid` via the noncomputable
415+
`Complex.instNormedField` instance. -/
416+
instance : AddCommMonoid ℂ :=
417+
delta% inferInstance
418+
419+
end computable_shortcuts
405420

406421
/-- The "real part" map, considered as an additive group homomorphism. -/
407422
def reAddGroupHom : ℂ →+ ℝ where
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/-
2+
This test must import everything to make the noncomputable instances which we don't want available.
3+
-/
4+
import Mathlib
5+
6+
/-!
7+
# Tests that basic algebraic typeclasses are computable
8+
9+
Frequently mathlib will have instances of the form
10+
```
11+
instance : FooAndBar X := ...
12+
13+
-- in a later file
14+
noncomputable instance : VeryNoncomputableFoo X := ...
15+
```
16+
which means that `Foo X` will sometimes be synthesized noncomputably even though a computable
17+
version is available.
18+
19+
TODO: test this more exhaustively.
20+
-/
21+
22+
macro "#guard_computable" t:term : command =>
23+
`(#guard_msgs (drop info) in #eval $t)
24+
25+
section Real
26+
27+
#guard_computable LinearMap.mul ℝ ℝ 2 3
28+
#guard_computable LinearEquiv.neg ℝ (2 : ℝ)
29+
30+
end Real
31+
32+
section Complex
33+
34+
#guard_computable LinearMap.mul ℂ ℂ 2 3
35+
#guard_computable LinearEquiv.neg ℂ (2 : ℂ)
36+
37+
end Complex

0 commit comments

Comments
 (0)