Enabling TypeFamilies makes code more rigorous

I have a module encoded and working, but I can’t enter two function signatures for it, because to enter them I have to enable the TypeFamilies extension for the module, and when I do, it will no longer build.

For one of them, I need TypeFamilies because it uses persisent / esqueleto functions .

The correct type would be, I believe:

 getByIds :: (PersistEntityBackend a ~ SqlBackend) => EntityField a (Key a) -> [Key a] -> SqlPersistM [Entity a] 

(ghc offers a more general signature)

Another uses hsqml .

ghc offers this signature, but I think it could be simplified:

  prop :: forall tr b. (Marshal tr, Typeable b, MarshalMode tr ICanReturnTo () ~ Yes) => String -> (b -> tr) -> Member (GetObjType (ObjRef b)) 

The bottom line is that without TypeFamilies I cannot write these signatures. However, when I turn on TypeFamilies , the code will not be generated, and I do not understand why. The error looks like some polymorphic functions suddenly become monomorphic.

The error output is relatively long, you can find here .

I have TypeFamilies enabled in several other application modules without any problems, and this allows me to record signatures without problems using SqlBackend and ICanReturnTo .

Is there something wrong with this module that prevents it from building with TypeFamilies ? Or do I need to enable another extension to fix it? I did not expect to just enable this extension to break the compilation.

+6
source share
1 answer

A type equality restriction ~ can only be written if you include TypeFamilies or GADTs .

However, including TypeFamilies or GADTs also allows MonoLocalBinds . As the name implies, it disables the generalization of locally defined variables,

If MonoLocalBinds prevents code compilation, you must either write down generic type signatures or separate such locales into top-level definitions. Writing generic types is sometimes a little tricky; in such cases, you can try to request GHCi, or you can enable NoMonomorphismRestriction , write unannotated top-level definitions, and then look at the types being inferred.

+9
source

Source: https://habr.com/ru/post/984860/


All Articles