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

Add MonadFix instance for Vector #178

Closed
treeowl opened this issue Jul 5, 2017 · 2 comments · Fixed by #312
Closed

Add MonadFix instance for Vector #178

treeowl opened this issue Jul 5, 2017 · 2 comments · Fixed by #312
Milestone

Comments

@treeowl
Copy link
Contributor

treeowl commented Jul 5, 2017

I believe this should be equivalent to the MonadFix instance for []:

instance MonadFix Vector where
  mfix f
    | V.null v0 = V.empty
    | otherwise = v
    where
      v = V.generate (V.length v0)
                     (\i -> if i == 0
                            then V.head v0
                            else f (v ! i) ! i)
      v0 = fix (f . V.head)

Of course, that generate can be replaced with something slightly more efficient, but I don't know if it's worth the trouble.

@treeowl
Copy link
Contributor Author

treeowl commented Jul 5, 2017

Actually, it may be worth the trouble. We don't really want to hold on to v0 any longer than necessary. So we should likely walk over it monadically to pull the thunks out, using the MonadFix instance for ST s. I'm not completely sure that works, but I think it probably does.

treeowl added a commit to treeowl/vector that referenced this issue Jul 5, 2017
I *believe* this is equivalent to the instance for `[]`. Writing
QuickCheck properties for `mfix` seems pretty tricky, so I just
added a small unit test.

Fixes haskell#178
treeowl added a commit to treeowl/vector that referenced this issue Jul 5, 2017
I *believe* this is equivalent to the instance for `[]`. Writing
QuickCheck properties for `mfix` seems pretty tricky, so I just
added a small unit test.

Fixes haskell#178
treeowl added a commit to treeowl/vector that referenced this issue Jul 5, 2017
I *believe* this is equivalent to the instance for `[]`. Writing
QuickCheck properties for `mfix` seems pretty tricky, so I just
added a small unit test.

Fixes haskell#178
treeowl added a commit to treeowl/vector that referenced this issue Jul 5, 2017
I *believe* this is equivalent to the instance for `[]`. Writing
QuickCheck properties for `mfix` seems pretty tricky, so I just
added a small unit test.

Fixes haskell#178
@treeowl
Copy link
Contributor Author

treeowl commented Jul 11, 2017

Actually, some of this attempt to avoid leaks may be useless (particularly the part that forces use of runST). I'll post an update and explanation.

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

Successfully merging a pull request may close this issue.

2 participants