/- Copyright (c) 2022 YaΓ«l Dillies, Violeta HernΓ‘ndez Palacios. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: YaΓ«l Dillies, Violeta HernΓ‘ndez Palacios, Grayson Burton, Vladimir Ivanov -/ import data.nat.interval import data.int.succ_pred import order.atoms /-! # Graded orders This file defines graded orders, also known as ranked orders. A `𝕆`-graded order is an order `Ξ±` equipped with a distinguished "grade" function `Ξ± β†’ 𝕆` which should be understood as giving the "height" of the elements. Usual graded orders are `β„•`-graded, cograded orders are `β„•α΅’α΅ˆ`-graded, but we can also grade by `β„€`, and polytopes are naturally `fin n`-graded. Visually, `grade β„• a` is the height of `a` in the Hasse diagram of `Ξ±`. ## Main declarations * `grade_order`: Graded order. * `grade_min_order`: Graded order where minimal elements have minimal grades. * `grade_max_order`: Graded order where maximal elements have maximal grades. * `grade_bounded_order`: Graded order where minimal elements have minimal grades and maximal elements have maximal grades. * `grade`: The grade of an element. Because an order can admit several gradings, the first argument is the order we grade by. * `grade_max_order`: Graded orders with maximal elements. All maximal elements have the same grade. * `max_grade`: The maximum grade in a `grade_max_order`. * `order_embedding.grade`: The grade of an element in a linear order as an order embedding. ## How to grade your order Here are the translations between common references and our `grade_order`: * [Stanley][stanley2012] defines a graded order of rank `n` as an order where all maximal chains have "length" `n` (so the number of elements of a chain is `n + 1`). This corresponds to `grade_bounded_order (fin (n + 1)) Ξ±`. * [Engel][engel1997]'s ranked orders are somewhere between `grade_order β„• Ξ±` and `grade_min_order β„• Ξ±`, in that he requires `βˆƒ a, is_min a ∧ grade β„• a + 0` rather than `βˆ€ a, is_min a β†’ grade β„• a = 0`. He defines a graded order as an order where all minimal elements have grade `0` and all maximal elements have the same grade. This is roughly a less bundled version of `grade_bounded_order (fin n) Ξ±`, assuming we discard orders with infinite chains. ## Implementation notes One possible definition of graded orders is as the bounded orders whose flags (maximal chains) all have the same finite length (see Stanley p. 99). However, this means that all graded orders must have minimal and maximal elements and that the grade is not data. Instead, we define graded orders by their grade function, without talking about flags yet. ## References * [Konrad Engel, *Sperner Theory*][engel1997] * [Richard Stanley, *Enumerative Combinatorics*][stanley2012] -/ set_option old_structure_cmd true open finset nat order_dual variables {𝕆 β„™ Ξ± Ξ² : Type*} /-- An `𝕆`-graded order is an order `Ξ±` equipped with a strictly monotone function `grade 𝕆 : Ξ± β†’ 𝕆` which preserves order covering (`covby`). -/ class grade_order (𝕆 Ξ± : Type*) [preorder 𝕆] [preorder Ξ±] := (grade : Ξ± β†’ 𝕆) (grade_strict_mono : strict_mono grade) (covby_grade ⦃a b : α⦄ : a β‹– b β†’ grade a β‹– grade b) /-- A `𝕆`-graded order where minimal elements have minimal grades. -/ class grade_min_order (𝕆 Ξ± : Type*) [preorder 𝕆] [preorder Ξ±] extends grade_order 𝕆 Ξ± := (is_min_grade ⦃a : α⦄ : is_min a β†’ is_min (grade a)) /-- A `𝕆`-graded order where maximal elements have maximal grades. -/ class grade_max_order (𝕆 Ξ± : Type*) [preorder 𝕆] [preorder Ξ±] extends grade_order 𝕆 Ξ± := (is_max_grade ⦃a : α⦄ : is_max a β†’ is_max (grade a)) /-- A `𝕆`-graded order where minimal elements have minimal grades and maximal elements have maximal grades. -/ class grade_bounded_order (𝕆 Ξ± : Type*) [preorder 𝕆] [preorder Ξ±] extends grade_min_order 𝕆 Ξ±, grade_max_order 𝕆 Ξ± section preorder -- grading variables [preorder 𝕆] section preorder -- graded order variables [preorder Ξ±] section grade_order variables (𝕆) [grade_order 𝕆 Ξ±] {a b : Ξ±} /-- The grade of an element in a graded order. Morally, this is the number of elements you need to go down by to get to `βŠ₯`. -/ def grade : Ξ± β†’ 𝕆 := grade_order.grade protected lemma covby.grade (h : a β‹– b) : grade 𝕆 a β‹– grade 𝕆 b := grade_order.covby_grade h variables {𝕆} lemma grade_strict_mono : strict_mono (grade 𝕆 : Ξ± β†’ 𝕆) := grade_order.grade_strict_mono lemma covby_iff_lt_covby_grade : a β‹– b ↔ a < b ∧ grade 𝕆 a β‹– grade 𝕆 b := ⟨λ h, ⟨h.1, h.grade _⟩, and.imp_right $ Ξ» h c ha hb, h.2 (grade_strict_mono ha) $ grade_strict_mono hb⟩ end grade_order section grade_min_order variables (𝕆) [grade_min_order 𝕆 Ξ±] {a : Ξ±} protected lemma is_min.grade (h : is_min a) : is_min (grade 𝕆 a) := grade_min_order.is_min_grade h variables {𝕆} @[simp] lemma is_min_grade_iff : is_min (grade 𝕆 a) ↔ is_min a := ⟨grade_strict_mono.is_min_of_apply, is_min.grade _⟩ end grade_min_order section grade_max_order variables (𝕆) [grade_max_order 𝕆 Ξ±] {a : Ξ±} protected lemma is_max.grade (h : is_max a) : is_max (grade 𝕆 a) := grade_max_order.is_max_grade h variables {𝕆} @[simp] lemma is_max_grade_iff : is_max (grade 𝕆 a) ↔ is_max a := ⟨grade_strict_mono.is_max_of_apply, is_max.grade _⟩ end grade_max_order end preorder -- graded order lemma grade_mono [partial_order Ξ±] [grade_order 𝕆 Ξ±] : monotone (grade 𝕆 : Ξ± β†’ 𝕆) := grade_strict_mono.monotone section linear_order -- graded order variables [linear_order Ξ±] [grade_order 𝕆 Ξ±] {a b : Ξ±} lemma grade_injective : function.injective (grade 𝕆 : Ξ± β†’ 𝕆) := grade_strict_mono.injective @[simp] lemma grade_le_grade_iff : grade 𝕆 a ≀ grade 𝕆 b ↔ a ≀ b := grade_strict_mono.le_iff_le @[simp] lemma grade_lt_grade_iff : grade 𝕆 a < grade 𝕆 b ↔ a < b := grade_strict_mono.lt_iff_lt @[simp] lemma grade_eq_grade_iff : grade 𝕆 a = grade 𝕆 b ↔ a = b := grade_injective.eq_iff lemma grade_ne_grade_iff : grade 𝕆 a β‰  grade 𝕆 b ↔ a β‰  b := grade_injective.ne_iff lemma grade_covby_grade_iff : grade 𝕆 a β‹– grade 𝕆 b ↔ a β‹– b := (covby_iff_lt_covby_grade.trans $ and_iff_right_of_imp $ Ξ» h, grade_lt_grade_iff.1 h.1).symm end linear_order -- graded order end preorder -- grading section partial_order variables [partial_order 𝕆] [preorder Ξ±] @[simp] lemma grade_bot [order_bot 𝕆] [order_bot Ξ±] [grade_min_order 𝕆 Ξ±] : grade 𝕆 (βŠ₯ : Ξ±) = βŠ₯ := (is_min_bot.grade _).eq_bot @[simp] lemma grade_top [order_top 𝕆] [order_top Ξ±] [grade_max_order 𝕆 Ξ±] : grade 𝕆 (⊀ : Ξ±) = ⊀ := (is_max_top.grade _).eq_top end partial_order /-! ### Instances -/ variables [preorder 𝕆] [preorder β„™] [preorder Ξ±] [preorder Ξ²] instance preorder.to_grade_bounded_order : grade_bounded_order Ξ± Ξ± := { grade := id, is_min_grade := Ξ» _, id, is_max_grade := Ξ» _, id, grade_strict_mono := strict_mono_id, covby_grade := Ξ» a b, id } @[simp] lemma grade_self (a : Ξ±) : grade Ξ± a = a := rfl /-! #### Dual -/ instance [grade_order 𝕆 Ξ±] : grade_order π•†α΅’α΅ˆ Ξ±α΅’α΅ˆ := { grade := to_dual ∘ grade 𝕆 ∘ of_dual, grade_strict_mono := grade_strict_mono.dual, covby_grade := Ξ» a b h, (h.of_dual.grade _).to_dual } instance [grade_max_order 𝕆 Ξ±] : grade_min_order π•†α΅’α΅ˆ Ξ±α΅’α΅ˆ := { is_min_grade := Ξ» _, is_max.grade _, ..order_dual.grade_order } instance [grade_min_order 𝕆 Ξ±] : grade_max_order π•†α΅’α΅ˆ Ξ±α΅’α΅ˆ := { is_max_grade := Ξ» _, is_min.grade _, ..order_dual.grade_order } instance [grade_bounded_order 𝕆 Ξ±] : grade_bounded_order π•†α΅’α΅ˆ Ξ±α΅’α΅ˆ := { ..order_dual.grade_min_order, ..order_dual.grade_max_order } @[simp] lemma grade_to_dual [grade_order 𝕆 Ξ±] (a : Ξ±) : grade π•†α΅’α΅ˆ (to_dual a) = to_dual (grade 𝕆 a) := rfl @[simp] lemma grade_of_dual [grade_order 𝕆 Ξ±] (a : Ξ±α΅’α΅ˆ) : grade 𝕆 (of_dual a) = of_dual (grade π•†α΅’α΅ˆ a) := rfl /-! #### Lifting a graded order -/ /-- Lifts a graded order along a strictly monotone function. -/ @[reducible] -- See note [reducible non-instances] def grade_order.lift_left [grade_order 𝕆 Ξ±] (f : 𝕆 β†’ β„™) (hf : strict_mono f) (hcovby : βˆ€ a b, a β‹– b β†’ f a β‹– f b) : grade_order β„™ Ξ± := { grade := f ∘ grade 𝕆, grade_strict_mono := hf.comp grade_strict_mono, covby_grade := Ξ» a b h, hcovby _ _ $ h.grade _ } /-- Lifts a graded order along a strictly monotone function. -/ @[reducible] -- See note [reducible non-instances] def grade_min_order.lift_left [grade_min_order 𝕆 Ξ±] (f : 𝕆 β†’ β„™) (hf : strict_mono f) (hcovby : βˆ€ a b, a β‹– b β†’ f a β‹– f b) (hmin : βˆ€ a, is_min a β†’ is_min (f a)) : grade_min_order β„™ Ξ± := { is_min_grade := Ξ» a ha, hmin _ $ ha.grade _, ..grade_order.lift_left f hf hcovby } /-- Lifts a graded order along a strictly monotone function. -/ @[reducible] -- See note [reducible non-instances] def grade_max_order.lift_left [grade_max_order 𝕆 Ξ±] (f : 𝕆 β†’ β„™) (hf : strict_mono f) (hcovby : βˆ€ a b, a β‹– b β†’ f a β‹– f b) (hmax : βˆ€ a, is_max a β†’ is_max (f a)) : grade_max_order β„™ Ξ± := { is_max_grade := Ξ» a ha, hmax _ $ ha.grade _, ..grade_order.lift_left f hf hcovby } /-- Lifts a graded order along a strictly monotone function. -/ @[reducible] -- See note [reducible non-instances] def grade_bounded_order.lift_left [grade_bounded_order 𝕆 Ξ±] (f : 𝕆 β†’ β„™) (hf : strict_mono f) (hcovby : βˆ€ a b, a β‹– b β†’ f a β‹– f b) (hmin : βˆ€ a, is_min a β†’ is_min (f a)) (hmax : βˆ€ a, is_max a β†’ is_max (f a)) : grade_bounded_order β„™ Ξ± := { ..grade_min_order.lift_left f hf hcovby hmin, ..grade_max_order.lift_left f hf hcovby hmax } /-- Lifts a graded order along a strictly monotone function. -/ @[reducible] -- See note [reducible non-instances] def grade_order.lift_right [grade_order 𝕆 Ξ²] (f : Ξ± β†’ Ξ²) (hf : strict_mono f) (hcovby : βˆ€ a b, a β‹– b β†’ f a β‹– f b) : grade_order 𝕆 Ξ± := { grade := grade 𝕆 ∘ f, grade_strict_mono := grade_strict_mono.comp hf, covby_grade := Ξ» a b h, (hcovby _ _ h).grade _ } /-- Lifts a graded order along a strictly monotone function. -/ @[reducible] -- See note [reducible non-instances] def grade_min_order.lift_right [grade_min_order 𝕆 Ξ²] (f : Ξ± β†’ Ξ²) (hf : strict_mono f) (hcovby : βˆ€ a b, a β‹– b β†’ f a β‹– f b) (hmin : βˆ€ a, is_min a β†’ is_min (f a)) : grade_min_order 𝕆 Ξ± := { is_min_grade := Ξ» a ha, (hmin _ ha).grade _, ..grade_order.lift_right f hf hcovby } /-- Lifts a graded order along a strictly monotone function. -/ @[reducible] -- See note [reducible non-instances] def grade_max_order.lift_right [grade_max_order 𝕆 Ξ²] (f : Ξ± β†’ Ξ²) (hf : strict_mono f) (hcovby : βˆ€ a b, a β‹– b β†’ f a β‹– f b) (hmax : βˆ€ a, is_max a β†’ is_max (f a)) : grade_max_order 𝕆 Ξ± := { is_max_grade := Ξ» a ha, (hmax _ ha).grade _, ..grade_order.lift_right f hf hcovby } /-- Lifts a graded order along a strictly monotone function. -/ @[reducible] -- See note [reducible non-instances] def grade_bounded_order.lift_right [grade_bounded_order 𝕆 Ξ²] (f : Ξ± β†’ Ξ²) (hf : strict_mono f) (hcovby : βˆ€ a b, a β‹– b β†’ f a β‹– f b) (hmin : βˆ€ a, is_min a β†’ is_min (f a)) (hmax : βˆ€ a, is_max a β†’ is_max (f a)) : grade_bounded_order 𝕆 Ξ± := { ..grade_min_order.lift_right f hf hcovby hmin, ..grade_max_order.lift_right f hf hcovby hmax } /-! #### `fin n`-graded to `β„•`-graded to `β„€`-graded -/ /-- A `fin n`-graded order is also `β„•`-graded. We do not mark this an instance because `n` is not inferrable. -/ @[reducible] -- See note [reducible non-instances] def grade_order.fin_to_nat (n : β„•) [grade_order (fin n) Ξ±] : grade_order β„• Ξ± := grade_order.lift_left (_ : fin n β†’ β„•) fin.coe_strict_mono $ Ξ» _ _, covby.coe_fin /-- A `fin n`-graded order is also `β„•`-graded. We do not mark this an instance because `n` is not inferrable. -/ @[reducible] -- See note [reducible non-instances] def grade_min_order.fin_to_nat (n : β„•) [grade_min_order (fin n) Ξ±] : grade_min_order β„• Ξ± := grade_min_order.lift_left (_ : fin n β†’ β„•) fin.coe_strict_mono (Ξ» _ _, covby.coe_fin) $ Ξ» a h, begin unfreezingI { cases n }, { exact (@fin.elim0 (Ξ» _, false) $ grade (fin 0) a).elim }, rw [h.eq_bot, fin.bot_eq_zero], exact is_min_bot, end instance grade_order.nat_to_int [grade_order β„• Ξ±] : grade_order β„€ Ξ± := grade_order.lift_left _ int.coe_nat_strict_mono $ Ξ» _ _, covby.cast_int