Skip to content

[RFC] Refactor Flag type - #3572

Closed
ttuegel wants to merge 12 commits into
haskell:masterfrom
ttuegel:refactor-flags
Closed

ttuegel wants to merge 12 commits into
haskell:masterfrom
ttuegel:refactor-flags

Conversation

@ttuegel

@ttuegel ttuegel commented Jul 17, 2016

Copy link
Copy Markdown
Member

Before I get too deeply invested in this, I wanted to request feedback.

This begins to realize my vision for refactoring the Flag type in Cabal. I am trying to solve two problems:

  1. The use of the partial function fromFlag.
  2. The problem of "configuration creep". I mean "configuration" in the sense of "The Configuration Problem," not in the sense of "cabal configure". The primary symptom of this is that default values for flags are strewn throughout the codebase.

To get feedback, I have refactored TestFlags as a model for what I would like to do to the rest of the codebase. Except for deprecations and new exports, I have maintained API compatibility. I have not removed any exports or (AFAIK) changed the types of any exports.

The TestFlags type is replaced with TestConfig a where a has kind * -> *. The easiest way to understand what this type parameter does is to consider this synonym:

type TestFlags = TestConfig Flag

which is equivalent to the old definition of TestFlags. TestConfig a's sole constructor is named TestFlags to keep compatibility, I have just generalized the type.

Besides Flag, I have introduced

newtype Final a = Final { fromFinal :: a }

and I provide

finalizeTestFlags :: TestFlags -> TestConfig Final

which calls fromFlagOrDefault on each field with the appropriate default values. The result of finalizeTestFlags is a TestConfig with all fields populated. For compatibility, there is a trivial

testConfigToFlags :: TestConfig Final -> TestFlags

which does the obvious thing. There is marginally more boilerplate this way, but I think having concentrated boilerplate is better than having fromFlag throughout the codebase. To organize the boilerplate better and reduce recompilation times, I am splitting up Distribution.Simple.Setup, starting with Distribution.Simple.Command.Test.

Before I continue this refactoring for the other commands, do I hear any comments or objections?

/cc interested parties: @ezyang @23Skidoo @dcoutts

@@ -0,0 +1,188 @@
{-# LANGUAGE DeriveGeneric #-}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The interesting changes are mainly in this module.

@ezyang

ezyang commented Jul 17, 2016

Copy link
Copy Markdown
Contributor

I like this plan. Given the constraint that we want both flaggy and non-flaggy data types, I think this is the only possible thing you can do. I do have some inline comments so I'll put them in.

-- Portability : portable
--
-- A monoid for command-line options such that successive options override
-- their precedents.

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.

Obviously, eventually we should have docs for everything here.

@23Skidoo

Copy link
Copy Markdown
Member

I like this change.

newtype Final a = Final { fromFinal :: a }

Starting with base-4.8 you can also use Data.Functor.Identity, but I guess that using it doesn't really buy us anything. Final is easier to type OTOH.

@ttuegel

ttuegel commented Jul 17, 2016

Copy link
Copy Markdown
Member Author

Starting with base-4.8 you can also use Data.Functor.Identity, but I guess that using it doesn't really buy us anything. Final is easier to type OTOH.

Also, I don't think we want to pick up the stray instances. For example, Monoid a => Monoid (Identity a) would be particularly confusing, I think.

@ttuegel

ttuegel commented Jul 17, 2016

Copy link
Copy Markdown
Member Author

There is one incompatible change here that I didn't think of before. Import lists like

import Distribution.Simple.Setup ( TestFlags(..) )

must now become

import Distribution.Simple.Setup ( TestFlags, TestConfig(..) )

On the one hand, this is a pretty breaking change. On the other hand, we have setup dependencies now, so in principle this is not so bad. Thoughts?

@23Skidoo

Copy link
Copy Markdown
Member

That will break a lot of setup scripts. We'll probably have to change the default custom-setup upper bound to 1.26.

/cc @hvr

@ttuegel

ttuegel commented Jul 17, 2016

Copy link
Copy Markdown
Member Author

If that's much trouble, I could also increase the boilerplate level a notch by introducing separate data types for TestFlags and TestConfig. That would actually eliminate the need for Final, so the amount of boilerplate probably stays about the same.

@23Skidoo

Copy link
Copy Markdown
Member

That would actually eliminate the need for Final, so the amount of boilerplate probably stays about the same.

If that's indeed the case, the BC solution is of course preferable (if it's not too much trouble).

