Skip to content

Shadowed identifiers should be preceded by a dot when printed #572

Closed

Description

@GoogleCodeExporter
postulate P : Set -> Set

test : (B : Set) -> P B -> P B
test = λ p p -> {!!}

When doing C-c C-. one gets:

Goal: P p
————————
p : P p
p : Set

The shadowed p should be printed as .p to get

Goal : P .p
p : P .p
.p : Set

But what about doubly-shadowed identifiers? ..p ...p ....p ?

Original issue reported on code.google.com by [email protected] on 21 Feb 2012 at 4:22

Activity

  1. added
    type: enhancement
    ux: printing
    on Aug 8, 2015
  2. added this to the milestone on May 24, 2018
  3. added
    shadowing
    on May 28, 2018
  4. jespercockx commented on Nov 14, 2018

    @jespercockx
    Member

    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).

  5. nad commented on Nov 14, 2018

    @nad
    Collaborator

    And mark it as being out of scope, as discussed in #3127?

  6. self-assigned this
    on Dec 6, 2018
  7. jespercockx commented on Dec 6, 2018

    @jespercockx
    Member

    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:

    Goal: P p
    ————————
    p : P p₁
    p₁ : Set
    
  8. nad commented on Dec 7, 2018

    @nad
    Collaborator

    I think the fresh name should also be marked as being out of scope.

  9. added 2 commits that reference this issue on Dec 9, 2018
  10. 4 remaining items

  11. jespercockx commented on Dec 10, 2018

    @jespercockx
    Member

    I changed the printer so it prints the qualified name for shadowed definitions, for example:

    Goal: Issue572.A
    ————————————————————————————————————————————————————————————
    A : Set
    
  12. added 3 commits that reference this issue on Dec 10, 2018
  13. Saizan commented on Mar 25, 2019

    @Saizan
    Collaborator

    @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.

  14. jespercockx commented on Mar 25, 2019

    @jespercockx
    Member

    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?

  15. Saizan commented on Mar 25, 2019

    @Saizan
    Collaborator

    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

    data Grpd : Set where
      trunc : (x₄ y₂ : Grpd) (x₅ y₃ : x₄ ≡ y₂) (x₆ y₄ : x₅ ≡ y₃) → x₆ ≡ y₄
    
    test2 : Grpd → Grpd
    test2 (trunc a b c d e f i j k) = { }0

    The code above generates 6 constrains for ?0, the last two get printed as

      test2 (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₁)
        : Grpd

    while I'm pretty confident that, e.g., the two i₁ in test2 (x₆ i₁ i₁) are different variables.

    Also because the following typechecks

    test3 : Grpd → Grpd
    test3 (trunc a b c d e f i j k) = trunc _ _ _ _ (\ i j → test3 (e i j)) (\ i j → test3 (f i j)) i j k
  16. jespercockx commented on Mar 26, 2019

    @jespercockx
    Member

    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

    when (not $ isNoName x) $ do
    registerForShadowing x
    ys <- getContextNames
    forM_ ys $ \y ->
    when (not (isNoName y) && sameRoot x y) $ tellShadowing x y
    , you could try to add some debug output to see if that part of the code is actually run for the variables in your example.

  17. Saizan commented on Mar 26, 2019

    @Saizan
    Collaborator

    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.

  18. Saizan commented on Apr 2, 2019

    @Saizan
    Collaborator

    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?

  19. jespercockx commented on Apr 2, 2019

    @jespercockx
    Member

    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?

  20. Saizan commented on Apr 2, 2019

    @Saizan
    Collaborator

    Doesn't the documentation for Env explicitly allow for vars in takenVarNames that are not in ScopeInfo?

    data Env = 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.

  21. Saizan commented on Apr 2, 2019

    @Saizan
    Collaborator

    Here's another example, this time the problem shows up when printing goal types.

    open import Agda.Builtin.Sigma
    open import Agda.Builtin.Equality
    
    postulate
      A : Set
      B : A → Set
      a : 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:

    ?0 : B x
    ?1 : B x
    ?2 : B x → B x → A
  22. jespercockx commented on Apr 3, 2019

    @jespercockx
    Member

    I'll open a new issue for that.

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

Metadata

Metadata

Assignees

Labels

shadowingtype: enhancementux: printing

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions