Opened 6 years ago

Closed 6 years ago

Last modified 5 years ago

#2953 closed feature request (fixed)

deriving Functor, Foldable, Traversable

Reported by: twanvl Owned by: twanvl
Priority: normal Milestone: 6.12.1
Component: Compiler Version: 6.11
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Test Case:
Blocked By: Blocking:
Related Tickets: Differential Revisions:

Attachments (2)

deriving-functor-tests.patch (24.3 KB) - added by twanvl 6 years ago.
testcases for deriving Functor
deriving-functor-tests2.patch.gz (98.3 KB) - added by twanvl 6 years ago.
testcases for deriving Functor, Foldable, Traversable

Download all attachments as: .zip

Change History (19)

comment:1 Changed 6 years ago by twanvl

  • Owner set to twanvl

Changed 6 years ago by twanvl

testcases for deriving Functor

comment:2 Changed 6 years ago by twanvl

As a start, I have implemented the deriving for the Functor class only.

For some reason the patch is too big to attach, it can be found at http://twan.home.fmf.nl/files/deriving-functor2.patch.gz. A patch with testcases is attached.

Changed 6 years ago by twanvl

testcases for deriving Functor, Foldable, Traversable

comment:3 Changed 6 years ago by twanvl

Deriving Foldable and Traversable instances is now also possible. The code is very similar, so I abstracted it a bit.

The patch is still too large to attach, it can be found at http://twan.home.fmf.nl/files/deriving-functor3.patch.gz.

comment:4 Changed 6 years ago by twanvl

  • Owner changed from twanvl to igloo

comment:5 Changed 6 years ago by simonpj

  • difficulty set to Unknown
  • Owner changed from igloo to twanvl

Dear twanval

Thanks for doing this. I've taken a look, polished a little, and will commit shortly.

What is missing is a section in the user manual describing the changes. You need a section in 8.5 describing the feature, and suitable update in the flag-reference parts too. By "describing the feature" I mean that you should explain what does and doesn't work; in particular, about the special cases for tuples and functions. I've added some comments in the code, in TcGenDeriv for Functor to help myself understand, that may help as a starting point.

Could you manage to do that? I will commit the patch meanwhile, but it's really incomplete without the documentation. Many thanks

Simon

PS: Looking at the code, I do agree with you that

  • Checking the side conditions
  • Generating the constraints

could be combined. Perhaps the CanDerive constructor should contain a list of constraints? If you felt like doing this, great. That would eliminate one traversal, and tidy up the code a bit.

comment:6 Changed 6 years ago by simonpj

PS: there's just one flag to control all three new classes, namely -XDeriveFunctor. Is that the right name? Is there a term that would cover all three?

Simon

comment:7 Changed 6 years ago by simonpj

PPS: The patch I have pushed is:

Mon Feb  2 13:48:29 GMT 2009  [email protected]
  * Add the ability to derive instances of Functor, Foldable, Traversable

DO NOT use twanvl's patches! I un-recorded them, polished, and re-recorded a single patch. If you use twanvl's as well, chaos will result.

Simon

comment:8 Changed 6 years ago by igloo

  • Milestone set to 6.12 branch

comment:9 Changed 6 years ago by twanvl

I'll get to work on the documentation.

Regarding the flag name:

  • In a sense Functor and Foldable are specializations of Traversable, so -XDeriveTraversable might make more sense. On the other hand, it is possible to derive Functor instances for some types containing functions, where it is not possible to make a Traversable instance.
  • For Data and Typeable the flag is -XDeriveDataTypeable, so we could also go with -XDeriveFunctorFoldableTraversable, but that is a bit long.
  • The last alternative is to make three flags, one for each class. This is slightly more work for the programmer if he wants to derive all three instances.

If I had to choose, I think I would prefer the last alternative.

comment:10 Changed 6 years ago by simonpj

The other possibility is to change -XDeriveDataTypeable to -XDeriveExtraClasses, and give up on specifying exactly which extra classes. That'd be my personal inclination. What do others think?

Simon

comment:11 follow-up: Changed 6 years ago by igloo

If we did that then it wouldn't be clear what a Cabal file saying it needs the DeriveExtraClasses extension actually meant. Would using ghc 6.10.1 be OK (only derives Data), would you need 6.12.1 (also derives Functor), or even 6.14.1 (derives something that isn't implemented yet).

I don't know if cabal-install does this yet, but part of the plan was that if you told it to install a package foo, and foo needed an extension that your compiler doesn't support, then cabal-install would fail immediately, rather than downloading and building all of foo's dependencies first.

comment:12 in reply to: ↑ 11 Changed 6 years ago by duncan

Replying to igloo:

I don't know if cabal-install does this yet, but part of the plan was that if you told it to install a package foo, and foo needed an extension that your compiler doesn't support, then cabal-install would fail immediately, rather than downloading and building all of foo's dependencies first.

Currently it only fails at configure time, but certainly it is the plan to check these kinds of dependencies up front (along with build tools, C libs, etc).

comment:13 Changed 6 years ago by simonpj

Just to say that this feature is now implemented in the HEAD. Keeping this ticket open only because the choice of flag name is still open to question. Currently

  • -XDeriveFunctor enables Functor, Traversable, and Foldable

Is that what we want?

Simon

comment:14 Changed 6 years ago by igloo

Separate -XDeriveFunctor, -XDeriveTraversable and -XDeriveFoldable would be best in my opinion.

comment:15 Changed 6 years ago by igloo

  • Milestone changed from 6.12 branch to 6.12.1

comment:16 Changed 6 years ago by simonpj

  • Resolution set to fixed
  • Status changed from new to closed

OK done

Thu May 28 08:50:31 BST 2009  [email protected]
  * Separate flags -XDeriveFunctor, -XDeriveFoldable, -XDeriveTraversable
  
  See Trac #2953. This patch implements a distinct flag for each extended
  class that may be automatically derived.  And I updated the user manual
  to reflect the fact that we can now derive Functor, Foldable, Traversable.

Simon

comment:17 Changed 5 years ago by MartijnVanSteenbergen

This is exciting stuff. :-D

Note: See TracTickets for help on using tickets.