Synthesis of type families over newtypes broken if instance of the underlying type is dissimilar
Given:
newtype Age Int
type class F a
type instance F Int = Char
type instance F Age = Bool
x :: F Age
During HDL generation, Clash might sometimes think that x :: Char, because it picks the type instance of the underlying type.
Also, given:
newtype Bar Int
type class G a
type instance G Bar = Bool
x :: G Bar
Clash might throw an error saying that it doesn't know how to encode the type G Int, because it picks the underlying representation of Bar and because there is no type instance for G Int and type family reduction becomes stuck.
To avoid issues like these, make sure that the type instance of the type underlying the newtype is the same as the type instance of the newtype itself. This means that derived type instances are always okay.
Note for those wanting to fix this bug:The underlying cause for this is that Clash throws away casts when translating GHC Core to Clash Core. Simply adding these casts to the core language and then tranlating GHC Core casts to Clash Core casts is insufficient: you will also need to add extra transformations to the normalization phase to propagate these casts.