{-# LANGUAGE GHC2024 #-} {-# LANGUAGE AllowAmbiguousTypes #-} {-# LANGUAGE PatternSynonyms #-} -- base import GHC.TypeLits (SNat, KnownNat (natSing), natVal, withSomeSNat, fromSNat) class KnownNat n => ValidUINTn n data UINTn where UINTn :: ValidUINTn n => SNat n -> UINTn instance ValidUINTn 1 instance ValidUINTn 2 instance ValidUINTn 3 instance ValidUINTn 4 instance Show UINTn where show (UINTn sn) = "UINT" <> show (natVal sn * 8) getValidUINTn :: Integer -> Maybe UINTn getValidUINTn x = withSomeSNat x $ \maybeSn -> maybeSn >>= \sn -> let n = fromSNat sn in if n == 1 then Just (UINTn (natSing @1)) else if n == 2 then Just (UINTn (natSing @2)) else if n == 2 then Just (UINTn (natSing @3)) else if n == 4 then Just (UINTn (natSing @4)) else Nothing main = do print $ UINTn (natSing @2) print $ getValidUINTn 4 print $ getValidUINTn 5