Zulip Chat Archive
Stream: new members
Topic: Custom inequality operators
Arjun Vedantham (Dec 11 2024 at 02:03):
Silly question, but is there a way to define custom inequality operators (less than, greater than, etc.) for inductive types?
Eric Wieser (Dec 11 2024 at 02:05):
Yes, write
instance : LE YourInductiveType where
le x y := sorry
Arjun Vedantham (Dec 11 2024 at 02:15):
Great, thanks. Posting this for posterity: https://lean-lang.org/functional_programming_in_lean/type-classes/standard-classes.html
edklencl (Dec 11 2024 at 02:22):
(deleted)
Arjun Vedantham (Dec 11 2024 at 21:31):
How can I use these operations in a proof? For context, I have a type like
inductive NatOrBool : Type where
| natural (n: ℕ)
| boolean (b: Bool)
and I want to prove something like
NatOrBool.natural 0 < NatOrBool.natural 1
but I'm not sure how to actually use the lt
definition for this type, or how to destructure the type to use Nat inequality here
Arjun Vedantham (Dec 11 2024 at 21:33):
I tried using apply NatOrBool.natural
, but that doesn't make any progress (probably not using it correctly?)
Kevin Buzzard (Dec 11 2024 at 22:00):
You're going to have to define lt
on your type first. Is .natural 1 < .boolean true
for example? You need to make an instance of LT NatOrBool
.
Arjun Vedantham (Dec 11 2024 at 22:05):
Right, I have an lt
definition for this type that looks like:
instance : LT NatOrBool where
lt x y := match x with
| NatOrBool.natural n => match y with
| NatOrBool.natural n' => n > n'
| NatOrBool.boolean _ => False
| NatOrBool.boolean _ => False
Kevin Buzzard (Dec 11 2024 at 22:11):
Well then you can't prove NatOrBool.natural 0 < NatOrBool.natural 1
because it's false :-)
Kevin Buzzard (Dec 11 2024 at 22:13):
import Mathlib
inductive NatOrBool : Type where
| natural (n: ℕ)
| boolean (b: Bool)
instance : LT NatOrBool where
lt x y := match x with
| NatOrBool.natural n => match y with
| NatOrBool.natural n' => n > n'
| NatOrBool.boolean _ => False
| NatOrBool.boolean _ => False
example : NatOrBool.natural 0 > NatOrBool.natural 1 := by
change 0 < 1
omega
Arjun Vedantham (Dec 11 2024 at 23:11):
...oh
right. lol. Thanks
Kevin Buzzard (Dec 12 2024 at 00:09):
Here's the kind of low-level API which you want to prove immediately after your definition, and then everything should be straightforward.
import Mathlib
inductive NatOrBool : Type where
| natural (n: ℕ)
| boolean (b: Bool)
instance : LT NatOrBool where
lt x y := match x with
| NatOrBool.natural n => match y with
| NatOrBool.natural n' => n < n' -- changed from >
| NatOrBool.boolean _ => False
| NatOrBool.boolean _ => False
namespace NatOrBool
lemma natural_lt_natural (a b : ℕ) : natural a < natural b ↔ a < b := by rfl
lemma not_lt_boolean (n : NatOrBool) (x : Bool) : ¬ n < boolean x := by
cases n
· rintro ⟨⟩
· rintro ⟨⟩
lemma boolean_not_lt (x : Bool) (n : NatOrBool) : ¬ boolean x < n := by
rintro ⟨⟩
end NatOrBool
Last updated: May 02 2025 at 03:31 UTC