You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Shadowed identifiers should be preceded by a dot when printed #572
shadowingProblems caused by re-using names in the surface syntaxtype: enhancementIssues and pull requests about possible improvementsux: printingIssues relating to how terms are printed for display
I think a better solution would be to apply the standard renaming scheme and rename one of the two variables to p1 (not entirely sure which one would be the best candidate for renaming).
I have a plan that will fix both this and #3127. In the example from the OP, it would rename the existing variable when adding the new p to the context, so you would see the following:
@jespercockx Would it make sense to move the ConcreteNames and ShadowingNames maps to TCEnv so that they stay in sync with the Context? It would help wrt build/enterClosure.
Well the point of putting them in the TCState is exactly because they must survive after exiting from an addContext, so putting them in the TCEnv would defeat the purpose. Can you explain what is the problem you're trying to solve?
Unfortunately I haven't written anything about the new handling of shadowing, no. The implementation relies on a fair dose of magic (or its closest equivalent, mutable state). Below is a (probably not very successful) attempt at explaining how it works.
The idea is that we keep track of which variables are added in which order to the context during typechecking (using addContext), in order to later come up with better names for them. For example, we don't want to pick x for the name of a hidden (system-generated) variable if another variable named x by the user shadows it. Likewise, if the user gave two variables the same name x then we must rename the one that is shadowed into something else (it's fine to rename it because the user can anyway not refer to it anymore). This information about which (abstract) names could potentially shadow other (abstract) names is stored in the ShadowingNames. But then there's another problem that we don't want to change how we print a given abstract name after we already showed it to the user, which is why we keep track of the concrete name associated to abstract names in the ConcreteNames map. Except that in the situation where the user explicitly shadows a name, we must rename it even if we already printed it. So we keep for each abstract name not just one concrete name, but a list of concrete names which are tried in order until one is found that is not shadowed by a user-written name.
I'm not sure what's going on in your example, but maybe you're never using addContext (or similar function) to add all the variables to the context, thus circumventing the shadowing machinery? The relevant magic is happening at
I think part of my problem is that through build/enterClosure the ScopeInfo gets out of sync. But still it's weird that only interval variables seem affected by this.
Ok, so I think the problem is triggered when takenVarNames in the AbsToCon environment has some names that are not covered by ScopeInfo but they do have entries in stConcreteNames that would shadow each other.
It seems what's going wrong is that toConcreteName will happily use a name from stConcreteNames even if it shadow another name used for a "takenVar" as long as it doesn't shadow something from ScopeInfo.
chooseName correctly avoids any name that could be used for a takenVar, but the loop in toConcreteName doesn't get to that point in this scenario.
Maybe the shadowing check in toConcreteName needs to be more clever?
How are you ending up in a situation where the takenVarNames contains names that are not in the currentScope? That should ordinarily not be possible (or some internal invariant is broken). Are you not using addBinding to add variables to the scope during printing?
Doesn't the documentation for Env explicitly allow for vars in takenVarNames that are not in ScopeInfo?
dataEnv= Env { takenVarNames :: Set A.Name
-- ^ Abstract names currently in scope. Unlike the -- ScopeInfo, this includes names for hidden -- arguments inserted by the system.
Anyway, I'm not writing any AbsToCon code, this is just constraints getting stored as a closure and then printed.
Here's another example, this time the problem shows up when printing goal types.
open importAgda.Builtin.Sigmaopen importAgda.Builtin.EqualitypostulateA :SetB : A →Seta : A
Pi : (A →Set) →Set
Pi B = {x : A} → B x
foo : Pi \ y → Σ (B y) \ _ → Pi \ z → Σ (y ≡ a → B z) \ _ → B y → B z → A
foo = { }0 , (\ { refl → { }1 }) , { }2
The goal type for ?2 then gets printed with both variables as x:
shadowingProblems caused by re-using names in the surface syntaxtype: enhancementIssues and pull requests about possible improvementsux: printingIssues relating to how terms are printed for display
Activity
jespercockx commentedon Nov 14, 2018
I think a better solution would be to apply the standard renaming scheme and rename one of the two variables to
p1(not entirely sure which one would be the best candidate for renaming).nad commentedon Nov 14, 2018
And mark it as being out of scope, as discussed in #3127?
jespercockx commentedon Dec 6, 2018
I have a plan that will fix both this and #3127. In the example from the OP, it would rename the existing variable when adding the new
pto the context, so you would see the following:nad commentedon Dec 7, 2018
I think the fresh name should also be marked as being out of scope.
[ re agda#572 ] Properly deal with shadowed names (finally!)
[ re agda#572 ] Explicitly show renaming of shadowed variables
4 remaining items
jespercockx commentedon Dec 10, 2018
I changed the printer so it prints the qualified name for shadowed definitions, for example:
[ re #572 ] Properly deal with shadowed names (finally!)
[ re #572 ] Explicitly show renaming of shadowed variables
[ #572 ] Print qualified name for shadowed definitions
Saizan commentedon Mar 25, 2019
@jespercockx Would it make sense to move the
ConcreteNamesandShadowingNamesmaps toTCEnvso that they stay in sync with theContext? It would help wrtbuild/enterClosure.jespercockx commentedon Mar 25, 2019
Well the point of putting them in the
TCStateis exactly because they must survive after exiting from anaddContext, so putting them in theTCEnvwould defeat the purpose. Can you explain what is the problem you're trying to solve?Saizan commentedon Mar 25, 2019
Ok, I guess I was making wrong assumptions about how this is supposed to work. (Is it written somewhere?)
Anyhow I'm getting shadowing problems for constrains generated by HITs
The code above generates 6 constrains for
?0, the last two get printed astest2 (x₆ i₁ i₁) = ?0 (a = x) (b = x₁) (c = x₅) (d = y₃) (e = x₆) (f = y₄) (i = i0) (j = i₁) (k = i₁) : Grpd test2 (y₄ i₁ i₁) = ?0 (a = x) (b = x₁) (c = x₅) (d = y₃) (e = x₆) (f = y₄) (i = i1) (j = i₁) (k = i₁) : Grpdwhile I'm pretty confident that, e.g., the two
i₁intest2 (x₆ i₁ i₁)are different variables.Also because the following typechecks
jespercockx commentedon Mar 26, 2019
Unfortunately I haven't written anything about the new handling of shadowing, no. The implementation relies on a fair dose of magic (or its closest equivalent, mutable state). Below is a (probably not very successful) attempt at explaining how it works.
The idea is that we keep track of which variables are added in which order to the context during typechecking (using addContext), in order to later come up with better names for them. For example, we don't want to pick
xfor the name of a hidden (system-generated) variable if another variable namedxby the user shadows it. Likewise, if the user gave two variables the same namexthen we must rename the one that is shadowed into something else (it's fine to rename it because the user can anyway not refer to it anymore). This information about which (abstract) names could potentially shadow other (abstract) names is stored in theShadowingNames. But then there's another problem that we don't want to change how we print a given abstract name after we already showed it to the user, which is why we keep track of the concrete name associated to abstract names in theConcreteNamesmap. Except that in the situation where the user explicitly shadows a name, we must rename it even if we already printed it. So we keep for each abstract name not just one concrete name, but a list of concrete names which are tried in order until one is found that is not shadowed by a user-written name.I'm not sure what's going on in your example, but maybe you're never using
addContext(or similar function) to add all the variables to the context, thus circumventing the shadowing machinery? The relevant magic is happening atagda/src/full/Agda/TypeChecking/Monad/Context.hs
Lines 151 to 155 in 87fb827
Saizan commentedon Mar 26, 2019
I think part of my problem is that through
build/enterClosuretheScopeInfogets out of sync. But still it's weird that only interval variables seem affected by this.Saizan commentedon Apr 2, 2019
Ok, so I think the problem is triggered when
takenVarNamesin theAbsToConenvironment has some names that are not covered byScopeInfobut they do have entries instConcreteNamesthat would shadow each other.It seems what's going wrong is that
toConcreteNamewill happily use a name fromstConcreteNameseven if it shadow another name used for a "takenVar" as long as it doesn't shadow something fromScopeInfo.chooseNamecorrectly avoids any name that could be used for atakenVar, but the loop intoConcreteNamedoesn't get to that point in this scenario.Maybe the shadowing check in
toConcreteNameneeds to be more clever?jespercockx commentedon Apr 2, 2019
How are you ending up in a situation where the
takenVarNamescontains names that are not in thecurrentScope? That should ordinarily not be possible (or some internal invariant is broken). Are you not usingaddBindingto add variables to the scope during printing?Saizan commentedon Apr 2, 2019
Doesn't the documentation for
Envexplicitly allow for vars intakenVarNamesthat are not inScopeInfo?Anyway, I'm not writing any
AbsToConcode, this is just constraints getting stored as a closure and then printed.Saizan commentedon Apr 2, 2019
Here's another example, this time the problem shows up when printing goal types.
The goal type for
?2then gets printed with both variables asx:jespercockx commentedon Apr 3, 2019
I'll open a new issue for that.