-
Notifications
You must be signed in to change notification settings - Fork 71
Define generic implementations of Struct
and Typed
methods. Refs #564.
#566
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
Conversation
0c90e55
to
606abcb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change Manager: Can you please make the following changes:
convert3 :: String -> String
convert3 = convert3' True True
convert3' :: Bool -- ^ Is this the first letter
-> Bool -- ^ Was the previous letter capital
-> String -- ^ Remainder of the string
-> String
convert3' _ _ [] = []
convert3' _ v (x:[])
| v && isUpper x = toLower x : []
| isUpper x = '_' : toLower x : []
| otherwise = x : []
convert3' b v (x1:x2:xs)
| not b && isUpper x1 && (isLower x2 || not v)
= '_' : toLower x1 : convert3' False (isUpper x1) (x2:xs)
| otherwise
= toLower x1 : convert3' False (isUpper x1) (x2:xs) |
…ods. Refs Copilot-Language#564. Currently, using structs in Copilot requires defining instances of several type classes. Defining those instances is unnecessarily cumbersome, and it exposes the user to a substantial amount of Haskell even when they want to stay at the level of the Copilot language. This commit 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.
…opilot-Language#564. Currently, using structs in Copilot requires defining instances of several type classes. Defining those instances is unnecessarily cumbersome, and it exposes the user to a substantial amount of Haskell even when they want to stay at the level of the Copilot language. In the previous commit, we added `Generic`-based defaults for several type class methods, including the `updateField` method of `Struct`. 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. This commit adds a verbal warning to the `updateField` Haddocks to make users aware of this potential problem.
…. Refs Copilot-Language#564. Currently, using structs in Copilot requires defining instances of several type classes. Defining those instances is unnecessarily cumbersome, and it exposes the user to a substantial amount of Haskell even when they want to stay at the level of the Copilot language. In a previous commit, we added `Generic`-based defaults for the methods of the `Struct` and `Typed` classes. In this commit, we leverage these default implementations to remove much of the boilerplate code needed to define `Struct` and `Typed` instances in the struct-related `copilot` examples. 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).
606abcb
to
cbab0cc
Compare
Implementor: Fix implemented, review requested. |
Change Manager: Verified that:
|
This adds functionality for implementing the class methods of
Struct
andTyped
usingGHC.Generics
incopilot-core
. As such, one can now easily define instances of these classes by deriving aGeneric
instance for the struct data type, i.e.,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
orTyped
methods. This is because severalStruct
instances in the wild currently do not define implementations ofupdateField
, and moreover, they also do not defineGeneric
instances for the corresponding data types. As such, changing the default implementation ofupdateField
to use aGeneric
-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 theirStruct
andTyped
instances using these new functions. Note that I have intentionally not usedupdateFieldDefault
in theStructsUpdateField.hs
example, as that example is intended to demonstrate how one would implementupdateField
by hand.Fixes #564. Supersedes #516.