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

Define generic implementations of Struct and Typed methods. Refs #564. #566

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

RyanGlScott
Copy link
Contributor

@RyanGlScott RyanGlScott commented Nov 13, 2024

This adds functionality for implementing the class methods of Struct and Typed using GHC.Generics in copilot-core. As such, one can now easily define instances of these classes by deriving a Generic instance for the struct data type, i.e.,

data MyStructType = ...
  deriving Generic

instance Struct MyStructType where
  typeName = typeNameDefault
  toValues = toValuesDefault
  updateField = updateFieldDefault

instance Typed MyStructType where
  typeOf = typeOfDefault

This work is based off of an initial implementation by Marten Wijnja (@Qqwy).

Note that I do not yet change any of the default implementations of any Struct or Typed methods. This is because several Struct instances in the wild currently do not define implementations of updateField, and moreover, they also do not define Generic instances for the corresponding data types. As such, changing the default implementation of updateField to use a Generic-based default would cause these instances in the wild to no longer compile. We will explore changing the default implementations after a suitable transition period.

I have also modified the struct-related copilot examples to implement their Struct and Typed instances using these new functions. Note that I have intentionally not used updateFieldDefault in the StructsUpdateField.hs example, as that example is intended to demonstrate how one would implement updateField by hand.

Fixes #564. Supersedes #516.

@ivanperez-keera
Copy link
Member

ivanperez-keera commented Nov 13, 2024

Change Manager: You mention #561 in your commit messages but you mean #516. It's ok not to mention #516 at all since you are bringing the changes you want from it and #516 won't be merged.

RyanGlScott and others added 5 commits November 13, 2024 16:10
…ods. Refs Copilot-Language#564.

This adds functionality for implementing the class methods of `Struct` and
`Typed` using `GHC.Generics`. As such, one can now easily define instances of
these classes by deriving a `Generic` instance for the struct data type, i.e.,

```hs
data MyStructType = ...
  deriving Generic

instance Struct MyStructType where
  typeName = typeNameDefault
  toValues = toValuesDefault
  updateField = updateFieldDefault

instance Typed MyStructType where
  typeOf = typeOfDefault
```

This work is based off of an initial implementation by Marten Wijnja (@Qqwy).

Note that I do not yet change any of the default implementations of any
`Struct` or `Typed` methods. This is because several `Struct` instances in the
wild currently do not define implementations of `updateField`, and moreover,
they also do not define `Generic` instances for the corresponding data types.
As such, changing the default implementation of `updateField` to use a
`Generic`-based default would cause these instances in the wild to no longer
compile. We will explore changing the default implementations after a suitable
transition period.

Co-authored-by: Marten Wijnja <[email protected]>
…opilot-Language#564.

In the future, we plan to change the default implementation for `updateField`
such that it will require a `Generic` instance. This will break any existing
`Struct` instance that omits an implementation of `updateField` and also does
not define a `Generic` instance for the struct data type. Unfortunately, there
does not appear to be a way for GHC to warn about this combination of
properties, but we can at least warn about this in the `updateField` Haddocks.
…. Refs Copilot-Language#564.

Using recently added `Generic`-based implementations, much of the boilerplate
code needed to define `Struct` and `Typed` in the struct-related `copilot`
examples can be replaced with much simpler implementations. Note that I have
intentionally _not_ used `updateFieldDefault` in the `StructsUpdateField.hs`
example, as that example is intended to demonstrate how one would implement
`updateField` by hand.

This work is based off of an initial implementation by Marten Wijnja (@Qqwy).

Co-authored-by: Marten Wijnja <[email protected]>
@RyanGlScott RyanGlScott force-pushed the develop-generic-struct-typed-methods branch from 0c90e55 to 606abcb Compare November 13, 2024 21:12
Copy link
Contributor Author

@RyanGlScott RyanGlScott left a comment

Choose a reason for hiding this comment

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

You mention #561 in your commit messages but you mean #516.

Oops, my apologies!

It's ok not to mention #516 at all since you are bringing the changes you want from it and #516 won't be merged.

Sounds good. I've removed mention of #516 from the commit messages.

copilot-core/src/Copilot/Core/Type.hs Outdated Show resolved Hide resolved
copilot/examples/Structs.hs Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

copilot-core: Facilitate use of structs
2 participants