Skip to content

Instances for Record#171

Merged
garyb merged 7 commits into
purescript:compiler/0.12from
i-am-tom:compiler/0.12
May 3, 2018
Merged

Instances for Record#171
garyb merged 7 commits into
purescript:compiler/0.12from
i-am-tom:compiler/0.12

Conversation

@i-am-tom
Copy link
Copy Markdown

Hullo!

This PR adds what I think/hope are reasonable implementations for all the standard type classes applicable to records. Please, please, please tear this apart, in case I've missed anything - being in the prelude, it is very tricky to test, and I'm worried I'll have missed something the type-checker wouldn't have caught.

However, if you can get the tests running, you probably don't need to worry. I, personally, couldn't, but I might be missing something obvious?

Thanks!

Tom Harding added 4 commits April 29, 2018 15:47
This commit is to be followed by several others, slowly building up the
set of record instances in the prelude by way of RowToList.
Adding more and more type classes for records. We're getting somewhere!
As far as I can tell, there are now instances for all applicable
typeclasses given for primitive records. I've even added tests, which
typecheck (good news), though I don't think any of them actually run.
@MonoidMusician
Copy link
Copy Markdown

Hey looking nice, thanks for your work!!

For things like HeytingAlgebra => BooleanAlgebra, I think you actually will need to make sure that each type in the row also implements BooleanAlgebra in order for the whole record to satisfy the laws (same thing for Ring => CommutativeRing).

So this might mean you'll have to implement EachIsBooleanAlgebra that checks that for each type in the row...

... or the other option is you parameterize HeytingAlgebraRow over a argument that specifies whether you want BooleanAlgebra from each type or just HeytingAlgebra. (In general I like the idea of encoding laws as parameters on the typeclass, but that's obviously out of scope here haha.)

P.S. Especially if you go with the former method, the you could just use HeytingAlgebra (Record row) as the constraint instead of RL.RowToList row list, HeytingAlgebraRow list row row focus. Not a big deal though :)

Also, I'm curious why focus is exposed as a parameter to each class?

Comment thread src/Data/HeytingAlgebra.purs Outdated
, impliesRecordImpl
, conjRecordImpl
, disjRecordImpl
, notRecordImpl
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe we could drop Impl from all these kinds of names - usually that's for FFI things where there's a non-Impl version in scope too.

Perhaps the classes should be using Record in their name rather than Row too, since they are specialised to records in particular.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thinking about it, maybe all these should be defined in modules like Data.HeytingAlgebra.Record or something like that, that way they don't have to be exported from these "primary" modules, given that there's not really a need to use these functions outside of the record instances.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Only problem here is that they all require things to have HeytingAlgebra constraints, which would cause a circular dependency :(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In that case, how about putting all of this in Data.HeytingAlgebra.Internal, and then re-exporting the important bits from Data.HeytingAlgebra?

Comment thread src/Data/Internal/Record.purs Outdated
@@ -0,0 +1,14 @@
module Data.Internal.Record where
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Data.Internal is a bit of a weird namespace - perhaps it could be Unsafe.Record like we have Unsafe.Coerce and Unsafe.Reference (unsafeRefEq).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@LiamGoodacre mentioned potentially just merging Data.Record.Unsafe from purescript-record in here?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sure, that works.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

which I guess also clears up the comment below, too :D Will sort this out (and remove focus from all the classes - thanks @MonoidMusician) at lunch 👍

Comment thread src/Data/Internal/Record.purs Outdated
module Data.Internal.Record where

-- | *Really* unsafely get a value from a record. You really shouldn't be using
-- | this function unless you know what you're doing.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Perhaps we could refine these comments a bit 😄. The unsafeCoerce comment is pretty detailed actually: https://pursuit.purescript.org/packages/purescript-unsafe-coerce/3.0.0/docs/Unsafe.Coerce#v:unsafeCoerce

At least talking about the purpose / how to use them safely (such as it is) would be good.

@garyb
Copy link
Copy Markdown
Member

garyb commented May 3, 2018

Did some more work on this:

  • didn't end up adding the Internal modules because that caused issues with being able to derive Eq (since it was no longer Data.Eq.Eq)
  • removed focus parameters
  • renamed things from Row to Record as appropriate
  • added new record classes for BooleanAlgebra and CommutativeRing, since the way they were done in here upgraded any record-of-HeytingAlgebra to be a record-of-BooleanAlgebra regardless of whether the values had BooleanAlgebra instances (and same for CommutativeRing)
  • Moved over Record.Unsafe's function

@garyb
Copy link
Copy Markdown
Member

garyb commented May 3, 2018

I'll merge this for now and we can make any further changes or fixes that are needed on the branch. Thanks @i-am-tom!

@garyb garyb merged commit a57a34d into purescript:compiler/0.12 May 3, 2018
@i-am-tom
Copy link
Copy Markdown
Author

i-am-tom commented May 3, 2018

You're brilliant, @garyb - thanks so much!

@i-am-tom i-am-tom deleted the compiler/0.12 branch May 3, 2018 17:17
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.

5 participants