Skip to content

Lazy Cofree constructor #92

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

Merged
merged 3 commits into from
Dec 11, 2017
Merged

Conversation

natefaubion
Copy link
Contributor

  • Adds a Lazy instance.
  • Adds buildCofree which unfolds a label and subtree together.

* Adds a `Lazy` instance.
* Adds `buildCofree` which unfolds a label and subtree together.
@natefaubion
Copy link
Contributor Author

Should probably fix hoistCofree since it's strict.

Cofree (e s) (defer \_ -> unfoldCofree e n <$> n s)
Cofree (defer \_ -> Tuple (e s) (unfoldCofree e n <$> n s))

buildCofree
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like this is used, and it looks like a duplicate of unfoldCofree essentially. Is the idea just to save an extra allocation?

I would like to have only one, and deprecate the unfoldCofree if we decide to keep this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with unfoldCofree is that the label and the functor are calculated independently, so they cannot share work. buildCofree supersedes unfoldCofree.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm happy to go with buildCofree, although it really is the natural unfold, of course, so I would prefer if we could use the name unfoldCofree. I suggest just adding a note that unfoldCofree will be replaced by buildCofree in a future release, please.


-- | Lazily creates a value of type `Cofree f a` from a label and a
-- | functor-full of "subtrees".
liftC :: forall f a. (Unit -> Tuple a (f (Cofree f a))) -> Cofree f a
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a slightly unclear choice of name when we already have mkCofree. I would prefer to be more explicit and call it something like mkCofreeLazy but I'm open to other ideas.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was just being consistent with liftF, which is an equally unclear name. Happy to change it to whatever.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree we should change both. My open PR calls it liftFree actually, and moves it into a class.

liftC :: forall f a. (Unit -> Tuple a (f (Cofree f a))) -> Cofree f a
liftC = Cofree <<< defer
deferCofree :: forall f a. (Unit -> Tuple a (f (Cofree f a))) -> Cofree f a
deferCofree = Cofree <<< defer
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've called this deferCofree since it's just a specialized implementation of the Lazy instance.

@paf31 paf31 merged commit c17c481 into purescript:master Dec 11, 2017
@paf31
Copy link
Contributor

paf31 commented Dec 11, 2017

Great, thanks!

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 this pull request may close these issues.

2 participants