@ttuegel

ttuegel commented Jul 18, 2016

Copy link
Copy Markdown
Member Author

I can maintain backwards compatibility with minimal boilerplate using RecordWildCards. I recognize that's not a popular extension. If we want to go without, record field names become verbose very quickly.

@23Skidoo

Copy link
Copy Markdown
Member

Using RecordWildCards in Cabal code is perfectly fine, there is already a bunch of modules with this extension enabled.

@lspitzner

Copy link
Copy Markdown
Collaborator

I have been using something rather similar in a recent project, and used the following abstraction:

class CZip k where
  cZip :: (forall a . f a -> g a -> h a) -> k f -> k g -> k h

instance CZip SomeConf where ...

One has to write that (boilerplaty) instance, but automatically gets:

fromMaybeSomeConf :: SomeConf Final -> SomeConf Maybe -> SomeConf Final
fromMaybeSomeConf = cZip (\def -> maybe def Final)

toMaybeSomeConf :: SomeConf Final -> SomeConf Maybe
toMaybeSomeConf c = cZip (\_ x -> Just $ copoint x) c c

For that to work, SomeConf needs to have the form

data SomeConf f = SomeConf { field1 :: f m1, field2 :: f m2, .. }

where m1, m2, .. are Semigroups. E.g. most likely stuff like Semigroup.Last Bool, Semigroup.Last Int or [FilePath].

  • The current Flag combines the semantics of Maybe and Semigroup.Last. In this approach, these are separated.
  • This means that the merging behaviour is encoded in the type of each field. It is implicit documentation (and annoying for usage). But you need to copoint the Finals anyway with the above approach.
  • The default is a trivial SomeConf Final value, not mixed with any other logic.
  • It should be trivial to automate the CZip instance via TH.

Not sure if this would be applicable for cabal, or if this would cause (more) breakage.

@ezyang

ezyang commented Jul 18, 2016

Copy link
Copy Markdown
Contributor

@lspitzner Very interesting. Unfortunately we need to keep the old version of *Flags around for BC. I wonder if we shouldn't convert into one of these functor-parametrized types, in preparation for one day making the BC-breaking change. I'll defer to @ttuegel on this one!

@ttuegel

ttuegel commented Jul 18, 2016

Copy link
Copy Markdown
Member Author

I wonder if we shouldn't convert into one of these functor-parametrized types, in preparation for one day making the BC-breaking change.

That's what I originally did, but we discovered that it breaks downstream import lists.

However, I'm happy enough with the fully-compatible, separate TestFlags and TestConfig + RecordWildCards implementation. It has a little (but not much) more boilerplate than I originally wanted, but all the boilerplate is concentrated in Distribution.Simple.Command.* modules. We can even remove common boilerplate patterns like

let distPref = fooDistPref flags
    verbosity = fooVerbosity flags

So, I think it's a big win.

@hvr

hvr commented Jul 18, 2016

Copy link
Copy Markdown
Member

I mostly agree with @23Skidoo here. But I'd also point out we can empirically find out how many packages would be affected (i.e. break) on Hackage to help decide whether it'd be more economical to retrofit setup-depends or tigthen cabal's implicit upper bound.

@lspitzner

Copy link
Copy Markdown
Collaborator

Sure. I was in a "if we break things, break them correctly" mode, but if we can maintain BC that is better than avoiding small amounts of boilerplate.

@ttuegel

ttuegel commented Jul 18, 2016

Copy link
Copy Markdown
Member Author

I was in a "if we break things, break them correctly" mode

Agree 100%! 👍

Thanks for the feedback, everyone. I will close this for now and reopen when I have the complete, backwards-compatible refactoring.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants