Skip to content

Commit

Permalink
fix: defaultInstance priorities for Neg Int and OfScientific Float
Browse files Browse the repository at this point in the history
  • Loading branch information
leodemoura committed Jan 25, 2021
1 parent b575087 commit d408c83
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/Init/Data/Int/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,14 @@ protected def mul (m n : @& Int) : Int :=
| negSucc m, ofNat n => negOfNat (succ m * n)
| negSucc m, negSucc n => ofNat (succ m * succ n)

@[defaultInstance]
/-
The `Neg Int` default instance must have priority higher than `low` since
the default instance `OfNat Nat n` has `low` priority.
```
#check -42
```
-/
@[defaultInstance mid]
instance : Neg Int where
neg := Int.neg
instance : Add Int where
Expand Down
10 changes: 9 additions & 1 deletion src/Init/Data/OfScientific.lean
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import Init.Meta
import Init.Data.Float
import Init.Data.Nat

Expand All @@ -15,6 +16,13 @@ import Init.Data.Nat
class OfScientific (α : Type u) where
ofScientific : Nat → Bool → Nat → α

@[defaultInstance low]
/-
The `OfScientifi Float` must have priority higher than `mid` since
the default instance `Neg Int` has `mid` priority.
```
#check -42.0 -- must be Float
```
-/
@[defaultInstance mid+1]
instance : OfScientific Float where
ofScientific m s e := Float.ofScientific m s e
1 change: 1 addition & 0 deletions src/Init/Notation.lean
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ macro "maxPrec!" : term => `(1024)

macro "default" : prio => `(1000)
macro "low" : prio => `(100)
macro "mid" : prio => `(1000)
macro "high" : prio => `(10000)
macro "(" p:prio ")" : prio => p

Expand Down
6 changes: 6 additions & 0 deletions tests/lean/negFloat.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#check 42
#check -42
#check -42.0
#eval -42.0
#eval Neg.neg 42.0
#eval 1.0 * -42.0
6 changes: 6 additions & 0 deletions tests/lean/negFloat.lean.expected.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
42 : Nat
-42 : Int
-42.0 : Float
-42.000000
-42.000000
-42.000000

0 comments on commit d408c83

Please sign in to comment.