Skip to content
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

discussion: describe relation between Foldable and Unfoldable and there indexed versions #84

Open
safareli opened this issue Nov 29, 2017 · 6 comments
Labels
status: needs more info This issue needs more info before any action can be done.

Comments

@safareli
Copy link
Contributor

safareli commented Nov 29, 2017

I had an idea:

What if instead of

Map.fromFoldable $ Map.toUnfoldable someMap

You could do:

unfoldWithIndex $ foldWithIndex someMap

where

foldWithIndex   ∷ ∀ g f idx a
    . Unfoldable g
    ⇒ FolableWithIndex idx f
    ⇒ f  idx   a
    → g (idx × a)
unfoldWithIndex ∷ ∀ g f idx a
    . Foldable g
    ⇒ UnfoldableWithIndex idx f
    ⇒ g (idx × a)
    → f  idx   a

p.s. names foldWithIndex and unfoldWithIndex are arbitrary just types meter

@safareli
Copy link
Contributor Author

this two functions are describing relation between all 4 classes, we can derive a law
unfoldWithIndex <<< foldWithIndex === id

doing it the other way might be be an id (duplicate indexes in list for example)
unfoldWithIndex >>> foldWithIndex === id

@paf31
Copy link
Contributor

paf31 commented Nov 29, 2017

I don't think you need a new UnfoldableWithIndex class, just unfold the index as you go.

@safareli
Copy link
Contributor Author

yes but in that case you need special functions like Map.fromFoldable.
when Map could be an instance of UnfoldableWithIndex which means one could do unfoldrWithIndex and get as result a Map or any UnfoldableWithIndex.

@paf31
Copy link
Contributor

paf31 commented Nov 29, 2017

Oh I see, you're unfolding with access to the key of the Map. There's something wrong with the kinds in your types though. f is applied to too many type arguments.

class UnfoldableWithIndex i f | f -> i where
  unfoldrWithIndex :: (s -> Maybe ({ value :: a, index :: i, next :: s }) -> s -> f a

Is that what you want?

That seems strange to me, since I can't write an UnfoldableWithIndex Int List instance, for example.

@safareli
Copy link
Contributor Author

ah yes I was thinking index part was visible in types.
so they should be like this:

foldWithIndex ∷ ∀ g f idx a
    . Unfoldable g
    ⇒ FolableWithIndex idx f
    ⇒ f  a
    → g (idx × a)
unfoldWithIndex ∷ ∀ g f idx a
    . Foldable g
    ⇒ UnfoldableWithIndex idx f
    ⇒ g (idx × a)
    → f  a

That seems strange to me, since I can't write an UnfoldableWithIndex Int List instance, for example.

that's expected I think because linked-ness of List, I guss we can do:
UnfoldableWithIndex Int Array and UnfoldableWithIndex i (Map i)

@safareli
Copy link
Contributor Author

actually we can name them fromFoldable and toUnfoldable:

fromFoldable ∷ ∀ g f idx a
    . Foldable g
    ⇒ UnfoldableWithIndex idx f
    ⇒ g (idx × a)
    → f a
toUnfoldable ∷ ∀ g f idx a
    . Unfoldable g
    ⇒ FolableWithIndex idx f
    ⇒ f a
    → g (idx × a)

@JordanMartinez JordanMartinez added the status: needs more info This issue needs more info before any action can be done. label Dec 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs more info This issue needs more info before any action can be done.
Projects
None yet
Development

No branches or pull requests

3 participants