I have found what causes the bug and implemented a possibly fix for it. The problem has to do with source types that aren't expanded before tidying them.

Here is a small example where the error occurs:

-----
module Test where

newtype A a = A (forall b. b -> a)

test :: forall q b. q -> A b
test _ = undefined
-----


When generating Core for this program we get:

-----
%module Test
%newtype Test.A a = %forall b . b -> a;
Test.A :: %forall a . (%forall b . b -> a) -> %forall b . b -> a =
%note "InlineMe"
\ @ a (tpl::%forall b . b -> a) -> tpl;
Test.zdgfromA :: %forall a . (%forall b . b -> a) ->
%forall b . b -> a =
\ @ a (g::%forall b . b -> a) -> g;
Test.zdgtoA :: %forall a . (%forall b . b -> a) ->
%forall b . b -> a =
Test.A;
Test.test :: %forall q b . q -> %forall b . b -> b =
\ @ q @ b (ds::q) -> GHCziErr.undefined @ (%forall b . b -> b);
-----

Here the type for Test.test is wrong since the second binding of b shadows the first binding of b. We do also pass the wrong type to GHCziErr.undefined.

The tidyXXX functions should take care of this and rename the second b to b1. After diving into GHC I found that the problem is that the types and expressions are tidied before the source types are expanded. When generating external core the source types are expanded first in make_ty in MkExternalCore.lhs.


I believe that there are three possible fixes:

1. Make sure that all type variables that are bound in non recursive newtypes are made unique
2. Expand all non recursive newtypes before using tidyXXX
3. Not expand the non recursive newtypes when generating external core


I have implemented the third alternative. I changed the function make_ty in MkExternalCore.lhs to:

-----
make_ty :: Type -> C.Ty
make_ty (TyVarTy tv) = C.Tvar (make_var_id (tyVarName tv))
make_ty (AppTy t1 t2) = C.Tapp (make_ty t1) (make_ty t2)
make_ty (TyConApp tc ts) = foldl C.Tapp (C.Tcon (make_con_qid (tyConName tc))) (map make_ty ts)
make_ty (FunTy t1 t2) = make_ty (TyConApp funTyCon [t1,t2])
make_ty (ForAllTy tv t) = C.Tforall (make_tbind tv) (make_ty t)
make_ty (SourceTy (NType tc ts)) = foldl C.Tapp (C.Tcon (make_con_qid (tyConName tc))) (map make_ty ts)
make_ty (SourceTy p) = make_ty (sourceTypeRep p)
make_ty (NoteTy _ t) = make_ty t
-----


Now the generated Core for the program listed above becomes:

-----
%module Test
%newtype Test.A a = %forall b . b -> a;
Test.A :: %forall a . (%forall b . b -> a) -> Test.A a =
%note "InlineMe"
\ @ a (tpl::%forall b . b -> a) -> tpl;
Test.zdgfromA :: %forall a . Test.A a -> %forall b . b -> a =
\ @ a (g::Test.A a) -> g;
Test.zdgtoA :: %forall a . (%forall b . b -> a) -> Test.A a =
Test.A;
Test.test :: %forall q b . q -> Test.A b =
\ @ q @ b (ds::q) -> GHCziErr.undefined @ (Test.A b);
-----




Sincerely,
Tobias

_______________________________________________
Glasgow-haskell-bugs mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs

Reply via email to