Skip to content

Commit 77d2cc9

Browse files
committed
Make deriving Bounded phantom types work
1 parent 92c4a26 commit 77d2cc9

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

plutus-tx/src/PlutusTx/Bounded/TH.hs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,20 @@ deriveBounded name = do
2121
, TH.datatypeCons = cons
2222
} <-
2323
TH.reifyDatatype name
24+
25+
roles <- reifyRoles name
26+
2427
let
2528
-- The purpose of the `TH.VarT . varTToName` roundtrip is to remove the kind
2629
-- signatures attached to the type variables in `tyVars0`. Otherwise, the
2730
-- `KindSignatures` extension would be needed whenever `length tyVars0 > 0`.
2831
tyVars = TH.VarT . varTToName <$> tyVars0
32+
33+
nonPhantomTyVars = VarT . varTToName . snd <$> filter ((/= PhantomR) . fst) (zip roles tyVars0)
34+
2935
instanceCxt :: TH.Cxt
30-
instanceCxt = TH.AppT (TH.ConT ''Bounded) <$> tyVars
36+
instanceCxt = TH.AppT (TH.ConT ''Bounded) <$> nonPhantomTyVars
37+
3138
instanceType :: TH.Type
3239
instanceType = TH.AppT (TH.ConT ''Bounded) $ foldl' TH.AppT (TH.ConT tyConName) tyVars
3340

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
instance PlutusTx.Bounded.Class.Bounded (Bounded.Spec.PhantomADT e_0)
2+
where {PlutusTx.Bounded.Class.minBound = Bounded.Spec.PhantomADT PlutusTx.Bounded.Class.minBound;
3+
{-# INLINABLE PlutusTx.Bounded.Class.minBound #-};
4+
PlutusTx.Bounded.Class.maxBound = Bounded.Spec.PhantomADT PlutusTx.Bounded.Class.maxBound;
5+
{-# INLINABLE PlutusTx.Bounded.Class.maxBound #-}}

plutus-tx/test/Bounded/Spec.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ boundedTests =
4242
in testGroup
4343
"PlutusTx.Enum tests"
4444
[ testCase "conforms to haskell" $ (Tx.minBound @SomeVeryLargeEnum, Tx.maxBound @SomeVeryLargeEnum) @?= (HS.minBound, HS.maxBound)
45-
, -- currently does not work with polymorphic phantom types, remove the type annotation when support is added
46-
testCase "phantom" $ Tx.minBound @(PhantomADT ()) @?= HS.minBound
45+
, testCase "phantom" $ Tx.minBound @(PhantomADT ()) @?= HS.minBound
4746
, runTestNested
4847
["test", "Bounded", "Golden"]
4948
[ $(goldenCodeGen "SomeVeryLargeEnum" (deriveBounded ''SomeVeryLargeEnum))
5049
, $(goldenCodeGen "Unit" (deriveBounded ''()))
5150
, $(goldenCodeGen "Ordering" (deriveBounded ''Ordering))
5251
, $(goldenCodeGen "SingleConstructor" (deriveBounded ''SingleConstructor))
52+
, $(goldenCodeGen "PhantomADT" (deriveBounded ''PhantomADT))
5353
]
5454
]

0 commit comments

Comments
 (0)