-
Notifications
You must be signed in to change notification settings - Fork 454
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: upstream NatCast and IntCast (#3347)
This upstreams NatCast and IntCast alone independent of norm_cast in #3322. This will allow more efficiently upstreaming parts of Std.Data.Int relevant for omega. --------- Co-authored-by: Scott Morrison <[email protected]>
- Loading branch information
1 parent
e29d75a
commit 1d9074c
Showing
12 changed files
with
134 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/- | ||
Copyright (c) 2014 Mario Carneiro. All rights reserved. | ||
Released under Apache 2.0 license as described in the file LICENSE. | ||
Authors: Mario Carneiro, Gabriel Ebner | ||
-/ | ||
prelude | ||
import Init.Coe | ||
|
||
/-! | ||
# `NatCast` | ||
We introduce the typeclass `NatCast R` for a type `R` with a "canonical | ||
homomorphism" `Nat → R`. The typeclass carries the data of the function, | ||
but no required axioms. | ||
This typeclass was introduced to support a uniform `simp` normal form | ||
for such morphisms. | ||
Without such a typeclass, we would have specific coercions such as | ||
`Int.ofNat`, but also later the generic coercion from `Nat` into any | ||
Mathlib semiring (including `Int`), and we would need to use `simp` to | ||
move between them. However `simp` lemmas expressed using a non-normal | ||
form on the LHS would then not fire. | ||
Typically different instances of this class for the same target type `R` | ||
are definitionally equal, and so differences in the instance do not | ||
block `simp` or `rw`. | ||
This logic also applies to `Int` and so we also introduce `IntCast` alongside | ||
`Int. | ||
## Note about coercions into arbitrary types: | ||
Coercions such as `Nat.cast` that go from a concrete structure such as | ||
`Nat` to an arbitrary type `R` should be set up as follows: | ||
```lean | ||
instance : CoeTail Nat R where coe := ... | ||
instance : CoeHTCT Nat R where coe := ... | ||
``` | ||
It needs to be `CoeTail` instead of `Coe` because otherwise type-class | ||
inference would loop when constructing the transitive coercion `Nat → | ||
Nat → Nat → ...`. Sometimes we also need to declare the `CoeHTCT` | ||
instance if we need to shadow another coercion. | ||
-/ | ||
|
||
/-- Type class for the canonical homomorphism `Nat → R`. -/ | ||
class NatCast (R : Type u) where | ||
/-- The canonical map `Nat → R`. -/ | ||
protected natCast : Nat → R | ||
|
||
instance : NatCast Nat where natCast n := n | ||
|
||
/-- | ||
Canonical homomorphism from `Nat` to a type `R`. | ||
It contains just the function, with no axioms. | ||
In practice, the target type will likely have a (semi)ring structure, | ||
and this homomorphism should be a ring homomorphism. | ||
The prototypical example is `Int.ofNat`. | ||
This class and `IntCast` exist to allow different libraries with their own types that can be notated as natural numbers to have consistent `simp` normal forms without needing to create coercion simplification sets that are aware of all combinations. Libraries should make it easy to work with `NatCast` where possible. For instance, in Mathlib there will be such a homomorphism (and thus a `NatCast R` instance) whenever `R` is an additive monoid with a `1`. | ||
-/ | ||
@[coe, reducible, match_pattern] protected def Nat.cast {R : Type u} [NatCast R] : Nat → R := | ||
NatCast.natCast | ||
|
||
-- see the notes about coercions into arbitrary types in the module doc-string | ||
instance [NatCast R] : CoeTail Nat R where coe := Nat.cast | ||
|
||
-- see the notes about coercions into arbitrary types in the module doc-string | ||
instance [NatCast R] : CoeHTCT Nat R where coe := Nat.cast |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
def f1 : Int → Nat → Nat → Int := | ||
fun a b c => a + (Int.ofNat b - Int.ofNat c) | ||
fun a b c => a + (↑b - ↑c) | ||
def f2 : Int → Nat → Nat → Int := | ||
fun a b c => Int.ofNat b - Int.ofNat c + a | ||
fun a b c => ↑b - ↑c + a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
binrel_binop.lean:1:8-1:11: warning: declaration uses 'sorry' | ||
ex1 (a : Int) (b c : Nat) : a = Int.ofNat b - Int.ofNat c | ||
ex1 (a : Int) (b c : Nat) : a = ↑b - ↑c | ||
binrel_binop.lean:5:8-5:11: warning: declaration uses 'sorry' | ||
ex2 (a : Int) (b c : Nat) : a = Int.ofNat b - Int.ofNat c | ||
ex2 (a : Int) (b c : Nat) : a = ↑b - ↑c | ||
binrel_binop.lean:9:8-9:11: warning: declaration uses 'sorry' | ||
ex3 (a : Int) (b c : Nat) : a = Int.ofNat (b - c) | ||
ex3 (a : Int) (b c : Nat) : a = ↑(b - c) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
Int.ofNat n ^ 2 + m ^ 2 : Int | ||
↑n ^ 2 + m ^ 2 : Int | ||
n ^ 2 + 1 : Nat | ||
Int.ofNat n ^ 2 + 1 : Int | ||
Int.ofNat n ^ 2 + Int.ofNat 1 : Int | ||
↑n ^ 2 + 1 : Int | ||
↑n ^ 2 + ↑1 : Int | ||
q ^ n + 1 : Rat | ||
q ^ m + 1 : Rat | ||
q ^ Int.ofNat n + 1 : Rat | ||
q ^ ↑n + 1 : Rat | ||
12 * q + 1 ≤ 13 * q ^ 2 : Prop | ||
12 * q + 1 ≤ 13 * q ^ 2 : Prop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters