Skip to content

searching in list #989

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

Closed
mechvel opened this issue Dec 5, 2019 · 10 comments · Fixed by #2428
Closed

searching in list #989

mechvel opened this issue Dec 5, 2019 · 10 comments · Fixed by #2428

Comments

@mechvel
Copy link
Contributor

mechvel commented Dec 5, 2019

Searching in a list can be expressed via lib-1.2 as

search  :  ∀ {p} {P : Pred A p} → Decidable P → (xs : List A) →
                                                (∃ (\x → P x)) ⊎ (¬ Any P xs)
search P? xs  with any P? xs
...    | yes anyP-xs =   inj₁ (satisfied anyP-xs)
...    | no ¬anyP-xs =   inj₂ ¬anyP-xs 

First, is there a ready function in lib-1.2 to express this?
Second, it is more desirable to have (for a setoid)

search  :  ∀ {p} {P : Pred A p} → Decidable P → (xs : List A) →
                                               (∃ (\x → x ∈ xs × P x)) ⊎ (¬ Any P xs)

Has lib-1.2 something for this?

@gallais
Copy link
Member

gallais commented Dec 6, 2019

∃ (\x → x ∈ xs × P x)) is essentially Any P xs as find demonstrates.

So you can get that by using any, then cast Dec to a sum using fromDec,
and finally map over the first element of the sum using map₁.

@mechvel
Copy link
Contributor Author

mechvel commented Dec 6, 2019

This latter search version is usable and more generic than find, and its implementation needs a wise additional combination of any, fromDec, map₁.
So that it probably worth to add this search to the library.

@gallais
Copy link
Member

gallais commented Dec 6, 2019

In "Generic Level Polymorphic N-ary Functions", I have

decide : Π[ P ∪ Q ]  Π[ Any P ∪ All Q ]

It seems to also be missing from the stdlib and would also be a fine way
to implement this search via fromDec, find & All¬⇒¬Any.

@jamesmckinna
Copy link
Contributor

In "Generic Level Polymorphic N-ary Functions", I have

decide : Π[ P ∪ Q ]  Π[ Any P ∪ All Q ]

It seems to also be missing from the stdlib and would also be a fine way to implement this search via fromDec, find & All¬⇒¬Any.

decide was added in #1595 so you could in v2.1 define

module _ (P? : Decidable P) where

  private
    helper :  xs  All (∁ P) xs ⊎ Any P xs
    helper = All.decide (Sum.swap ∘ (Dec.toSum ∘ P?))

  search :  xs  ¬ Any P xs ⊎ ∃ (\x  x ∈ xs × P x)
  search = Sum.map All¬⇒¬Any find ∘ helper

But it's not clear (to me, at least) which, if any, of helper or search belongs in the library, by contrast with decide?
Suggest closing this issue?

@MatthewDaggitt
Copy link
Contributor

I wouldn't be against helper being added to Data.List.Relation.Unary.All. I feel we should avoid using \x → x ∈ xs × P x as much as possible so not too keen on search.

@jamesmckinna
Copy link
Contributor

I wouldn't be against helper being added to Data.List.Relation.Unary.All. I feel we should avoid using \x → x ∈ xs × P x as much as possible so not too keen on search.

Should we call this function (ie: helper) search instead (it's the 'essence' of search in Underwood, Gent and Caldwell "Search Algorithms in type Theory" Theoretical Computer Science 232 (2000) 55–90), and client-side post-processing (as above) can fix up the types to deliver whatever a user such as @mechvel might want instead...?

@mechvel
Copy link
Contributor Author

mechvel commented Jul 4, 2024

Concerning the essence of the subject (which I do not recall/understand now) , let the team decide, and then I would decide how to apply this. My deal is mainly to suggest an issue.

But there is a problem of the English language. My English is not enough to understand may be half of what jamesmckinna writes. I shall be grateful to people if they put sentences somehow in a simpler way.

@jamesmckinna
Copy link
Contributor

Thanks Sergei for the reminder to try to write in simpler language!

@mechvel
Copy link
Contributor Author

mechvel commented Jul 4, 2024

I somehow exaggerated this (a new word for me). At least this latter fragment of

and client-side post-processing (as above) can fix up the types to deliver whatever a user such as @mechvel might
want instead...?

remains totally enigmatic for me.
( Anyway I do not need to solve this particular sentence, because the team would make a decision, and then I would see what to do with it).

@jamesmckinna
Copy link
Contributor

Apologies ;again) for having been enigmatic!

My intention was to contrast

  • finding a definition which is a good 'fit' with existing definitions in the library
  • which nevertheless does not make it difficult for client uses, such as yours, to be available via writing adaptor code

Excessive jargon, and writing in too much haste on my phone, got in the way this time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants