This PR changes the interface of the `ForIn`, `ForIn'`, and `ForM` typeclasses to not take a `Monad m` parameter. This is a breaking change for most downstream `instance`s, which will will now need to assume `[Monad m]`. The rationale is that if the provider of an instance requires `m` to be a Monad, they should assume this up front. This makes it possible for the instanve to assume `LawfulMonad m` or some other stronger requirement, and also to provided a concrete instance for a particular `m` without assuming a non-canonical `Monad` structure on it. Zulip: [#lean4 > Monad assumptions in fields of other typeclasses @ 💬](https://leanprover.zulipchat.com/#narrow/channel/270676-lean4/topic/Monad.20assumptions.20in.20fields.20of.20other.20typeclasses/near/537102158)
67 lines
2.1 KiB
Text
67 lines
2.1 KiB
Text
/-
|
||
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
|
||
Released under Apache 2.0 license as described in the file LICENSE.
|
||
Author: Leonardo de Moura
|
||
-/
|
||
module
|
||
|
||
prelude
|
||
public import Lean.Data.PersistentHashMap
|
||
|
||
public section
|
||
|
||
namespace Lean
|
||
universe u v
|
||
|
||
structure PersistentHashSet (α : Type u) [BEq α] [Hashable α] where
|
||
(set : PersistentHashMap α Unit)
|
||
|
||
abbrev PHashSet (α : Type u) [BEq α] [Hashable α] := PersistentHashSet α
|
||
|
||
namespace PersistentHashSet
|
||
|
||
@[inline] def empty [BEq α] [Hashable α] : PersistentHashSet α :=
|
||
{ set := PersistentHashMap.empty }
|
||
|
||
instance [BEq α] [Hashable α] : Inhabited (PersistentHashSet α) where
|
||
default := empty
|
||
|
||
instance [BEq α] [Hashable α] : EmptyCollection (PersistentHashSet α) :=
|
||
⟨empty⟩
|
||
|
||
variable {_ : BEq α} {_ : Hashable α}
|
||
|
||
@[inline] def isEmpty (s : PersistentHashSet α) : Bool :=
|
||
s.set.isEmpty
|
||
|
||
@[inline] def insert (s : PersistentHashSet α) (a : α) : PersistentHashSet α :=
|
||
{ set := s.set.insert a () }
|
||
|
||
@[inline] def erase (s : PersistentHashSet α) (a : α) : PersistentHashSet α :=
|
||
{ set := s.set.erase a }
|
||
|
||
@[inline] def find? (s : PersistentHashSet α) (a : α) : Option α :=
|
||
match s.set.findEntry? a with
|
||
| some (a, _) => some a
|
||
| none => none
|
||
|
||
@[inline] def contains (s : PersistentHashSet α) (a : α) : Bool :=
|
||
s.set.contains a
|
||
|
||
@[inline] def foldM {β : Type v} {m : Type v → Type w} [Monad m] (f : β → α → m β) (init : β) (s : PersistentHashSet α) : m β :=
|
||
s.set.foldlM (init := init) fun d a _ => f d a
|
||
|
||
@[inline] def fold {β : Type v} (f : β → α → β) (init : β) (s : PersistentHashSet α) : β :=
|
||
Id.run $ s.foldM (pure <| f · ·) init
|
||
|
||
def toList (s : PersistentHashSet α) : List α :=
|
||
s.set.toList.map (·.1)
|
||
|
||
protected def forIn {_ : BEq α} {_ : Hashable α} [Monad m]
|
||
(s : PersistentHashSet α) (init : σ) (f : α → σ → m (ForInStep σ)) : m σ := do
|
||
PersistentHashMap.forIn s.set init fun p s => f p.1 s
|
||
|
||
instance {_ : BEq α} {_ : Hashable α} [Monad m] : ForIn m (PersistentHashSet α) α where
|
||
forIn := PersistentHashSet.forIn
|
||
|
||
end PersistentHashSet
|