Adding context to rewrite rules

牧云@^-^@ 提交于 2019-12-04 11:39:27

Normally, one can add type signatures to the variables in the forall. Something like

{-# RULES "myrule" forall (x :: H a => T a). g (f x) = h x #-}

Now, that doesn't quite work in this case because T may not be injective. Fortunately, I think TypeApplications gives us a way out of this problem by allowing us to inform GHC that the type variable a in the type T a of x is the same as the one in h:

{-# LANGUAGE TypeFamilies, TypeApplications, AllowAmbiguousTypes #-}

 ...

{-# RULES "myrule" forall (x :: H a => T a). g (f x) = h @a x #-}

We don't need to enable ScopedTypeVariables (even if we are relying on it to make sure the as are the same) because it is on by default in rewrite rules.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!