#12777 closed bug (fixed)

reify yields the wrong type in the presence of functional dependencies

Reported by: facundo.dominguez Owned by:
Priority: high Milestone: 8.0.2
Component: Template Haskell Version: 8.0.1
Keywords: template-haskell reify Cc: simonpj, mboes, goldfire, bgamari
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s): Phab:D2659
Wiki Page:

Description

reify yields an incomplete type in the following program.

{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE FunctionalDependencies #-}
module B where
import Language.Haskell.TH as TH
import Language.Haskell.TH.Syntax as TH

class C a b | a -> b where
  yo :: a -> IO b

instance C Bool Int where
  yo = undefined

t3 :: IO ()
t3 = do
  x <- yo True
  $(do addModFinalizer $ TH.reify 'x >>= runIO . print
       [| return () |]
   )
$ inplace/bin/ghc-stage2 B.hs -fforce-recomp
[1 of 1] Compiling B                ( B.hs, B.o )
VarI x_1627401898 (VarT a_1627404604) Nothing

The problem seems to be that finalizers run before functional dependencies are considered.

Hacking ghc to run finalizers after simplifyTop produces the expected output instead:

$ inplace/bin/ghc-stage2 B.hs -fforce-recomp
[1 of 1] Compiling B                ( B.hs, B.o )
VarI x_1627404863 (ConT GHC.Types.Int) Nothing

Would anyone object to running finalizers after simplifyTop? This implies that finalizers shouldn't add definitions (with addTopDecls) which depend on simplifyTop to be type-checked.

Another option is to call simplifyTop before and after running finalizers. But is it safe to do so?

Change History (10)

comment:1 Changed 14 months ago by facundo.dominguez

Cc: bgamari added

comment:2 Changed 14 months ago by facundo.dominguez

Summary: reify yields the wrong type in the presence of functional rependenciesreify yields the wrong type in the presence of functional dependencies

comment:3 Changed 14 months ago by facundo.dominguez

Keywords: template-haskell reify added

comment:4 Changed 14 months ago by goldfire

Component: CompilerTemplate Haskell

comment:5 Changed 14 months ago by goldfire

Calling simplifyTop multiple times should be OK, as long as it's passed different constraints each time. Actually seems reasonable here.

comment:6 Changed 14 months ago by facundo.dominguez

Component: Template HaskellCompiler
Differential Rev(s): Phab:D2659
Status: newpatch

comment:7 Changed 14 months ago by goldfire

Component: CompilerTemplate Haskell

The "Template Haskell" component applies to all bugs/fixes that are primarily about TH, even if the code changed is in GHC proper. Thanks.

comment:8 Changed 13 months ago by bgamari

Priority: normalhigh

Mathieu would like to see this for 8.0.2.

comment:9 Changed 13 months ago by Ben Gamari <ben@…>

In 231a3ae1/ghc:

Have reify work for local variables with functional dependencies.

It turned out that finalizers were run too early and information
resulting from simplifying constraints was not available.

This patch runs finalizers after a first call to simplifyTop, and
then calls simplifyTop a second time to deal with constraints
that could result from running the finalizers.

Fixes T12777

Test Plan: ./validate

Reviewers: goldfire, simonpj, bgamari, austin

Reviewed By: simonpj

Subscribers: mpickering, mboes, thomie

Differential Revision: https://phabricator.haskell.org/D2659

GHC Trac Issues: #12777

comment:10 Changed 13 months ago by bgamari

Resolution: fixed
Status: patchclosed

This was merged to ghc-8.0 as e7c12cdaa7df8a7c71395da026c003ed36d3cbe6.

Note: See TracTickets for help on using tickets.