Repository navigation
Description
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
jespercockx commentedon Mar 7, 2021
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:
.agda-libfile..agda-libfile 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.
jespercockx commentedon Mar 7, 2021
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-Kbeing 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.gallais commentedon Mar 7, 2021
I think the solution to this problem (and the related one of importing
--safemodules 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.
cubicalandwith-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
--safeand--allow-unsolved-metashave both been setin 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.
jespercockx commentedon Mar 7, 2021
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.
--safeand--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.gallais commentedon Mar 7, 2021
#5098 is explicitly about being able to import
--safemodules while using--allow-unsolved-metason the command-line.nad commentedon Mar 7, 2021
I agree.
My understanding is that @andreasabel suggested that we should change the meaning of
--allow-unsolved-metasso that, if--safeis also given, then you are allowed to have unsolved metas, but no more than zero of them. ;)andreasabel commentedon Mar 7, 2021
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 -iand then you can stillrm -f.That files can override global flags locally is likewise common. E.g. I can run
ghc -Wall Foo.hsand thenFoo.hscan switch off some warnings it intends to violate, like{-# OPTIONS_GHC -fno-warn-unused-binds #-}or the like.Inside a
.agdaor.agda-libfile 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.agdafile (thinkyacc). 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.:
.cabal/config)We do not have 1 or 2 afaik, but these could come in the future.
jespercockx commentedon Mar 7, 2021
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-checkin the file itself should override an explicit--safegiven in an.agda-libfile 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).
jespercockx commentedon Mar 8, 2021
This issue is blocking #4908 as it is currently impossible to ensure that a certain unsafe feature (e.g.
--cumulativityor--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--safeis currently the only exception to this rule.)nad commentedon Mar 8, 2021
Whatever conclusion we come to in the end, if
--safeis given on the command line, then I really think that all unsafe features, in all files, should be disallowed.jespercockx commentedon Mar 8, 2021
I agree, and I think this should not be a special case for
--safebut apply to all flags that disable a certain feature, e.g. if I write--no-cumulativitythen I don't want to find out later that this option was overridden somewhere else.andreasabel commentedon Mar 9, 2021
Different flag types may need different overriding behavior. I agree about
--safeand 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.nad commentedon Jun 21, 2021
@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.
nad commentedon Jun 21, 2021
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:
.agda-libfiles:More could be added later.
Some options must not be overridden (this category includes at least
--safe), others are allowed to be overridden..agda-libfile can always be overridden.Some options can be given in options pragmas and
.agda-libfiles, others not.How is the set of options in effect for a given file computed?
.agda-libfile, if any..agda-libfile) 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..agda-liboptions.Is this too complicated? Too restrictive?
MatthewDaggitt commentedon Jun 22, 2021
That scheme all makes sense to me apart from the precedence order:
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:
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
--safeon 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:nad commentedon Jun 22, 2021
I don't think it should be possible to override
--safegiven 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.MatthewDaggitt commentedon Jun 23, 2021
I don't quite see the problem? Someone creates a file which they mark as
--safeat 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--unsafeflag 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--safeshouldn'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?
nad commentedon Jun 23, 2021
Scenario:
{-# OPTIONS --safe #-}.In this situation I want to be sure (ignoring Agda bugs) that nothing unsafe was used.
How can you draw this conclusion if
--safecan be overridden?MatthewDaggitt commentedon Jun 25, 2021
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/Unsafe2are locally marked--unsafe,Safeis locally marked--safeandOtheris not marked at all.then:
Unsafewith the--safeflag on the command line will obviously fail immediately.Unsafewith the--unsafeflag on the command line will succeed, with the following execution. Agda will first checkUnsafewith the flags--unsafeand see that it depends onSafe. When checkingSafeit will then update the set of active flags to--safeand checkOtherunder the--safeflag as well. After this is done it will go back up the tree toUnsafe, replacing--safewith--unsafeas it goes back pastSafe. It then type checksUnsafe2using the--unsafeflag.Obviously you're going to need to type-check unmarked modules with the least restrictive options, as otherwise if you type-check
Unsafe2beforeSafenaively you're going to end up having to type-checkOthertwice, once unsafely then once safely.I think this is the only scheme that both preserves 1) the guarantee that modules marked as
--safelocally are type checked as safe and 2) doesn't leave us in the very undesirable situation of preventing--unsafelibraries from depending on--safelibraries. Was this what you meant @nad?If this is the scheme you had in mind then I think the following in your proposal:
is missing a couple of cases. In particular don't you also need:
nad commentedon Jun 27, 2021
(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.
MatthewDaggitt commentedon Jul 4, 2021
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
--safeor--unsafe, you should check it with--safeby 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!)
#6238: Allow flat matching on inductive families (#6239)