Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instances in local modules are automatically imported potentially causing unintended clashes #3174

Open
lukaszcz opened this issue Nov 14, 2024 · 0 comments

Comments

@lukaszcz
Copy link
Collaborator

This seems intuitive, but has some unintended consequences in combination with instance resolution.

Consider:

module instanceModule;

import Stdlib.Data.List.Base open;
import Stdlib.Trait.Show open;
import Stdlib.Data.String open;

module showModule;

showList {A} {{Show A}} : List A -> String
  | nil := "nil"
  | (h :: t) := Show.show h ++str " :: " ++str showList t;

instance
showListI {A} {{Show A}} : Show (List A) :=
   mkShow@{show := showList};

end;

showList' {A} {{Show A}} : List A -> String := Show.show;

This type-checks, because the after the definition of showModule it is automatically imported, which adds all instances declared inside the module to instance search. This makes it impossible to define a different instance for Show (List A) in another local module.

The following

module instanceModule;

import Stdlib.Data.List.Base open;
import Stdlib.Trait.Show open;
import Stdlib.Data.String open;

module showModule;

showList {A} {{Show A}} : List A -> String
  | nil := "nil"
  | (h :: t) := Show.show h ++str " :: " ++str showList t;

instance
showListI {A} {{Show A}} : Show (List A) :=
   mkShow@{show := showList};

end;

module showModule2;

instance
showListI {A} {{Show A}} : Show (List A) :=
   mkShow@{
     show : List A -> String
       | nil := "nil"
       | (h :: t) := Show.show h ++str " :: " ++str show t;
   };

end;

fails the type-checker with

/Users/heliax/Documents/progs/juvix/instanceModule.juvix:22:1-10: error:
The instance showListI is subsumed by instances at:
  • /Users/heliax/Documents/progs/juvix/instanceModule.juvix:14:1-10

I'm not sure how much of a problem this is in actual programs, but I did encounter this when trying to use modules to hide things when writing the documentation in Juvix markdown.

@lukaszcz lukaszcz added this to the 0.6.10 milestone Nov 15, 2024
@lukaszcz lukaszcz changed the title Local modules are automatically imported Instances in local modules are automatically imported potentially causing unintended clashes Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant