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 IdentityT #121

Merged
merged 10 commits into from
Oct 15, 2020
Merged

Add IdentityT #121

merged 10 commits into from
Oct 15, 2020

Conversation

JordanMartinez
Copy link
Contributor

Fixes #120

I likely haven't added every instance that IdentityT could have, and I'm not sure whether all of these instances are law-abiding. What I have below was based on MaybeT. I'm guessing this should also have instances for the Contravariant transformer classes as well?

@JordanMartinez
Copy link
Contributor Author

Build is failing with this error. I think this was explained on Discourse somewhere.


$ TAG=$(wget -q -O - https://github.com/purescript/purescript/releases/latest --server-response --max-redirect 0 2>&1 | sed -n -e 's/.*Location:.*tag///p')

0.19s$ wget -O $HOME/purescript.tar.gz https://github.com/purescript/purescript/releases/download/$TAG/linux64.tar.gz

--2020-04-08 00:17:14-- https://github.com/purescript/purescript/releases/download//linux64.tar.gz

Resolving github.com (github.com)... 140.82.114.3

Connecting to github.com (github.com)|140.82.114.3|:443... connected.

HTTP request sent, awaiting response... 404 Not Found

2020-04-08 00:17:14 ERROR 404: Not Found.

The command "wget -O $HOME/purescript.tar.gz https://github.com/purescript/purescript/releases/download/$TAG/linux64.tar.gz" failed and exited with 8 during .

@hdgarrood
Copy link
Contributor

Ah yeah, here's the relevant thread: https://discourse.purescript.org/t/new-404-ci-failures-in-core-libraries/1225/8?u=hdgarrood

Would you like to try updating this repo? I think preferably the Travis fix would go into a separate PR.

@JordanMartinez
Copy link
Contributor Author

Mind retriggering this build now that the CI script has been fixed?

@garyb
Copy link
Member

garyb commented Apr 8, 2020

Just restarting the build didn't seem to work, will try closing and reopening this. If that doesn't work, might need to rebase/merge master.

@garyb garyb closed this Apr 8, 2020
@garyb garyb reopened this Apr 8, 2020
instance applicativeIdentityT :: Applicative m => Applicative (IdentityT m) where
pure a = IdentityT (pure a)

instance altIdentityT :: (Monad m, Alt m) => Alt (IdentityT m) where
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need the Monad constraint here?

Copy link
Contributor

Choose a reason for hiding this comment

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

Alt only requires Functor, so perhaps this instance could be (Functor m, Alt m) => Alt (IdentityT m)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Probably, I mainly copied/pasted from the Haskell library.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in latest commits


instance monadErrorIdentityT :: MonadError e m => MonadError e (IdentityT m) where
catchError (IdentityT m) h =
IdentityT $ catchError m (\a -> case h a of IdentityT b -> b)
Copy link
Contributor

Choose a reason for hiding this comment

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

I would probably write this as IdentityT $ catchError m (runIdentityT <<< h)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in latest commits

instance monadIdentityT :: Monad m => Monad (IdentityT m)

instance monadRecIdentityT :: MonadRec m => MonadRec (IdentityT m) where
tailRecM f a = IdentityT $
Copy link
Contributor

Choose a reason for hiding this comment

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

This instance looks a bit suspicious to me. I think it might not be stack-safe. Is it using the MonadRec m constraint? I.e. does the code still compile if you remove the constraint?

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, removing that would make it compile as long as it had a Monad constraint on the m. I believe the implementation in the latest commits properly implement it now.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, the current implementation looks great 👍

Copy link
Contributor

@hdgarrood hdgarrood left a comment

Choose a reason for hiding this comment

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

Sorry, this has just only occurred to me: I think all of the instances you have explicit implementations for here, other than MonadTrans, can (and probably should?) be derive newtype instance.

instance altIdentityT :: (Functor m, Alt m) => Alt (IdentityT m) where
alt (IdentityT x) (IdentityT y) = IdentityT (x <|> y)

instance plusIdentityT :: (Monad m, Plus m) => Plus (IdentityT m) where
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this one only needs Functor m too?

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. Fixed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does it really need Functor? For context, see this image of our type class hierarchy. Seems like Plus should be enough:
https://github.com/JordanMartinez/purescript-jordans-reference/blob/latestRelease/41-Ecosystem/Type-Classes/Type-Class-Relationships.svg

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, good point! since Plus m already implies Alt m, which implies Functor m.

instance plusIdentityT :: (Monad m, Plus m) => Plus (IdentityT m) where
empty = IdentityT empty

instance alternativeIdentityT :: (Monad m, Alternative m) => Alternative (IdentityT m)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we only need Applicative m here

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh but also Applicative is implied by Alternative, so I think we can probably just get rid of the Monad constraint entirely.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup. You're right. This has been fixed in latest commits.

@JordanMartinez
Copy link
Contributor Author

I think all of the instances you have explicit implementations for here, other than MonadTrans, can (and probably should?) be derive newtype instance.

🤦‍♂️ You're right! Wow... I can't believe I did that. It's not the first time either...

@JordanMartinez JordanMartinez merged commit 48034f0 into purescript:master Oct 15, 2020
@JordanMartinez JordanMartinez deleted the addIdentityT branch October 15, 2020 02:40
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.

Add IdentityT
3 participants