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

Incorrect type role for n in Vector n a #126

Open
Cajunvoodoo opened this issue Sep 23, 2024 · 3 comments · May be fixed by #127
Open

Incorrect type role for n in Vector n a #126

Cajunvoodoo opened this issue Sep 23, 2024 · 3 comments · May be fixed by #127

Comments

@Cajunvoodoo
Copy link

Currently, Vector has the inferred role type role Vector representational phantom nominal, which allows the following:

newtype Foo = Foo (Vector 10 Int)

exVec :: Vector 15 Int
exVec = fromJust $ fromListN @15 [1..15]

exFoo :: Foo
exFoo = coerce exVec -- no error

There is no error because n is phantom, so the coercion always works.
Instead, Vector should have type role Vector representational nominal nominal to disallow this coercion.

@kozross
Copy link
Collaborator

kozross commented Sep 23, 2024

I'm amazed this leaked through! Thanks, will fix.

@kozross
Copy link
Collaborator

kozross commented Sep 23, 2024

Actually, are you sure this works? To coerce through the Vector newtype, the constructor needs to be in scope, and I'm fairly sure there's no way to achieve this without importing an internal module.

@kozross kozross linked a pull request Sep 23, 2024 that will close this issue
@Cajunvoodoo
Copy link
Author

Cajunvoodoo commented Sep 23, 2024

It's not coercing the internal representation of vector, merely Vector n a -> Foo see:

>>> import Data.Vector.Sized qualified as Sized
>>> import Data.Maybe (fromJust)
>>> newtype Foo = Foo (Sized.Vector 1 Int) deriving Show
>>> exVec = fromJust $ Sized.fromListN @5 [1..5]
>>> exVec
>>> coerce @(Sized.Vector 5 Int) @Foo exVec
Vector [1,2,3,4,5]
Foo (Vector [1,2,3,4,5])

while Foo exVec results in the expected type error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants