@@ -607,19 +607,46 @@ def cofinite : filter α :=
607607 inter_sets := assume s t (hs : finite (-s)) (ht : finite (-t)),
608608 by simp [compl_inter, finite_union, ht, hs] }
609609
610- /-- The monadic bind operation on filter is defined the usual way in terms of `map` and `join`. -/
610+ /-- The monadic bind operation on filter is defined the usual way in terms of `map` and `join`.
611+
612+ Unfortunately, this `bind` does not result in the expected applicative. See `filter.seq` for the
613+ applicative instance. -/
611614def bind (f : filter α) (m : α → filter β) : filter β := join (map m f)
612615
613- instance : monad filter :=
614- { bind := @bind,
615- pure := λ(α : Type u) x, principal {x},
616- map := @filter.map }
616+ /-- The applicative sequentiation operation. This is not induced by the bind operation. -/
617+ def seq (f : filter (α → β)) (g : filter α) : filter β :=
618+ ⟨{ s | ∃u∈f.sets, ∃t∈g.sets, (∀m∈u, ∀x∈t, (m : α → β) x ∈ s) },
619+ ⟨univ, univ_mem_sets, univ, univ_mem_sets, by simp⟩,
620+ assume s₀ s₁ ⟨t₀, t₁, h₀, h₁, h⟩ hst, ⟨t₀, t₁, h₀, h₁, assume x hx y hy, hst $ h _ hx _ hy⟩,
621+ assume s₀ s₁ ⟨t₀, ht₀, t₁, ht₁, ht⟩ ⟨u₀, hu₀, u₁, hu₁, hu⟩,
622+ ⟨t₀ ∩ u₀, inter_mem_sets ht₀ hu₀, t₁ ∩ u₁, inter_mem_sets ht₁ hu₁,
623+ assume x ⟨hx₀, hx₁⟩ x ⟨hy₀, hy₁⟩, ⟨ht _ hx₀ _ hy₀, hu _ hx₁ _ hy₁⟩⟩⟩
624+
625+ instance : has_pure filter := ⟨λ(α : Type u) x, principal {x}⟩
626+
627+ instance : has_bind filter := ⟨@filter.bind⟩
628+
629+ instance : has_seq filter := ⟨@filter.seq⟩
630+
631+ instance : functor filter := { map := @filter.map }
617632
618- instance : is_lawful_monad filter :=
633+ section
634+ -- this section needs to be before applicative, otherwiese the wrong instance will be chosen
635+ protected def monad : monad filter := { map := @filter.map }
636+
637+ local attribute [instance] filter.monad
638+ protected def is_lawful_monad : is_lawful_monad filter :=
619639{ id_map := assume α f, filter_eq rfl,
620640 pure_bind := assume α β a f, by simp [bind, Sup_image],
621641 bind_assoc := assume α β γ f m₁ m₂, filter_eq rfl,
622642 bind_pure_comp_eq_map := assume α β f x, filter_eq $ by simp [bind, join, map, preimage, principal] }
643+ end
644+
645+ instance : applicative filter := { map := @filter.map, seq := @filter.seq }
646+
647+ instance : alternative filter :=
648+ { failure := λα, ⊥,
649+ orelse := λα x y, x ⊔ y }
623650
624651@[simp] lemma pure_def (x : α) : pure x = principal {x} := rfl
625652
@@ -630,10 +657,6 @@ by simp; exact id
630657
631658@[simp] lemma bind_def {α β} (f : filter α) (m : α → filter β) : f >>= m = bind f m := rfl
632659
633- instance : alternative filter :=
634- { failure := λα, ⊥,
635- orelse := λα x y, x ⊔ y }
636-
637660/- map and comap equations -/
638661section map
639662variables {f f₁ f₂ : filter α} {g g₁ g₂ : filter β} {m : α → β} {m' : β → γ} {s : set α} {t : set β}
@@ -871,14 +894,11 @@ show join (map f (principal s)) = (⨆x ∈ s, f x),
871894
872895lemma seq_mono {β : Type u} {f₁ f₂ : filter (α → β)} {g₁ g₂ : filter α}
873896 (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁ <*> g₁ ≤ f₂ <*> g₂ :=
874- le_trans (bind_mono2 hf) (bind_mono $ univ_mem_sets' $ assume f, map_mono hg)
897+ assume s ⟨t, ht, u, hu, h⟩, ⟨t, hf ht, u, hg hu, h⟩
875898
876899@[simp] lemma mem_pure_sets {a : α} {s : set α} :
877900 s ∈ (pure a : filter α).sets ↔ a ∈ s := by simp
878901
879- @[simp] lemma mem_return_sets {a : α} {s : set α} :
880- s ∈ (return a : filter α).sets ↔ a ∈ s := mem_pure_sets
881-
882902lemma infi_neq_bot_of_directed {f : ι → filter α}
883903 (hn : nonempty α) (hd : directed (≥) f) (hb : ∀i, f i ≠ ⊥): (infi f) ≠ ⊥ :=
884904let ⟨x⟩ := hn in
0 commit comments