summaryrefslogtreecommitdiff
path: root/testsuite/tests/tcplugins/NullaryPlugin.hs
blob: 060c1aa2f2cecb28d6ddf0599182f164ce53f637 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
{-# LANGUAGE RecordWildCards #-}

module NullaryPlugin where

-- base
import Data.Maybe
  ( catMaybes )

-- ghc
import GHC.Core.Class
  ( Class(..) )
import GHC.Core.DataCon
  ( classDataCon )
import GHC.Core.Make
  ( mkCoreConApps )
import GHC.Plugins
  ( Plugin )
import GHC.Tc.Plugin
  ( TcPluginM )
import GHC.Tc.Types
  ( TcPluginSolveResult(..) )
import GHC.Tc.Types.Constraint
  ( Ct(..) )
import GHC.Tc.Types.Evidence
  ( EvBindsVar, EvTerm(EvExpr) )

-- common
import Common
  ( PluginDefs(..)
  , mkPlugin, don'tRewrite
  )

--------------------------------------------------------------------------------

-- This plugin solves Wanted 'Nullary' constraints.
-- To do this, we just look through the Wanteds,
-- find any constraint whose className matches that of 'Nullary',
-- in which case we provide evidence (a nullary dictionary).

plugin :: Plugin
plugin = mkPlugin solver don'tRewrite

-- Solve "Nullary".
solver :: [String]
       -> PluginDefs -> EvBindsVar -> [Ct] -> [Ct] -> [Ct]
       -> TcPluginM TcPluginSolveResult
solver _args defs _ev _gs _ds ws = do
  solved <- catMaybes <$> traverse ( solveCt defs ) ws
  pure $ TcPluginOk solved []

solveCt :: PluginDefs -> Ct -> TcPluginM ( Maybe (EvTerm, Ct) )
solveCt ( PluginDefs {..} ) ct@( CDictCan { cc_class } )
  | className cc_class == className nullary
  , let
      evTerm :: EvTerm
      evTerm = EvExpr $ mkCoreConApps ( classDataCon cc_class ) []
  = pure $ Just ( evTerm, ct )
solveCt _ ct = pure Nothing