diff --git a/library/init/algebra.lean b/library/init/algebra.lean new file mode 100644 index 0000000000..13e97c8cd8 --- /dev/null +++ b/library/init/algebra.lean @@ -0,0 +1,57 @@ +/- +Copyright (c) 2016 Microsoft Corporation. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Leonardo de Moura +-/ +prelude +import init.logic init.binary init.combinator init.meta.interactive + +universe variable u + +class semigroup (A : Type u) extends has_mul A := +(mul_assoc : ∀ a b c : A, a * b * c = a * (b * c)) + +variables {A : Type u} + +@[simp] lemma mul_assoc [semigroup A] : ∀ a b c : A, a * b * c = a * (b * c) := +semigroup.mul_assoc + +class comm_semigroup (A : Type u) extends semigroup A := +(mul_comm : ∀ a b : A, a * b = b * a) + +@[simp] lemma mul_comm [comm_semigroup A] : ∀ a b : A, a * b = b * a := +comm_semigroup.mul_comm + +@[simp] lemma mul_left_comm [comm_semigroup A] : ∀ a b c : A, a * (b * c) = b * (a * c) := +left_comm mul mul_comm mul_assoc + +class left_cancel_semigroup (A : Type u) extends semigroup A := +(mul_left_cancel : ∀ a b c : A, a * b = a * c → b = c) + +lemma mul_left_cancel [left_cancel_semigroup A] {a b c : A} : a * b = a * c → b = c := +left_cancel_semigroup.mul_left_cancel a b c + +class right_cancel_semigroup (A : Type u) extends semigroup A := +(mul_right_cancel : ∀ a b c : A, a * b = c * b → a = c) + +lemma mul_right_cancel [right_cancel_semigroup A] {a b c : A} : a * b = c * b → a = c := +right_cancel_semigroup.mul_right_cancel a b c + +class monoid (A : Type u) extends semigroup A, has_one A := +(one_mul : ∀ a : A, 1 * a = a) (mul_one : ∀ a : A, a * 1 = a) + +@[simp] lemma one_mul [monoid A] : ∀ a : A, 1 * a = a := +monoid.one_mul + +@[simp] lemma mul_one [monoid A] : ∀ a : A, a * 1 = a := +monoid.mul_one + +class comm_monoid (A : Type u) extends monoid A, comm_semigroup A + +class group (A : Type u) extends monoid A, has_inv A := +(mul_left_inv : ∀ a : A, a⁻¹ * a = 1) + +@[simp] lemma mul_left_inv [group A] : ∀ a : A, a⁻¹ * a = 1 := +group.mul_left_inv + +class comm_group (A : Type u) extends group A, comm_monoid A diff --git a/library/init/default.lean b/library/init/default.lean index c388186d56..3e6b056fa2 100644 --- a/library/init/default.lean +++ b/library/init/default.lean @@ -12,4 +12,4 @@ import init.monad init.option init.state init.fin init.list init.char init.strin import init.monad_combinators init.set import init.timeit init.trace init.unsigned init.ordering init.list_classes init.coe import init.wf init.nat_div init.meta init.instances -import init.sigma_lex init.simplifier init.id_locked +import init.sigma_lex init.simplifier init.id_locked init.algebra diff --git a/tests/lean/run/group.lean b/tests/lean/run/group.lean deleted file mode 100644 index 4c153525e3..0000000000 --- a/tests/lean/run/group.lean +++ /dev/null @@ -1,38 +0,0 @@ -section - variable {A : Type*} - variable f : A → A → A - variable one : A - variable inv : A → A - local infixl `*` := f - local postfix `^-1`:100 := inv - definition is_assoc := ∀ a b c, (a*b)*c = a*b*c - definition is_id := ∀ a, a*one = a - definition is_inv := ∀ a, a*a^-1 = one -end - -inductive [class] group_struct (A : Type*) : Type* -| mk_group_struct : Π (mul : A → A → A) (one : A) (inv : A → A), is_assoc mul → is_id mul one → is_inv mul one inv → group_struct - -inductive group : Type* -| mk_group : Π (A : Type*), group_struct A → group - -definition carrier (g : group) : Type* -:= group.rec (λ c s, c) g - -attribute [instance] -definition group_to_struct (g : group) : group_struct (carrier g) -:= group.rec (λ (A : Type*) (s : group_struct A), s) g - -check group_struct - -definition mul1 {A : Type*} {s : group_struct A} (a b : A) : A -:= group_struct.rec (λ mul one inv h1 h2 h3, mul) s a b - -infixl `*` := mul1 - -constant G1 : group.{1} -constant G2 : group.{1} -constants a b c : (carrier G2) -constants d e : (carrier G1) -check a * b * b -check d * e diff --git a/tests/lean/run/record10.lean b/tests/lean/run/record10.lean index 8611ce4465..dc833a745d 100644 --- a/tests/lean/run/record10.lean +++ b/tests/lean/run/record10.lean @@ -1,6 +1,3 @@ -structure [class] semigroup (A : Type) extends has_mul A := -(assoc : ∀ a b c, mul (mul a b) c = mul a (mul b c)) - print prefix semigroup print "=======================" diff --git a/tests/lean/run/simplifier_canonize_instances.lean b/tests/lean/run/simplifier_canonize_instances.lean index fe167f1b00..4cbc34ac2a 100644 --- a/tests/lean/run/simplifier_canonize_instances.lean +++ b/tests/lean/run/simplifier_canonize_instances.lean @@ -11,8 +11,6 @@ do (new_target, Heq) ← target >>= simplify failed [], try reflexivity universe l -constants (group : Type* → Type) -attribute [class] group constants (f₁ : Π (X : Type*) (X_grp : group X), X) constants (f₂ : Π (X : Type*) [X_grp : group X], X) diff --git a/tests/lean/run/st_options.lean b/tests/lean/run/st_options.lean deleted file mode 100644 index f981d9a6ca..0000000000 --- a/tests/lean/run/st_options.lean +++ /dev/null @@ -1,59 +0,0 @@ -structure [class] semigroup (A : Type*) extends has_mul A := -(mul_assoc : ∀a b c, mul (mul a b) c = mul a (mul b c)) - -structure [class] comm_semigroup (A : Type*) extends semigroup A := -(mul_comm : ∀a b, mul a b = mul b a) - -structure [class] left_cancel_semigroup (A : Type*) extends semigroup A := -(mul_left_cancel : ∀a b c, mul a b = mul a c → b = c) - -structure [class] right_cancel_semigroup (A : Type*) extends semigroup A := -(mul_right_cancel : ∀a b c, mul a b = mul c b → a = c) - -structure [class] add_semigroup (A : Type*) extends has_add A := -(add_assoc : ∀a b c, add (add a b) c = add a (add b c)) - -structure [class] add_comm_semigroup (A : Type*) extends add_semigroup A := -(add_comm : ∀a b, add a b = add b a) - -structure [class] add_left_cancel_semigroup (A : Type*) extends add_semigroup A := -(add_left_cancel : ∀a b c, add a b = add a c → b = c) - -structure [class] add_right_cancel_semigroup (A : Type*) extends add_semigroup A := -(add_right_cancel : ∀a b c, add a b = add c b → a = c) - -structure [class] monoid (A : Type*) extends semigroup A, has_one A := -(one_mul : ∀a, mul one a = a) (mul_one : ∀a, mul a one = a) - -structure [class] comm_monoid (A : Type*) extends monoid A, comm_semigroup A - -structure [class] add_monoid (A : Type*) extends add_semigroup A, has_zero A := -(zero_add : ∀a, add zero a = a) (add_zero : ∀a, add a zero = a) - -structure [class] add_comm_monoid (A : Type*) extends add_monoid A, add_comm_semigroup A - -structure [class] group (A : Type*) extends monoid A, has_inv A := -(mul_left_inv : ∀a, mul (inv a) a = one) - -structure [class] comm_group (A : Type*) extends group A, comm_monoid A - -structure [class] add_group (A : Type*) extends add_monoid A, has_neg A := -(add_left_inv : ∀a, add (neg a) a = zero) - -structure [class] add_comm_group (A : Type*) extends add_group A, add_comm_monoid A - -structure [class] distrib (A : Type*) extends has_mul A, has_add A := -(left_distrib : ∀a b c, mul a (add b c) = add (mul a b) (mul a c)) -(right_distrib : ∀a b c, mul (add a b) c = add (mul a c) (mul b c)) - -structure [class] mul_zero_class (A : Type*) extends has_mul A, has_zero A := -(zero_mul : ∀a, mul zero a = zero) -(mul_zero : ∀a, mul a zero = zero) - -structure [class] zero_ne_one_class (A : Type*) extends has_zero A, has_one A := -(zero_ne_one : zero ≠ one) - -structure [class] semiring (A : Type*) extends add_comm_monoid A, monoid A, distrib A, - mul_zero_class A, zero_ne_one_class A - -set_option pp.implicit true diff --git a/tests/lean/struct_class.lean b/tests/lean/struct_class.lean index 1cd37eb1ee..3b9d0ca652 100644 --- a/tests/lean/struct_class.lean +++ b/tests/lean/struct_class.lean @@ -1,3 +1,6 @@ +prelude +import init.core + structure [class] point (A : Type*) (B : Type*) := mk :: (x : A) (y : B) diff --git a/tests/lean/struct_class.lean.expected.out b/tests/lean/struct_class.lean.expected.out index 9454a1c030..f57ccc59d3 100644 --- a/tests/lean/struct_class.lean.expected.out +++ b/tests/lean/struct_class.lean.expected.out @@ -1,14 +1,7 @@ -alternative : (Type u → Type v) → Type (max (u+1) v) -applicative : (Type u → Type v) → Type (max (u+1) v) decidable : Prop → Type -functor : (Type u → Type v) → Type (max (u+1) v) has_add : Type u → Type (max 1 u) has_andthen : Type u → Type (max 1 u) has_append : Type u → Type (max 1 u) -has_coe : Type u → Type v → Type (max 1 (imax u v)) -has_coe_t : Type u → Type v → Type (max 1 (imax u v)) -has_coe_to_fun : Type u → Type (max u (v+1)) -has_coe_to_sort : Type u → Type (max u (v+1)) has_div : Type u → Type (max 1 u) has_dvd : Type u → Type (max 1 u) has_emptyc : Type u → Type (max 1 u) @@ -16,45 +9,25 @@ has_insert : Type u → (Type u → Type v) → Type (max 1 (imax u v)) has_inter : Type u → Type (max 1 u) has_inv : Type u → Type (max 1 u) has_le : Type u → Type (max 1 u) -has_lift : Type u → Type v → Type (max 1 (imax u v)) -has_lift_t : Type u → Type v → Type (max 1 (imax u v)) has_lt : Type u → Type (max 1 u) has_mem : Type u → (Type u → Type v) → Type (max 1 u v) has_mod : Type u → Type (max 1 u) has_mul : Type u → Type (max 1 u) has_neg : Type u → Type (max 1 u) has_one : Type u → Type (max 1 u) -has_ordering : Type → Type has_sdiff : Type u → Type (max 1 u) has_sep : Type u → (Type u → Type v) → Type (max 1 (imax (max 1 u) v)) has_sizeof : Type u → Type (max 1 u) has_ssubset : Type u → Type (max 1 u) has_sub : Type u → Type (max 1 u) has_subset : Type u → Type (max 1 u) -has_to_format : Type u → Type (max 1 u) -has_to_pexpr : Type u → Type (max 1 u) -has_to_string : Type u → Type (max 1 u) -has_to_tactic_format : Type → Type has_union : Type u → Type (max 1 u) has_zero : Type u → Type (max 1 u) -inhabited : Type u → Type (max 1 u) -is_associative : Π {A : Type u}, (A → A → A) → Type -monad : (Type u → Type v) → Type (max (u+1) v) -nonempty : Type u → Prop point : Type u_1 → Type u_2 → Type (max 1 u_1 u_2) -setoid : Type u → Type (max 1 u) -subsingleton : Type u → Prop -alternative : (Type u → Type v) → Type (max (u+1) v) -applicative : (Type u → Type v) → Type (max (u+1) v) decidable : Prop → Type -functor : (Type u → Type v) → Type (max (u+1) v) has_add : Type u → Type (max 1 u) has_andthen : Type u → Type (max 1 u) has_append : Type u → Type (max 1 u) -has_coe : Type u → Type v → Type (max 1 (imax u v)) -has_coe_t : Type u → Type v → Type (max 1 (imax u v)) -has_coe_to_fun : Type u → Type (max u (v+1)) -has_coe_to_sort : Type u → Type (max u (v+1)) has_div : Type u → Type (max 1 u) has_dvd : Type u → Type (max 1 u) has_emptyc : Type u → Type (max 1 u) @@ -62,31 +35,18 @@ has_insert : Type u → (Type u → Type v) → Type (max 1 (imax u v)) has_inter : Type u → Type (max 1 u) has_inv : Type u → Type (max 1 u) has_le : Type u → Type (max 1 u) -has_lift : Type u → Type v → Type (max 1 (imax u v)) -has_lift_t : Type u → Type v → Type (max 1 (imax u v)) has_lt : Type u → Type (max 1 u) has_mem : Type u → (Type u → Type v) → Type (max 1 u v) has_mod : Type u → Type (max 1 u) has_mul : Type u → Type (max 1 u) has_neg : Type u → Type (max 1 u) has_one : Type u → Type (max 1 u) -has_ordering : Type → Type has_sdiff : Type u → Type (max 1 u) has_sep : Type u → (Type u → Type v) → Type (max 1 (imax (max 1 u) v)) has_sizeof : Type u → Type (max 1 u) has_ssubset : Type u → Type (max 1 u) has_sub : Type u → Type (max 1 u) has_subset : Type u → Type (max 1 u) -has_to_format : Type u → Type (max 1 u) -has_to_pexpr : Type u → Type (max 1 u) -has_to_string : Type u → Type (max 1 u) -has_to_tactic_format : Type → Type has_union : Type u → Type (max 1 u) has_zero : Type u → Type (max 1 u) -inhabited : Type u → Type (max 1 u) -is_associative : Π {A : Type u}, (A → A → A) → Type -monad : (Type u → Type v) → Type (max (u+1) v) -nonempty : Type u → Prop point : Type u_1 → Type u_2 → Type (max 1 u_1 u_2) -setoid : Type u → Type (max 1 u) -subsingleton : Type u → Prop