Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type literal usage

Tags:

types

haskell

ghc

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?

like image 253
OllieB Avatar asked Aug 07 '14 13:08

OllieB


People also ask

What is literal type Python?

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.

What is the type of a string literal?

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 .

What is a string literal used for?

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.

What is string literal example?

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."'


1 Answers

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)
like image 65
Mark Whitfield Avatar answered Oct 05 '22 23:10

Mark Whitfield