Skip to content

Check that flags are not used together with their negation #5265

Open

Description

@jespercockx

Currently, Agda allows the following file:

{-# OPTIONS --guardedness --no-guardedness #-}
{-# OPTIONS --with-K --without-K #-}
{-# OPTIONS --sized-types --no-sized-types #-}
{-# OPTIONS --subtyping --no-subtyping #-}
{-# OPTIONS --cumulativity --no-cumulativity #-}
{-# OPTIONS --prop --no-prop #-}
{-# OPTIONS --universe-polymorphism --no-universe-polymorphism #-}
{-# OPTIONS --irrelevant-projections --no-irrelevant-projections #-}
{-# OPTIONS --copatterns --no-copatterns #-}
{-# OPTIONS --exact-split --no-exact-split #-}
{-# OPTIONS --confluence-check --no-confluence-check #-}
{-# OPTIONS --overlapping-instances --no-overlapping-instances #-}
{-# OPTIONS --qualified-instances --no-qualified-instances #-}
{-# OPTIONS --caching --no-caching #-}
{-# OPTIONS --auto-inline --no-auto-inline #-}

Whether a flag is considered enabled or disabled thus depends on which flag is given last. This could already be confusing by itself, but it is made worse by the recent addition of default flags to .agda-lib files: do the flags in the library file take precedence over the ones in the file itself or vice versa? And what about if one of these flags is also given on the command line? This is just asking for people to be confused.

My proposal would be to flat-out refuse the combination of a flag with its explicit negation. This would require the state of each flag to have a third possible 'default' state. We actually already have the internal infrastructure for this (the WithDefault type) but we're not using it consistently. And actually even for the flags that currently already use WithDefault (--subtyping, --sized-types, --guardedness, --without-K) this is currently not checked at the moment.

Obviously, this change would break backwards compatibility, but I think the impact would be relatively minor as I can't imagine many situations where someone would explicitly enable a flag together with its negation. Are there any other reasons why this change would not be a good idea?

Activity

  1. added
    ux: options
    type: discussion
    on Mar 7, 2021
  2. added this to the 2.6.2 milestone on Mar 7, 2021
  3. jespercockx commented on Mar 7, 2021

    @jespercockx
    MemberAuthor

    Since I was curious and it doesn't seem to be documented anywhere, I experimented with the current priority Agda currently assigns to the various ways to give pragma options. Apparently:

    • Flags in an {-# OPTIONS #-} pragma overwrite both flags given on the command-line and default flags given in an .agda-lib file.
    • Default flags given in an .agda-lib file overwrite the ones given on the command-line.

    Even if we decide not to disallow combinations of flags with their negations, this order seems rather backwards. I would expect an option given on the command-line to overwrite both default flags in the .agda-lib file and ones given explicitly in the file. At the very least, this behaviour should be documented somewhere.

  4. jespercockx commented on Mar 7, 2021

    @jespercockx
    MemberAuthor

    More in general, it would perhaps make sense to have a general notion of negation for flags, which would avoid the current need to implement each flag and its negation separately. We are currently already reasonably consistent with using --no-... for negated flags (--without-K being the only exception I can see), so this would mainly be a change in the way the flags are implemented. A more uniform way of handling flags and their negations would also prevent issues like this from popping up again in the future when new flags are added.

  5. gallais commented on Mar 7, 2021

    @gallais
    Member

    I think the solution to this problem (and the related one of importing --safe
    modules when an unsafe flag is set on the command line) is to separate flag
    parsing from the computation of the semantics of a set of flags.

    If we accumulate the flags as a set of commands, we can easily check that conflicting
    flags are not both members of the set at the same time (e.g. cubical and with-K).

    This would also allow us to locally escalate restrictions in files that demand
    it by explicitly computing the merger of the CLI & the file flags. At the moment
    we can't tell whether --safe and --allow-unsolved-metas have both been set
    in the file (bad), both on the command line (bad) or respectively in the file
    and the command line (fine).

    This is a big refactoring though.

  6. jespercockx commented on Mar 7, 2021

    @jespercockx
    MemberAuthor

    I'm not sure if I agree with that solution, I'd prefer a simple semantics where it doesn't matter where the flags are given or in what order. So if two flags are incompatible (e.g. --safe and --allow-unsolved-metas) then that should be an error regardless of whether each of the flag was given in the file, on the command-line, or in the library file. Anything beyond that runs the risk of being confusing to all but a small set of expert users.

  7. gallais commented on Mar 7, 2021

    @gallais
    Member

    #5098 is explicitly about being able to import --safe modules while using
    --allow-unsolved-metas on the command-line.

  8. nad commented on Mar 7, 2021

    @nad
    Collaborator

    Anything beyond that runs the risk of being confusing to all but a small set of expert users.

    I agree.

    #5098 is explicitly about being able to import --safe modules while using
    --allow-unsolved-metas on the command-line.

    My understanding is that @andreasabel suggested that we should change the meaning of --allow-unsolved-metas so that, if --safe is also given, then you are allowed to have unsolved metas, but no more than zero of them. ;)

  9. andreasabel commented on Mar 7, 2021

    @andreasabel
    Member

    That on the command line later given flags override earlier ones is common culture in *nix, I would not go against that. This is useful, e.g. if you alias rm=rm -i and then you can still rm -f.

    That files can override global flags locally is likewise common. E.g. I can run ghc -Wall Foo.hs and then Foo.hs can switch off some warnings it intends to violate, like {-# OPTIONS_GHC -fno-warn-unused-binds #-} or the like.

    Inside a .agda or .agda-lib file we could maybe be stricter and refuse conflicting options (set rather than list semantics). This would certainly be more declarative. I can, however, imagine situation where some wizard puzzles together an .agda file (think yacc). The wizard might put some default flags there which the user might want to undo by explicitly adding their negation.

    My general opinion is that we copy the dominant culture where it is not apparent bogus.

    Then we should think of a precedence, e.g.:

    1. System-wide default flags
    2. User default flags (think .cabal/config)
    3. Command-line flags
    4. Project-specific flags
    5. Local flags in files

    We do not have 1 or 2 afaik, but these could come in the future.

  10. jespercockx commented on Mar 7, 2021

    @jespercockx
    MemberAuthor

    My general opinion is that we copy the dominant culture where it is not apparent bogus.

    I'd say that the dominant culture is bogus in this case, since we have at least some flags that should not be overridable. Or do you think an unsafe flag like --no-termination-check in the file itself should override an explicit --safe given in an .agda-lib file or on the command line?

    More generally, I believe in the principle of least confusion for designing language features, and getting an error that a flag and its negation are conflicting is definitely less confusing than a flag being enabled despite it being disabled explicitly on the command line (or vice versa).

    EDIT: I misread your post at first, so I had to swap the order of my example (but my point still stands).

  11. jespercockx commented on Mar 8, 2021

    @jespercockx
    MemberAuthor

    This issue is blocking #4908 as it is currently impossible to ensure that a certain unsafe feature (e.g. --cumulativity or --sized-types) is off: you can write the negation of the flag, but this can always just be overwritten later so it doesn't provide any guarantees. (I guess --safe is currently the only exception to this rule.)

  12. nad commented on Mar 8, 2021

    @nad
    Collaborator

    Whatever conclusion we come to in the end, if --safe is given on the command line, then I really think that all unsafe features, in all files, should be disallowed.

  13. jespercockx commented on Mar 8, 2021

    @jespercockx
    MemberAuthor

    Whatever conclusion we come to in the end, if --safe is given on the command line, then I really think that all unsafe features, in all files, should be disallowed.

    I agree, and I think this should not be a special case for --safe but apply to all flags that disable a certain feature, e.g. if I write --no-cumulativity then I don't want to find out later that this option was overridden somewhere else.

  14. andreasabel commented on Mar 9, 2021

    @andreasabel
    Member

    I agree, and I think this should not be a special case for --safe but apply to all flags that disable a certain feature, e.g. if I write --no-cumulativity then I don't want to find out later that this option was overridden somewhere else.

    Different flag types may need different overriding behavior. I agree about --safe and its relatives. I also agree that within a file you shouldn't make contradicting decisions. For some flags like warnings it makes sense to disable some on the inside that are enabled on the outside, I suppose.

  15. modified the milestones: 2.6.2, 2.6.3 on Mar 31, 2021
  16. nad commented on Jun 21, 2021

    @nad
    Collaborator

    @MatthewDaggitt proposed (#5456) that there should be two fields in library files, one for flags that must not be overridden by options pragmas, and one for flags that are allowed to be overridden.

  17. nad commented on Jun 21, 2021

    @nad
    Collaborator

    I think we should aim for a design that is flexible, but that makes it hard to shoot yourself in the foot. A preliminary, incomplete design, based on what others have suggested:

    • One can give options in four places:

      • In options pragmas.
      • In .agda-lib files:
        • Overridable.
        • Not overridable.
      • On the command-line.

      More could be added later.

    • Some options must not be overridden (this category includes at least --safe), others are allowed to be overridden.

      • A possible exception: Options marked as overridable in a .agda-lib file can always be overridden.
    • Some options can be given in options pragmas and .agda-lib files, others not.

    • How is the set of options in effect for a given file computed?

      • For a given file the following options could have an effect:
        • Those in the file's options pragma, if any.
        • Those in the relevant .agda-lib file, if any.
        • Those given on the command-line.
      • If an option that must not be overridden (either because it can never be overridden, or because it is listed as not overridable in the .agda-lib file) is given, and a conflicting option is given in any of the locations listed above, then the conflicting option is ignored, and a warning presented to the user.
        • One could make it possible to silence this warning.
      • Otherwise the following precedence order is used to decide which options take effect:
        • For a single location later options override earlier ones.
          • Alternative design (related to the OP): For a single location (except the command-line) one could disallow conflicting options.
        • If two options are given in two different locations, then the one given at the location listed first in the following list takes precedence:
          • Options pragmas.
          • Command-line options.
          • Overridable .agda-lib options.

    Is this too complicated? Too restrictive?

  18. MatthewDaggitt commented on Jun 22, 2021

    @MatthewDaggitt
    Contributor

    That scheme all makes sense to me apart from the precedence order:

    If two options are given in two different locations, then the one given at the location listed first in the following list takes precedence:

    • Options pragmas.
    • Command-line options.
    • Overridable .agda-lib options.

    As @jespercockx and @andreasabel have previously stated, If a user is specifically providing an option on the command line, then they very definitely want to use that option, and having it overridden locally seems counter-intuitive to me. Much better to throw a warning that the local options are not compatible with it.

    It seems to me that the precedence should be:

    1. Command-line options.
    2. Non-overridable .agda-lib options
    3. Options pragmas.
    4. Overridable .agda-lib options.

    where any clashes between 1 vs 2 vs 3 throw an warning and clashes between 1,2,3 vs 4 are silently ignored.

    The advantage of using this precedence order is that it is no longer necessary to have certain options being inherently overridable or non-overridable. For example, if the user uses the flag --safe on the command line, it will type check the modules as safe everywhere and will error if the modules use unsafe features, even if the modules are marked as unsafe locally. i.e. the following point is no longer needed:

    • Some options must not be overridden (this category includes at least --safe), others are allowed to be overridden.
      • A possible exception: Options marked as overridable in a .agda-lib file can always be overridden.
  19. nad commented on Jun 22, 2021

    @nad
    Collaborator

    I don't think it should be possible to override --safe given in options pragmas. From the point of view of a person writing the code this may be fine, but I think we should also consider readers of the code.

  20. MatthewDaggitt commented on Jun 23, 2021

    @MatthewDaggitt
    Contributor

    I don't think it should be possible to override --safe given in options pragmas. From the point of view of a person writing the code this may be fine, but I think we should also consider readers of the code.

    I don't quite see the problem? Someone creates a file which they mark as --safe at the top using an options pragma. The reader comes along and sees this pragma and ascertains that this module only contains and depends on safe things. A third person (could be the reader) now comes along and passes the --unsafe flag in at the command line. The reader is not deceived, they are explicitly asking to check it with --unsafe, and if something can be checked with --safe shouldn't it also be checkable with --unsafe?

    @nad why should this be a problem? Can you give a concrete scenario where you think the precedence order I described above would mislead the reader/writer?

  21. nad commented on Jun 23, 2021

    @nad
    Collaborator

    Scenario:

    • I'm reading code that I know that Agda has accepted.
    • I don't know or remember which command-line options were used to check the code. Perhaps I'm reading some generated HTML file, and I might not even have access to all the source code.
    • The file that I'm looking at contains {-# OPTIONS --safe #-}.

    In this situation I want to be sure (ignoring Agda bugs) that nothing unsafe was used.

    The reader comes along and sees this pragma and ascertains that this module only contains and depends on safe things.

    How can you draw this conclusion if --safe can be overridden?

  22. MatthewDaggitt commented on Jun 25, 2021

    @MatthewDaggitt
    Contributor

    Hmm thanks for the example @nad. I think I may have misinterpreted your original proposal, apologies. If I now understand correctly, the set of flags in use will have to dynamically change during type-checking as we go down the module hierarchy. So to have a concrete example, if we have the following modules, where Unsafe/Unsafe2 are locally marked --unsafe, Safe is locally marked --safe and Other is not marked at all.

    Unsafe
      |   \
      |     \
     Safe    Unsafe2
      |     /
      |   /
     Other

    then:

    1. Checking Unsafe with the --safe flag on the command line will obviously fail immediately.
    2. Checking Unsafe with the --unsafe flag on the command line will succeed, with the following execution. Agda will first check Unsafe with the flags --unsafe and see that it depends on Safe. When checking Safe it will then update the set of active flags to --safe and check Other under the --safe flag as well. After this is done it will go back up the tree to Unsafe, replacing --safe with --unsafe as it goes back past Safe. It then type checks Unsafe2 using the --unsafe flag.

    Obviously you're going to need to type-check unmarked modules with the least restrictive options, as otherwise if you type-check Unsafe2 before Safe naively you're going to end up having to type-check Other twice, once unsafely then once safely.

    I think this is the only scheme that both preserves 1) the guarantee that modules marked as --safe locally are type checked as safe and 2) doesn't leave us in the very undesirable situation of preventing --unsafe libraries from depending on --safe libraries. Was this what you meant @nad?

    If this is the scheme you had in mind then I think the following in your proposal:

    For a given file the following options could have an effect:

    • Those in the file's options pragma, if any.
    • Those in the relevant .agda-lib file, if any.
    • Those given on the command-line.

    is missing a couple of cases. In particular don't you also need:

    • Those infective options in modules that depend on it.
    • Those co-infective options in modules that it depends on.
  23. nad commented on Jun 27, 2021

    @nad
    Collaborator

    In particular don't you also need:

    • Those infective options in modules that depend on it.

    • Those co-infective options in modules that it depends on.

    (An aside: I think I'm using the terms infective/coinfective in the opposite way. I think we should try to come up with less confusing terminology.)

    When you are checking a module, how do you know which options are in effect in all the modules that import it (perhaps in other projects)? I did not propose any changes to the rules regarding infective/coinfective options. With the current rules, if a certain module uses a coinfective option, then it is an error if any module that it depends on does not have this option enabled.

  24. MatthewDaggitt commented on Jul 4, 2021

    @MatthewDaggitt
    Contributor

    When you are checking a module, how do you know which options are in effect in all the modules that import it (perhaps in other projects)? I did not propose any changes to the rules regarding infective/coinfective options. With the current rules, if a certain module uses a coinfective option, then it is an error if any module that it depends on does not have this option enabled.

    I think you would have to type-check a module with the strictest version of options permissible. For example if you arrive at a module in the hierarchy and don't know whether you need to check it with --safe or --unsafe, you should check it with --safe by default. If you encounter an unsafe option then you don't throw an error but simply mark it as --unsafe.

    The question then is, are there any options which don't form an ordered set in this manner? I'm not familiar enough with some of the more obscure options to answer that one...

    (sorry for taking so long to reply in this conversation!)

  25. modified the milestones: 2.6.3, later on Aug 26, 2022
  26. added a commit that references this issue on Oct 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: discussionux: options

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions