I'm trying to understand the usage of haskell type literals. In particular, I thought I'd write a function that shows me the type literal for a custom type
newtype Fixed (p :: Nat) a = Fixed a
instance (KnownNat p) => Show (Fixed p a) where
show _ = show $ natVal (Proxy::Proxy p)
However, ghc (7.8) can't deduce KnownNat n0, which means I'm not constraining things as I think I should be. Can anyone suggest what is wrong?
Overview. Literals in Python is defined as the raw data assigned to variables or constants while programming. We mainly have five types of literals which includes string literals, numeric literals, boolean literals, literal collections and a special literal None.
A string literal contains a sequence of characters or escape sequences enclosed in double quotation mark symbols. A string literal with the prefix L is a wide string literal. A string literal without the prefix L is an ordinary or narrow string literal. The type of narrow string literal is array of char .
A "string literal" is a sequence of characters from the source character set enclosed in double quotation marks (" "). String literals are used to represent a sequence of characters which, taken together, form a null-terminated string.
A string literal is a sequence of zero or more characters enclosed within single quotation marks. The following are examples of string literals: 'Hello, world!' 'He said, "Take it or leave it."'
You need -XScopedTypeVariables
for GHC to recognize that the p
of your Proxy p
is the same as the p
of your type signature.
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
import Data.Proxy
import GHC.TypeLits
newtype Fixed (p :: Nat) a = Fixed a
instance (KnownNat p) => Show (Fixed p a) where
show _ = show $ natVal (Proxy::Proxy p)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With