Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - feat(data/sign): the sign function #12835

Closed
wants to merge 16 commits into from
44 changes: 44 additions & 0 deletions src/data/sign.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/-
Copyright (c) 2022 Eric Rodriguez. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Eric Rodriguez
-/
import order.basic

/-!
# Sign function

This file defines the sign function for types with zero and a decidable less-than relation, and
proves some basic theorems about it.
-/


variables {α : Type*} [has_zero α]

/-- The sign of an element - 1 if it's positive, -1 if negative, 0 otherwise. -/
ericrbg marked this conversation as resolved.
Show resolved Hide resolved
def sign [has_lt α] [decidable_rel ((<) : α → α → Prop)] (a : α) :=
ericrbg marked this conversation as resolved.
Show resolved Hide resolved
if 0 < a then (1 : ℤ) else if a < 0 then -1 else 0

section preorder

variables [preorder α] [decidable_rel ((<) : α → α → Prop)] {a b : α}

@[simp] lemma sign_zero : sign (0 : α) = 0 := by simp [sign]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For completeness, could you add the sign of positive and negative values?


end preorder

section linear_order

variables [linear_order α] {a b : α}

lemma sign_ne_zero (h : a ≠ 0) : sign a ≠ 0 :=
ericrbg marked this conversation as resolved.
Show resolved Hide resolved
begin
contrapose! h,
rw sign at h,
split_ifs at h,
{ cases h },
{ cases h },
exact ((lt_trichotomy a 0).resolve_left h_2).resolve_right h_1
end

end linear_order