Skip to content

add nats: natural numbers for lazy unfoldable #12

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
wants to merge 1 commit into from

Conversation

matthewleon
Copy link
Contributor

following conversation here: #11

This adds a dependency.

I've tested the laziness here: https://github.com/matthewleon/purescript-lists/tree/test-nats

@@ -88,6 +89,13 @@ none = unfoldr (const Nothing) unit
singleton :: forall f a. Unfoldable f => a -> f a
singleton = replicate 1

-- | Produce a Lazy Unfoldable structure representing the natural numbers.
nats :: forall f a. Unfoldable f => Semiring a => Lazy (f a) => f a
nats = defer \_ -> unfoldr increment zero
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think you need defer here, since any lazy structure should use it anyway. Did you try this on lazy lists?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you are right. Will fix.

@@ -18,7 +18,8 @@
],
"dependencies": {
"purescript-partial": "^1.2.0",
"purescript-tuples": "^4.0.0"
"purescript-tuples": "^4.0.0",
"purescript-control": "^3.3.1"
Copy link
Contributor

Choose a reason for hiding this comment

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

No need for this any more, right?

@@ -88,6 +89,13 @@ none = unfoldr (const Nothing) unit
singleton :: forall f a. Unfoldable f => a -> f a
singleton = replicate 1

-- | Produce a Lazy Unfoldable structure representing the natural numbers.
nats :: forall f a. Unfoldable f => Semiring a => Lazy (f a) => 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.

f might be lazy but in the wrong way. I think we need a comment to say that the result is actually infinite.

@matthewleon
Copy link
Contributor Author

matthewleon commented Dec 11, 2017 via email

@hdgarrood
Copy link
Contributor

Can we generalise this to iterate :: forall f a. Unfoldable f => Lazy (f a) => (a -> a) -> a -> f a? So you could reconstruct nats via nats = iterate add zero?

@matthewleon
Copy link
Contributor Author

Good call.

@hdgarrood
Copy link
Contributor

A couple nitpicks I've noticed which are no longer relevant if we are generalising to iterate, but you might want to think about if you're defining nats elsewhere: some definitions of the natural numbers include zero and others don't, so I guess you'd at least want to say which one your nats is using (in fact, I guess it's more or less equally likely for people to want to use either version, which is mostly what led me to the above suggestion). Also, the result can't really be described as representing the natural numbers in cases where your semiring is finite; a good example of a finite semiring is the integers modulo n.

@matthewleon matthewleon mentioned this pull request Dec 28, 2017
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.

3 participants