I'm unable to automatically derive instances using DerivingVia
on any type that uses types from optics-core
like Prism'
; the error I get from compiler is:
src/Main.hs:24:13-19: error:
• Couldn't match type ‘Foo’ with ‘Stringable Foo’
arising from the coercion of the method ‘mkPrism’
from type ‘MyPrism (Stringable Foo)’ to type ‘MyPrism Foo’
• When deriving the instance for (MkPrism Foo)
|
24 | deriving (MkPrism) via (Stringable Foo)
| ^^^^^^^
The type in question is:
newtype MyPrism a
= MyPrism (Prism' String a)
The compilation succeeds if I replace the Prism'
type with its underlying representation, viz.:
newtype MyPrism a
= MyPrism (a -> String, String -> Maybe a)
- Why do I see coercion issues when using
Prism'
? Is it because theOptic
constructor is not exported by the package? - What is the usual way to solve this?
You can see the full program to reproduce this here: https://github.com/srid/q-optics-coercion/blob/f1e3df5e1c5c0e4a51eacb6439fb8c627e4c7bd5/src/Main.hs ... if you have Nix installed, you can run bin/run
in this repo.
EDIT: Once I import Optics.Internal.Optic
, the error message becomes:
src/Main.hs:25:13-19: error:
• Couldn't match representation of type ‘p i Foo Foo’
with that of ‘p i (Stringable Foo) (Stringable Foo)’
arising from the coercion of the method ‘mkPrism’
from type ‘MyPrism (Stringable Foo)’ to type ‘MyPrism Foo’
• When deriving the instance for (MkPrism Foo)
|
25 | deriving (MkPrism) via (Stringable Foo)
|