codec icon indicating copy to clipboard operation
codec copied to clipboard

Types of f_1 and f_2 cannot be inferred

Open Lysxia opened this issue 9 years ago • 1 comments

Trying to type

:t (,)
  $>> f_1 >-< pure ()
  >>> f_2 >-< pure 'a' :: (Applicative fr, Applicative fw) => Codec fr fw ((), Char)

results in some "could not deduce" errors

Could not deduce (Buildable ((), Char) y0)
Could not deduce (Field1 ((), Char) () (a0 -> b0 -> (a0, b0)) b1)
Could not deduce (Field2 ((), Char) Char b1 y0)

While an instance Field1 (a, b) a (a -> c -> (a, b)) (X -> c -> (a, b)) exists, it cannot be selected here because the compiler cannot first deduce that ((), Char) should be equal to (a0, b0) (fundeps would then take care of the rest).

Annotating (,) :: () -> Char -> ((), Char) makes the above typecheck, though this causes f_1 and f_2 to be rather annoying to use.

Making them regular functions would be a better fix IMO. Is there a reason why they are defined in typeclasses that I am missing?

Lysxia avatar Oct 09 '16 20:10 Lysxia

Also, having something of this type would cover most uses of f_1 and f_2:

(Applicative fr, Applicative fw) => Codec fr fw a -> Codec fr fw b -> Codec fr fw (a, b)

I guess control on the ordering would be the only remaining benefit of these fields, which can be subsumed as well by providing a symmetrical function, or by flipping manually mapCodec swap swap.

Lysxia avatar Oct 09 '16 20:10 Lysxia